Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ottl/pkg] Remove the ottlarg struct tag for the arguments and rely on field ordering #25705

Merged
merged 2 commits into from
Sep 13, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Removing the check for the ottlarg struct tag
  • Loading branch information
rnishtala-sumo committed Aug 22, 2023
commit fae54c3cb58b1c4a563ecd87534a3249bf61a31d
10 changes: 1 addition & 9 deletions pkg/ottl/expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
17 changes: 0 additions & 17 deletions pkg/ottl/expression_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -688,11 +688,6 @@ func Test_FunctionGetter(t *testing.T) {
&multipleArgsArguments{},
functionWithStringGetter,
),
createFactory[any](
"no_struct_tag",
&noStructTagFunctionArguments{},
functionWithStringGetter,
),
NewFactory(
"cannot_create_function",
&stringGetterArguments{},
Expand Down Expand Up @@ -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{}]{
Expand Down
24 changes: 2 additions & 22 deletions pkg/ottl/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"errors"
"fmt"
"reflect"
"strconv"
"strings"
)

Expand Down Expand Up @@ -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))
Expand All @@ -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
Expand Down
100 changes: 0 additions & 100 deletions pkg/ottl/functions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,31 +63,6 @@ func Test_NewFunctionCall_invalid(t *testing.T) {
errorFunctionArguments{},
functionThatHasAnError,
),
createFactory(
"no_struct_tag",
&noStructTagFunctionArguments{},
functionThatHasAnError,
),
createFactory(
"wrong_struct_tag",
&wrongTagFunctionArguments{},
functionThatHasAnError,
),
createFactory(
"bad_struct_tag",
&badStructTagFunctionArguments{},
functionThatHasAnError,
),
createFactory(
"negative_struct_tag",
&negativeStructTagFunctionArguments{},
functionThatHasAnError,
),
createFactory(
"out_of_bounds_struct_tag",
&outOfBoundsStructTagFunctionArguments{},
functionThatHasAnError,
),
createFactory(
"testing_unknown_function",
&functionGetterArguments{},
Expand Down Expand Up @@ -349,61 +324,6 @@ func Test_NewFunctionCall_invalid(t *testing.T) {
Function: "non_pointer",
},
},
{
name: "no struct tags",
inv: editor{
Function: "no_struct_tag",
Arguments: []value{
{
String: ottltest.Strp("str"),
},
},
},
},
{
name: "using the wrong struct tag",
inv: editor{
Function: "wrong_struct_tag",
Arguments: []value{
{
String: ottltest.Strp("str"),
},
},
},
},
{
name: "non-integer struct tags",
inv: editor{
Function: "bad_struct_tag",
Arguments: []value{
{
String: ottltest.Strp("str"),
},
},
},
},
{
name: "struct tag index too low",
inv: editor{
Function: "negative_struct_tag",
Arguments: []value{
{
String: ottltest.Strp("str"),
},
},
},
},
{
name: "struct tag index too high",
inv: editor{
Function: "out_of_bounds_struct_tag",
Arguments: []value{
{
String: ottltest.Strp("str"),
},
},
},
},
}

for _, tt := range tests {
Expand Down Expand Up @@ -1516,26 +1436,6 @@ func functionWithEnum(Enum) (ExprFunc[interface{}], error) {
}, nil
}

type noStructTagFunctionArguments struct {
StringArg string
}

type badStructTagFunctionArguments struct {
StringArg string `ottlarg:"a"`
}

type negativeStructTagFunctionArguments struct {
StringArg string `ottlarg:"-1"`
}

type outOfBoundsStructTagFunctionArguments struct {
StringArg string `ottlarg:"1"`
}

type wrongTagFunctionArguments struct {
StringArg string `argument:"1"`
}

func createFactory[A any](name string, args A, fn any) Factory[any] {
createFunction := func(fCtx FunctionContext, oArgs Arguments) (ExprFunc[any], error) {
fArgs, ok := oArgs.(A)
Expand Down
4 changes: 2 additions & 2 deletions pkg/ottl/ottlfuncs/func_substring.go
Original file line number Diff line number Diff line change
Expand Up @@ -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] {
Expand Down