Skip to content

Commit

Permalink
Removing the check for the ottlarg struct tag
Browse files Browse the repository at this point in the history
  • Loading branch information
rnishtala-sumo committed Aug 21, 2023
1 parent c956719 commit 3ebc818
Show file tree
Hide file tree
Showing 5 changed files with 5 additions and 130 deletions.
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
80 changes: 0 additions & 80 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
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

0 comments on commit 3ebc818

Please sign in to comment.