From 4e58bff87d0b875025865f3d40e130dc5da2332a Mon Sep 17 00:00:00 2001 From: Bogdan Drutu Date: Fri, 30 Sep 2022 08:49:15 -0700 Subject: [PATCH] [chore] pkg/ottl check for condition first Signed-off-by: Bogdan Drutu --- pkg/ottl/functions.go | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/pkg/ottl/functions.go b/pkg/ottl/functions.go index e6a641986504..a84c5a94f753 100644 --- a/pkg/ottl/functions.go +++ b/pkg/ottl/functions.go @@ -28,23 +28,24 @@ type EnumParser func(*EnumSymbol) (*Enum, error) type Enum int64 func (p *Parser[K]) newFunctionCall(inv invocation) (ExprFunc[K], error) { - if f, ok := p.functions[inv.Function]; ok { - args, err := p.buildArgs(inv, reflect.TypeOf(f)) - if err != nil { - return nil, err - } - - returnVals := reflect.ValueOf(f).Call(args) + f, ok := p.functions[inv.Function] + if !ok { + return nil, fmt.Errorf("undefined function %v", inv.Function) + } + args, err := p.buildArgs(inv, reflect.TypeOf(f)) + if err != nil { + return nil, err + } - if returnVals[1].IsNil() { - err = nil - } else { - err = returnVals[1].Interface().(error) - } + returnVals := reflect.ValueOf(f).Call(args) - return returnVals[0].Interface().(ExprFunc[K]), err + if returnVals[1].IsNil() { + err = nil + } else { + err = returnVals[1].Interface().(error) } - return nil, fmt.Errorf("undefined function %v", inv.Function) + + return returnVals[0].Interface().(ExprFunc[K]), err } func (p *Parser[K]) buildArgs(inv invocation, fType reflect.Type) ([]reflect.Value, error) {