github.com/GuanceCloud/cliutils@v1.1.21/pipeline/ptinput/funcs/fn_lowercase.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  	"strings"
    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  )
    17  
    18  func LowercaseChecking(ctx *runtime.Task, funcExpr *ast.CallExpr) *errchain.PlError {
    19  	if len(funcExpr.Param) != 1 {
    20  		return runtime.NewRunError(ctx, fmt.Sprintf(
    21  			"func %s expected 1 args", funcExpr.Name), 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  	return nil
    29  }
    30  
    31  func Lowercase(ctx *runtime.Task, funcExpr *ast.CallExpr) *errchain.PlError {
    32  	if len(funcExpr.Param) != 1 {
    33  		return runtime.NewRunError(ctx, fmt.Sprintf(
    34  			"func %s expected 1 args", funcExpr.Name), funcExpr.NamePos)
    35  	}
    36  
    37  	key, err := getKeyName(funcExpr.Param[0])
    38  	if err != nil {
    39  		return runtime.NewRunError(ctx, err.Error(), funcExpr.Param[0].StartPos())
    40  	}
    41  
    42  	cont, err := ctx.GetKeyConv2Str(key)
    43  	if err != nil {
    44  		l.Debug(err)
    45  		return nil
    46  	}
    47  
    48  	v := strings.ToLower(cont)
    49  	_ = addKey2PtWithVal(ctx.InData(), key, v, ast.String, ptinput.KindPtDefault)
    50  
    51  	return nil
    52  }