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