github.com/GuanceCloud/cliutils@v1.1.21/pipeline/ptinput/funcs/fn_group.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 GroupChecking(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 var start, end float64 28 29 if len(funcExpr.Param) == 4 { 30 if _, err := getKeyName(funcExpr.Param[3]); err != nil { 31 return runtime.NewRunError(ctx, err.Error(), funcExpr.Param[3].StartPos()) 32 } 33 } 34 35 var set []*ast.Node 36 if funcExpr.Param[1].NodeType == ast.TypeListLiteral { 37 set = funcExpr.Param[1].ListLiteral().List 38 } 39 40 if len(set) != 2 { 41 return runtime.NewRunError(ctx, fmt.Sprintf( 42 "param between range value `%v' is not expected", set), 43 funcExpr.Param[1].StartPos()) 44 } 45 46 switch set[0].NodeType { //nolint:exhaustive 47 case ast.TypeFloatLiteral: 48 start = set[0].FloatLiteral().Val 49 case ast.TypeIntegerLiteral: 50 start = float64(set[0].IntegerLiteral().Val) 51 52 default: 53 return runtime.NewRunError(ctx, fmt.Sprintf( 54 "range value `%v' is not expected", set), set[0].StartPos()) 55 } 56 57 switch set[1].NodeType { //nolint:exhaustive 58 case ast.TypeFloatLiteral: 59 end = set[1].FloatLiteral().Val 60 case ast.TypeIntegerLiteral: 61 end = float64(set[1].IntegerLiteral().Val) 62 default: 63 return runtime.NewRunError(ctx, fmt.Sprintf( 64 "range value `%v' is not expected", set), set[1].StartPos()) 65 } 66 67 if start > end { 68 return runtime.NewRunError(ctx, fmt.Sprintf( 69 "range value start %v must le end %v", start, end), funcExpr.NamePos) 70 } 71 72 return nil 73 } 74 75 func Group(ctx *runtime.Task, funcExpr *ast.CallExpr) *errchain.PlError { 76 if len(funcExpr.Param) < 3 || len(funcExpr.Param) > 4 { 77 return runtime.NewRunError(ctx, fmt.Sprintf( 78 "func `%s' expected 3 or 4 args", funcExpr.Name), funcExpr.NamePos) 79 } 80 81 key, err := getKeyName(funcExpr.Param[0]) 82 if err != nil { 83 return runtime.NewRunError(ctx, err.Error(), funcExpr.Param[0].StartPos()) 84 } 85 86 cont, err := ctx.GetKey(key) 87 if err != nil { 88 l.Debugf("key `%v' not exist, ignored", key) 89 return nil //nolint:nilerr 90 } 91 92 var start, end float64 93 94 var set []*ast.Node 95 if funcExpr.Param[1].NodeType == ast.TypeListLiteral { 96 set = funcExpr.Param[1].ListLiteral().List 97 } 98 99 if len(set) != 2 { 100 return runtime.NewRunError(ctx, fmt.Sprintf( 101 "param between range value `%v' is not expected", set), funcExpr.Param[1].StartPos()) 102 } 103 104 switch set[0].NodeType { //nolint:exhaustive 105 case ast.TypeIntegerLiteral: 106 start = float64(set[0].IntegerLiteral().Val) 107 case ast.TypeFloatLiteral: 108 start = set[0].FloatLiteral().Val 109 default: 110 return runtime.NewRunError(ctx, fmt.Sprintf("range value `%v' is not expected", set), 111 funcExpr.Param[1].StartPos()) 112 } 113 114 switch set[1].NodeType { //nolint:exhaustive 115 case ast.TypeIntegerLiteral: 116 end = float64(set[1].IntegerLiteral().Val) 117 case ast.TypeFloatLiteral: 118 end = set[1].FloatLiteral().Val 119 default: 120 return runtime.NewRunError(ctx, fmt.Sprintf("range value `%v' is not expected", set), 121 funcExpr.Param[1].StartPos()) 122 } 123 124 if start > end { 125 return runtime.NewRunError(ctx, fmt.Sprintf("range value start %v must le end %v", start, end), 126 funcExpr.Param[1].StartPos()) 127 } 128 129 if GroupHandle(cont.Value, start, end) { 130 value := funcExpr.Param[2] 131 132 var val any 133 var dtype ast.DType 134 135 switch value.NodeType { //nolint:exhaustive 136 case ast.TypeFloatLiteral: 137 val = value.FloatLiteral().Val 138 dtype = ast.Float 139 case ast.TypeIntegerLiteral: 140 val = value.IntegerLiteral().Val 141 dtype = ast.Int 142 case ast.TypeStringLiteral: 143 val = value.StringLiteral().Val 144 dtype = ast.String 145 case ast.TypeBoolLiteral: 146 val = value.BoolLiteral().Val 147 dtype = ast.String 148 default: 149 l.Debugf("unknown group elements: %s", value.NodeType) 150 return runtime.NewRunError(ctx, "unsupported group type", 151 funcExpr.NamePos) 152 } 153 154 if len(funcExpr.Param) == 4 { 155 if k, err := getKeyName(funcExpr.Param[3]); err == nil { 156 key = k 157 } else { 158 return runtime.NewRunError(ctx, err.Error(), funcExpr.Param[4].StartPos()) 159 } 160 } 161 _ = addKey2PtWithVal(ctx.InData(), key, val, dtype, ptinput.KindPtDefault) 162 } 163 164 return nil 165 }