jldb/fstore/stateutil_test.go

51 lines
954 B
Go
Raw Normal View History

2023-10-13 09:43:27 +00:00
package fstore
import (
"path/filepath"
"strings"
)
type StoreState struct {
Path string
IsDir bool
Dirs map[string]StoreState
Files map[string]StoreState
FileData string
}
func NewStoreState(in map[string]string) StoreState {
root := StoreState{
Path: "/",
IsDir: true,
Dirs: map[string]StoreState{},
Files: map[string]StoreState{},
}
for path, fileData := range in {
slugs := strings.Split(path[1:], "/") // Remove leading slash.
parent := root
// Add directories.
for _, part := range slugs[:len(slugs)-1] {
if _, ok := parent.Dirs[part]; !ok {
parent.Dirs[part] = StoreState{
Path: filepath.Join(parent.Path, part),
IsDir: true,
Dirs: map[string]StoreState{},
Files: map[string]StoreState{},
}
}
parent = parent.Dirs[part]
}
parent.Files[slugs[len(slugs)-1]] = StoreState{
Path: path,
IsDir: false,
FileData: fileData,
}
}
return root
}