Skip to content

Commit

Permalink
Fixing merge recursive (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexjunioravila authored and diegoholiveira committed Sep 20, 2019
1 parent 3bd88ff commit 723da1c
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 5 deletions.
8 changes: 4 additions & 4 deletions jsonlogic.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,16 +288,16 @@ func substr(values interface{}) interface{} {
return string(runes[from:to])
}

func merge(values interface{}) interface{} {
func merge(values interface{}, level int8) interface{} {
result := make([]interface{}, 0)

if isPrimitive(values) {
if isPrimitive(values) || level > 1 {
return append(result, values)
}

if isSlice(values) {
for _, value := range values.([]interface{}) {
_values := merge(value).([]interface{})
_values := merge(value, level+1).([]interface{})

result = append(result, _values...)
}
Expand Down Expand Up @@ -518,7 +518,7 @@ func operation(operator string, values, data interface{}) interface{} {
}

if operator == "merge" {
return merge(values)
return merge(values, 0)
}

if operator == "if" {
Expand Down
30 changes: 30 additions & 0 deletions jsonlogic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,3 +336,33 @@ func TestAbsoluteValue(t *testing.T) {

assert.JSONEq(t, "2", result.String())
}

func TestMergeArrayOfArrays(t *testing.T) {
rule := strings.NewReader(`{
"merge": [
[
[
"18800000",
"18800969"
]
],
[
[
"19840000",
"19840969"
]
]
]
}`)
data := strings.NewReader(`{}`)

expectedResult := "[[\"18800000\",\"18800969\"],[\"19840000\",\"19840969\"]]"

var result bytes.Buffer
err := Apply(rule, data, &result)
if err != nil {
t.Fatal(err)
}

assert.JSONEq(t, expectedResult, result.String())
}
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Implementation of [JSON Logic](http://jsonlogic.com) in Go Lang.
## What's JSON Logic?

JSON Logic is a DSL to write logic decisions in JSON. It's has a great specification and is very simple to learn.
The official website has a great documentation with examples: http://jsonlogic.com
The [official website](http://jsonlogic.com) has a great documentation with examples.


## How to use it
Expand Down

0 comments on commit 723da1c

Please sign in to comment.