diff --git a/tag/process.go b/tag/process.go index a2fa4eb..0ba9f8b 100644 --- a/tag/process.go +++ b/tag/process.go @@ -40,19 +40,36 @@ func processQueries(tagmap map[string]Set, query Query) Set { // initialize as first query q := query.Tags[0] + // NOTE: clone so that we don't accidentally overwrite the incoming tagmap + set = maps.Clone(tagmap[q]) // empty query // TODO: do I want to handle WILD and a tag? Actual regex? - if query.Op == WILD && q == "" { + if len(query.Tags) == 1 && query.Op == WILD && q == "" { // NOTE: this is all files with at least one tag and therefore of value: set.Union(slices.Collect(maps.Values(tagmap))...) return set } + // no need for set logic in this case: + if len(query.Tags) == 1 { + return set + } + // if first query matches nothing: set will be nil and Union will fail: + // TODO: solve this in set.Union by moving to pointer receiver. + if set == nil { + for _, t := range query.Tags { + if tagmap[t] != nil { + set = tagmap[t] + break + } + } + } + // all tagmap[q] are still nil: + if set == nil { + return set + } - // NOTE: clone so that we don't accidentally overwrite the incoming tagmap - set = maps.Clone(tagmap[q]) - // when queries < 2, this won't run - for i := 1; i < len(query.Tags); i++ { + for i := 0; i < len(query.Tags); i++ { q = query.Tags[i] switch query.Op { case OR: diff --git a/tag/set.go b/tag/set.go index 6015d37..d6fd926 100644 --- a/tag/set.go +++ b/tag/set.go @@ -9,6 +9,8 @@ import ( type Set map[string]bool // add members to the "set" +// TODO: this probably should have been a pointer receiver to handle the nil map case. +// currently doing some roundabout checks before calling. func (s Set) Add(mems ...string) { for _, m := range mems { s[m] = true