Skip to content

Commit

Permalink
Mfw 4086 (#332)
Browse files Browse the repository at this point in the history
* Added generic array comparable
Added StringArrayComparable

* Added unit tests

---------

Co-authored-by: Christian Rasmussen <crasmussen@arista.com>
  • Loading branch information
rasmussen-untangle and Christian Rasmussen authored Dec 7, 2023
1 parent 727a705 commit 502e4c5
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 0 deletions.
61 changes: 61 additions & 0 deletions booleval/array_comparable.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package booleval

import "net"

// ArrayComparable is a Comparable for arrays of Comparables.
type ArrayComparable struct {
theThings []Comparable
}

var _ Comparable = ArrayComparable{}

// NewArrayComparable returns a Comparable for a array of value.
func NewArrayComparable(value any) ArrayComparable {
var comparables []Comparable
switch val := value.(type) {
case []string:
for _, v := range val {
comparables = append(comparables, NewStringComparable(v))
}
// We loose the type if we have more than one case listed.
case []int:
for _, v := range val {
if iComp, err := NewIntegerComparableFromAny(v); err == nil {
comparables = append(comparables, iComp)
}
}
case []net.IP:
for _, v := range val {
if ipComp, err := NewIPOrIPNetComparable(v.String()); err == nil {
comparables = append(comparables, ipComp)
}
}
case []net.IPNet:
for _, v := range val {
if ipComp, err := NewIPOrIPNetComparable(v.String()); err == nil {
comparables = append(comparables, ipComp)
}
}
}
return ArrayComparable{comparables}
}

// Equal returns true if other is is equal to any of the comparables in the array
func (s ArrayComparable) Equal(other any) (bool, error) {
for _, v := range s.theThings {
if equal, err := v.Equal(other); err == nil && equal {
return true, nil
}
}
return false, nil
}

// Greater returns true if other is is greater than any of the things in the array
func (s ArrayComparable) Greater(other any) (bool, error) {
for _, v := range s.theThings {
if greater, err := v.Greater(other); err == nil && greater {
return true, nil
}
}
return false, nil
}
11 changes: 11 additions & 0 deletions booleval/comparables_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,3 +289,14 @@ func TestStrings(t *testing.T) {
{eq, 1, false, false}}
testDriver(t, comp, tests)
}

func TestStringArrays(t *testing.T) {
comp := NewStringArrayComparable([]string{"a_one", "a_two"})
tests := []valueCondTest{
{eq, "a_one", true, false},
{eq, "a_two", true, false},
{eq, "a", false, false},
{eq, &valueCondTest{}, false, false},
{eq, 1, false, false}}
testDriver(t, comp, tests)
}
29 changes: 29 additions & 0 deletions booleval/eval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func TestExprs(t *testing.T) {
timeComparable1999 := TimeComparable{time: time1}
time2 := time.Date(2000, time.April, int(time.Monday), 0, 0, 0, 0, location)
timeComparable2000 := TimeComparable{time: time2}
stringArrayComparable := NewStringArrayComparable([]string{"doodle", "toodle"})
tests := []struct {
expr Expression
result bool
Expand Down Expand Up @@ -292,6 +293,34 @@ func TestExprs(t *testing.T) {
false,
false,
},
{NewSimpleExpression(
AndOfOrsMode,
[][]*AtomicExpression{
{
{"==", stringArrayComparable, "myStringArray"},
},
{
{"==", stringArrayComparable, "toodle"},
},
},
),
false,
false,
},
{NewSimpleExpression(
OrOfAndsMode,
[][]*AtomicExpression{
{
{"==", stringArrayComparable, "myStringArray"},
},
{
{"==", stringArrayComparable, "toodle"},
},
},
),
true,
false,
},
}

for _, test := range tests {
Expand Down
5 changes: 5 additions & 0 deletions booleval/string_comparable.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,8 @@ func (s StringComparable) Equal(other any) (bool, error) {
}

var _ Comparable = StringComparable{}

// NewStringArrayComparable returns a Comparable for an array of strings.
func NewStringArrayComparable(val []string) ArrayComparable {
return NewArrayComparable(val)
}

0 comments on commit 502e4c5

Please sign in to comment.