Skip to content

Commit

Permalink
feature: update TagMap Reindex() to unmarshal actual Note objects
Browse files Browse the repository at this point in the history
  • Loading branch information
Omnikron13 committed May 18, 2024
1 parent 48043ae commit 71548c4
Showing 1 changed file with 5 additions and 19 deletions.
24 changes: 5 additions & 19 deletions tag/tagmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ import (
"os"
"path/filepath"

"github.com/omnikron13/zelkata/note"
"github.com/omnikron13/zelkata/paths"

"gopkg.in/yaml.v3"
)

// TagMap is a map of tag names to Tag structs, representing all tags in the system.
Expand Down Expand Up @@ -77,30 +76,17 @@ func (m *TagMap) Reindex() error {
return err
}
for _, file := range files {
// TODO: rework Note/Meta so they can be unmarshalled into properly and replace this hacky generic map
note := map[string]any{}
b, err := os.ReadFile(filepath.Join(paths.Notes(), file.Name()))
if err != nil {
return err
}
if err := yaml.Unmarshal(b, note); err != nil {
note, err := note.ReadFile(filepath.Join(paths.Notes(), file.Name()))
if err != nil {
return err
}
tags, ok := note["tags"].([]any)
if !ok {
return errors.New("Tags field is not an array")
}
for _, t := range tags {
t, ok := t.(string)
if !ok {
return errors.New("Tag is not a string")
}
for _, t := range note.Tags {
tag := m.Get(t)
if tag == nil {
tag = &Tag{Name: t, Notes: []string{}}
_ = m.Add(t, tag)
}
tag.Notes = append(tag.Notes, note["id"].(string))
tag.Notes = append(tag.Notes, note.ID)
}
}
return nil
Expand Down

0 comments on commit 71548c4

Please sign in to comment.