From 287d0eadd6e1908da090a2c2b4641c567f70cc40 Mon Sep 17 00:00:00 2001 From: Diego Henrique Oliveira Date: Mon, 16 Oct 2023 09:53:17 -0300 Subject: [PATCH] Use the first value when min / max --- jsonlogic.go | 25 ++++++++++++++++++------- jsonlogic_test.go | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 7 deletions(-) diff --git a/jsonlogic.go b/jsonlogic.go index bd5c676..babe7f8 100644 --- a/jsonlogic.go +++ b/jsonlogic.go @@ -4,7 +4,6 @@ import ( "encoding/json" "fmt" "io" - "math" "sort" "strings" @@ -195,10 +194,16 @@ func _in(value interface{}, values interface{}) bool { } func max(values interface{}) interface{} { - bigger := math.SmallestNonzeroFloat64 + converted := values.([]interface{}) + size := len(converted) + if size == 0 { + return nil + } + + bigger := toNumber(converted[0]) - for _, n := range values.([]interface{}) { - _n := toNumber(n) + for i := 1; i < size; i++ { + _n := toNumber(converted[i]) if _n > bigger { bigger = _n } @@ -208,10 +213,16 @@ func max(values interface{}) interface{} { } func min(values interface{}) interface{} { - smallest := math.MaxFloat64 + converted := values.([]interface{}) + size := len(converted) + if size == 0 { + return nil + } + + smallest := toNumber(converted[0]) - for _, n := range values.([]interface{}) { - _n := toNumber(n) + for i := 1; i < size; i++ { + _n := toNumber(converted[i]) if smallest > _n { smallest = _n } diff --git a/jsonlogic_test.go b/jsonlogic_test.go index 1747f23..07b0b02 100644 --- a/jsonlogic_test.go +++ b/jsonlogic_test.go @@ -741,6 +741,48 @@ func TestIssue58_example(t *testing.T) { assert.JSONEq(t, expected, result.String()) } +func TestIssue71_example_empty_min(t *testing.T) { + data := strings.NewReader(`{}`) + logic := strings.NewReader(`{"min":[]}`) + + var result bytes.Buffer + err := Apply(logic, data, &result) + if err != nil { + t.Fatal(err) + } + + expected := `null` + assert.JSONEq(t, expected, result.String()) +} + +func TestIssue71_example_empty_max(t *testing.T) { + data := strings.NewReader(`{}`) + logic := strings.NewReader(`{"max":[]}`) + + var result bytes.Buffer + err := Apply(logic, data, &result) + if err != nil { + t.Fatal(err) + } + + expected := `null` + assert.JSONEq(t, expected, result.String()) +} + +func TestIssue71_example_max(t *testing.T) { + data := strings.NewReader(`{}`) + logic := strings.NewReader(`{"max":[-3, -2]}`) + + var result bytes.Buffer + err := Apply(logic, data, &result) + if err != nil { + t.Fatal(err) + } + + expected := `-2` + assert.JSONEq(t, expected, result.String()) +} + func TestJsonLogicWithSolvedVars(t *testing.T) { rule := json.RawMessage(`{ "or":[