-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Added generic array comparable Added StringArrayComparable * Added unit tests --------- Co-authored-by: Christian Rasmussen <crasmussen@arista.com>
- Loading branch information
1 parent
727a705
commit 502e4c5
Showing
4 changed files
with
106 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters