Skip to content

Commit 99d9c07

Browse files
committed
[pkg/ottl] Make LambdaExpression arguments pointers
1 parent 8bee89f commit 99d9c07

4 files changed

Lines changed: 72 additions & 4 deletions

File tree

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Use this changelog template to create an entry for release notes.
2+
3+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
4+
change_type: breaking
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. receiver/filelog)
7+
component: pkg/ottl
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: Switch `LambdaExpression` arguments to be pointers.
11+
12+
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
13+
issues: []
14+
15+
# (Optional) One or more lines of additional information to render under the primary note.
16+
# These lines will be padded with 2 spaces and then inserted directly into the document.
17+
# Use pipe (|) for multiline entries.
18+
subtext: |
19+
These values are intended to be mutable and are passed by reference in normal use,
20+
so this keeps symmetry between the argument declaration and their use by the function.
21+
22+
# If your change doesn't affect end users or the exported elements of any package,
23+
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
24+
# Optional: The change log or logs in which this entry should be included.
25+
# e.g. '[user]' or '[user, api]'
26+
# Include 'user' if the change is relevant to end users.
27+
# Include 'api' if there is a change to a library API.
28+
# Default: '[user]'
29+
change_logs: [api]

pkg/ottl/functions.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,8 @@ func (p *parseContext[K]) buildArgs(ed editor, argsVal reflect.Value) error {
460460
val = StandardFunctionGetter[K]{FCtx: FunctionContext{Set: p.telemetrySettings}, Fact: f}
461461
case fieldType.Kind() == reflect.Slice:
462462
val, err = p.buildSliceArg(arg.Value, fieldType)
463+
case fieldType.Kind() == reflect.Pointer:
464+
val, err = p.buildArg(arg.Value, fieldType.Elem())
463465
default:
464466
val, err = p.buildArg(arg.Value, fieldType)
465467
}
@@ -736,7 +738,7 @@ func (p *parseContext[K]) buildArg(argVal value, argType reflect.Type) (any, err
736738
if err != nil {
737739
return nil, err
738740
}
739-
return *lambExpr, nil
741+
return lambExpr, nil
740742
default:
741743
return nil, fmt.Errorf("unsupported argument type: %s", name)
742744
}

pkg/ottl/functions_test.go

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,11 @@ func Test_NewFunctionCall_invalid(t *testing.T) {
9191
&optionalArgsArguments{},
9292
functionWithOptionalArgs,
9393
),
94+
createFactory[any](
95+
"testing_non_pointer_lambda",
96+
&nonPointerLambdaArguments{},
97+
functionWithNonPointerLambda,
98+
),
9499
)
95100

96101
p, _ := NewParser(
@@ -588,6 +593,28 @@ func Test_NewFunctionCall_invalid(t *testing.T) {
588593
},
589594
},
590595
},
596+
{
597+
name: "lambda expression argument that is not a pointer",
598+
inv: editor{
599+
Function: "testing_non_pointer_lambda",
600+
Arguments: []argument{
601+
{
602+
Value: value{
603+
Lambda: &lambdaExpr{
604+
Params: []localIdentifierDecl{"value"},
605+
Body: lambdaBody{
606+
Value: &value{
607+
Literal: &mathExprLiteral{
608+
Path: &path{Fields: []field{{Name: "value"}}},
609+
},
610+
},
611+
},
612+
},
613+
},
614+
},
615+
},
616+
},
617+
},
591618
}
592619

593620
for _, tt := range tests {
@@ -2531,13 +2558,23 @@ func functionWithFunctionGetter(FunctionGetter[any]) (ExprFunc[any], error) {
25312558
}, nil
25322559
}
25332560

2561+
type nonPointerLambdaArguments struct {
2562+
Expr LambdaExpression[any]
2563+
}
2564+
2565+
func functionWithNonPointerLambda(LambdaExpression[any]) (ExprFunc[any], error) {
2566+
return func(context.Context, any) (any, error) {
2567+
return nil, nil
2568+
}, nil
2569+
}
2570+
25342571
type evalLambdaArguments[K any] struct {
2535-
Expr LambdaExpression[K]
2572+
Expr *LambdaExpression[K]
25362573
Args []Getter[K]
25372574
}
25382575

25392576
//nolint:unparam // returning (ExprFunc[K], error) is required by this local test framework
2540-
func evalLambdaFunction[K any](expr LambdaExpression[any], args []Getter[K]) (ExprFunc[K], error) {
2577+
func evalLambdaFunction[K any](expr *LambdaExpression[any], args []Getter[K]) (ExprFunc[K], error) {
25412578
return func(ctx context.Context, tCtx K) (any, error) {
25422579
lambda, err := expr.Activate(ctx, len(args))
25432580
if err != nil {

pkg/ottl/parser_collection_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -924,7 +924,7 @@ func mockParser(t *testing.T, options ...Option[any]) *Parser[any] {
924924
}, nil
925925
})
926926

927-
mockLambdaFactory := NewFactory("Lambda", &struct{ Expr LambdaExpression[any] }{},
927+
mockLambdaFactory := NewFactory("Lambda", &struct{ Expr *LambdaExpression[any] }{},
928928
func(FunctionContext, Arguments) (ExprFunc[any], error) {
929929
return nil, nil
930930
})

0 commit comments

Comments
 (0)