Skip to content

Commit

Permalink
Added and tested IsNull and IsPresent conditions. Renamed Empty as Is…
Browse files Browse the repository at this point in the history
…Empty
  • Loading branch information
redjack96 committed Sep 3, 2024
1 parent a34fa0d commit 83a95dd
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 11 deletions.
57 changes: 48 additions & 9 deletions internal/fc/conditions.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,17 @@ const (
Or CondEnum = "Or"
Not CondEnum = "Not"
Const CondEnum = "Const"
Eq CondEnum = "Eq" // also works for strings
Diff CondEnum = "Diff" // also works for strings
Greater CondEnum = "Greater" // also works for strings
Smaller CondEnum = "Smaller" // also works for strings
Empty CondEnum = "Empty" // for collections
Eq CondEnum = "Eq" // also works for strings
Diff CondEnum = "Diff" // also works for strings
Greater CondEnum = "Greater" // also works for strings
Smaller CondEnum = "Smaller" // also works for strings
IsEmpty CondEnum = "IsEmpty" // for collections
IsNull CondEnum = "IsNull" // returns true for the value nil or the string "null"
IsPresent CondEnum = "IsPresent"
IsNumeric CondEnum = "IsNumeric" // for collections
IsString CondEnum = "IsString"
IsBoolean CondEnum = "IsBoolean"
IsTimestamp CondEnum = "IsTimestamp"
StringMatches CondEnum = "StringMatches"
)

Expand Down Expand Up @@ -105,8 +110,12 @@ func (c Condition) String() string {
return "? < ?"
}
return fmt.Sprintf("%v < %v", c.Op[0], c.Op[1])
case Empty:
return "empty input"
case IsEmpty:
return fmt.Sprintf("IsEmpty(%v)", c.Op[0])
case IsNull:
return fmt.Sprintf("IsNull(%v)", c.Op[0])
case IsPresent:
return fmt.Sprintf("IsPresent(%v)", c.Op[0])
case IsNumeric:
return fmt.Sprintf("IsNumeric(%v)", c.Op[0])
case StringMatches:
Expand Down Expand Up @@ -283,7 +292,7 @@ func (c Condition) Test(input map[string]interface{}) (bool, error) {
fmt.Printf("condition Smaller: second operand '%v' cannot be converted to string\n", c.Op[1])
return false, nil
}
case Empty:
case IsEmpty:
ops, err := c.findInputs(input)
if err != nil {
return false, err
Expand All @@ -293,6 +302,20 @@ func (c Condition) Test(input map[string]interface{}) (bool, error) {
return false, err
}
return len(slice) == 0, nil
case IsNull:
ops, err := c.findInputs(input)
if err != nil {
return false, err
}
op1 := ops[0]
return op1 == "null" || op1 == nil, nil
case IsPresent:
ops, err := c.findInputs(input)
if err != nil {
return false, err
}
op1 := ops[0]
return !(op1 == "null" || op1 == nil), nil
case IsNumeric:
isNumeric := func(s interface{}) bool {
_, err := function.Int{}.Convert(s)
Expand Down Expand Up @@ -608,12 +631,28 @@ func NewSmallerParamCondition(param1 *ParamOrValue, param2 *ParamOrValue) Condit

func NewEmptyCondition(collection []interface{}) Condition {
return Condition{
Type: Empty,
Type: IsEmpty,
Op: collection,
Find: make([]bool, 1), // all false,
}
}

func NewIsNullParamCondition(param1 *ParamOrValue) Condition {
return Condition{
Type: IsNull,
Find: []bool{param1.IsParam},
Op: []interface{}{param1.GetOperand()},
}
}

func NewIsPresentParamCondition(param1 *ParamOrValue) Condition {
return Condition{
Type: IsPresent,
Find: []bool{param1.IsParam},
Op: []interface{}{param1.GetOperand()},
}
}

func NewIsNumericParamCondition(param1 *ParamOrValue) Condition {
return Condition{
Type: IsNumeric,
Expand Down
40 changes: 38 additions & 2 deletions internal/test/condition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
var predicate1 = fc.Predicate{Root: fc.Condition{Type: fc.And, Find: []bool{false, false}, Sub: []fc.Condition{{Type: fc.Eq, Op: []interface{}{2, 2}, Find: []bool{false, false}}, {Type: fc.Greater, Op: []interface{}{4, 2}, Find: []bool{false, false}}}}}
var predicate2 = fc.Predicate{Root: fc.Condition{Type: fc.Or, Find: []bool{false, false}, Sub: []fc.Condition{{Type: fc.Const, Op: []interface{}{true}, Find: []bool{false}}, {Type: fc.Smaller, Op: []interface{}{4, 2}, Find: []bool{false, false}}}}}
var predicate3 = fc.Predicate{Root: fc.Condition{Type: fc.Or, Find: []bool{false, false}, Sub: []fc.Condition{predicate1.Root, {Type: fc.Smaller, Op: []interface{}{4, 2}, Find: []bool{false, false}}}}}
var predicate4 = fc.Predicate{Root: fc.Condition{Type: fc.Not, Find: []bool{false}, Sub: []fc.Condition{{Type: fc.Empty, Op: []interface{}{1, 2, 3, 4}, Find: []bool{false}}}}}
var predicate4 = fc.Predicate{Root: fc.Condition{Type: fc.Not, Find: []bool{false}, Sub: []fc.Condition{{Type: fc.IsEmpty, Op: []interface{}{1, 2, 3, 4}, Find: []bool{false}}}}}

func TestPredicateMarshal(t *testing.T) {

Expand Down Expand Up @@ -56,7 +56,7 @@ func TestPrintPredicate(t *testing.T) {
predicate3.Print()

str4 := predicate4.LogicString()
utils.AssertEquals(t, "!(empty input)", str4)
utils.AssertEquals(t, "!(IsEmpty(1))", str4)
predicate4.Print()
}

Expand Down Expand Up @@ -214,3 +214,39 @@ func TestBooleanEquals(t *testing.T) {
utils.AssertEqualsMsg(t, ok, test.equals, fmt.Sprintf("test %d: expected %v to match %v", i+1, test.firstBoolean, test.secondBoolean))
}
}

func TestIsNullIsPresent(t *testing.T) {
tests := []struct {
value interface{}
shouldBeNil bool
}{
{nil, true},
{"null", true},
{"", false},
{[]byte{}, false},
{0, false},
{false, false},
}

for i, test := range tests {
cond := fc.NewIsNullParamCondition(fc.NewValue(test.value))
ok, err := cond.Test(map[string]interface{}{})
utils.AssertNil(t, err)
utils.AssertEqualsMsg(t, test.shouldBeNil, ok, fmt.Sprintf("test %d: expected IsNull(%v) to be %v", i+1, test.value, test.shouldBeNil))

cond2 := fc.NewIsPresentParamCondition(fc.NewValue(test.value))
ok2, err2 := cond2.Test(map[string]interface{}{})
utils.AssertNil(t, err2)
utils.AssertEqualsMsg(t, !test.shouldBeNil, ok2, fmt.Sprintf("test %d: expected IsNull(%v) to be %v", i+1, test.value, !test.shouldBeNil))
}

cond := fc.NewIsNullParamCondition(fc.NewParam("non-existent"))
ok, err := cond.Test(map[string]interface{}{})
utils.AssertNil(t, err)
utils.AssertTrue(t, ok)

cond2 := fc.NewIsPresentParamCondition(fc.NewParam("non-existent"))
ok2, err2 := cond2.Test(map[string]interface{}{})
utils.AssertNil(t, err2)
utils.AssertFalse(t, ok2)
}

0 comments on commit 83a95dd

Please sign in to comment.