github.com/GuanceCloud/cliutils@v1.1.21/pipeline/ptinput/funcs/funcs_query_refer_table.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 "github.com/spf13/cast" 16 ) 17 18 func QueryReferTableChecking(ctx *runtime.Task, funcExpr *ast.CallExpr) *errchain.PlError { 19 err := normalizeFuncArgsDeprecated(funcExpr, []string{"table_name", "key", "value"}, 3) 20 if err != nil { 21 return runtime.NewRunError(ctx, err.Error(), funcExpr.NamePos) 22 } 23 24 if _, err := getKeyName(funcExpr.Param[0]); err != nil { 25 return runtime.NewRunError(ctx, err.Error(), funcExpr.Param[0].StartPos()) 26 } 27 28 switch funcExpr.Param[1].NodeType { //nolint:exhaustive 29 case ast.TypeStringLiteral, ast.TypeIdentifier, ast.TypeAttrExpr: 30 default: 31 return runtime.NewRunError(ctx, fmt.Sprintf("param key expects StringLiteral, Identifier or AttrExpr, got %s", 32 funcExpr.Param[1]), funcExpr.Param[1].StartPos()) 33 } 34 35 switch funcExpr.Param[2].NodeType { //nolint:exhaustive 36 case ast.TypeIdentifier, ast.TypeAttrExpr, 37 ast.TypeStringLiteral, ast.TypeBoolLiteral, 38 ast.TypeFloatLiteral, ast.TypeIntegerLiteral: 39 default: 40 return runtime.NewRunError(ctx, fmt.Sprintf( 41 "param key expects StringLiteral, NumberLiteral, BoolLiteral, AttrExpr or Identifier, got %s", 42 funcExpr.Param[2]), funcExpr.Param[2].StartPos()) 43 } 44 45 // TODO: pos param 4: selected([]string) 46 47 // if len(funcExpr.Param) == 4 { 48 // switch v := funcExpr.Param[3].(type) { 49 // case parser.FuncArgList: 50 // for _, item := range v { 51 // switch item.(type) { 52 // case *parser.StringLiteral: 53 // default: 54 // return fmt.Errorf("param key expects StringLiteral, got %s", 55 // reflect.TypeOf(funcExpr.Param[2]).String()) 56 // } 57 // } 58 // case nil: 59 // default: 60 // return fmt.Errorf("param key expects FuncArgList, got %s", 61 // reflect.TypeOf(funcExpr.Param[3]).String()) 62 // } 63 // } 64 65 return nil 66 } 67 68 func QueryReferTable(ctx *runtime.Task, funcExpr *ast.CallExpr) *errchain.PlError { 69 var tableName string 70 71 tname, dtype, err := runtime.RunStmt(ctx, funcExpr.Param[0]) 72 if err != nil { 73 return err 74 } 75 if dtype != ast.String { 76 return runtime.NewRunError(ctx, "param expect string", funcExpr.Param[0].StartPos()) 77 } 78 79 tableName = tname.(string) 80 81 var colName string 82 switch funcExpr.Param[1].NodeType { //nolint:exhaustive 83 case ast.TypeStringLiteral: 84 colName = funcExpr.Param[1].StringLiteral().Val 85 case ast.TypeIdentifier, ast.TypeAttrExpr: 86 key, _ := getKeyName(funcExpr.Param[1]) 87 val, err := ctx.GetKey(key) 88 if err != nil { 89 l.Debug(err) 90 return runtime.NewRunError(ctx, err.Error(), funcExpr.Param[1].StartPos()) 91 } 92 switch val.DType { //nolint:exhaustive 93 case ast.String: 94 colName = cast.ToString(val.Value) 95 default: 96 err := fmt.Errorf("unsupported column name value type %s", val.DType) 97 l.Debug(err) 98 return runtime.NewRunError(ctx, err.Error(), funcExpr.Param[1].StartPos()) 99 } 100 default: 101 return nil 102 } 103 104 var colValue any 105 switch funcExpr.Param[2].NodeType { //nolint:exhaustive 106 case ast.TypeIdentifier, ast.TypeAttrExpr: 107 key, _ := getKeyName(funcExpr.Param[2]) 108 if val, err := ctx.GetKey(key); err != nil { 109 l.Debugf("key %s not found: %v", key, err) 110 return nil 111 } else { 112 colValue = val.Value 113 } 114 case ast.TypeStringLiteral: 115 colValue = funcExpr.Param[2].StringLiteral().Val 116 case ast.TypeIntegerLiteral: 117 colValue = funcExpr.Param[2].IntegerLiteral().Val 118 case ast.TypeFloatLiteral: 119 colValue = funcExpr.Param[2].FloatLiteral().Val 120 case ast.TypeBoolLiteral: 121 colValue = funcExpr.Param[2].BoolLiteral().Val 122 } 123 124 // TODO: pos param 4: selected([]string) 125 126 // var selectd []string 127 // switch v := funcExpr.Param[3].(type) { 128 // case parser.FuncArgList: 129 // for _, item := range v { 130 // if item, ok := item.(*parser.StringLiteral); ok { 131 // selectd = append(selectd, item.Val) 132 // } 133 // } 134 // case nil: 135 // } 136 137 pt, errR := getPoint(ctx.InData()) 138 if errR != nil { 139 return nil 140 } 141 refT := pt.GetPlReferTables() 142 if refT == nil { 143 return nil 144 } 145 146 if vMap, ok := refT.Query(tableName, 147 []string{colName}, []any{colValue}, nil); ok { 148 for k, v := range vMap { 149 var dtype ast.DType 150 switch v.(type) { 151 case string: 152 dtype = ast.String 153 case bool: 154 dtype = ast.Bool 155 case int64: 156 dtype = ast.Int 157 case float64: 158 dtype = ast.Float 159 default: 160 return nil 161 } 162 _ = addKey2PtWithVal(ctx.InData(), k, v, dtype, ptinput.KindPtDefault) 163 } 164 } 165 return nil 166 }