From f06a04e0f003c556c9273b2c4660b201d179598e Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Wed, 27 Nov 2024 21:21:29 +0200 Subject: refactoring tags --- internal/entry/entry.go | 19 ++----- internal/entry/entry_test.go | 10 ++-- internal/entry/sharetags.go | 42 -------------- internal/entry/sharetags_test.go | 117 --------------------------------------- 4 files changed, 9 insertions(+), 179 deletions(-) delete mode 100644 internal/entry/sharetags.go delete mode 100644 internal/entry/sharetags_test.go (limited to 'internal/entry') diff --git a/internal/entry/entry.go b/internal/entry/entry.go index 3e21328..70b8396 100644 --- a/internal/entry/entry.go +++ b/internal/entry/entry.go @@ -9,7 +9,6 @@ import ( "strings" "time" - "codeberg.org/snonux/gos/internal/config" "codeberg.org/snonux/gos/internal/oi" "codeberg.org/snonux/gos/internal/prompt" "codeberg.org/snonux/gos/internal/timestamp" @@ -50,7 +49,7 @@ type Entry struct { Path string Time time.Time State State - tags map[string]struct{} + Tags map[string]struct{} } func (en Entry) String() string { @@ -64,7 +63,7 @@ func (en Entry) String() string { // or for inboxed: /foo.txt // or inboxed with tags: /foo.prio.ask.txt func New(filePath string) (Entry, error) { - en := Entry{Path: filePath, tags: make(map[string]struct{})} + en := Entry{Path: filePath, Tags: make(map[string]struct{})} // We want to get the STAMP! parts := strings.Split(filePath, ".") @@ -147,20 +146,10 @@ func (en *Entry) MarkPosted() error { } func (en Entry) HasTag(tag string) bool { - _, ok := en.tags[tag] + _, ok := en.Tags[tag] return ok } -// Valid tags are: share:foo[,...] -// whereas foo can be a supported platform such as linkedin, mastodon, etc. -// foo can also be prefixed with - to exclude it. See unit tests for examples. -func (en Entry) PlatformExcluded(args config.Args, platformStr string) (bool, error) { - s, err := newShareTags(args, en.tags) - fmt.Println(s) - return slices.Contains(s.excludes, platformStr) || - !slices.Contains(s.includes, platformStr), err -} - func (en Entry) Edit() error { if err := prompt.EditFile(en.Path); err != nil { return err @@ -184,7 +173,7 @@ func (en Entry) FileAction(question string) error { func (en Entry) extractTags(parts []string) { for _, part := range parts { if slices.Contains(validTags, part) || strings.HasPrefix(part, "share:") { - en.tags[part] = struct{}{} + en.Tags[part] = struct{}{} } } } diff --git a/internal/entry/entry_test.go b/internal/entry/entry_test.go index 6a54b05..f9df524 100644 --- a/internal/entry/entry_test.go +++ b/internal/entry/entry_test.go @@ -52,12 +52,12 @@ func TestEntryTags(t *testing.T) { for _, expectedTag := range strings.Split(tagsStr, ".") { if expectedTag == "invalid" { if en.HasTag(expectedTag) { - t.Errorf("didn't expect tag '%s' to be present, but got '%v'", expectedTag, en.tags) + t.Errorf("didn't expect tag '%s' to be present, but got '%v'", expectedTag, en.Tags) } continue } if !en.HasTag(expectedTag) { - t.Errorf("expected tag '%s' to be present, but got '%v'", expectedTag, en.tags) + t.Errorf("expected tag '%s' to be present, but got '%v'", expectedTag, en.Tags) } } } @@ -115,12 +115,12 @@ func TestHasTag(t *testing.T) { if err != nil { t.Error(err) } - if len(expectedTags) != len(en.tags) { - t.Errorf("expected '%d' tags but got '%d'", len(expectedTags), len(en.tags)) + if len(expectedTags) != len(en.Tags) { + t.Errorf("expected '%d' tags but got '%d'", len(expectedTags), len(en.Tags)) } for _, tag := range expectedTags { if !en.HasTag(tag) { - t.Errorf("expected tag '%s' but got '%s'", tag, en.tags) + t.Errorf("expected tag '%s' but got '%s'", tag, en.Tags) } } } diff --git a/internal/entry/sharetags.go b/internal/entry/sharetags.go deleted file mode 100644 index 3075e2f..0000000 --- a/internal/entry/sharetags.go +++ /dev/null @@ -1,42 +0,0 @@ -package entry - -import ( - "slices" - "strings" - - "codeberg.org/snonux/gos/internal/config" -) - -// TODO: Own package only dealing with tags, and put all tag code in there. -type shareTags struct { - includes []string // The platforms to include - excludes []string // The platforms to exclude -} - -func newShareTags(args config.Args, tags map[string]struct{}) (shareTags, error) { - var s shareTags - - for tag := range tags { - if !strings.HasPrefix(tag, "share:") { - continue - } - for _, t := range strings.Split(tag[6:], ":") { - if strings.HasPrefix(t, "-") { - s.excludes = append(s.excludes, strings.ToLower(t[1:])) - } else { - s.includes = append(s.includes, strings.ToLower(t)) - } - } - } - - if len(s.includes) == 0 { - for platformStr := range args.Platforms { - if slices.Contains(s.excludes, strings.ToLower(platformStr)) { - continue - } - s.includes = append(s.includes, strings.ToLower(platformStr)) - } - } - - return s, nil -} diff --git a/internal/entry/sharetags_test.go b/internal/entry/sharetags_test.go deleted file mode 100644 index 73ba8db..0000000 --- a/internal/entry/sharetags_test.go +++ /dev/null @@ -1,117 +0,0 @@ -package entry - -import ( - "slices" - "strings" - "testing" - - "codeberg.org/snonux/gos/internal/config" -) - -func TestShareTagsPositive(t *testing.T) { - args := config.Args{Platforms: map[string]int{ - "mastodon": 100, - "linkedin": 100, - }} - testTable := map[string]shareTags{ - "./foo/bar.without.tags.txt.20240101-010101.queued": { - includes: []string{"mastodon", "linkedin"}, - }, - "./foo/bar.share:linkeDin.txt.20240101-010101.queued": { - includes: []string{"linkedin"}, - }, - "./foo/bar.share:-LinkedIn.txt.20240101-010101.queued": { - includes: []string{"mastodon"}, - excludes: []string{"linkedin"}, - }, - "./foo/bar.share:linkedin:mastOdon.txt.20240101-010101.queued": { - includes: []string{"linkedin", "mastodon"}, - }, - "./foo/bar.share:linkediN:-mastodon:XCOM.txt.20240101-010101.queued": { - includes: []string{"linkedin", "xcom"}, - excludes: []string{"mastodon"}, - }, - "./foo/bar/ql-e7657e8a1ab573f84ad0dbc55199e937.share:-mastodon.txt.20241018-105524.queued": { - includes: []string{"linkedin"}, - excludes: []string{"mastodon"}, - }, - } - - for filePath, expectedResult := range testTable { - t.Run(filePath, func(t *testing.T) { - shareTags, err := newShareTags(args, filePathTags(filePath)) - if err != nil { - t.Error(err) - } - if !sameElements(shareTags.includes, expectedResult.includes) { - t.Errorf("Expected includes to be %v but got %v with %s", - expectedResult.includes, shareTags.includes, filePath) - } - if !sameElements(shareTags.excludes, expectedResult.excludes) { - t.Errorf("Expected excludes to be %v but got %v with %s", - expectedResult.excludes, shareTags.excludes, filePath) - } - }) - - } -} -func TestShareTagsNegative(t *testing.T) { - args := config.Args{Platforms: map[string]int{ - string("mastodon"): 100, - string("linkedin"): 100, - }} - testTable := map[string]shareTags{ - "./foo/bar.without.tags.txt.20240101-010101.queued": { - includes: []string{"linkedin"}, - }, - "./foo/bar.share:linkedIn.txt.20240101-010101.queued": { - includes: []string{"mastodon"}, - }, - "./foo/bar.share:-liNkedin.txt.20240101-010101.queued": { - includes: []string{"linkedin"}, - }, - "./foo/bar.share:linkedin:mastodon.txt.20240101-010101.queued": { - includes: []string{"oups", "mastodon"}, - }, - "./foo/bar.share:linkedin:-MASTODON:xcom.txt.20240101-010101.queued": { - includes: []string{"linkedin", "xcom"}, - excludes: []string{"mastodon", "xcom"}, - }, - } - - for filePath, unexpectedResult := range testTable { - t.Run(filePath, func(t *testing.T) { - shareTags, err := newShareTags(args, filePathTags(filePath)) - if err != nil { - t.Error(err) - } - if sameElements(shareTags.includes, unexpectedResult.includes) && - sameElements(shareTags.excludes, unexpectedResult.excludes) { - t.Errorf("expected %v not to be the actual result with %s", - unexpectedResult, filePath) - } - }) - - } -} - -// Can't use slices.Equal as order of elements may be different. -func sameElements(a, b []string) bool { - if len(a) != len(b) { - return false - } - for _, elem := range a { - if !slices.Contains(b, elem) { - return false - } - } - return true -} - -func filePathTags(filePath string) map[string]struct{} { - tags := make(map[string]struct{}) - for _, tag := range strings.Split(filePath, ".") { - tags[tag] = struct{}{} - } - return tags -} -- cgit v1.2.3