github.com/GuanceCloud/cliutils@v1.1.21/pipeline/ptinput/funcs/fn_default_time.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 DefaultTimeChecking(ctx *runtime.Task, funcExpr *ast.CallExpr) *errchain.PlError {
    18  	if len(funcExpr.Param) < 1 {
    19  		return runtime.NewRunError(ctx, fmt.Sprintf(
    20  			"func %s Expect at least one arg", 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  	if len(funcExpr.Param) > 1 {
    28  		switch funcExpr.Param[1].NodeType { //nolint:exhaustive
    29  		case ast.TypeStringLiteral:
    30  		default:
    31  			return runtime.NewRunError(ctx, fmt.Sprintf("param key expect StringLiteral, got %s",
    32  				funcExpr.Param[1].NodeType), funcExpr.Param[1].StartPos())
    33  		}
    34  	}
    35  
    36  	return nil
    37  }
    38  
    39  func DefaultTime(ctx *runtime.Task, funcExpr *ast.CallExpr) *errchain.PlError {
    40  	if len(funcExpr.Param) < 1 {
    41  		return runtime.NewRunError(ctx, fmt.Sprintf(
    42  			"func %s expect at least one arg", funcExpr.Name), funcExpr.NamePos)
    43  	}
    44  
    45  	key, err := getKeyName(funcExpr.Param[0])
    46  	if err != nil {
    47  		return runtime.NewRunError(ctx, err.Error(), funcExpr.Param[0].StartPos())
    48  	}
    49  
    50  	cont, err := ctx.GetKeyConv2Str(key)
    51  	if err != nil {
    52  		l.Debugf("key `%v' not exist, ignored", key)
    53  		return nil //nolint:nilerr
    54  	}
    55  
    56  	var tz string
    57  	if len(funcExpr.Param) > 1 {
    58  		switch funcExpr.Param[1].NodeType { //nolint:exhaustive
    59  		case ast.TypeStringLiteral:
    60  			tz = funcExpr.Param[1].StringLiteral().Val
    61  		default:
    62  			err = fmt.Errorf("param key expect StringLiteral, got %s",
    63  				funcExpr.Param[1].NodeType)
    64  			usePointTime(ctx, key, err)
    65  			return nil
    66  		}
    67  	}
    68  
    69  	if nanots, err := TimestampHandle(cont, tz); err != nil {
    70  		usePointTime(ctx, key, err)
    71  	} else {
    72  		addKey2PtWithVal(ctx.InData(), key, nanots, ast.Int, ptinput.KindPtDefault)
    73  	}
    74  
    75  	return nil
    76  }
    77  
    78  func usePointTime(ctx *runtime.Task, key string, err error) {
    79  	_ = addKey2PtWithVal(ctx.InData(), runtime.PlRunInfoField, fmt.Sprintf("time convert failed: %v", err),
    80  		ast.String, ptinput.KindPtDefault)
    81  
    82  	_ = addKey2PtWithVal(ctx.InData(), key, pointTime(ctx.InData()), ast.Int, ptinput.KindPtDefault)
    83  }