From 916cad6803f8be9259b736c231d2c66852bf7694 Mon Sep 17 00:00:00 2001 From: Raj Nishtala Date: Mon, 21 Aug 2023 11:48:48 -0400 Subject: [PATCH] Removing the check for the ottlarg struct tag --- pkg/ottl/expression.go | 10 +--------- pkg/ottl/expression_test.go | 17 ----------------- pkg/ottl/functions.go | 24 ++---------------------- pkg/ottl/ottlfuncs/func_substring.go | 4 ++-- 4 files changed, 5 insertions(+), 50 deletions(-) diff --git a/pkg/ottl/expression.go b/pkg/ottl/expression.go index 82e9b0407ebcb..bd885afd22566 100644 --- a/pkg/ottl/expression.go +++ b/pkg/ottl/expression.go @@ -279,15 +279,7 @@ func (g StandardFunctionGetter[K]) Get(args Arguments) (Expr[K], error) { } for i := 0; i < fArgsVal.NumField(); i++ { field := argsVal.Field(i) - argIndex, err := getArgumentIndex(i, argsVal) - if err != nil { - return Expr[K]{}, err - } - fArgIndex, err := getArgumentIndex(argIndex, fArgsVal) - if err != nil { - return Expr[K]{}, err - } - fArgsVal.Field(fArgIndex).Set(field) + fArgsVal.Field(i).Set(field) } fn, err := g.fact.CreateFunction(g.fCtx, fArgs) if err != nil { diff --git a/pkg/ottl/expression_test.go b/pkg/ottl/expression_test.go index b9d2451e81aa2..bdb36c07a1278 100644 --- a/pkg/ottl/expression_test.go +++ b/pkg/ottl/expression_test.go @@ -688,11 +688,6 @@ func Test_FunctionGetter(t *testing.T) { &multipleArgsArguments{}, functionWithStringGetter, ), - createFactory[any]( - "no_struct_tag", - &noStructTagFunctionArguments{}, - functionWithStringGetter, - ), NewFactory( "cannot_create_function", &stringGetterArguments{}, @@ -751,18 +746,6 @@ func Test_FunctionGetter(t *testing.T) { valid: false, expectedErrorMsg: "incorrect number of arguments. Expected: 4 Received: 1", }, - { - name: "Invalid Arguments struct tag", - getter: StandardStringGetter[interface{}]{ - Getter: func(ctx context.Context, tCtx interface{}) (interface{}, error) { - return nil, nil - }, - }, - function: StandardFunctionGetter[any]{fCtx: FunctionContext{Set: componenttest.NewNopTelemetrySettings()}, fact: functions["no_struct_tag"]}, - want: "anything", - valid: false, - expectedErrorMsg: "no `ottlarg` struct tag on Arguments field \"StringArg\"", - }, { name: "Cannot create function", getter: StandardStringGetter[interface{}]{ diff --git a/pkg/ottl/functions.go b/pkg/ottl/functions.go index 967f804533637..730410e169dbf 100644 --- a/pkg/ottl/functions.go +++ b/pkg/ottl/functions.go @@ -7,7 +7,6 @@ import ( "errors" "fmt" "reflect" - "strconv" "strings" ) @@ -47,22 +46,6 @@ func (p *Parser[K]) newFunctionCall(ed editor) (Expr[K], error) { return Expr[K]{exprFunc: fn}, err } -func getArgumentIndex(index int, args reflect.Value) (int, error) { - argsType := args.Type() - fieldTag, ok := argsType.Field(index).Tag.Lookup("ottlarg") - if !ok { - return 0, fmt.Errorf("no `ottlarg` struct tag on Arguments field %q", argsType.Field(index).Name) - } - argNum, err := strconv.Atoi(fieldTag) - if err != nil { - return 0, fmt.Errorf("ottlarg struct tag on field %q is not a valid integer: %w", argsType.Field(index).Name, err) - } - if argNum < 0 || argNum >= args.NumField() { - return 0, fmt.Errorf("ottlarg struct tag on field %q has value %d, but must be between 0 and %d", argsType.Field(index).Name, argNum, args.NumField()) - } - return argNum, nil -} - func (p *Parser[K]) buildArgs(ed editor, argsVal reflect.Value) error { if len(ed.Arguments) != argsVal.NumField() { return fmt.Errorf("incorrect number of arguments. Expected: %d Received: %d", argsVal.NumField(), len(ed.Arguments)) @@ -71,12 +54,9 @@ func (p *Parser[K]) buildArgs(ed editor, argsVal reflect.Value) error { for i := 0; i < argsVal.NumField(); i++ { field := argsVal.Field(i) fieldType := field.Type() - argNum, err := getArgumentIndex(i, argsVal) - if err != nil { - return err - } - argVal := ed.Arguments[argNum] + argVal := ed.Arguments[i] var val any + var err error switch { case strings.HasPrefix(fieldType.Name(), "FunctionGetter"): var name string diff --git a/pkg/ottl/ottlfuncs/func_substring.go b/pkg/ottl/ottlfuncs/func_substring.go index ef35fa9896fac..974a110b8716c 100644 --- a/pkg/ottl/ottlfuncs/func_substring.go +++ b/pkg/ottl/ottlfuncs/func_substring.go @@ -12,8 +12,8 @@ import ( type SubstringArguments[K any] struct { Target ottl.StringGetter[K] - Start int64 - Length int64 + Start ottl.IntGetter[K] + Length ottl.IntGetter[K] } func NewSubstringFactory[K any]() ottl.Factory[K] {