github.com/GuanceCloud/cliutils@v1.1.21/pipeline/ptinput/funcs/fn_grok.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/grok" 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 GrokChecking(ctx *runtime.Task, funcExpr *ast.CallExpr) *errchain.PlError { 19 if funcExpr.Grok != nil { 20 return nil 21 } 22 23 if len(funcExpr.Param) < 2 || len(funcExpr.Param) > 3 { 24 return runtime.NewRunError(ctx, fmt.Sprintf( 25 "func %s expected 2 or 3 args", funcExpr.Name), funcExpr.NamePos) 26 } 27 28 if len(funcExpr.Param) == 3 { 29 switch funcExpr.Param[2].NodeType { //nolint:exhaustive 30 case ast.TypeBoolLiteral: 31 default: 32 return runtime.NewRunError(ctx, fmt.Sprintf( 33 "param key expect BoolLiteral, got `%s'", 34 funcExpr.Param[2].NodeType), funcExpr.Param[2].StartPos()) 35 } 36 } 37 38 if _, err := getKeyName(funcExpr.Param[0]); err != nil { 39 return runtime.NewRunError(ctx, err.Error(), funcExpr.Param[0].StartPos()) 40 } 41 42 var pattern string 43 switch funcExpr.Param[1].NodeType { //nolint:exhaustive 44 case ast.TypeStringLiteral: 45 pattern = funcExpr.Param[1].StringLiteral().Val 46 default: 47 return runtime.NewRunError(ctx, fmt.Sprintf( 48 "expect StringLiteral, got %s", 49 funcExpr.Param[1].NodeType), funcExpr.Param[1].StartPos()) 50 } 51 52 gRe, err := grok.CompilePattern(pattern, ctx) 53 if err != nil { 54 return runtime.NewRunError(ctx, err.Error(), funcExpr.NamePos) 55 } 56 funcExpr.Grok = gRe 57 return nil 58 } 59 60 func Grok(ctx *runtime.Task, funcExpr *ast.CallExpr) *errchain.PlError { 61 grokRe := funcExpr.Grok 62 if grokRe == nil { 63 ctx.Regs.ReturnAppend(false, ast.Bool) 64 return runtime.NewRunError(ctx, "no grok obj", funcExpr.NamePos) 65 } 66 var err error 67 68 key, err := getKeyName(funcExpr.Param[0]) 69 if err != nil { 70 ctx.Regs.ReturnAppend(false, ast.Bool) 71 return runtime.NewRunError(ctx, err.Error(), funcExpr.Param[0].StartPos()) 72 } 73 74 val, err := ctx.GetKeyConv2Str(key) 75 if err != nil { 76 ctx.Regs.ReturnAppend(false, ast.Bool) 77 return nil 78 } 79 80 trimSpace := true 81 if len(funcExpr.Param) == 3 { 82 switch funcExpr.Param[2].NodeType { //nolint:exhaustive 83 case ast.TypeBoolLiteral: 84 trimSpace = funcExpr.Param[2].BoolLiteral().Val 85 default: 86 ctx.Regs.ReturnAppend(false, ast.Bool) 87 return runtime.NewRunError(ctx, fmt.Sprintf("param key expect BoolLiteral, got `%s'", 88 funcExpr.Param[2].NodeType), funcExpr.Param[2].StartPos()) 89 } 90 } 91 92 if grokRe.WithTypeInfo() { 93 result, err := grokRe.RunWithTypeInfo(val, trimSpace) 94 if err != nil { 95 ctx.Regs.ReturnAppend(false, ast.Bool) 96 return nil 97 } 98 for i, name := range grokRe.MatchNames() { 99 var dtype ast.DType 100 if result[i] == nil { 101 dtype = ast.Nil 102 } else { 103 switch result[i].(type) { 104 case int64: 105 dtype = ast.Int 106 case float64: 107 dtype = ast.Float 108 case string: 109 dtype = ast.String 110 case bool: 111 dtype = ast.Bool 112 default: 113 continue 114 } 115 } 116 if ok := addKey2PtWithVal(ctx.InData(), name, result[i], dtype, ptinput.KindPtDefault); !ok { 117 ctx.Regs.ReturnAppend(false, ast.Bool) 118 return nil 119 } 120 } 121 } else { 122 result, err := grokRe.Run(val, trimSpace) 123 if err != nil { 124 ctx.Regs.ReturnAppend(false, ast.Bool) 125 return nil 126 } 127 for i, name := range grokRe.MatchNames() { 128 if ok := addKey2PtWithVal(ctx.InData(), name, result[i], ast.String, ptinput.KindPtDefault); !ok { 129 ctx.Regs.ReturnAppend(false, ast.Bool) 130 return nil 131 } 132 } 133 } 134 135 ctx.Regs.ReturnAppend(true, ast.Bool) 136 return nil 137 }