Просмотр исходного кода

fix(func): latest func should not return current value if when not matched

Signed-off-by: Jiyong Huang <huangjy@emqx.io>
Jiyong Huang 2 лет назад
Родитель
Сommit
fccb8f72b6

+ 5 - 7
internal/binder/function/funcs_analytic.go

@@ -208,8 +208,11 @@ func registerAnalyticFunc() {
 			if !ok {
 			if !ok {
 				return fmt.Errorf("when arg is not a bool but got %v", args[len(args)-2]), false
 				return fmt.Errorf("when arg is not a bool but got %v", args[len(args)-2]), false
 			}
 			}
-
-			if args[0] == nil {
+			// notice nil is ignored in latest
+			if validData && args[0] != nil {
+				ctx.PutState(key, args[0])
+				return args[0], true
+			} else {
 				v, err := ctx.GetState(key)
 				v, err := ctx.GetState(key)
 				if err != nil {
 				if err != nil {
 					return fmt.Errorf("error getting state for %s: %v", key, err), false
 					return fmt.Errorf("error getting state for %s: %v", key, err), false
@@ -223,11 +226,6 @@ func registerAnalyticFunc() {
 				} else {
 				} else {
 					return v, true
 					return v, true
 				}
 				}
-			} else {
-				if validData {
-					ctx.PutState(key, args[0])
-				}
-				return args[0], true
 			}
 			}
 		},
 		},
 		val: func(_ api.FunctionContext, args []ast.Expr) error {
 		val: func(_ api.FunctionContext, args []ast.Expr) error {

+ 1 - 1
internal/binder/function/funcs_analytic_test.go

@@ -1259,7 +1259,7 @@ func TestLatestExecWithWhen(t *testing.T) {
 				false,
 				false,
 				"self",
 				"self",
 			},
 			},
-			result: "bar",
+			result: "foo",
 		},
 		},
 		{ // 4
 		{ // 4
 			args: []interface{}{
 			args: []interface{}{