Repository: diegoholiveira/jsonlogic
Pull request #94 (2025-01-05 14:59:14)

Mark `ApplyInterface` as public method


Patch:
diff --git a/jsonlogic.go b/jsonlogic.go
index b11efee..b298393 100644
--- a/jsonlogic.go
+++ b/jsonlogic.go
@@ -511,7 +511,7 @@ func Apply(rule, data io.Reader, result io.Writer) error {
 		return err
 	}
 
-	output, err := applyInterface(_rule, _data)
+	output, err := ApplyInterface(_rule, _data)
 	if err != nil {
 		return err
 	}
@@ -561,7 +561,7 @@ func ApplyRaw(rule, data json.RawMessage) (json.RawMessage, error) {
 		return nil, err
 	}
 
-	result, err := applyInterface(_rule, _data)
+	result, err := ApplyInterface(_rule, _data)
 	if err != nil {
 		return nil, err
 	}
@@ -569,9 +569,9 @@ func ApplyRaw(rule, data json.RawMessage) (json.RawMessage, error) {
 	return json.Marshal(&result)
 }
 
-// applyInterface receives a rule and data as interface{} and returns the result
+// ApplyInterface receives a rule and data as interface{} and returns the result
 // of the rule applied to the data.
-func applyInterface(rule, data interface{}) (output interface{}, err error) {
+func ApplyInterface(rule, data interface{}) (output interface{}, err error) {
 	defer func() {
 		if e := recover(); e != nil {
 			// fmt.Println("stacktrace from panic: \n" + string(debug.Stack()))


Test patch:
diff --git a/helpers_test.go b/helpers_test.go
deleted file mode 100644
index dc2018b..0000000
--- a/helpers_test.go
+++ /dev/null
@@ -1,133 +0,0 @@
-package jsonlogic
-
-import (
-	"testing"
-
-	"github.com/stretchr/testify/assert"
-)
-
-func TestHelperIsTrue(t *testing.T) {
-	assert.False(t, isTrue(nil))
-}
-
-func TestToSliceOfNumbers(t *testing.T) {
-	json_parsed := []interface{}{"0.0", "-10.0"}
-	input := interface{}(json_parsed)
-	expected := []float64{0.0, -10.0}
-	assert.Equal(t, expected, toSliceOfNumbers(input))
-}
-
-func TestIsNumber(t *testing.T) {
-	tests := []struct {
-		name   string
-		input  interface{}
-		expect bool
-	}{
-		{
-			name:   "int max",
-			input:  9223372036854775807,
-			expect: true,
-		},
-		{
-			name:   "int min",
-			input:  -9223372036854775808,
-			expect: true,
-		},
-		{
-			name:   "float",
-			input:  1.121,
-			expect: true,
-		},
-		{
-			name:   "string",
-			input:  "123",
-			expect: false,
-		},
-		{
-			name:   "boolean",
-			input:  true,
-			expect: false,
-		},
-	}
-
-	for _, test := range tests {
-		t.Run(test.name, func(t *testing.T) {
-			assert.Equal(t, test.expect, isNumber(test.input))
-		})
-	}
-}
-
-func TestToNumber(t *testing.T) {
-	tests := []struct {
-		name   string
-		input  interface{}
-		expect float64
-	}{
-		{
-			name:   "int max value",
-			input:  9223372036854775807,
-			expect: 9223372036854775807,
-		},
-		{
-			name:   "int min value",
-			input:  -9223372036854775808,
-			expect: -9223372036854775808,
-		},
-		{
-			name:   "int min value",
-			input:  -9223372036854775808,
-			expect: -9223372036854775808,
-		},
-		{
-			name:   "int as a string",
-			input:  "123",
-			expect: 123,
-		},
-		{
-			name:   "float",
-			input:  1.121,
-			expect: 1.121,
-		},
-	}
-
-	for _, test := range tests {
-		t.Run(test.name, func(t *testing.T) {
-			assert.Equal(t, test.expect, toNumber(test.input))
-		})
-	}
-}
-
-func TestToString(t *testing.T) {
-	var tests = []struct {
-		name   string
-		input  interface{}
-		expect string
-	}{
-		{
-			name:   "nil value",
-			input:  nil,
-			expect: "",
-		},
-		{
-			name:   "string value",
-			input:  "value",
-			expect: "value",
-		},
-		{
-			name:   "int value",
-			input:  9223372036854775807,
-			expect: "9223372036854775807",
-		},
-		{
-			name:   "float value",
-			input:  1.21,
-			expect: "1.21",
-		},
-	}
-
-	for _, test := range tests {
-		t.Run(test.name, func(t *testing.T) {
-			assert.Equal(t, test.expect, toString(test.input))
-		})
-	}
-}
diff --git a/jsonlogic_test.go b/jsonlogic_test.go
index 5e50f59..44af119 100644
--- a/jsonlogic_test.go
+++ b/jsonlogic_test.go
@@ -1,4 +1,4 @@
-package jsonlogic
+package jsonlogic_test
 
 import (
 	"bytes"
@@ -10,6 +10,7 @@ import (
 
 	"github.com/stretchr/testify/assert"
 
+	"github.com/diegoholiveira/jsonlogic/v3"
 	"github.com/diegoholiveira/jsonlogic/v3/internal"
 )
 
@@ -20,7 +21,7 @@ func TestRulesFromJsonLogic(t *testing.T) {
 		t.Run(fmt.Sprintf("Scenario_%d", i), func(t *testing.T) {
 			var result bytes.Buffer
 
-			err := Apply(test.Rule, test.Data, &result)
+			err := jsonlogic.Apply(test.Rule, test.Data, &result)
 			if err != nil {
 				t.Fatal(err)
 			}
@@ -38,7 +39,7 @@ func TestDivWithOnlyOneValue(t *testing.T) {
 
 	var result bytes.Buffer
 
-	err := Apply(rule, data, &result)
+	err := jsonlogic.Apply(rule, data, &result)
 	if err != nil {
 		t.Fatal(err)
 	}
@@ -66,7 +67,7 @@ func TestSetAValue(t *testing.T) {
 	}`)
 
 	var result bytes.Buffer
-	err := Apply(rule, data, &result)
+	err := jsonlogic.Apply(rule, data, &result)
 	if err != nil {
 		t.Fatal(err)
 	}
@@ -102,7 +103,7 @@ func TestLocalContext(t *testing.T) {
 	}`)
 
 	var result bytes.Buffer
-	err := Apply(rule, data, &result)
+	err := jsonlogic.Apply(rule, data, &result)
 	if err != nil {
 		t.Fatal(err)
 	}
@@ -136,7 +137,7 @@ func TestMapWithZeroValue(t *testing.T) {
 	}`)
 
 	var result bytes.Buffer
-	err := Apply(rule, data, &result)
+	err := jsonlogic.Apply(rule, data, &result)
 	if err != nil {
 		t.Fatal(err)
 	}
@@ -172,7 +173,7 @@ func TestListOfRanges(t *testing.T) {
 	}`)
 
 	var result bytes.Buffer
-	err := Apply(rule, data, &result)
+	err := jsonlogic.Apply(rule, data, &result)
 	if err != nil {
 		t.Fatal(err)
 	}
@@ -199,7 +200,7 @@ func TestSomeWithLists(t *testing.T) {
 	data := strings.NewReader(`{}`)
 
 	var result bytes.Buffer
-	err := Apply(rule, data, &result)
+	err := jsonlogic.Apply(rule, data, &result)
 	if err != nil {
 		t.Fatal(err)
 	}
@@ -221,7 +222,7 @@ func TestAllWithLists(t *testing.T) {
 	data := strings.NewReader("{}")
 
 	var result bytes.Buffer
-	err := Apply(rule, data, &result)
+	err := jsonlogic.Apply(rule, data, &result)
 	if err != nil {
 		t.Fatal(err)
 	}
@@ -250,7 +251,7 @@ func TestAllWithArrayOfMapData(t *testing.T) {
 	  }
 	`)
 	var result bytes.Buffer
-	err := Apply(rule, data, &result)
+	err := jsonlogic.Apply(rule, data, &result)
 	if err != nil {
 		t.Fatal(err)
 	}
@@ -271,7 +272,7 @@ func TestNoneWithLists(t *testing.T) {
 	data := strings.NewReader("{}")
 
 	var result bytes.Buffer
-	err := Apply(rule, data, &result)
+	err := jsonlogic.Apply(rule, data, &result)
 	if err != nil {
 		t.Fatal(err)
 	}
@@ -303,7 +304,7 @@ func TestInOperatorWorksWithMaps(t *testing.T) {
 	}`)
 
 	var result bytes.Buffer
-	err := Apply(rule, data, &result)
+	err := jsonlogic.Apply(rule, data, &result)
 	if err != nil {
 		t.Fatal(err)
 	}
@@ -323,7 +324,7 @@ func TestAbsoluteValue(t *testing.T) {
 	}`)
 
 	var result bytes.Buffer
-	err := Apply(rule, data, &result)
+	err := jsonlogic.Apply(rule, data, &result)
 	if err != nil {
 		t.Fatal(err)
 	}
@@ -353,7 +354,7 @@ func TestMergeArrayOfArrays(t *testing.T) {
 	expectedResult := "[[\"18800000\",\"18800969\"],[\"19840000\",\"19840969\"]]"
 
 	var result bytes.Buffer
-	err := Apply(rule, data, &result)
+	err := jsonlogic.Apply(rule, data, &result)
 	if err != nil {
 		t.Fatal(err)
 	}
@@ -371,7 +372,7 @@ func TestDataWithDefaultValueWithApplyRaw(t *testing.T) {
 
 	var expected json.RawMessage = json.RawMessage("3")
 
-	output, err := ApplyRaw(rule, nil)
+	output, err := jsonlogic.ApplyRaw(rule, nil)
 	if err != nil {
 		t.Fatal(err)
 	}
@@ -388,7 +389,7 @@ func TestDataWithDefaultValueWithApplyInterface(t *testing.T) {
 	}
 
 	expected := float64(3)
-	output, err := applyInterface(rule, nil)
+	output, err := jsonlogic.ApplyInterface(rule, nil)
 	if err != nil {
 		t.Fatal(err)
 	}
@@ -404,7 +405,7 @@ func TestMissingOperators(t *testing.T) {
 		},
 	}
 
-	_, err := applyInterface(rule, nil)
+	_, err := jsonlogic.ApplyInterface(rule, nil)
 
 	assert.EqualError(t, err, "The operator \"sum\" is not supported")
 }
@@ -414,7 +415,7 @@ func TestZeroDivision(t *testing.T) {
 	data := strings.NewReader(`{}`)
 	var result bytes.Buffer
 
-	Apply(logic, data, &result) // nolint:errcheck
+	jsonlogic.Apply(logic, data, &result) // nolint:errcheck
 
 	assert.JSONEq(t, `0`, result.String())
 }
@@ -431,7 +432,7 @@ func TestSliceWithOnlyWithNumbersAsKey(t *testing.T) {
 	}`)
 
 	var result bytes.Buffer
-	err := Apply(rule, data, &result)
+	err := jsonlogic.Apply(rule, data, &result)
 	if err != nil {
 		t.Fatal(err)
 	}
@@ -453,7 +454,7 @@ func TestMapWithOnlyWithNumbersAsKey(t *testing.T) {
 	}`)
 
 	var result bytes.Buffer
-	err := Apply(rule, data, &result)
+	err := jsonlogic.Apply(rule, data, &result)
 	if err != nil {
 		t.Fatal(err)
 	}
@@ -474,7 +475,7 @@ func TestBetweenIsBiggerEq(t *testing.T) {
 	data := strings.NewReader(`{}`)
 
 	var result bytes.Buffer
-	err := Apply(rule, data, &result)
+	err := jsonlogic.Apply(rule, data, &result)
 	if err != nil {
 		t.Fatal(err)
 	}
@@ -495,7 +496,7 @@ func TestBetweenIsBigger(t *testing.T) {
 	data := strings.NewReader(`{}`)
 
 	var result bytes.Buffer
-	err := Apply(rule, data, &result)
+	err := jsonlogic.Apply(rule, data, &result)
 	if err != nil {
 		t.Fatal(err)
 	}
@@ -510,7 +511,7 @@ func TestUnaryOperation(t *testing.T) {
 	data := strings.NewReader(`{"some_key": "value"}`)
 
 	var result bytes.Buffer
-	assert.Nil(t, Apply(logic, data, &result))
+	assert.Nil(t, jsonlogic.Apply(logic, data, &result))
 
 	assert.JSONEq(t, `true`, result.String())
 }
@@ -520,7 +521,7 @@ func TestInOperatorAgainstNil(t *testing.T) {
 	data := strings.NewReader(`{"accounts":[{"name":"account-1","tags":{"tag-1":"abc"}}, {"name":"account-2","tags":{"tag-2":"xyz"}}]}`)
 
 	var result bytes.Buffer
-	err := Apply(rule, data, &result)
+	err := jsonlogic.Apply(rule, data, &result)
 	if err != nil {
 		t.Fatal(err)
 	}
@@ -542,7 +543,7 @@ func TestReduceFilterAndContains(t *testing.T) {
 	data := strings.NewReader(`{"data":{"level1":{"level2":[{"access":true }]}}}}`)
 
 	var result bytes.Buffer
-	err := Apply(rule, data, &result)
+	err := jsonlogic.Apply(rule, data, &result)
 	if err != nil {
 		t.Fatal(err)
 	}
@@ -557,7 +558,7 @@ func TestReduceFilterAndNotContains(t *testing.T) {
 	data := strings.NewReader(`{"data":{"level1":{"level2":[{"access":false }]}}}}`)
 
 	var result bytes.Buffer
-	err := Apply(rule, data, &result)
+	err := jsonlogic.Apply(rule, data, &result)
 	if err != nil {
 		t.Fatal(err)
 	}
@@ -576,12 +577,12 @@ func TestReduceWithUnsupportedValue(t *testing.T) {
 		"data": []interface{}{"str"},
 	}
 
-	_, err := applyInterface(rule, data)
+	_, err := jsonlogic.ApplyInterface(rule, data)
 	assert.EqualError(t, err, "The type \"<nil>\" is not supported")
 }
 
 func TestAddOperator(t *testing.T) {
-	AddOperator("strlen", func(values, data interface{}) interface{} {
+	jsonlogic.AddOperator("strlen", func(values, data interface{}) interface{} {
 		v, ok := values.(string)
 
 		if ok {
@@ -593,7 +594,7 @@ func TestAddOperator(t *testing.T) {
 	data := strings.NewReader(`{"foo": "bar"}`)
 
 	var result bytes.Buffer
-	err := Apply(logic, data, &result)
+	err := jsonlogic.Apply(logic, data, &result)
 	if err != nil {
 		t.Fatal(err)
 	}
@@ -609,7 +610,7 @@ func TestInWithOneParam(t *testing.T) {
 
 	var result bytes.Buffer
 
-	err := Apply(rule, data, &result)
+	err := jsonlogic.Apply(rule, data, &result)
 	if err != nil {
 		t.Fatal(err)
 	}
@@ -623,7 +624,7 @@ func TestEqualWithList(t *testing.T) {
 
 	var result bytes.Buffer
 
-	err := Apply(rule, data, &result)
+	err := jsonlogic.Apply(rule, data, &result)
 	if err != nil {
 		t.Fatal(err)
 	}
diff --git a/math_test.go b/math_test.go
index b6dd3cf..e790643 100644
--- a/math_test.go
+++ b/math_test.go
@@ -1,9 +1,10 @@
-package jsonlogic
+package jsonlogic_test
 
 import (
 	"encoding/json"
 	"testing"
 
+	"github.com/diegoholiveira/jsonlogic/v3"
 	"github.com/stretchr/testify/assert"
 )
 
@@ -17,7 +18,7 @@ func TestSubOperation(t *testing.T) {
 
 	var expected json.RawMessage = json.RawMessage("-10")
 
-	output, err := ApplyRaw(rule, nil)
+	output, err := jsonlogic.ApplyRaw(rule, nil)
 	if err != nil {
 		t.Fatal(err)
 	}
diff --git a/validator_test.go b/validator_test.go
index 8d1f8af..671905c 100644
--- a/validator_test.go
+++ b/validator_test.go
@@ -1,4 +1,4 @@
-package jsonlogic
+package jsonlogic_test
 
 import (
 	"fmt"
@@ -7,6 +7,8 @@ import (
 	"testing"
 
 	"github.com/stretchr/testify/assert"
+
+	"github.com/diegoholiveira/jsonlogic/v3"
 )
 
 func TestJSONLogicValidator(t *testing.T) {
@@ -47,7 +49,7 @@ func TestJSONLogicValidator(t *testing.T) {
 
 	for name, scenario := range scenarios {
 		t.Run(fmt.Sprintf("SCENARIO:%s", name), func(t *testing.T) {
-			assert.Equal(t, scenario.IsValid, IsValid(scenario.Rule))
+			assert.Equal(t, scenario.IsValid, jsonlogic.IsValid(scenario.Rule))
 		})
 	}
 }