-
Notifications
You must be signed in to change notification settings - Fork 456
Expand file tree
/
Copy pathpath_compiler.go
More file actions
226 lines (199 loc) · 5.48 KB
/
Copy pathpath_compiler.go
File metadata and controls
226 lines (199 loc) · 5.48 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
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
225
226
package jsonparser
import (
"errors"
"strings"
)
var (
errEmptyPath = errors.New("jsonparser: path must not be empty")
errMalformedPath = errors.New("jsonparser: malformed path")
errUnterminatedKey = errors.New("jsonparser: unterminated quoted key")
)
// ParsePath converts a JSONPath-style path into the path components accepted
// by Get, Set, Delete, ArrayEach, and EachKey.
// SYS-REQ-114
func ParsePath(jsonPath string) ([]string, error) {
if jsonPath == "" {
return nil, errEmptyPath
}
switch {
case jsonPath == "$":
return []string{}, nil
case strings.HasPrefix(jsonPath, "$."):
jsonPath = jsonPath[2:]
case strings.HasPrefix(jsonPath, "$["):
jsonPath = jsonPath[1:]
case jsonPath[0] == '$':
return nil, errMalformedPath
}
if jsonPath == "" {
return nil, errMalformedPath
}
// A path component is either a dot-delimited key or bracket notation.
// Counting both separators gives an exact capacity for ordinary paths and
// a safe upper bound for quoted keys containing dots or brackets.
parts := make([]string, 0, 1+strings.Count(jsonPath, ".")+strings.Count(jsonPath, "["))
for pos := 0; pos < len(jsonPath); {
switch jsonPath[pos] {
case '.':
return nil, errMalformedPath
case '"':
key, next, err := parseQuotedPathKey(jsonPath, pos)
if err != nil {
return nil, err
}
parts = append(parts, key)
pos = next
case '[':
// A root array path or a bracket immediately following a dot has
// no key component before its index.
default:
start := pos
for pos < len(jsonPath) && jsonPath[pos] != '.' && jsonPath[pos] != '[' {
if jsonPath[pos] == ']' || jsonPath[pos] == '"' {
return nil, errMalformedPath
}
pos++
}
if start == pos {
return nil, errMalformedPath
}
parts = append(parts, jsonPath[start:pos])
}
for pos < len(jsonPath) && jsonPath[pos] == '[' {
component, next, err := parseBracketPathComponent(jsonPath, pos)
if err != nil {
return nil, err
}
parts = append(parts, component)
pos = next
}
if pos == len(jsonPath) {
break
}
if jsonPath[pos] != '.' {
return nil, errMalformedPath
}
pos++
if pos == len(jsonPath) {
return nil, errMalformedPath
}
}
if len(parts) == 0 {
return nil, errMalformedPath
}
return parts, nil
}
// SYS-REQ-114
func parseQuotedPathKey(path string, start int) (string, int, error) {
contentStart := start + 1
for pos := contentStart; pos < len(path); pos++ {
switch path[pos] {
case '\\':
// Skip the escaped byte while locating the closing quote. Unescape
// below performs complete JSON escape validation.
pos++
if pos >= len(path) {
return "", 0, errUnterminatedKey
}
case '"':
content := path[contentStart:pos]
if strings.IndexByte(content, '\\') == -1 {
return content, pos + 1, nil
}
unescaped, err := Unescape([]byte(content), nil)
if err != nil {
return "", 0, errMalformedPath
}
return string(unescaped), pos + 1, nil
default:
if path[pos] < 0x20 {
return "", 0, errMalformedPath
}
}
}
return "", 0, errUnterminatedKey
}
// SYS-REQ-114
func parseBracketPathComponent(path string, start int) (string, int, error) {
end := start + 1
for end < len(path) && path[end] != ']' {
if path[end] == '[' || path[end] == '"' {
return "", 0, errMalformedPath
}
end++
}
if end == len(path) || end == start+1 {
return "", 0, errMalformedPath
}
if path[start+1] == '*' {
if end != start+2 {
return "", 0, errMalformedPath
}
} else {
for pos := start + 1; pos < end; pos++ {
if path[pos] < '0' || path[pos] > '9' {
return "", 0, errMalformedPath
}
}
}
return path[start : end+1], end + 1, nil
}
// CompiledPath stores a parsed path for repeated operations.
// SYS-REQ-114
type CompiledPath struct {
parts []string
}
// CompilePath parses jsonPath once for reuse.
// SYS-REQ-114
func CompilePath(jsonPath string) (CompiledPath, error) {
parts, err := ParsePath(jsonPath)
if err != nil {
return CompiledPath{}, err
}
return CompiledPath{parts: parts}, nil
}
// Get resolves the compiled path in data.
// Verifies: SYS-REQ-001 (Get)
// SYS-REQ-114
func (c CompiledPath) Get(data []byte) ([]byte, ValueType, int, error) {
return Get(data, c.parts...)
}
// GetString resolves the compiled path and returns its string value.
// SYS-REQ-114
func (c CompiledPath) GetString(data []byte) (string, error) {
return GetString(data, c.parts...)
}
// GetInt resolves the compiled path and returns its integer value.
// SYS-REQ-114
func (c CompiledPath) GetInt(data []byte) (int64, error) {
return GetInt(data, c.parts...)
}
// Set writes value at the compiled path.
// Verifies: SYS-REQ-009 (Set)
// SYS-REQ-114
func (c CompiledPath) Set(data []byte, value []byte) ([]byte, error) {
return Set(data, value, c.parts...)
}
// Delete removes the value at the compiled path.
// SYS-REQ-114
func (c CompiledPath) Delete(data []byte) []byte {
return Delete(data, c.parts...)
}
// ArrayEach iterates over the array at the compiled path.
// SYS-REQ-114
func (c CompiledPath) ArrayEach(data []byte, cb func([]byte, ValueType, int, error)) (int, error) {
return ArrayEach(data, cb, c.parts...)
}
// EachKey resolves the compiled path using EachKey.
// SYS-REQ-114
func (c CompiledPath) EachKey(data []byte, cb func(int, []byte, ValueType, error)) error {
EachKey(data, cb, c.parts)
return nil
}
// Parts returns a copy of the compiled path components.
// SYS-REQ-114
func (c CompiledPath) Parts() []string {
parts := make([]string, len(c.parts))
copy(parts, c.parts)
return parts
}