export File and Tree fields

This commit is contained in:
Arnas Udovicius 2021-05-12 08:17:43 +03:00
parent b75c11b5ea
commit a9e6c702b9
2 changed files with 35 additions and 31 deletions

40
tree.go
View file

@ -14,18 +14,18 @@ import (
)
type File struct {
id string
name string
fullPath string
category []string
tags []string
meta map[string]string
Id string
Name string
FullPath string
Category []string
Tags []string
Meta map[string]string
}
type Tree struct {
path string
dirs []Tree
files []File
Path string
Dirs []Tree
Files []File
}
func BuildTree(dirPath string, meta []string) (Tree, error) {
@ -194,7 +194,7 @@ func getMaxId(dir string) (int64, error) {
func readPath(dirPath string, category []string, meta []string) (Tree, error) {
tree := Tree{}
tree.path = dirPath
tree.Path = dirPath
files, err := ioutil.ReadDir(dirPath)
if err != nil {
@ -208,7 +208,7 @@ func readPath(dirPath string, category []string, meta []string) (Tree, error) {
if err != nil {
return tree, err
}
tree.dirs = append(tree.dirs, nextDir)
tree.Dirs = append(tree.Dirs, nextDir)
continue
}
@ -220,7 +220,7 @@ func readPath(dirPath string, category []string, meta []string) (Tree, error) {
if err != nil {
return tree, err
}
tree.files = append(tree.files, nextFile)
tree.Files = append(tree.Files, nextFile)
}
return tree, nil
@ -228,10 +228,10 @@ func readPath(dirPath string, category []string, meta []string) (Tree, error) {
func readFile(file fs.FileInfo, fullPath string, category []string, meta []string) (File, error) {
f := File{
name: file.Name(),
fullPath: fullPath,
category: category,
meta: map[string]string{},
Name: file.Name(),
FullPath: fullPath,
Category: category,
Meta: map[string]string{},
}
osf, err := os.Open(fullPath)
@ -254,12 +254,16 @@ func readFile(file fs.FileInfo, fullPath string, category []string, meta []strin
tags = append(tags, strings.Trim(tag, " "))
}
f.tags = tags
f.Tags = tags
}
if strings.HasPrefix(line, "id:") {
line = strings.TrimPrefix(line, "id:")
f.Id = strings.Trim(line, " ")
}
for _, option := range meta {
if strings.HasPrefix(line, option) {
line = strings.TrimPrefix(line, option + ":")
f.meta[option] = strings.Trim(line, " ")
f.Meta[option] = strings.Trim(line, " ")
}
}
}

View file

@ -28,21 +28,21 @@ func TestSunny(t *testing.T) {
t.Errorf("Got error: %v", err)
}
if len(tree.dirs) != 1 {
t.Errorf("Should be 1 subdirectory, got %v", len(tree.dirs))
if len(tree.Dirs) != 1 {
t.Errorf("Should be 1 subdirectory, got %v", len(tree.Dirs))
}
if tree.dirs[0].files[0].name != "file3.md" {
t.Errorf("File should be 'file3.md', got %v", tree.dirs[0].files[0].name)
if tree.Dirs[0].Files[0].Name != "file3.md" {
t.Errorf("File should be 'file3.md', got %v", tree.Dirs[0].Files[0].Name)
}
if tree.files[0].name != "file1.md" {
t.Errorf("File should be 'file1.md', got %v", tree.files[0].name)
if tree.Files[0].Name != "file1.md" {
t.Errorf("File should be 'file1.md', got %v", tree.Files[0].Name)
}
if len(tree.files[0].tags) != 2 {
t.Errorf("File 'file1.md' should have 2 tags, got %v", len(tree.files[0].tags))
if len(tree.Files[0].Tags) != 2 {
t.Errorf("File 'file1.md' should have 2 tags, got %v", len(tree.Files[0].Tags))
}
if tree.files[0].tags[1] != "t2" {
t.Errorf("File 'file1.md' 2nd tag 't2', got %v", tree.files[0].tags[1])
if tree.Files[0].Tags[1] != "t2" {
t.Errorf("File 'file1.md' 2nd tag 't2', got %v", tree.Files[0].Tags[1])
}
t.Log(tree)
}
@ -62,11 +62,11 @@ func TestMeta(t *testing.T) {
t.Errorf("Got error: %v", err)
}
if tree.files[0].name != "file1.md" {
t.Errorf("File should be 'file1.md', got %v", tree.files[0].name)
if tree.Files[0].Name != "file1.md" {
t.Errorf("File should be 'file1.md', got %v", tree.Files[0].Name)
}
if tree.files[0].meta["option1"] != "test option" {
if tree.Files[0].Meta["option1"] != "test option" {
t.Error("File 'file1.md' should have 'option1' with value 'test option'")
}
}