Repository navigation
Expand file tree
/
Copy pathwalk.go
More file actions
107 lines (102 loc) · 2.82 KB
/
Copy pathwalk.go
File metadata and controls
107 lines (102 loc) · 2.82 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package dbmeta
import "strings"
// withDefaults returns args with the default of every parameter it leaves
// out, for a query that Binding.Walk answers. An argument the query does not
// take is ErrUnknownParam, and a parameter with no default that args leaves
// out is ErrMissingParam, as they are for a statement.
func withDefaults(params []Param, args map[string]any) (map[string]any, error) {
known := make(map[string]bool, len(params))
for _, p := range params {
known[p.Name] = true
}
for name := range args {
if !known[name] {
return nil, ErrUnknownParam
}
}
out := make(map[string]any, len(params))
for _, p := range params {
v, ok := args[p.Name]
switch {
case ok:
// A list is joined by commas, as bind joins it for a statement.
if list, isList := v.([]string); isList {
v = strings.Join(list, ",")
}
out[p.Name] = v
case p.Default != nil:
out[p.Name] = p.Default
default:
return nil, ErrMissingParam
}
}
return out, nil
}
// Like reports whether s matches pattern the way SQL's LIKE does: % is any
// run of characters, _ is one, and a backslash makes the next character its
// own. An empty pattern matches everything, which is what every parameter
// here means by empty. It is for a query that Binding.Walk answers, whose
// statements cannot take the pattern, so the rows are matched here. See D146.
func Like(pattern, s string) bool {
if pattern == "" {
return true
}
p, t := []rune(pattern), []rune(s)
var match func(i, j int) bool
match = func(i, j int) bool {
for i < len(p) {
switch c := p[i]; c {
case '%':
for i < len(p) && p[i] == '%' {
i++
}
if i == len(p) {
return true
}
for k := j; k <= len(t); k++ {
if match(i, k) {
return true
}
}
return false
case '_':
if j == len(t) {
return false
}
i, j = i+1, j+1
default:
if c == '\\' && i+1 < len(p) {
i++
c = p[i]
}
if j == len(t) || t[j] != c {
return false
}
i, j = i+1, j+1
}
}
return j == len(t)
}
return match(0, 0)
}
// LikeFold is [Like] with the case of letters ignored, for a product whose
// unquoted names are case insensitive, as CQL's are. A caller passes the
// pattern the way a person typed it, and the model folds both sides. See D202.
func LikeFold(pattern, s string) bool {
return Like(strings.ToLower(pattern), strings.ToLower(s))
}
// ListHas reports whether list, which is words joined by commas, is empty or
// names word. It reads a parameter such as types, which holds a list, for a
// model that filters in Go. An empty list means every word, as every
// parameter here means by empty. [InList] is the same test as SQL.
func ListHas(list, word string) bool {
if list == "" {
return true
}
for w := range strings.SplitSeq(list, ",") {
if w == word {
return true
}
}
return false
}