github.com/GuanceCloud/cliutils@v1.1.21/pipeline/ptinput/funcs/fn_group_in.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 11 "github.com/GuanceCloud/cliutils/pipeline/ptinput" 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 GroupInChecking(ctx *runtime.Task, funcExpr *ast.CallExpr) *errchain.PlError { 18 if len(funcExpr.Param) < 3 || len(funcExpr.Param) > 4 { 19 return runtime.NewRunError(ctx, fmt.Sprintf( 20 "func %s expected 3 or 4 args", funcExpr.Name), 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 if len(funcExpr.Param) == 4 { 28 switch funcExpr.Param[3].NodeType { //nolint:exhaustive 29 case ast.TypeAttrExpr, ast.TypeStringLiteral, ast.TypeIdentifier: 30 default: 31 return runtime.NewRunError(ctx, fmt.Sprintf( 32 "param new-key expect AttrExpr, StringLiteral or Identifier, got %s", 33 funcExpr.Param[3].NodeType), funcExpr.Param[3].StartPos()) 34 } 35 } 36 return nil 37 } 38 39 func GroupIn(ctx *runtime.Task, funcExpr *ast.CallExpr) *errchain.PlError { 40 if len(funcExpr.Param) < 3 || len(funcExpr.Param) > 4 { 41 return runtime.NewRunError(ctx, fmt.Sprintf("func %s expected 3 or 4 args", funcExpr.Name), 42 funcExpr.NamePos) 43 } 44 45 key, err := getKeyName(funcExpr.Param[0]) 46 if err != nil { 47 return runtime.NewRunError(ctx, err.Error(), funcExpr.Param[0].StartPos()) 48 } 49 50 cont, err := ctx.GetKey(key) 51 if err != nil { 52 // l.Debugf("key '%v' not exist, ignored", key) 53 return nil //nolint:nilerr 54 } 55 56 var set []any 57 switch funcExpr.Param[1].NodeType { //nolint:exhaustive 58 case ast.TypeIdentifier: 59 if v, err := ctx.GetKey(funcExpr.Param[1].Identifier().Name); err == nil && 60 v.DType == ast.List { 61 if v, ok := v.Value.([]any); ok { 62 set = v 63 } 64 } 65 case ast.TypeListLiteral: 66 if v, dtype, err := runtime.RunListInitExpr(ctx, 67 funcExpr.Param[1].ListLiteral()); err == nil && dtype == ast.List { 68 if v, ok := v.([]any); ok { 69 set = v 70 } 71 } 72 } 73 74 if GroupInHandle(cont.Value, set) { 75 value := funcExpr.Param[2] 76 77 var val any 78 var dtype ast.DType 79 80 switch value.NodeType { //nolint:exhaustive 81 case ast.TypeIntegerLiteral: 82 val = value.IntegerLiteral().Val 83 dtype = ast.Int 84 case ast.TypeFloatLiteral: 85 val = value.FloatLiteral().Val 86 dtype = ast.Float 87 case ast.TypeStringLiteral: 88 val = value.StringLiteral().Val 89 dtype = ast.String 90 case ast.TypeBoolLiteral: 91 val = value.BoolLiteral().Val 92 dtype = ast.String 93 default: 94 return nil 95 } 96 97 if len(funcExpr.Param) == 4 { 98 if k, err := getKeyName(funcExpr.Param[3]); err == nil { 99 key = k 100 } else { 101 return runtime.NewRunError(ctx, err.Error(), funcExpr.Param[3].StartPos()) 102 } 103 } 104 _ = addKey2PtWithVal(ctx.InData(), key, val, dtype, ptinput.KindPtDefault) 105 } 106 107 return nil 108 }