-
Notifications
You must be signed in to change notification settings - Fork 1
/
redux_reader_match.go
73 lines (63 loc) 路 1.41 KB
/
redux_reader_match.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package kvas
import (
"golang.org/x/exp/maps"
"golang.org/x/exp/slices"
"strings"
)
func (rdx *redux) MatchAsset(asset string, terms []string, scope []string, options ...MatchOption) []string {
if scope == nil {
scope = rdx.Keys(asset)
}
matches := make(map[string]interface{})
for _, term := range terms {
if !slices.Contains(options, CaseSensitive) {
term = strings.ToLower(term)
}
for _, key := range scope {
if values, ok := rdx.GetAllValues(asset, key); !ok {
continue
} else if anyValueMatchesTerm(term, values, options...) {
matches[key] = nil
}
}
}
return maps.Keys(matches)
}
type MatchOption int
const (
CaseSensitive = iota
FullMatch
)
func (rdx *redux) Match(query map[string][]string, options ...MatchOption) []string {
var matches []string
for asset, terms := range query {
if !rdx.HasAsset(asset) {
continue
}
matches = rdx.MatchAsset(asset, terms, matches, options...)
}
return matches
}
func anyValueMatchesTerm(term string, values []string, options ...MatchOption) bool {
anyCase := true
contains := true
if options != nil {
anyCase = !slices.Contains(options, CaseSensitive)
contains = !slices.Contains(options, FullMatch)
}
for _, val := range values {
if anyCase {
val = strings.ToLower(val)
}
if contains {
if strings.Contains(val, term) {
return true
}
} else {
if val == term {
return true
}
}
}
return false
}