This repository has been archived by the owner on Jan 23, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 119
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add comparison functions (#990)
Signed-off-by: Owen Rumney <[email protected]>
- Loading branch information
1 parent
6f5330d
commit 70b4605
Showing
9 changed files
with
540 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,10 @@ | ||
package functions | ||
|
||
func Coalesce(args ...interface{}) interface{} { | ||
for _, arg := range args { | ||
if arg != nil { | ||
return arg | ||
} | ||
} | ||
return 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package functions | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func Test_Coalesce(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
args []interface{} | ||
expected interface{} | ||
}{ | ||
{ | ||
name: "coalesce with nil", | ||
args: []interface{}{ | ||
nil, | ||
}, | ||
expected: nil, | ||
}, | ||
{ | ||
name: "coalesce with nil and string", | ||
args: []interface{}{ | ||
nil, | ||
"test", | ||
}, | ||
expected: "test", | ||
}, | ||
{ | ||
name: "coalesce with nil and string and int", | ||
args: []interface{}{ | ||
nil, | ||
"test", | ||
1, | ||
}, | ||
expected: "test", | ||
}, | ||
{ | ||
name: "coalesce with nil and nil and array", | ||
args: []interface{}{ | ||
nil, | ||
nil, | ||
[]interface{}{"a", "b", "c"}, | ||
}, | ||
expected: []interface{}{"a", "b", "c"}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
actual := Coalesce(tt.args...) | ||
assert.Equal(t, tt.expected, actual) | ||
}) | ||
} | ||
} |
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,25 @@ | ||
package functions | ||
|
||
func Equals(args ...interface{}) interface{} { | ||
if len(args) != 2 { | ||
return false | ||
} | ||
|
||
slice1, ok := args[0].([]interface{}) | ||
if ok { | ||
slice2, ok := args[1].([]interface{}) | ||
if ok { | ||
if len(slice1) != len(slice2) { | ||
return false | ||
} | ||
for i := 0; i < len(slice1); i++ { | ||
if slice1[i] != slice2[i] { | ||
return false | ||
} | ||
} | ||
return true | ||
} | ||
} | ||
|
||
return args[0] == args[1] | ||
} |
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,111 @@ | ||
package functions | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func Test_Equals(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
args []interface{} | ||
expected interface{} | ||
}{ | ||
{ | ||
name: "equals with nil", | ||
args: []interface{}{ | ||
nil, | ||
}, | ||
expected: false, | ||
}, | ||
{ | ||
name: "equals with nil and string", | ||
args: []interface{}{ | ||
nil, | ||
"test", | ||
}, | ||
expected: false, | ||
}, | ||
{ | ||
name: "equals with nil and string and int", | ||
args: []interface{}{ | ||
nil, | ||
"test", | ||
1, | ||
}, | ||
expected: false, | ||
}, | ||
{ | ||
name: "equals with nil and nil and array", | ||
args: []interface{}{ | ||
nil, | ||
nil, | ||
[]interface{}{"a", "b", "c"}, | ||
}, | ||
expected: false, | ||
}, | ||
{ | ||
name: "equals with nil and nil", | ||
args: []interface{}{ | ||
nil, | ||
nil, | ||
}, | ||
expected: true, | ||
}, | ||
{ | ||
name: "equals with string and string", | ||
args: []interface{}{ | ||
"test", | ||
"test", | ||
}, | ||
expected: true, | ||
}, | ||
{ | ||
name: "equals with string and string", | ||
args: []interface{}{ | ||
"test", | ||
"test1", | ||
}, | ||
expected: false, | ||
}, | ||
{ | ||
name: "equals with int and int", | ||
args: []interface{}{ | ||
1, | ||
1, | ||
}, | ||
expected: true, | ||
}, | ||
{ | ||
name: "equals with int and int", | ||
args: []interface{}{ | ||
1, | ||
2, | ||
}, | ||
expected: false, | ||
}, | ||
{ | ||
name: "equals with array and array", | ||
args: []interface{}{ | ||
[]interface{}{"a", "b", "c"}, | ||
[]interface{}{"a", "b", "c"}, | ||
}, | ||
expected: true, | ||
}, | ||
{ | ||
name: "equals with array and array", | ||
args: []interface{}{ | ||
[]interface{}{"a", "b", "c"}, | ||
[]interface{}{"a", "b", "d"}, | ||
}, | ||
expected: false, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
actual := Equals(tt.args...) | ||
assert.Equal(t, tt.expected, actual) | ||
}) | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package functions | ||
|
||
func Greater(args ...interface{}) interface{} { | ||
|
||
if len(args) != 2 { | ||
return false | ||
} | ||
|
||
switch arg0 := args[0].(type) { | ||
case int: | ||
arg1, ok := args[1].(int) | ||
if ok { | ||
return arg0 > arg1 | ||
} | ||
case string: | ||
arg1, ok := args[1].(string) | ||
if ok { | ||
return arg0 > arg1 | ||
} | ||
} | ||
|
||
return false | ||
} | ||
|
||
func GreaterOrEquals(args ...interface{}) interface{} { | ||
|
||
if len(args) != 2 { | ||
return false | ||
} | ||
|
||
switch arg0 := args[0].(type) { | ||
case nil: | ||
return args[1] == nil | ||
case int: | ||
arg1, ok := args[1].(int) | ||
if ok { | ||
return arg0 >= arg1 | ||
} | ||
case string: | ||
arg1, ok := args[1].(string) | ||
if ok { | ||
return arg0 >= arg1 | ||
} | ||
} | ||
|
||
return false | ||
} |
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,119 @@ | ||
package functions | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func Test_Greater(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
args []interface{} | ||
expected interface{} | ||
}{ | ||
|
||
{ | ||
name: "greater with nil and string", | ||
args: []interface{}{ | ||
nil, | ||
"test", | ||
}, | ||
expected: false, | ||
}, | ||
{ | ||
name: "greater with nil and nil", | ||
args: []interface{}{ | ||
nil, | ||
nil, | ||
}, | ||
expected: false, | ||
}, | ||
{ | ||
name: "greater with string and string", | ||
args: []interface{}{ | ||
"test", | ||
"test", | ||
}, | ||
expected: false, | ||
}, | ||
{ | ||
name: "greater with string and int", | ||
args: []interface{}{ | ||
"test", | ||
1, | ||
}, | ||
expected: false, | ||
}, | ||
{ | ||
name: "greater with int and int", | ||
args: []interface{}{ | ||
1, | ||
1, | ||
}, | ||
expected: false, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
actual := Greater(tt.args...) | ||
assert.Equal(t, tt.expected, actual) | ||
}) | ||
} | ||
} | ||
|
||
func Test_GreaterThanOrEqual(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
args []interface{} | ||
expected interface{} | ||
}{ | ||
|
||
{ | ||
name: "greater with nil and string", | ||
args: []interface{}{ | ||
nil, | ||
"test", | ||
}, | ||
expected: false, | ||
}, | ||
{ | ||
name: "greater with nil and nil", | ||
args: []interface{}{ | ||
nil, | ||
nil, | ||
}, | ||
expected: true, | ||
}, | ||
{ | ||
name: "greater with string and string", | ||
args: []interface{}{ | ||
"test", | ||
"test", | ||
}, | ||
expected: true, | ||
}, | ||
{ | ||
name: "greater with string and int", | ||
args: []interface{}{ | ||
"test", | ||
1, | ||
}, | ||
expected: false, | ||
}, | ||
{ | ||
name: "greater with int and int", | ||
args: []interface{}{ | ||
1, | ||
1, | ||
}, | ||
expected: true, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
actual := GreaterOrEquals(tt.args...) | ||
assert.Equal(t, tt.expected, actual) | ||
}) | ||
} | ||
} |
Oops, something went wrong.