From a9e6c702b9525ee3ac1be8cbfd6b8ad139303191 Mon Sep 17 00:00:00 2001 From: Arnas Udovicius Date: Wed, 12 May 2021 08:17:43 +0300 Subject: [PATCH] export File and Tree fields --- tree.go | 40 ++++++++++++++++++++++------------------ tree_test.go | 26 +++++++++++++------------- 2 files changed, 35 insertions(+), 31 deletions(-) diff --git a/tree.go b/tree.go index 539ad07..1744a1e 100644 --- a/tree.go +++ b/tree.go @@ -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, " ") } } } diff --git a/tree_test.go b/tree_test.go index 1c7c3fc..42b048b 100644 --- a/tree_test.go +++ b/tree_test.go @@ -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'") } }