github.com/GuanceCloud/cliutils@v1.1.21/pipeline/ptinput/funcs/fn_gjson.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/tidwall/gjson"
    16  )
    17  
    18  func GJSONChecking(ctx *runtime.Task, funcExpr *ast.CallExpr) *errchain.PlError {
    19  	if err := normalizeFuncArgsDeprecated(funcExpr, []string{
    20  		"input", "json_path", "key_name",
    21  	}, 2); 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.TypeIdentifier, ast.TypeStringLiteral:
    31  	default:
    32  		return runtime.NewRunError(ctx, fmt.Sprintf("expect AttrExpr, IndexExpr or Identifier, got %s",
    33  			funcExpr.Param[1].NodeType), funcExpr.Param[1].StartPos())
    34  	}
    35  
    36  	if funcExpr.Param[2] != nil {
    37  		switch funcExpr.Param[2].NodeType { //nolint:exhaustive
    38  		case ast.TypeIdentifier, ast.TypeStringLiteral:
    39  		default:
    40  			return runtime.NewRunError(ctx, fmt.Sprintf("expect AttrExpr or Identifier, got %s",
    41  				funcExpr.Param[2].NodeType), funcExpr.Param[2].StartPos())
    42  		}
    43  	}
    44  
    45  	return nil
    46  }
    47  
    48  func GJSON(ctx *runtime.Task, funcExpr *ast.CallExpr) *errchain.PlError {
    49  	srcKey, err := getKeyName(funcExpr.Param[0])
    50  	if err != nil {
    51  		return runtime.NewRunError(ctx, err.Error(), funcExpr.Param[0].StartPos())
    52  	}
    53  
    54  	jpath, dtype, errP := runtime.RunStmt(ctx, funcExpr.Param[1])
    55  	if errP != nil {
    56  		return errP
    57  	}
    58  	if dtype != ast.String {
    59  		return runtime.NewRunError(ctx, "param data type expect string",
    60  			funcExpr.Param[1].StartPos())
    61  	}
    62  
    63  	targetKey := jpath.(string)
    64  
    65  	if funcExpr.Param[2] != nil {
    66  		tk, dtype, errP := runtime.RunStmt(ctx, funcExpr.Param[2])
    67  		if errP != nil {
    68  			return errP
    69  		}
    70  		if dtype != ast.String {
    71  			return runtime.NewRunError(ctx, "param data type expect string",
    72  				funcExpr.Param[2].StartPos())
    73  		}
    74  
    75  		targetKey = tk.(string)
    76  	}
    77  
    78  	cont, err := ctx.GetKeyConv2Str(srcKey)
    79  	if err != nil {
    80  		l.Debug(err)
    81  		return nil
    82  	}
    83  
    84  	res := gjson.Get(cont, jpath.(string))
    85  	rType := res.Type
    86  
    87  	var v any
    88  	switch rType {
    89  	case gjson.Number:
    90  		v = res.Float()
    91  		dtype = ast.Float
    92  	case gjson.True, gjson.False:
    93  		v = res.Bool()
    94  		dtype = ast.Bool
    95  	case gjson.String, gjson.JSON:
    96  		v = res.String()
    97  		dtype = ast.String
    98  	default:
    99  		return nil
   100  	}
   101  
   102  	_ = addKey2PtWithVal(ctx.InData(), targetKey, v, dtype, ptinput.KindPtDefault)
   103  	return nil
   104  }