|
@@ -16,9 +16,11 @@ package function
|
|
|
|
|
|
import (
|
|
|
"fmt"
|
|
|
+ "reflect"
|
|
|
|
|
|
"github.com/lf-edge/ekuiper/pkg/api"
|
|
|
"github.com/lf-edge/ekuiper/pkg/ast"
|
|
|
+ "github.com/lf-edge/ekuiper/pkg/cast"
|
|
|
)
|
|
|
|
|
|
func registerObjectFunc() {
|
|
@@ -155,4 +157,56 @@ func registerObjectFunc() {
|
|
|
},
|
|
|
check: returnNilIfHasAnyNil,
|
|
|
}
|
|
|
+ builtins["erase"] = builtinFunc{
|
|
|
+ fType: ast.FuncTypeScalar,
|
|
|
+ exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
|
|
|
+ contains := func(array []string, target string) bool {
|
|
|
+ for _, v := range array {
|
|
|
+ if target == v {
|
|
|
+ return true
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return false
|
|
|
+ }
|
|
|
+ if len(args) != 2 {
|
|
|
+ return fmt.Errorf("the argument number should be 2, got %v", len(args)), false
|
|
|
+ }
|
|
|
+ res := make(map[string]interface{})
|
|
|
+ argMap, ok := args[0].(map[string]interface{})
|
|
|
+ if !ok {
|
|
|
+ return fmt.Errorf("the first argument should be map[string]interface{}, got %v", args[0]), false
|
|
|
+ }
|
|
|
+ eraseArray := make([]string, 0)
|
|
|
+ v := reflect.ValueOf(args[1])
|
|
|
+ switch v.Kind() {
|
|
|
+ case reflect.Slice:
|
|
|
+ array, err := cast.ToStringSlice(args[1], cast.CONVERT_ALL)
|
|
|
+ if err != nil {
|
|
|
+ return err, false
|
|
|
+ }
|
|
|
+ eraseArray = append(eraseArray, array...)
|
|
|
+ case reflect.String:
|
|
|
+ str := args[1].(string)
|
|
|
+ for k, v := range argMap {
|
|
|
+ if k != str {
|
|
|
+ res[k] = v
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return res, true
|
|
|
+ default:
|
|
|
+ return fmt.Errorf("the augument should be slice or string"), false
|
|
|
+ }
|
|
|
+ for k, v := range argMap {
|
|
|
+ if !contains(eraseArray, k) {
|
|
|
+ res[k] = v
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return res, true
|
|
|
+ },
|
|
|
+ val: func(_ api.FunctionContext, args []ast.Expr) error {
|
|
|
+ return ValidateAtLeast(2, len(args))
|
|
|
+ },
|
|
|
+ check: returnNilIfHasAnyNil,
|
|
|
+ }
|
|
|
}
|