github.com/GuanceCloud/cliutils@v1.1.21/pipeline/ptinput/funcs/fn_rename.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/platypus/pkg/ast"
    12  	"github.com/GuanceCloud/platypus/pkg/engine/runtime"
    13  	"github.com/GuanceCloud/platypus/pkg/errchain"
    14  )
    15  
    16  func RenameChecking(ctx *runtime.Task, funcExpr *ast.CallExpr) *errchain.PlError {
    17  	if len(funcExpr.Param) != 2 {
    18  		return runtime.NewRunError(ctx, fmt.Sprintf(
    19  			"func %s expected 2 args", funcExpr.Name), funcExpr.NamePos)
    20  	}
    21  
    22  	if _, err := getKeyName(funcExpr.Param[0]); err != nil {
    23  		return runtime.NewRunError(ctx, err.Error(), funcExpr.Param[0].StartPos())
    24  	}
    25  
    26  	switch funcExpr.Param[1].NodeType { //nolint:exhaustive
    27  	case ast.TypeAttrExpr, ast.TypeIdentifier:
    28  	default:
    29  		return runtime.NewRunError(ctx, fmt.Sprintf(
    30  			"param key expect Identifier or AttrExpr, got `%s'",
    31  			funcExpr.Param[1].NodeType), funcExpr.Param[1].StartPos())
    32  	}
    33  	return nil
    34  }
    35  
    36  func Rename(ctx *runtime.Task, funcExpr *ast.CallExpr) *errchain.PlError {
    37  	if len(funcExpr.Param) != 2 {
    38  		return runtime.NewRunError(ctx, fmt.Sprintf(
    39  			"func %s expected 2 args", funcExpr.Name), funcExpr.NamePos)
    40  	}
    41  
    42  	to, err := getKeyName(funcExpr.Param[0])
    43  	if err != nil {
    44  		return runtime.NewRunError(ctx, err.Error(), funcExpr.Param[0].StartPos())
    45  	}
    46  
    47  	from, err := getKeyName(funcExpr.Param[1])
    48  	if err != nil {
    49  		return runtime.NewRunError(ctx, err.Error(), funcExpr.Param[1].StartPos())
    50  	}
    51  
    52  	_ = renamePtKey(ctx.InData(), to, from)
    53  
    54  	return nil
    55  }