Bladeren bron

fix(func): array_flatten tolerate single value (#2192)

Signed-off-by: Jiyong Huang <huangjy@emqx.io>
ngjaying 1 jaar geleden
bovenliggende
commit
ffe3bcef32
2 gewijzigde bestanden met toevoegingen van 17 en 7 verwijderingen
  1. 5 5
      internal/binder/function/funcs_array.go
  2. 12 2
      internal/binder/function/funcs_array_test.go

+ 5 - 5
internal/binder/function/funcs_array.go

@@ -468,13 +468,13 @@ func registerArrayFunc() {
 			var output []interface{}
 
 			for _, val := range array {
-				innerArr, ok := val.([]interface{})
-				if !ok {
-					return errorArrayNotArrayElementError, false
+				switch vt := val.(type) {
+				case []interface{}:
+					output = append(output, vt...)
+				default:
+					output = append(output, vt)
 				}
-				output = append(output, innerArr...)
 			}
-
 			return output, true
 		},
 		val: func(ctx api.FunctionContext, args []ast.Expr) error {

+ 12 - 2
internal/binder/function/funcs_array_test.go

@@ -490,9 +490,9 @@ func TestArrayCommonFunctions(t *testing.T) {
 		{
 			name: "array_flatten",
 			args: []interface{}{
-				[]interface{}{1, 2, 3}, 4,
+				[]interface{}{1, 2, 3},
 			},
-			result: errorArrayNotArrayElementError,
+			result: []interface{}{1, 2, 3},
 		},
 		{
 			name: "array_flatten",
@@ -505,6 +505,16 @@ func TestArrayCommonFunctions(t *testing.T) {
 			result: []interface{}{1, 2, 3, 4, 5, 6},
 		},
 		{
+			name: "array_flatten",
+			args: []interface{}{
+				[]interface{}{
+					[]interface{}{1, 2, 3},
+					4,
+				},
+			},
+			result: []interface{}{1, 2, 3, 4},
+		},
+		{
 			name: "array_distinct",
 			args: []interface{}{
 				[]interface{}{1, 2, 3},