Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes and refactor #91

Merged
merged 7 commits into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
go: ['1.14', '1.15', '1.16', '1.18', '1.19', '1.20', '1.21']
go: ['1.16', '1.18', '1.19', '1.20', '1.21']
name: Running with Go ${{ matrix.go }}
steps:
- name: Install Go
Expand Down
22 changes: 15 additions & 7 deletions arrays.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package jsonlogic

import "fmt"
import (
"fmt"
)

type ErrReduceDataType struct {
dataType string
Expand Down Expand Up @@ -95,15 +97,21 @@ func reduce(values, data interface{}) interface{} {
accumulator interface{}
valueType string
)

{
if isBool(parsed[2]) {
accumulator = isTrue(parsed[2])
initialValue := parsed[2]
if isMap(initialValue) {
initialValue = apply(initialValue, data)
}

if isBool(initialValue) {
accumulator = isTrue(initialValue)
valueType = "bool"
} else if isNumber(parsed[2]) {
accumulator = toNumber(parsed[2])
} else if isNumber(initialValue) {
accumulator = toNumber(initialValue)
valueType = "number"
} else if isString(parsed[2]) {
accumulator = toString(parsed[2])
} else if isString(initialValue) {
accumulator = toString(initialValue)
valueType = "string"
} else {
panic(ErrReduceDataType{
Expand Down
102 changes: 35 additions & 67 deletions arrays_test.go
Original file line number Diff line number Diff line change
@@ -1,111 +1,79 @@
package jsonlogic
package jsonlogic_test

import (
"encoding/json"
"bytes"
"strings"
"testing"

"github.com/stretchr/testify/assert"

"github.com/diegoholiveira/jsonlogic/v3"
)

func TestFilterParseTheSubjectFromFirstPosition(t *testing.T) {
var parsed interface{}

err := json.Unmarshal([]byte(`[
rule := strings.NewReader(`{"filter": [
[1,2,3,4,5],
{"%":[{"var":""},2]}
]`), &parsed)
if err != nil {
panic(err)
}

result := filter(parsed, nil)

var expected interface{}
]}`)

json.Unmarshal([]byte(`[1,3,5]`), &expected) // nolint:errcheck
var result bytes.Buffer

assert.Equal(t, expected, result)
err := jsonlogic.Apply(rule, nil, &result)
assert.Nil(t, err)
assert.JSONEq(t, `[1,3,5]`, result.String())
}

func TestFilterParseTheSubjectFromNullValue(t *testing.T) {
var parsed interface{}

err := json.Unmarshal([]byte(`[
rule := strings.NewReader(`{"filter": [
null,
{"%":[{"var":""},2]}
]`), &parsed)
if err != nil {
panic(err)
}

result := filter(parsed, nil)
]}`)

var expected interface{}
var result bytes.Buffer

json.Unmarshal([]byte(`[]`), &expected) // nolint:errcheck

assert.Equal(t, expected, result)
err := jsonlogic.Apply(rule, nil, &result)
assert.Nil(t, err)
assert.JSONEq(t, `[]`, result.String())
}

func TestReduceSkipNullValues(t *testing.T) {
var parsed interface{}

err := json.Unmarshal([]byte(`[
rule := strings.NewReader(`{"reduce": [
[1,2,null,4,5],
{"+":[{"var":"current"}, {"var":"accumulator"}]},
0
]`), &parsed)
if err != nil {
panic(err)
}
]}`)

result := reduce(parsed, nil)
var result bytes.Buffer

var expected interface{}

json.Unmarshal([]byte(`12`), &expected) // nolint:errcheck

assert.Equal(t, expected, result)
err := jsonlogic.Apply(rule, nil, &result)
assert.Nil(t, err)
assert.JSONEq(t, `12`, result.String())
}

func TestReduceBoolValues(t *testing.T) {
var parsed interface{}

err := json.Unmarshal([]byte(`[
rule := strings.NewReader(`{"reduce": [
[true,false,true,null],
{"or":[{"var":"current"}, {"var":"accumulator"}]},
false
]`), &parsed)
if err != nil {
panic(err)
}

result := reduce(parsed, nil)
]}`)

var expected interface{}
var result bytes.Buffer

json.Unmarshal([]byte(`true`), &expected) // nolint:errcheck

assert.Equal(t, expected, result)
err := jsonlogic.Apply(rule, nil, &result)
assert.Nil(t, err)
assert.JSONEq(t, `true`, result.String())
}

func TestReduceStringValues(t *testing.T) {
var parsed interface{}

err := json.Unmarshal([]byte(`[
rule := strings.NewReader(`{"reduce": [
["a",null,"b"],
{"cat":[{"var":"current"}, {"var":"accumulator"}]},
""
]`), &parsed)
if err != nil {
panic(err)
}

result := reduce(parsed, nil)

var expected interface{}
]}`)

json.Unmarshal([]byte(`"ba"`), &expected) // nolint:errcheck
var result bytes.Buffer

assert.Equal(t, expected, result)
err := jsonlogic.Apply(rule, nil, &result)
assert.Nil(t, err)
assert.JSONEq(t, `"ba"`, result.String())
}
6 changes: 5 additions & 1 deletion comp.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func equals(a, b interface{}) bool {
}

if isNumber(a) {
return toNumber(a) == toNumber(b)
return isPrimitive(b) && toNumber(a) == toNumber(b)
}

if isBool(a) {
Expand All @@ -85,5 +85,9 @@ func equals(a, b interface{}) bool {
return isTrue(a) == isTrue(b)
}

if !isString(a) || !isString(b) {
return false
}

return toString(a) == toString(b)
}
37 changes: 0 additions & 37 deletions internal/issues/0083_test.go

This file was deleted.

3 changes: 1 addition & 2 deletions internal/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"encoding/json"
"io"
"io/ioutil"
"log"
"net/http"
"reflect"
Expand Down Expand Up @@ -42,7 +41,7 @@ func GetScenariosFromOfficialTestSuite() Tests {
return tests
}

buffer, _ := ioutil.ReadAll(response.Body)
buffer, _ := io.ReadAll(response.Body)

response.Body.Close()

Expand Down
Loading
Loading