Skip to content
This repository has been archived by the owner on Jan 23, 2025. It is now read-only.

Commit

Permalink
feat: add comparison functions (#990)
Browse files Browse the repository at this point in the history
Signed-off-by: Owen Rumney <[email protected]>
  • Loading branch information
owenrumney authored Oct 6, 2022
1 parent 6f5330d commit 70b4605
Show file tree
Hide file tree
Showing 9 changed files with 540 additions and 0 deletions.
10 changes: 10 additions & 0 deletions pkg/scanners/azure/functions/coalesce.go
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
}
56 changes: 56 additions & 0 deletions pkg/scanners/azure/functions/coalesce_test.go
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)
})
}
}
25 changes: 25 additions & 0 deletions pkg/scanners/azure/functions/equals.go
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]
}
111 changes: 111 additions & 0 deletions pkg/scanners/azure/functions/equals_test.go
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)
})
}
}
6 changes: 6 additions & 0 deletions pkg/scanners/azure/functions/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ var generalFuncs = map[string]func(...interface{}) interface{}{
"union:": Union,
"uniqueString": UniqueString,
"uri": Uri,
"coalesce": Coalesce,
"equals": Equals,
"greater": Greater,
"greaterOrEquals": GreaterOrEquals,
"less": Less,
"lessOrEquals": LessOrEquals,
}

func Evaluate(deploymentProvider DeploymentData, name string, args ...interface{}) interface{} {
Expand Down
47 changes: 47 additions & 0 deletions pkg/scanners/azure/functions/greater.go
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
}
119 changes: 119 additions & 0 deletions pkg/scanners/azure/functions/greater_test.go
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)
})
}
}
Loading

0 comments on commit 70b4605

Please sign in to comment.