Skip to content

Commit 57853d8

Browse files
authored
Merge pull request #193 from AllenX2018/fix-issue-159
fix issue #159
2 parents 3fdec1a + 50f9457 commit 57853d8

File tree

2 files changed

+56
-1
lines changed

2 files changed

+56
-1
lines changed

parser.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -922,7 +922,12 @@ func ArrayEach(data []byte, cb func(value []byte, dataType ValueType, offset int
922922
return -1, MalformedObjectError
923923
}
924924

925-
offset = 1
925+
nT := nextToken(data)
926+
if nT == -1 {
927+
return -1, MalformedJsonError
928+
}
929+
930+
offset = nT+1
926931

927932
if len(keys) > 0 {
928933
if offset = searchKeys(data, keys...); offset == -1 {

parser_test.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1383,6 +1383,56 @@ func TestArrayEach(t *testing.T) {
13831383
}, "a", "b")
13841384
}
13851385

1386+
func TestArrayEachWithWhiteSpace(t *testing.T) {
1387+
//Issue #159
1388+
count := 0
1389+
funcError := func([]byte, ValueType, int, error) { t.Errorf("Run func not allow") }
1390+
funcSuccess := func(value []byte, dataType ValueType, index int, err error) {
1391+
count++
1392+
1393+
switch count {
1394+
case 1:
1395+
if string(value) != `AAA` {
1396+
t.Errorf("Wrong first item: %s", string(value))
1397+
}
1398+
case 2:
1399+
if string(value) != `BBB` {
1400+
t.Errorf("Wrong second item: %s", string(value))
1401+
}
1402+
case 3:
1403+
if string(value) != `CCC` {
1404+
t.Errorf("Wrong third item: %s", string(value))
1405+
}
1406+
default:
1407+
t.Errorf("Should process only 3 items")
1408+
}
1409+
}
1410+
1411+
type args struct {
1412+
data []byte
1413+
cb func(value []byte, dataType ValueType, offset int, err error)
1414+
keys []string
1415+
}
1416+
tests := []struct {
1417+
name string
1418+
args args
1419+
wantErr bool
1420+
}{
1421+
{"Array with white space", args{[]byte(` ["AAA", "BBB", "CCC"]`), funcSuccess, []string{}}, false},
1422+
{"Array with only one character after white space", args{[]byte(` 1`), funcError, []string{}}, true},
1423+
{"Only white space", args{[]byte(` `), funcError, []string{}}, true},
1424+
}
1425+
for _, tt := range tests {
1426+
t.Run(tt.name, func(t *testing.T) {
1427+
_, err := ArrayEach(tt.args.data, tt.args.cb, tt.args.keys...)
1428+
if (err != nil) != tt.wantErr {
1429+
t.Errorf("ArrayEach() error = %v, wantErr %v", err, tt.wantErr)
1430+
return
1431+
}
1432+
})
1433+
}
1434+
}
1435+
13861436
func TestArrayEachEmpty(t *testing.T) {
13871437
funcError := func([]byte, ValueType, int, error) { t.Errorf("Run func not allow") }
13881438

0 commit comments

Comments
 (0)