-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathselection.go
148 lines (139 loc) · 2.87 KB
/
selection.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
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package jsonmask
import (
"fmt"
"strings"
"unicode/utf8"
)
type Selection map[string]Selection
// used for testing purposes
func (s Selection) equal(other Selection) bool {
if other == nil {
return false
}
if len(s) != len(other) {
return false
}
for k, v := range s {
if !v.equal(other[k]) {
return false
}
}
return true
}
// The syntax is loosely based on XPath:
//
// a select a field 'a'
// a,b,c comma-separated list will select multiple fields
// a/b/c path will select a field from its parent
// a(b,c) sub-selection will select many fields from a parent
// a/*/c the star * wildcard will select all items in a field
// a,b/c(d,e(f,g/h)),i
//
func Compile(str string) (Selection, error) {
if !utf8.ValidString(str) {
return nil, fmt.Errorf("invalid fields")
}
tokens := make([]string, 0, len(str)/2)
var state uint8
var b strings.Builder
b.Grow(len(str) / 2)
for _, r := range str {
switch r {
case ',':
switch state {
case 4:
tokens = append(tokens, b.String())
case 3:
// nothing to do
default:
return nil, fmt.Errorf("invalid char before ','")
}
tokens = append(tokens, ",")
b.Reset()
state = 1
case '/':
if state != 4 {
return nil, fmt.Errorf("invalid char before '/'")
}
tokens = append(tokens, b.String())
tokens = append(tokens, "/")
b.Reset()
state = 2
case '(':
if state != 4 {
return nil, fmt.Errorf("invalid char before '('")
}
tokens = append(tokens, b.String())
tokens = append(tokens, "(")
b.Reset()
state = 2
case ')':
switch state {
case 4:
tokens = append(tokens, b.String())
case 3:
// nothing to do
default:
return nil, fmt.Errorf("invalid char before ')'")
}
tokens = append(tokens, ")")
b.Reset()
state = 3
default:
if state == 3 {
return nil, fmt.Errorf("invalid ')' before a char")
}
b.WriteRune(r)
state = 4
}
}
switch state {
case 4:
tokens = append(tokens, b.String())
case 3:
// nothing to do
default:
return nil, fmt.Errorf("invalid end")
}
node := make(Selection)
err := buildSelection(tokens, node)
return node, err
}
func buildSelection(tokens []string, root Selection) error {
if len(tokens) == 0 {
return nil
}
var child Selection
node := root
for i := 0; i < len(tokens); i++ {
switch tokens[i] {
case ",":
node = root
case "/":
node = child
case "(":
end := findCloseIndex(tokens, i+1)
if end == -1 {
return fmt.Errorf("sub-selector not close")
}
if err := buildSelection(tokens[i+1:end], child); err != nil {
return err
}
i = end
case ")":
return fmt.Errorf("invalid field char: ')'")
default:
child = make(Selection)
node[tokens[i]] = child
}
}
return nil
}
func findCloseIndex(tokens []string, start int) int {
for i := len(tokens) - 1; i >= start; i-- {
if tokens[i] == ")" {
return i
}
}
return -1
}