Skip to content

Commit

Permalink
json processor append now expands
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeffail committed Jun 18, 2018
1 parent 624dabd commit ab60597
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 9 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.

## Unreleased

## 0.14.2 - 2018-06-18

## Changed

- The `json` processor will now `append` array values in expanded form.

## 0.14.0 - 2018-06-15

### Added
Expand Down
33 changes: 25 additions & 8 deletions lib/processor/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ with the config:
` + "``` yaml" + `
json:
operator: set
parts: [0]
path: some.path
value:
Expand All @@ -83,6 +84,11 @@ objects in the path are created (unless there is a collision).
If a non-array value already exists in the target path it will be replaced by an
array containing the original value as well as the new value.
If the value is an array the elements of the array are expanded into the new
array. E.g. if the target is an array ` + "`[0,1]`" + ` and the value is also an
array ` + "`[2,3]`" + `, the result will be ` + "`[0,1,2,3]`" + ` as opposed to
` + "`[0,1,[2,3]]`" + `.
#### ` + "`delete`" + `
Removes a key identified by the dot path. If the path does not exist this is a
Expand Down Expand Up @@ -177,10 +183,10 @@ func NewJSONConfig() JSONConfig {

//------------------------------------------------------------------------------

type jsonOperator func(body, value interface{}) (interface{}, error)
type jsonOperator func(body interface{}, value rawJSONValue) (interface{}, error)

func newSetOperator(path []string) jsonOperator {
return func(body, value interface{}) (interface{}, error) {
return func(body interface{}, value rawJSONValue) (interface{}, error) {
if len(path) == 0 {
return value, nil
}
Expand All @@ -196,7 +202,7 @@ func newSetOperator(path []string) jsonOperator {
}

func newSelectOperator(path []string) jsonOperator {
return func(body, value interface{}) (interface{}, error) {
return func(body interface{}, value rawJSONValue) (interface{}, error) {
gPart, err := gabs.Consume(body)
if err != nil {
return nil, err
Expand All @@ -219,7 +225,7 @@ func newSelectOperator(path []string) jsonOperator {
}

func newDeleteOperator(path []string) jsonOperator {
return func(body, value interface{}) (interface{}, error) {
return func(body interface{}, value rawJSONValue) (interface{}, error) {
if len(path) == 0 {
return nil, nil
}
Expand All @@ -237,22 +243,33 @@ func newDeleteOperator(path []string) jsonOperator {
}

func newAppendOperator(path []string) jsonOperator {
return func(body, value interface{}) (interface{}, error) {
return func(body interface{}, value rawJSONValue) (interface{}, error) {
gPart, err := gabs.Consume(body)
if err != nil {
return nil, err
}

var array []interface{}

var valueParsed interface{}
if err = json.Unmarshal(value, &valueParsed); err != nil {
return nil, err
}
switch t := valueParsed.(type) {
case []interface{}:
array = t
default:
array = append(array, t)
}

switch t := gPart.S(path...).Data().(type) {
case []interface{}:
t = append(t, value)
t = append(t, array...)
array = t
case nil:
array = []interface{}{value}
array = append([]interface{}{t}, array...)
default:
array = []interface{}{t, value}
array = append([]interface{}{t}, array...)
}
gPart.Set(array, path...)

Expand Down
23 changes: 22 additions & 1 deletion lib/processor/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,20 @@ func TestJSONAppend(t *testing.T) {
input: `{"foo":{"bar":5}}`,
output: `{"foo":{"bar":[5,{"baz":1}]}}`,
},
{
name: "append nil 1",
path: "foo.bar",
value: `{"baz":1}`,
input: `{"foo":{"bar":null}}`,
output: `{"foo":{"bar":[null,{"baz":1}]}}`,
},
{
name: "append nil 2",
path: "foo.bar",
value: `{"baz":1}`,
input: `{"foo":{"bar":[null]}}`,
output: `{"foo":{"bar":[null,{"baz":1}]}}`,
},
{
name: "append collision 1",
path: "foo.bar",
Expand All @@ -163,7 +177,14 @@ func TestJSONAppend(t *testing.T) {
path: "foo.bar",
value: `[1,2,3]`,
input: `{"foo":{"bar":[0]}}`,
output: `{"foo":{"bar":[0,[1,2,3]]}}`,
output: `{"foo":{"bar":[0,1,2,3]}}`,
},
{
name: "append array 2",
path: "foo.bar",
value: `[1,2,3]`,
input: `{"foo":{"bar":0}}`,
output: `{"foo":{"bar":[0,1,2,3]}}`,
},
}

Expand Down

0 comments on commit ab60597

Please sign in to comment.