Skip to content

Commit

Permalink
feature: moved Tag.Parents to a set
Browse files Browse the repository at this point in the history
  • Loading branch information
Omnikron13 committed May 19, 2024
1 parent a147cb5 commit 1691d54
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
13 changes: 7 additions & 6 deletions tag/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,11 @@ type Tag struct {
Aliases []string


// TODO: define an actual TagSet type for this kind of functionality?
// Parents maps 'canonical' (human readable) tag names to their normalised (path friendly) form. It is implemented as
// a map rather than a simple slice to essentially act as a set, with the normalised forms being just a convenience.
Parents map[string]string
// Parents is a set of Tags that can be considered to be directly 'above' this tag in a perceptual hierarchy. It is
// probably best to lean on the broad side when considering what conceptually constitutes a 'parent'. For example,
// 'mathematics' could be considered a parent of 'algebra', 'geometry', etc. but also 'physics', 'engineering', even
// 'music'.
Parents sets.Set[string]

// Children ?

Expand Down Expand Up @@ -220,9 +221,9 @@ func (t *Tag) UnmarshalYAML(value *yaml.Node) error {
t.Icon = icon.(string)
}
if parents, ok := data["parents"]; ok {
t.Parents = map[string]string{}
t.Parents = sets.New[string]()
for _, p := range parents.([]any) {
t.Parents[p.(string)] = normaliseName(p.(string))
t.Parents.Insert(p.(string))
}
}
if relations, ok := data["relations"]; ok {
Expand Down
20 changes: 12 additions & 8 deletions tag/tag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ func Test_LoadPath(t *testing.T) {
func Test_normaliseName(t *testing.T) {
assert.Equal(t, "test-tag-name", normaliseName("Test TAG name"))
assert.Equal(t, "already-normalised", normaliseName("already-normalised"))
tags, _ := LoadAll()
for name, _ := range tags {
t.Logf("tag: %v", name)
}
}


Expand Down Expand Up @@ -56,10 +60,10 @@ func Test_MarshalYAML(t *testing.T) {
"QWERTYUIOP",
"ASDFGHJKLZ",
),
Parents: map[string]string {
"Parent 1": "parent-1",
"Parent Number Two": "parent-number-two",
},
Parents: sets.New(
"Parent 1",
"Parent Number Two",
),
Relations: map[string]string {
"Relation 1": "similar subject",
"Relation Number Two": "first encountered in the same book",
Expand Down Expand Up @@ -101,10 +105,10 @@ func Test_UnmarshalYAML(t *testing.T) {
"QWERTYUIOP",
"ASDFGHJKLZ",
),
Parents: map[string]string {
"Parent 1": "parent-1",
"Parent Number Two": "parent-number-two",
},
Parents: sets.New(
"Parent 1",
"Parent Number Two",
),
Relations: map[string]string {
"Relation 1": "similar subject",
"Relation Number Two": "first encountered in the same book",
Expand Down

0 comments on commit 1691d54

Please sign in to comment.