Skip to content

Commit

Permalink
fix issues buger#199
Browse files Browse the repository at this point in the history
  • Loading branch information
AllenX2018 committed May 15, 2020
1 parent 04f1202 commit daba909
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 1 deletion.
2 changes: 2 additions & 0 deletions benchmark/go.mod
@@ -1,5 +1,7 @@
module benchmarks

go 1.14

require (
github.com/Jeffail/gabs v1.2.0
github.com/a8m/djson v0.0.0-20170509170705-c02c5aef757f
Expand Down
2 changes: 2 additions & 0 deletions go.mod
@@ -1,3 +1,5 @@
module github.com/buger/jsonparser

go 1.13

require github.com/stretchr/testify v1.5.1
11 changes: 11 additions & 0 deletions go.sum
@@ -0,0 +1,11 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
2 changes: 1 addition & 1 deletion parser.go
Expand Up @@ -916,7 +916,7 @@ func internalGet(data []byte, keys ...string) (value []byte, dataType ValueType,
value = value[1 : len(value)-1]
}

return value, dataType, offset, endOffset, nil
return value[:len(value):len(value)], dataType, offset, endOffset, nil
}

// ArrayEach is used when iterating arrays, accepts a callback function with the same return arguments as `Get`.
Expand Down
23 changes: 23 additions & 0 deletions parser_test.go
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"fmt"
_ "fmt"
. "github.com/stretchr/testify/require"
"reflect"
"testing"
)
Expand Down Expand Up @@ -1929,3 +1930,25 @@ func TestParseString(t *testing.T) {
},
)
}

func TestIssues199(t *testing.T) {
should := New(t)
jsonObject := `[{"key1":"Foo","key2":"Bar","key3":"baz"},{"key1":"boo","key2":"dog","key3":"cat","key4":"mouse"}]`
var allSlices [][]byte
_, err := ArrayEach([]byte(jsonObject), func(value []byte, _ ValueType, _ int, err error) {
if err != nil {
t.Fatalf("failed: %s", err)
}
allSlices = append(allSlices, value)
})

if err != nil {
t.Fatalf("failed: %s", err)
}

newVal, err := Set(allSlices[0], []byte(`"mouse"`), "key4")
should.Nil(err)
should.Equal([]byte(`{"key1":"Foo","key2":"Bar","key3":"baz","key4":"mouse"}`),newVal)
should.Equal([]byte(`{"key1":"boo","key2":"dog","key3":"cat","key4":"mouse"}`),allSlices[1])

}

0 comments on commit daba909

Please sign in to comment.