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

feat: add array functions for Azure arm #988

Merged
merged 1 commit into from
Oct 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions pkg/scanners/azure/functions/array.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package functions

func Array(args ...interface{}) interface{} {

if len(args) != 1 {
return ""
}

switch ctype := args[0].(type) {
case int:
return []int{ctype}
case string:
return []string{ctype}
case map[string]interface{}:
var result []interface{}
for k, v := range ctype {
result = append(result, k, v)
}
return result
case interface{}:
switch ctype := ctype.(type) {
case []string:
return ctype
case []interface{}:
return ctype
}
}
return []interface{}{}
}
44 changes: 44 additions & 0 deletions pkg/scanners/azure/functions/array_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package functions

import (
"testing"

"github.com/stretchr/testify/assert"
)

func Test_Array(t *testing.T) {
test := []struct {
name string
input []interface{}
expected interface{}
}{
{
name: "array from an int",
input: []interface{}{1},
expected: []int{1},
},
{
name: "array from a string",
input: []interface{}{"hello"},
expected: []string{"hello"},
},
{
name: "array from a map",
input: []interface{}{map[string]interface{}{"hello": "world"}},
expected: []interface{}{"hello", "world"},
},
{
name: "array from an slice",
input: []interface{}{
[]string{"hello", "world"},
},
expected: []string{"hello", "world"},
},
}
for _, tt := range test {
t.Run(tt.name, func(t *testing.T) {
actual := Array(tt.input...)
assert.Equal(t, tt.expected, actual)
})
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package stringFunctions
package functions

import (
"encoding/base64"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package stringFunctions
package functions

import (
"encoding/json"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package stringFunctions
package functions

import "strings"

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package stringFunctions
package functions

import (
"testing"
Expand Down
28 changes: 28 additions & 0 deletions pkg/scanners/azure/functions/concat.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package functions

import (
"fmt"
)

func Concat(args ...interface{}) interface{} {

switch args[0].(type) {
case string:
var result string
for _, arg := range args {
result += fmt.Sprintf("%v", arg)
}
return result
case interface{}:
var result []interface{}
for _, arg := range args {
argArr, ok := arg.([]interface{})
if !ok {
continue
}
result = append(result, argArr...)
}
return result
}
return ""
}
94 changes: 94 additions & 0 deletions pkg/scanners/azure/functions/concat_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package functions

import (
"testing"

"github.com/stretchr/testify/require"
)

func Test_StringConcatenation(t *testing.T) {
tests := []struct {
name string
args []interface{}
expected string
}{
{
name: "simple string concatenation",
args: []interface{}{
"hello",
", ",
"world",
"!",
},
expected: "hello, world!",
},
{
name: "string concatenation with non strings",
args: []interface{}{
"pi to 3 decimal places is ",
3.142,
},
expected: "pi to 3 decimal places is 3.142",
},
{
name: "string concatenation with multiple primitives",
args: []interface{}{
"to say that ",
3,
" is greater than ",
5,
" would be ",
false,
},
expected: "to say that 3 is greater than 5 would be false",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
concatenated := Concat(tt.args...)
require.Equal(t, tt.expected, concatenated)
})
}
}

func Test_ArrayConcatenation(t *testing.T) {
tests := []struct {
name string
args []interface{}
expected []interface{}
}{
{
name: "simple array concatenation",
args: []interface{}{
[]interface{}{1, 2, 3},
[]interface{}{4, 5, 6},
},
expected: []interface{}{1, 2, 3, 4, 5, 6},
},
{
name: "array concatenation with non arrays",
args: []interface{}{
[]interface{}{1, 2, 3},
4,
},
expected: []interface{}{1, 2, 3},
},
{
name: "array concatenation with multiple primitives",
args: []interface{}{
[]interface{}{1, 2, 3},
4,
[]interface{}{5, 6, 7},
},
expected: []interface{}{1, 2, 3, 5, 6, 7},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
concatenated := Concat(tt.args...)
require.Equal(t, tt.expected, concatenated)
})
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package stringFunctions
package functions

import (
"fmt"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package stringFunctions
package functions

import (
"testing"
Expand Down
11 changes: 11 additions & 0 deletions pkg/scanners/azure/functions/create_array.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package functions

func CreateArray(args ...interface{}) interface{} {
var result []interface{}
if len(args) == 0 {
return result
}

result = append(result, args...)
return result
}
68 changes: 68 additions & 0 deletions pkg/scanners/azure/functions/create_array_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package functions

import (
"testing"

"github.com/stretchr/testify/assert"
)

func Test_CreateArray(t *testing.T) {

tests := []struct {
name string
args []interface{}
expected interface{}
}{
{
name: "create array with strings",
args: []interface{}{
"Hello",
"World",
},
expected: []interface{}{"Hello", "World"},
},
{
name: "create array with ints",

args: []interface{}{
1, 2, 3,
},
expected: []interface{}{1, 2, 3},
},
{
name: "create array with arrays",
args: []interface{}{
[]interface{}{1, 2, 3},
[]interface{}{4, 5, 6},
},
expected: []interface{}{[]interface{}{1, 2, 3}, []interface{}{4, 5, 6}},
},
{
name: "create arrau with maps",
args: []interface{}{
map[string]interface{}{
"one": "a",
},
map[string]interface{}{
"two": "b",
},
},
expected: []interface{}{
map[string]interface{}{
"one": "a",
},
map[string]interface{}{
"two": "b",
},
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
actual := CreateArray(tt.args...)
assert.Equal(t, tt.expected, actual)
})
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package stringFunctions
package functions

import (
"fmt"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package stringFunctions
package functions

import (
"testing"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package stringFunctions
package functions

func Empty(args ...interface{}) interface{} {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package stringFunctions
package functions

import (
"testing"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package stringFunctions
package functions

import "strings"

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package stringFunctions
package functions

import (
"testing"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package stringFunctions
package functions

func First(args ...interface{}) interface{} {
if len(args) != 1 {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package stringFunctions
package functions

import (
"testing"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package stringFunctions
package functions

import (
"fmt"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package stringFunctions
package functions

import (
"testing"
Expand Down
Loading