Skip to content

Commit

Permalink
Attempt to fix #185 - Set overrides items (#186)
Browse files Browse the repository at this point in the history
If you try to set an unknown key, simple object within a nested array, passing in the index, the parser will override the object and does not take into account the given index [1].

With current parser version, given this json as input:
```json
{
    "test": {
        "key": [
            {
                "innerKey": "innerKeyValue",
                "innerKey2": "innerKeyValue2"
            }
        ]
    }
}
```
and following path to set an unknown key: `"test", "key", "[1]", "newInnerKey"` with data `"new object"`, the output would be:
```json
{
    "test": {
        "key": [
            {
                "newInnerKey": "new object"
            }
        ]
    }
}
```
which I think it is wrong, since it overrides what index [0] had but the above path wanted to insert the object in index [1] instead.

With the changes done in this PR, given the same json input, path and data, the output would be:
```json
{
    "test": {
        "key": [
            {
                "innerKey": "innerKeyValue",
                "innerKey2": "innerKeyValue2"
            },
            {
                "newInnerKey": "new object"
            }
        ]
    }
}
```
the above result would be correct since we are setting a new object within the existing array in a different index [1], keeping what the array already had in previous index [0].

Added also a test to cover this scenario. All tests pass.

This appears to fix #185 and includes a test for it.

**Description**: What this PR does

**Benchmark before change**:
Benchmarks are broken.
  • Loading branch information
jreyeshdez committed Mar 21, 2020
1 parent 11fa7e4 commit cc7baf7
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
16 changes: 10 additions & 6 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -599,15 +599,17 @@ func createInsertComponent(keys []string, setValue []byte, comma, object bool) [
if comma {
buffer.WriteString(",")
}
if isIndex {
if isIndex && !comma {
buffer.WriteString("[")
} else {
if object {
buffer.WriteString("{")
}
buffer.WriteString("\"")
buffer.WriteString(keys[0])
buffer.WriteString("\":")
if !isIndex {
buffer.WriteString("\"")
buffer.WriteString(keys[0])
buffer.WriteString("\":")
}
}

for i := 1; i < len(keys); i++ {
Expand All @@ -627,7 +629,7 @@ func createInsertComponent(keys []string, setValue []byte, comma, object bool) [
buffer.WriteString("}")
}
}
if isIndex {
if isIndex && !comma {
buffer.WriteString("]")
}
if object && !isIndex {
Expand Down Expand Up @@ -776,7 +778,9 @@ func Set(data []byte, setValue []byte, keys ...string) (value []byte, err error)
depthOffset := endOffset
if depth != 0 {
// if subpath is a non-empty object, add to it
if data[startOffset] == '{' && data[startOffset+1+nextToken(data[startOffset+1:])] != '}' {
// or if subpath is a non-empty array, add to it
if (data[startOffset] == '{' && data[startOffset+1+nextToken(data[startOffset+1:])] != '}') ||
(data[startOffset] == '[' && data[startOffset+1+nextToken(data[startOffset+1:])] == '{') && keys[depth:][0][0] == 91 {
depthOffset--
startOffset = depthOffset
// otherwise, over-write it with a new object
Expand Down
8 changes: 8 additions & 0 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,14 @@ var setTests = []SetTest{
isFound: true,
data: `{"top":["one", "two", "value"]}`,
},
{
desc: "set unknown key (simple object within nested array)",
json: `{"test":{"key":[{"innerKey":"innerKeyValue", "innerKey2":"innerKeyValue2"}]}}`,
isFound: true,
path: []string{"test", "key", "[1]", "newInnerKey"},
setData: `"new object"`,
data: `{"test":{"key":[{"innerKey":"innerKeyValue", "innerKey2":"innerKeyValue2"},{"newInnerKey":"new object"}]}}`,
},
}

var getTests = []GetTest{
Expand Down

0 comments on commit cc7baf7

Please sign in to comment.