handle OR'd tags where the first tag matches nothing : assignment to nil map panic
This commit is contained in:
parent
185393d62d
commit
bd86e90174
2 changed files with 24 additions and 5 deletions
|
|
@ -40,19 +40,36 @@ func processQueries(tagmap map[string]Set, query Query) Set {
|
||||||
|
|
||||||
// initialize as first query
|
// initialize as first query
|
||||||
q := query.Tags[0]
|
q := query.Tags[0]
|
||||||
|
// NOTE: clone so that we don't accidentally overwrite the incoming tagmap
|
||||||
|
set = maps.Clone(tagmap[q])
|
||||||
|
|
||||||
// empty query
|
// empty query
|
||||||
// TODO: do I want to handle WILD and a tag? Actual regex?
|
// 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:
|
// NOTE: this is all files with at least one tag and therefore of value:
|
||||||
set.Union(slices.Collect(maps.Values(tagmap))...)
|
set.Union(slices.Collect(maps.Values(tagmap))...)
|
||||||
return set
|
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
|
for i := 0; i < len(query.Tags); i++ {
|
||||||
set = maps.Clone(tagmap[q])
|
|
||||||
// when queries < 2, this won't run
|
|
||||||
for i := 1; i < len(query.Tags); i++ {
|
|
||||||
q = query.Tags[i]
|
q = query.Tags[i]
|
||||||
switch query.Op {
|
switch query.Op {
|
||||||
case OR:
|
case OR:
|
||||||
|
|
|
||||||
|
|
@ -9,6 +9,8 @@ import (
|
||||||
type Set map[string]bool
|
type Set map[string]bool
|
||||||
|
|
||||||
// add members to the "set"
|
// 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) {
|
func (s Set) Add(mems ...string) {
|
||||||
for _, m := range mems {
|
for _, m := range mems {
|
||||||
s[m] = true
|
s[m] = true
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue