github.com/GuanceCloud/cliutils@v1.1.21/pipeline/ptinput/funcs/fn_set_measurement.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 SetMeasurementChecking(ctx *runtime.Task, funcExpr *ast.CallExpr) *errchain.PlError {
    17  	if len(funcExpr.Param) != 2 && len(funcExpr.Param) != 1 {
    18  		return runtime.NewRunError(ctx, fmt.Sprintf(
    19  			"func `%s' expected 1 or 2 args", funcExpr.Name), funcExpr.NamePos)
    20  	}
    21  	if _, err := getKeyName(funcExpr.Param[0]); err != nil {
    22  		return runtime.NewRunError(ctx, err.Error(), funcExpr.Param[0].StartPos())
    23  	}
    24  	if len(funcExpr.Param) == 2 {
    25  		switch funcExpr.Param[1].NodeType { //nolint:exhaustive
    26  		case ast.TypeBoolLiteral:
    27  		default:
    28  			return runtime.NewRunError(ctx, fmt.Sprintf(
    29  				"param type expect BoolLiteral, got `%s'",
    30  				funcExpr.Param[1].NodeType), funcExpr.Param[1].StartPos())
    31  		}
    32  	}
    33  
    34  	return nil
    35  }
    36  
    37  func SetMeasurement(ctx *runtime.Task, funcExpr *ast.CallExpr) *errchain.PlError {
    38  	if len(funcExpr.Param) != 2 && len(funcExpr.Param) != 1 {
    39  		return runtime.NewRunError(ctx, fmt.Sprintf(
    40  			"func `%s' expected 1 or 2 args", funcExpr.Name), funcExpr.NamePos)
    41  	}
    42  
    43  	val, dtype, err := runtime.RunStmt(ctx, funcExpr.Param[0])
    44  	if err != nil {
    45  		return nil
    46  	}
    47  	if dtype == ast.String {
    48  		if val, ok := val.(string); ok {
    49  			_ = setPtName(ctx.InData(), val)
    50  		}
    51  	}
    52  
    53  	if len(funcExpr.Param) == 2 &&
    54  		funcExpr.Param[1].NodeType == ast.TypeBoolLiteral {
    55  		if funcExpr.Param[1].BoolLiteral().Val {
    56  			switch funcExpr.Param[0].NodeType { //nolint:exhaustive
    57  			case ast.TypeIdentifier, ast.TypeAttrExpr:
    58  				if key, err := getKeyName(funcExpr.Param[0]); err == nil {
    59  					deletePtKey(ctx.InData(), key)
    60  				}
    61  			}
    62  		}
    63  	}
    64  
    65  	return nil
    66  }