github.com/GuanceCloud/cliutils@v1.1.21/pipeline/ptinput/funcs/fn_addkey.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 AddkeyChecking(ctx *runtime.Task, funcExpr *ast.CallExpr) *errchain.PlError {
    18  	if len(funcExpr.Param) > 2 || len(funcExpr.Param) < 1 {
    19  		return runtime.NewRunError(ctx, fmt.Sprintf(
    20  			"func %s expected 1 or 2 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  	return nil
    28  }
    29  
    30  func AddKey(ctx *runtime.Task, funcExpr *ast.CallExpr) *errchain.PlError {
    31  	if len(funcExpr.Param) != 2 && len(funcExpr.Param) != 1 {
    32  		return runtime.NewRunError(ctx, fmt.Sprintf(
    33  			"func %s expected 1 or 2 args", funcExpr.Name), funcExpr.NamePos)
    34  	}
    35  
    36  	key, err := getKeyName(funcExpr.Param[0])
    37  	if err != nil {
    38  		return runtime.NewRunError(ctx, err.Error(), funcExpr.Param[0].StartPos())
    39  	}
    40  
    41  	if len(funcExpr.Param) == 1 {
    42  		v, err := ctx.GetKey(key)
    43  		if err != nil {
    44  			l.Debug(err)
    45  			return nil
    46  		}
    47  		_ = addKey2PtWithVal(ctx.InData(), key, v.Value, v.DType, ptinput.KindPtDefault)
    48  		return nil
    49  	}
    50  
    51  	var val any
    52  	var dtype ast.DType
    53  
    54  	val, dtype, errRun := runtime.RunStmt(ctx, funcExpr.Param[1])
    55  	if errRun != nil {
    56  		return errRun.ChainAppend(ctx.Name(), funcExpr.NamePos)
    57  	}
    58  	_ = addKey2PtWithVal(ctx.InData(), key, val, dtype, ptinput.KindPtDefault)
    59  
    60  	return nil
    61  }