Skip to content

Commit

Permalink
support object returns in conditional checks (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
tlugger authored Sep 28, 2022
1 parent b177c64 commit 5c939b3
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
12 changes: 9 additions & 3 deletions jsonlogic.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,8 +350,6 @@ func all(values, data interface{}) interface{} {
return false
}



for _, value := range subject.([]interface{}) {
conditions := solveVars(parsed[1], value)
v := apply(conditions, value)
Expand Down Expand Up @@ -447,7 +445,15 @@ func parseValues(values, data interface{}) interface{} {
}

func apply(rules, data interface{}) interface{} {
for operator, values := range rules.(map[string]interface{}) {
ruleMap := rules.(map[string]interface{})

// A map with more than 1 key counts as a primitive
// end recursion
if len(ruleMap) > 1 {
return ruleMap
}

for operator, values := range ruleMap {
if operator == "filter" {
return filter(values, data)
}
Expand Down
17 changes: 17 additions & 0 deletions jsonlogic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -724,3 +724,20 @@ func TestIssue52_example2(t *testing.T) {
expected := `"jsonlogic"`
assert.JSONEq(t, expected, result.String())
}

func TestIssue58_example(t *testing.T) {
data := strings.NewReader(`{"foo": "bar"}`)
logic := strings.NewReader(`{"if":[
{"==":[{"var":"foo"},"bar"]},{"foo":"is_bar","path":"foo_is_bar"},
{"foo":"not_bar","path":"default_object"}
]}`)

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

expected := `{"foo":"is_bar","path":"foo_is_bar"}`
assert.JSONEq(t, expected, result.String())
}

0 comments on commit 5c939b3

Please sign in to comment.