Skip to content

Commit 5b691c8

Browse files
authored
Merge pull request #87 from vkd/master
fix #79 - allow empty array in ArrayEach
2 parents 8d54c8a + e2cc574 commit 5b691c8

File tree

3 files changed

+152
-106
lines changed

3 files changed

+152
-106
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
language: go
2-
go: 1.6
2+
go: 1.7
33
script: go test -v ./.

parser.go

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ func searchKeys(data []byte, keys ...string) int {
185185
var valueOffset int
186186

187187
ArrayEach(data[i:], func(value []byte, dataType ValueType, offset int, err error) {
188-
if (curIdx == aIdx) {
188+
if curIdx == aIdx {
189189
valueFound = value
190190
valueOffset = offset
191191
}
@@ -344,7 +344,7 @@ func EachKey(data []byte, cb func(int, []byte, ValueType, error), paths ...[]str
344344
continue
345345
}
346346

347-
aIdx, _ := strconv.Atoi(p[level][1: len(p[level]) - 1])
347+
aIdx, _ := strconv.Atoi(p[level][1 : len(p[level])-1])
348348
arrIdxFlags |= bitwiseFlags[aIdx+1]
349349
pIdxFlags |= bitwiseFlags[pi+1]
350350
}
@@ -354,10 +354,10 @@ func EachKey(data []byte, cb func(int, []byte, ValueType, error), paths ...[]str
354354

355355
var curIdx int
356356
arrOff, _ := ArrayEach(data[i:], func(value []byte, dataType ValueType, offset int, err error) {
357-
if (arrIdxFlags&bitwiseFlags[curIdx+1] != 0) {
357+
if arrIdxFlags&bitwiseFlags[curIdx+1] != 0 {
358358
for pi, p := range paths {
359359
if pIdxFlags&bitwiseFlags[pi+1] != 0 {
360-
aIdx, _ := strconv.Atoi(p[level-1][1: len(p[level-1]) - 1])
360+
aIdx, _ := strconv.Atoi(p[level-1][1 : len(p[level-1])-1])
361361

362362
if curIdx == aIdx {
363363
of := searchKeys(value, p[level:]...)
@@ -571,6 +571,17 @@ func ArrayEach(data []byte, cb func(value []byte, dataType ValueType, offset int
571571
offset++
572572
}
573573

574+
nO := nextToken(data[offset:])
575+
if nO == -1 {
576+
return offset, MalformedJsonError
577+
}
578+
579+
offset += nO
580+
581+
if data[offset] == ']' {
582+
return offset, nil
583+
}
584+
574585
for true {
575586
v, t, o, e := Get(data[offset:])
576587

@@ -583,7 +594,7 @@ func ArrayEach(data []byte, cb func(value []byte, dataType ValueType, offset int
583594
}
584595

585596
if t != NotExist {
586-
cb(v, t, offset + o - len(v), e)
597+
cb(v, t, offset+o-len(v), e)
587598
}
588599

589600
if e != nil {

0 commit comments

Comments
 (0)