-
-
Notifications
You must be signed in to change notification settings - Fork 135
/
selector.go
224 lines (197 loc) · 4.54 KB
/
selector.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package dasel
import (
"fmt"
"io"
"strings"
)
type ErrBadSelectorSyntax struct {
Part string
Message string
}
func (e ErrBadSelectorSyntax) Error() string {
return fmt.Sprintf("bad syntax: %s, around %s", e.Message, e.Part)
}
func (e ErrBadSelectorSyntax) Is(other error) bool {
o, ok := other.(*ErrBadSelectorSyntax)
if !ok {
return false
}
if o.Part != "" && o.Part != e.Part {
return false
}
if o.Message != "" && o.Message != e.Message {
return false
}
return true
}
type Selector struct {
funcName string
funcArgs []string
}
type SelectorResolver interface {
Original() string
Next() (*Selector, error)
}
func NewSelectorResolver(selector string, functions *FunctionCollection) SelectorResolver {
return &standardSelectorResolver{
functions: functions,
original: selector,
reader: strings.NewReader(selector),
separator: '.',
openFunc: '(',
closeFunc: ')',
argSeparator: ',',
escapeChar: '\\',
}
}
type standardSelectorResolver struct {
functions *FunctionCollection
original string
reader *strings.Reader
separator rune
openFunc rune
closeFunc rune
argSeparator rune
escapeChar rune
}
func (r *standardSelectorResolver) Original() string {
return r.original
}
// nextPart returns the next part.
// It returns true if there are more parts to the selector, or false if we reached the end.
func (r *standardSelectorResolver) nextPart() (string, bool) {
b := &strings.Builder{}
bracketDepth := 0
escaped := false
for {
readRune, _, err := r.reader.ReadRune()
if err == io.EOF {
return b.String(), false
}
if escaped {
b.WriteRune(readRune)
escaped = false
continue
} else if readRune == r.escapeChar {
b.WriteRune(readRune)
escaped = true
continue
} else if readRune == r.openFunc {
bracketDepth++
} else if readRune == r.closeFunc {
bracketDepth--
}
if readRune == r.separator && bracketDepth == 0 {
return b.String(), true
}
b.WriteRune(readRune)
}
}
func (r *standardSelectorResolver) Next() (*Selector, error) {
nextPart, moreParts := r.nextPart()
if nextPart == "" && !moreParts {
return nil, nil
}
if nextPart == "" && moreParts {
return &Selector{
funcName: "this",
funcArgs: []string{},
}, nil
}
if r.functions != nil {
if s := r.functions.ParseSelector(nextPart); s != nil {
return s, nil
}
}
var hasOpenedFunc, hasClosedFunc = false, false
bracketDepth := 0
var funcNameBuilder = &strings.Builder{}
var argBuilder = &strings.Builder{}
nextPartReader := strings.NewReader(nextPart)
funcName := ""
args := make([]string, 0)
escaped := false
for {
nextRune, _, err := nextPartReader.ReadRune()
if err == io.EOF {
if funcNameBuilder.Len() > 0 {
funcName = funcNameBuilder.String()
}
break
}
if err != nil {
return nil, fmt.Errorf("could not read selector: %w", err)
}
switch {
case nextRune == r.escapeChar && !escaped:
escaped = true
continue
case nextRune == r.openFunc && !escaped:
if !hasOpenedFunc {
hasOpenedFunc = true
funcName = funcNameBuilder.String()
if funcName == "" {
return nil, &ErrBadSelectorSyntax{
Part: nextPart,
Message: "function name required before open bracket",
}
}
} else {
argBuilder.WriteRune(nextRune)
}
bracketDepth++
case nextRune == r.closeFunc && !escaped:
if bracketDepth > 1 {
argBuilder.WriteRune(nextRune)
} else if bracketDepth == 1 {
hasClosedFunc = true
arg := argBuilder.String()
if arg != "" {
args = append(args, argBuilder.String())
}
} else if bracketDepth < 1 {
return nil, &ErrBadSelectorSyntax{
Part: nextPart,
Message: "too many closing brackets",
}
}
bracketDepth--
case hasOpenedFunc && nextRune == r.argSeparator && !escaped:
if bracketDepth > 1 {
argBuilder.WriteRune(nextRune)
} else if bracketDepth == 1 {
arg := argBuilder.String()
argBuilder.Reset()
if arg != "" {
args = append(args, arg)
}
}
case hasOpenedFunc:
if escaped {
escaped = false
}
argBuilder.WriteRune(nextRune)
case hasClosedFunc:
// Do not allow anything after the closeFunc
return nil, &ErrBadSelectorSyntax{
Part: nextPart,
Message: "selector function must end after closing bracket",
}
default:
if escaped {
escaped = false
}
funcNameBuilder.WriteRune(nextRune)
}
}
if !hasOpenedFunc {
return &Selector{
funcName: "property",
funcArgs: []string{funcName},
}, nil
}
return &Selector{
funcName: funcName,
funcArgs: args,
}, nil
}