Skip to content

Commit fae54c3

Browse files
Removing the check for the ottlarg struct tag
1 parent c956719 commit fae54c3

File tree

5 files changed

+5
-150
lines changed

5 files changed

+5
-150
lines changed

pkg/ottl/expression.go

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -279,15 +279,7 @@ func (g StandardFunctionGetter[K]) Get(args Arguments) (Expr[K], error) {
279279
}
280280
for i := 0; i < fArgsVal.NumField(); i++ {
281281
field := argsVal.Field(i)
282-
argIndex, err := getArgumentIndex(i, argsVal)
283-
if err != nil {
284-
return Expr[K]{}, err
285-
}
286-
fArgIndex, err := getArgumentIndex(argIndex, fArgsVal)
287-
if err != nil {
288-
return Expr[K]{}, err
289-
}
290-
fArgsVal.Field(fArgIndex).Set(field)
282+
fArgsVal.Field(i).Set(field)
291283
}
292284
fn, err := g.fact.CreateFunction(g.fCtx, fArgs)
293285
if err != nil {

pkg/ottl/expression_test.go

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -688,11 +688,6 @@ func Test_FunctionGetter(t *testing.T) {
688688
&multipleArgsArguments{},
689689
functionWithStringGetter,
690690
),
691-
createFactory[any](
692-
"no_struct_tag",
693-
&noStructTagFunctionArguments{},
694-
functionWithStringGetter,
695-
),
696691
NewFactory(
697692
"cannot_create_function",
698693
&stringGetterArguments{},
@@ -751,18 +746,6 @@ func Test_FunctionGetter(t *testing.T) {
751746
valid: false,
752747
expectedErrorMsg: "incorrect number of arguments. Expected: 4 Received: 1",
753748
},
754-
{
755-
name: "Invalid Arguments struct tag",
756-
getter: StandardStringGetter[interface{}]{
757-
Getter: func(ctx context.Context, tCtx interface{}) (interface{}, error) {
758-
return nil, nil
759-
},
760-
},
761-
function: StandardFunctionGetter[any]{fCtx: FunctionContext{Set: componenttest.NewNopTelemetrySettings()}, fact: functions["no_struct_tag"]},
762-
want: "anything",
763-
valid: false,
764-
expectedErrorMsg: "no `ottlarg` struct tag on Arguments field \"StringArg\"",
765-
},
766749
{
767750
name: "Cannot create function",
768751
getter: StandardStringGetter[interface{}]{

pkg/ottl/functions.go

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"errors"
88
"fmt"
99
"reflect"
10-
"strconv"
1110
"strings"
1211
)
1312

@@ -47,22 +46,6 @@ func (p *Parser[K]) newFunctionCall(ed editor) (Expr[K], error) {
4746
return Expr[K]{exprFunc: fn}, err
4847
}
4948

50-
func getArgumentIndex(index int, args reflect.Value) (int, error) {
51-
argsType := args.Type()
52-
fieldTag, ok := argsType.Field(index).Tag.Lookup("ottlarg")
53-
if !ok {
54-
return 0, fmt.Errorf("no `ottlarg` struct tag on Arguments field %q", argsType.Field(index).Name)
55-
}
56-
argNum, err := strconv.Atoi(fieldTag)
57-
if err != nil {
58-
return 0, fmt.Errorf("ottlarg struct tag on field %q is not a valid integer: %w", argsType.Field(index).Name, err)
59-
}
60-
if argNum < 0 || argNum >= args.NumField() {
61-
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())
62-
}
63-
return argNum, nil
64-
}
65-
6649
func (p *Parser[K]) buildArgs(ed editor, argsVal reflect.Value) error {
6750
if len(ed.Arguments) != argsVal.NumField() {
6851
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 {
7154
for i := 0; i < argsVal.NumField(); i++ {
7255
field := argsVal.Field(i)
7356
fieldType := field.Type()
74-
argNum, err := getArgumentIndex(i, argsVal)
75-
if err != nil {
76-
return err
77-
}
78-
argVal := ed.Arguments[argNum]
57+
argVal := ed.Arguments[i]
7958
var val any
59+
var err error
8060
switch {
8161
case strings.HasPrefix(fieldType.Name(), "FunctionGetter"):
8262
var name string

pkg/ottl/functions_test.go

Lines changed: 0 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -63,31 +63,6 @@ func Test_NewFunctionCall_invalid(t *testing.T) {
6363
errorFunctionArguments{},
6464
functionThatHasAnError,
6565
),
66-
createFactory(
67-
"no_struct_tag",
68-
&noStructTagFunctionArguments{},
69-
functionThatHasAnError,
70-
),
71-
createFactory(
72-
"wrong_struct_tag",
73-
&wrongTagFunctionArguments{},
74-
functionThatHasAnError,
75-
),
76-
createFactory(
77-
"bad_struct_tag",
78-
&badStructTagFunctionArguments{},
79-
functionThatHasAnError,
80-
),
81-
createFactory(
82-
"negative_struct_tag",
83-
&negativeStructTagFunctionArguments{},
84-
functionThatHasAnError,
85-
),
86-
createFactory(
87-
"out_of_bounds_struct_tag",
88-
&outOfBoundsStructTagFunctionArguments{},
89-
functionThatHasAnError,
90-
),
9166
createFactory(
9267
"testing_unknown_function",
9368
&functionGetterArguments{},
@@ -349,61 +324,6 @@ func Test_NewFunctionCall_invalid(t *testing.T) {
349324
Function: "non_pointer",
350325
},
351326
},
352-
{
353-
name: "no struct tags",
354-
inv: editor{
355-
Function: "no_struct_tag",
356-
Arguments: []value{
357-
{
358-
String: ottltest.Strp("str"),
359-
},
360-
},
361-
},
362-
},
363-
{
364-
name: "using the wrong struct tag",
365-
inv: editor{
366-
Function: "wrong_struct_tag",
367-
Arguments: []value{
368-
{
369-
String: ottltest.Strp("str"),
370-
},
371-
},
372-
},
373-
},
374-
{
375-
name: "non-integer struct tags",
376-
inv: editor{
377-
Function: "bad_struct_tag",
378-
Arguments: []value{
379-
{
380-
String: ottltest.Strp("str"),
381-
},
382-
},
383-
},
384-
},
385-
{
386-
name: "struct tag index too low",
387-
inv: editor{
388-
Function: "negative_struct_tag",
389-
Arguments: []value{
390-
{
391-
String: ottltest.Strp("str"),
392-
},
393-
},
394-
},
395-
},
396-
{
397-
name: "struct tag index too high",
398-
inv: editor{
399-
Function: "out_of_bounds_struct_tag",
400-
Arguments: []value{
401-
{
402-
String: ottltest.Strp("str"),
403-
},
404-
},
405-
},
406-
},
407327
}
408328

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

1519-
type noStructTagFunctionArguments struct {
1520-
StringArg string
1521-
}
1522-
1523-
type badStructTagFunctionArguments struct {
1524-
StringArg string `ottlarg:"a"`
1525-
}
1526-
1527-
type negativeStructTagFunctionArguments struct {
1528-
StringArg string `ottlarg:"-1"`
1529-
}
1530-
1531-
type outOfBoundsStructTagFunctionArguments struct {
1532-
StringArg string `ottlarg:"1"`
1533-
}
1534-
1535-
type wrongTagFunctionArguments struct {
1536-
StringArg string `argument:"1"`
1537-
}
1538-
15391439
func createFactory[A any](name string, args A, fn any) Factory[any] {
15401440
createFunction := func(fCtx FunctionContext, oArgs Arguments) (ExprFunc[any], error) {
15411441
fArgs, ok := oArgs.(A)

pkg/ottl/ottlfuncs/func_substring.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ import (
1212

1313
type SubstringArguments[K any] struct {
1414
Target ottl.StringGetter[K]
15-
Start int64
16-
Length int64
15+
Start ottl.IntGetter[K]
16+
Length ottl.IntGetter[K]
1717
}
1818

1919
func NewSubstringFactory[K any]() ottl.Factory[K] {

0 commit comments

Comments
 (0)