github.com/GuanceCloud/cliutils@v1.1.21/pipeline/ptinput/funcs/fn_nullif.go (about) 1 // Unless explicitly stated otherwise all files in this repository are licensed 2 // under the MIT License. 3 // This product includes software developed at Guance Cloud (https://www.guance.com/). 4 // Copyright 2021-present Guance, Inc. 5 6 package funcs 7 8 import ( 9 "fmt" 10 "reflect" 11 12 "github.com/GuanceCloud/platypus/pkg/ast" 13 "github.com/GuanceCloud/platypus/pkg/engine/runtime" 14 "github.com/GuanceCloud/platypus/pkg/errchain" 15 ) 16 17 func NullIfChecking(ctx *runtime.Task, funcExpr *ast.CallExpr) *errchain.PlError { 18 if len(funcExpr.Param) != 2 { 19 return runtime.NewRunError(ctx, fmt.Sprintf("func %s expected 2 args", funcExpr.Name), 20 funcExpr.NamePos) 21 } 22 23 if _, err := getKeyName(funcExpr.Param[0]); err != nil { 24 return runtime.NewRunError(ctx, err.Error(), funcExpr.Param[0].StartPos()) 25 } 26 27 return nil 28 } 29 30 func NullIf(ctx *runtime.Task, funcExpr *ast.CallExpr) *errchain.PlError { 31 if len(funcExpr.Param) != 2 { 32 return runtime.NewRunError(ctx, fmt.Sprintf( 33 "func %s expected 2 args", funcExpr.Name), funcExpr.NamePos) 34 } 35 36 key, err := getKeyName(funcExpr.Param[0]) 37 if err != nil { 38 return runtime.NewRunError(ctx, err.Error(), funcExpr.Param[0].StartPos()) 39 } 40 41 cont, err := ctx.GetKey(key) 42 if err != nil { 43 // l.Debugf("key `%v' not exist, ignored", key) 44 return nil //nolint:nilerr 45 } 46 47 var val interface{} 48 49 switch funcExpr.Param[1].NodeType { //nolint:exhaustive 50 case ast.TypeStringLiteral, ast.TypeFloatLiteral, ast.TypeIntegerLiteral, 51 ast.TypeBoolLiteral, ast.TypeNilLiteral: 52 if v, _, err := runtime.RunStmt(ctx, funcExpr.Param[1]); err == nil { 53 val = v 54 } 55 } 56 57 // todo key string 58 if reflect.DeepEqual(cont.Value, val) { 59 deletePtKey(ctx.InData(), key) 60 } 61 62 return nil 63 }