tag : simplify nil handling
This commit is contained in:
parent
bd86e90174
commit
762ced6734
2 changed files with 19 additions and 21 deletions
|
|
@ -34,7 +34,7 @@ func dateRange(entries []Entry, date string) []Entry {
|
||||||
func processQueries(tagmap map[string]Set, query Query) Set {
|
func processQueries(tagmap map[string]Set, query Query) Set {
|
||||||
set := Set{}
|
set := Set{}
|
||||||
// sanity check:
|
// sanity check:
|
||||||
if len(query.Tags) < 1 {
|
if len(query.Tags) == 0 {
|
||||||
return set
|
return set
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -42,6 +42,11 @@ func processQueries(tagmap map[string]Set, query Query) Set {
|
||||||
q := query.Tags[0]
|
q := query.Tags[0]
|
||||||
// NOTE: clone so that we don't accidentally overwrite the incoming tagmap
|
// NOTE: clone so that we don't accidentally overwrite the incoming tagmap
|
||||||
set = maps.Clone(tagmap[q])
|
set = maps.Clone(tagmap[q])
|
||||||
|
// 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 {
|
||||||
|
set = Set{}
|
||||||
|
}
|
||||||
|
|
||||||
// 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?
|
||||||
|
|
@ -50,32 +55,16 @@ func processQueries(tagmap map[string]Set, query Query) Set {
|
||||||
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:
|
// non-empty query of single tag: no need for set logic:
|
||||||
if len(query.Tags) == 1 {
|
if len(query.Tags) == 1 {
|
||||||
return set
|
return set
|
||||||
}
|
}
|
||||||
// if first query matches nothing: set will be nil and Union will fail:
|
for _, t := range query.Tags {
|
||||||
// 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
|
|
||||||
}
|
|
||||||
|
|
||||||
for i := 0; i < len(query.Tags); i++ {
|
|
||||||
q = query.Tags[i]
|
|
||||||
switch query.Op {
|
switch query.Op {
|
||||||
case OR:
|
case OR:
|
||||||
set.Union(tagmap[q])
|
set.Union(tagmap[t])
|
||||||
case AND:
|
case AND:
|
||||||
set.Intersect(tagmap[q])
|
set.Intersect(tagmap[t])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return set
|
return set
|
||||||
|
|
|
||||||
|
|
@ -89,6 +89,15 @@ func TestBadTag(t *testing.T) {
|
||||||
assert.False(t, ok)
|
assert.False(t, ok)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestBadTagOr(t *testing.T) {
|
||||||
|
entries := entriesGlobOrStdin(TEST_PATTERN)
|
||||||
|
tagmap := makeTagmap(entries)
|
||||||
|
queries := parseQuery("flob,bar")
|
||||||
|
fs := processQueries(tagmap, queries)
|
||||||
|
expected := Set{"01.foo.md": true, "02.foo.md": true, "03.bar.md": true}
|
||||||
|
assert.Equal(t, expected, fs)
|
||||||
|
}
|
||||||
|
|
||||||
// Since entriesGlobOrStdin() involves filesystem reads, we test the underlying logic.
|
// Since entriesGlobOrStdin() involves filesystem reads, we test the underlying logic.
|
||||||
func BenchmarkParseContent(b *testing.B) {
|
func BenchmarkParseContent(b *testing.B) {
|
||||||
e := entriesGlobOrStdin(TEST_PATTERN)[0]
|
e := entriesGlobOrStdin(TEST_PATTERN)[0]
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue