generated from WillAbides/goproject-tmpl
-
Notifications
You must be signed in to change notification settings - Fork 6
/
complex_readers.go
310 lines (283 loc) · 7.96 KB
/
complex_readers.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
package rjson
import (
"bytes"
"encoding/json"
"fmt"
"sync"
)
//go:generate script/generate-ragel-file read_machines.rl
const valueReaderMaxDepth = 10_000
// ValueReader is a handler for reading complex json data types (Objects and Arrays).
type ValueReader struct {
buf Buffer
pool sync.Pool
objVal map[string]interface{}
arrVal []interface{}
fieldNameBuf []byte
stringBuf []byte
depth int
newMapSize int
lastMapSize int
maxMapSize int
newSliceSize int
lastSliceSize int
}
func (h *ValueReader) borrowValueReader() *ValueReader {
x, ok := h.pool.Get().(*ValueReader)
if !ok {
x = &ValueReader{
depth: h.depth + 1,
}
}
x.newMapSize = 0
x.depth = h.depth + 1
return x
}
func (h *ValueReader) returnValueReader(x *ValueReader) {
x.arrVal = x.arrVal[:0]
h.pool.Put(x)
}
// HandleArrayValue implements ArrayValueHandler.HandleArrayValue
func (h *ValueReader) HandleArrayValue(data []byte) (p int, err error) {
var tknType TokenType
tknType, p, err = NextTokenType(data)
if err != nil {
return p, err
}
p--
data = data[p:]
var val interface{}
var pp int
switch tknType {
case ObjectStartType:
h2 := h.borrowValueReader()
if h2.depth > valueReaderMaxDepth {
return p, errMaxDepth
}
h2.newMapSize = h.maxMapSize
val, pp, err = h2.ReadObject(data)
mpLen := len(val.(map[string]interface{}))
if mpLen > h.maxMapSize {
h.maxMapSize = mpLen
}
h.returnValueReader(h2)
case ArrayStartType:
h2 := h.borrowValueReader()
if h2.depth > valueReaderMaxDepth {
return p, errMaxDepth
}
val, pp, err = h2.ReadArray(data)
h.returnValueReader(h2)
default:
val, pp, err = h.readSimpleValue(data, tknType)
}
if err == nil {
h.arrVal = append(h.arrVal, val)
}
return p + pp, err
}
// HandleObjectValue implements ValueObjectHandler.HandleObjectValue.
func (h *ValueReader) HandleObjectValue(fieldname, data []byte) (p int, err error) {
for i := 0; i < len(fieldname); i++ {
if fieldname[i] == '\\' {
h.fieldNameBuf, _, err = UnescapeStringContent(fieldname[i:], append(h.fieldNameBuf[:0], fieldname[:i]...))
if err != nil {
return 0, err
}
fieldname = h.fieldNameBuf
break
}
}
var tknType TokenType
tknType, p, err = NextTokenType(data)
if err != nil {
return p, err
}
p--
data = data[p:]
var val interface{}
var pp int
switch tknType {
case ObjectStartType:
h2 := h.borrowValueReader()
if h2.depth > valueReaderMaxDepth {
return p, errMaxDepth
}
h2.newMapSize = h.maxMapSize
val, pp, err = h2.ReadObject(data)
mpLen := len(val.(map[string]interface{}))
if mpLen > h.maxMapSize {
h.maxMapSize = mpLen
}
h.returnValueReader(h2)
case ArrayStartType:
h2 := h.borrowValueReader()
if h2.depth > valueReaderMaxDepth {
return p, errMaxDepth
}
val, pp, err = h2.ReadArray(data)
h.returnValueReader(h2)
default:
val, pp, err = h.readSimpleValue(data, tknType)
}
h.objVal[string(fieldname)] = val
return p + pp, err
}
func (h *ValueReader) readSimpleValue(data []byte, tknType TokenType) (val interface{}, p int, err error) {
switch tknType {
case NullType:
p, err = ReadNull(data)
return nil, p, err
case StringType:
h.stringBuf, p, err = ReadStringBytes(data, h.stringBuf[:0])
return string(h.stringBuf), p, err
case NumberType:
return ReadFloat64(data)
case TrueType, FalseType:
return ReadBool(data)
default:
return nil, p, fmt.Errorf("unexpected type")
}
}
// ReadValue allocates a ValueReader and returns ValueReader.ReadValue. You should probably use ValueReader.ReadValue
// instead so you don't have to allocate a new ValueReader for each call.
func ReadValue(data []byte) (val interface{}, p int, err error) {
h := ValueReader{}
return h.ReadValue(data)
}
// ReadValue reads a value at the beginning of data. The result will be a string, bool, nil, float64, []interface{}
// or map[string]interface{} depending on the json data type. p is the first position in data after the value.
func (h *ValueReader) ReadValue(data []byte) (val interface{}, p int, err error) {
var tknType TokenType
tknType, p, err = NextTokenType(data)
if err != nil {
return nil, p, err
}
p--
data = data[p:]
var pp int
switch tknType {
case ObjectStartType:
h2 := h.borrowValueReader()
val, pp, err = h2.ReadObject(data)
h.returnValueReader(h2)
case ArrayStartType:
h2 := h.borrowValueReader()
val, pp, err = h2.ReadArray(data)
h.returnValueReader(h2)
default:
val, pp, err = h.readSimpleValue(data, tknType)
}
if err != nil {
return nil, p + pp, err
}
return val, p + pp, err
}
func readValueCompat(data []byte) (val interface{}, p int, err error) {
decoder := json.NewDecoder(bytes.NewReader(data))
err = decoder.Decode(&val)
if err != nil {
return nil, int(decoder.InputOffset()), fmt.Errorf("invalid json")
}
return val, int(decoder.InputOffset()), nil
}
// ReadObject allocates a ValueReader and returns ValueReader.ReadObject. You should probably use ValueReader.ReadObject
// instead so you don't have to allocate a new ValueReader for each call.
func ReadObject(data []byte) (val map[string]interface{}, p int, err error) {
h := ValueReader{}
return h.ReadObject(data)
}
// ReadObject reads an object value from the front of data and returns it as a map[string]interface{}. p is the first
// position in data after the value.
func (h *ValueReader) ReadObject(data []byte) (val map[string]interface{}, p int, err error) {
if h.depth == 0 {
h.depth = 1
defer func() { h.depth = 0 }()
}
mapSize := h.newMapSize
if mapSize == 0 {
mapSize = h.lastMapSize
}
h.objVal = make(map[string]interface{}, mapSize)
p, err = HandleObjectValues(data[p:], h, &h.buf)
if err != nil {
return nil, p, err
}
valLen := len(h.objVal)
// make sure to return err for null
if valLen == 0 {
tknType, _, tknErr := NextTokenType(data)
if tknErr == nil && tknType == NullType {
return nil, p, errInvalidObject
}
}
h.lastMapSize = valLen
return h.objVal, p, nil
}
func readObjectCompat(data []byte) (val map[string]interface{}, p int, err error) {
decoder := json.NewDecoder(bytes.NewReader(data))
tkn, err := decoder.Token()
if err != nil {
return nil, int(decoder.InputOffset()), errInvalidObject
}
if tkn != json.Delim('{') {
return nil, int(decoder.InputOffset()), errInvalidObject
}
decoder = json.NewDecoder(bytes.NewReader(data))
err = decoder.Decode(&val)
if err != nil {
return nil, int(decoder.InputOffset()), err
}
return val, int(decoder.InputOffset()), nil
}
// ReadArray allocates a ValueReader and returns ValueReader.ReadArray. You should probably use ValueReader.ReadArray
// instead so you don't have to allocate a new ValueReader for each call.
func ReadArray(data []byte) (val []interface{}, p int, err error) {
h := ValueReader{}
return h.ReadArray(data)
}
// ReadArray reads an array from the front of data and returns it as a []interface{}. p is the first position in data
// after the value.
func (h *ValueReader) ReadArray(data []byte) (val []interface{}, p int, err error) {
if h.depth == 0 {
h.depth = 1
defer func() {
h.depth = 0
}()
}
sliceSize := h.newSliceSize
if sliceSize == 0 {
sliceSize = h.lastSliceSize
}
h.arrVal = make([]interface{}, 0, sliceSize)
p, err = HandleArrayValues(data, h, &h.buf)
if err != nil {
return nil, p, err
}
valLen := len(h.arrVal)
// make sure to return err for null
if valLen == 0 {
tknType, _, tknErr := NextTokenType(data)
if tknErr == nil && tknType == NullType {
return nil, p, errInvalidArray
}
}
h.lastSliceSize = valLen
return h.arrVal, p, err
}
func readArrayCompat(data []byte) (val []interface{}, p int, err error) {
decoder := json.NewDecoder(bytes.NewReader(data))
tkn, err := decoder.Token()
if err != nil {
return nil, int(decoder.InputOffset()), errInvalidArray
}
if tkn != json.Delim('[') {
return nil, int(decoder.InputOffset()), errInvalidArray
}
decoder = json.NewDecoder(bytes.NewReader(data))
err = decoder.Decode(&val)
if err != nil {
return nil, int(decoder.InputOffset()), err
}
return val, int(decoder.InputOffset()), nil
}