Skip to content

Commit 4b32de2

Browse files
committed
Merge branch 'pendo-io-cleanup-endfuncs'
2 parents e6e4e5f + 68a66e8 commit 4b32de2

File tree

2 files changed

+48
-38
lines changed

2 files changed

+48
-38
lines changed

parser.go

Lines changed: 18 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ import (
1111

1212
// Errors
1313
var (
14-
KeyPathNotFoundError = errors.New("Key path not found")
14+
KeyPathNotFoundError = errors.New("Key path not found")
1515
UnknownValueTypeError = errors.New("Unknown value type")
16-
MalformedJsonError = errors.New("Malformed JSON error")
17-
MalformedStringError = errors.New("Value is string, but can't find closing '\"' symbol")
18-
MalformedArrayError = errors.New("Value is array, but can't find closing ']' symbol")
19-
MalformedObjectError = errors.New("Value looks like object, but can't find closing '}' symbol")
20-
MalformedValueError = errors.New("Value looks like Number/Boolean/None, but can't find its end: ',' or '}' symbol")
16+
MalformedJsonError = errors.New("Malformed JSON error")
17+
MalformedStringError = errors.New("Value is string, but can't find closing '\"' symbol")
18+
MalformedArrayError = errors.New("Value is array, but can't find closing ']' symbol")
19+
MalformedObjectError = errors.New("Value looks like object, but can't find closing '}' symbol")
20+
MalformedValueError = errors.New("Value looks like Number/Boolean/None, but can't find its end: ',' or '}' symbol")
2121
)
2222

2323
func tokenEnd(data []byte) int {
@@ -31,19 +31,12 @@ func tokenEnd(data []byte) int {
3131
return -1
3232
}
3333

34-
35-
// Find position of next character which is not ' ', ',', '}' or ']'
36-
func nextToken(data []byte, skipComma bool) int {
34+
// Find position of next character which is not whitespace
35+
func nextToken(data []byte) int {
3736
for i, c := range data {
3837
switch c {
3938
case ' ', '\n', '\r', '\t':
4039
continue
41-
case ',':
42-
if !skipComma {
43-
continue
44-
} else {
45-
return i
46-
}
4740
default:
4841
return i
4942
}
@@ -125,18 +118,18 @@ func searchKeys(data []byte, keys ...string) int {
125118
i += strEnd
126119
keyEnd := i - 1
127120

128-
valueOffset := nextToken(data[i:], true)
121+
valueOffset := nextToken(data[i:])
129122
if valueOffset == -1 {
130123
return -1
131124
}
132125

133126
i += valueOffset
134127

135128
// if string is a Key, and key level match
136-
if data[i] == ':'{
129+
if data[i] == ':' {
137130
key := unsafeBytesToString(data[keyBegin:keyEnd])
138131

139-
if keyLevel == level-1 && // If key nesting level match current object nested level
132+
if keyLevel == level-1 && // If key nesting level match current object nested level
140133
keys[level-1] == key {
141134
keyLevel++
142135
// If we found all keys in path
@@ -203,8 +196,7 @@ func Get(data []byte, keys ...string) (value []byte, dataType ValueType, offset
203196
}
204197

205198
// Go to closest value
206-
nO := nextToken(data[offset:], false)
207-
199+
nO := nextToken(data[offset:])
208200
if nO == -1 {
209201
return []byte{}, NotExist, -1, MalformedJsonError
210202
}
@@ -286,19 +278,6 @@ func Get(data []byte, keys ...string) (value []byte, dataType ValueType, offset
286278
return value, dataType, endOffset, nil
287279
}
288280

289-
func nextArrayItem(data []byte) int {
290-
for i, c := range data {
291-
switch c {
292-
case ' ', '\n', '\r', '\t':
293-
continue
294-
default:
295-
return i
296-
}
297-
}
298-
299-
return -1
300-
}
301-
302281
// ArrayEach is used when iterating arrays, accepts a callback function with the same return arguments as `Get`.
303282
func ArrayEach(data []byte, cb func(value []byte, dataType ValueType, offset int, err error), keys ...string) (err error) {
304283
if len(data) == 0 {
@@ -313,8 +292,7 @@ func ArrayEach(data []byte, cb func(value []byte, dataType ValueType, offset int
313292
}
314293

315294
// Go to closest value
316-
nO := nextToken(data[offset:], false)
317-
295+
nO := nextToken(data[offset:])
318296
if nO == -1 {
319297
return MalformedJsonError
320298
}
@@ -345,11 +323,11 @@ func ArrayEach(data []byte, cb func(value []byte, dataType ValueType, offset int
345323

346324
offset += o
347325

348-
nextItem := nextArrayItem(data[offset:])
349-
if nextItem == -1 {
326+
skipToToken := nextToken(data[offset:])
327+
if skipToToken == -1 {
350328
return MalformedArrayError
351329
}
352-
offset += nextItem
330+
offset += skipToToken
353331

354332
if data[offset] == ']' {
355333
break
@@ -358,6 +336,8 @@ func ArrayEach(data []byte, cb func(value []byte, dataType ValueType, offset int
358336
if data[offset] != ',' {
359337
return MalformedArrayError
360338
}
339+
340+
offset++
361341
}
362342

363343
return nil

parser_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -565,3 +565,33 @@ func TestGetSlice(t *testing.T) {
565565
},
566566
)
567567
}
568+
569+
func TestArrayEach(t *testing.T) {
570+
mock := []byte(`{"a": { "b":[{"x": 1} ,{"x":2},{ "x":3}, {"x":4} ]}}`)
571+
count := 0
572+
573+
ArrayEach(mock, func(value []byte, dataType ValueType, offset int, err error) {
574+
count++
575+
576+
switch count {
577+
case 1:
578+
if string(value) != `{"x": 1}` {
579+
t.Errorf("Wrong first item: %s", string(value))
580+
}
581+
case 2:
582+
if string(value) != `{"x":2}` {
583+
t.Errorf("Wrong second item: %s", string(value))
584+
}
585+
case 3:
586+
if string(value) != `{ "x":3}` {
587+
t.Errorf("Wrong third item: %s", string(value))
588+
}
589+
case 4:
590+
if string(value) != `{"x":4}` {
591+
t.Errorf("Wrong forth item: %s", string(value))
592+
}
593+
default:
594+
t.Errorf("Should process only 4 items")
595+
}
596+
}, "a", "b")
597+
}

0 commit comments

Comments
 (0)