Skip to content

Commit

Permalink
Add support for decoding JSON arrays of arrays
Browse files Browse the repository at this point in the history
Add support for decoding JSON arrays of arrays by handling, during
slice decoding, when the next token is an array opening. This produces
nested []interface{} slices.

Closes getsops#640.
  • Loading branch information
nilium committed Mar 9, 2020
1 parent 7f350d8 commit 6ee2886
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
6 changes: 6 additions & 0 deletions stores/json/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@ func (store Store) sliceFromJSONDecoder(dec *json.Decoder) ([]interface{}, error
return slice, err
}
slice = append(slice, item)
} else if ok && delim.String() == "[" {
item, err := store.sliceFromJSONDecoder(dec)
if err != nil {
return slice, err
}
slice = append(slice, item)
} else {
slice = append(slice, t)
}
Expand Down
25 changes: 25 additions & 0 deletions stores/json/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,31 @@ func TestDecodeJSONArrayOfObjects(t *testing.T) {
assert.Equal(t, expected, branch)
}

func TestDecodeJSONArrayOfArrays(t *testing.T) {
in := `{"foo": [[["foo", {"bar": "foo"}]]]}`
expected := sops.TreeBranch{
sops.TreeItem{
Key: "foo",
Value: []interface{}{
[]interface{}{
[]interface{}{
"foo",
sops.TreeBranch{
sops.TreeItem{
Key: "bar",
Value: "foo",
},
},
},
},
},
},
}
branch, err := Store{}.treeBranchFromJSON([]byte(in))
assert.Nil(t, err)
assert.Equal(t, expected, branch)
}

func TestEncodeSimpleJSON(t *testing.T) {
branch := sops.TreeBranch{
sops.TreeItem{
Expand Down

0 comments on commit 6ee2886

Please sign in to comment.