From aeb0550ed95077b564b57085a705498df4ac5288 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lex=20C=C3=A1mara?= Date: Wed, 26 Nov 2025 09:53:36 +0100 Subject: [PATCH] [pkg/ottl] Add PSliceGetSetter and StandardPSliceGetSetter --- .chloggen/add-PSliceGetSetter.yaml | 27 +++++ pkg/ottl/expression.go | 33 ++++++ pkg/ottl/functions.go | 13 +++ pkg/ottl/functions_test.go | 159 +++++++++++++++++++++++++++++ 4 files changed, 232 insertions(+) create mode 100644 .chloggen/add-PSliceGetSetter.yaml diff --git a/.chloggen/add-PSliceGetSetter.yaml b/.chloggen/add-PSliceGetSetter.yaml new file mode 100644 index 0000000000000..eb99964575031 --- /dev/null +++ b/.chloggen/add-PSliceGetSetter.yaml @@ -0,0 +1,27 @@ +# Use this changelog template to create an entry for release notes. + +# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' +change_type: enhancement + +# The name of the component, or a single word describing the area of concern, (e.g. receiver/filelog) +component: pkg/ottl + +# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: Add PSliceGetSetter interface and StandardPSliceGetSetter type. + +# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. +issues: [44421] + +# (Optional) One or more lines of additional information to render under the primary note. +# These lines will be padded with 2 spaces and then inserted directly into the document. +# Use pipe (|) for multiline entries. +subtext: + +# If your change doesn't affect end users or the exported elements of any package, +# you should instead start your pull request title with [chore] or use the "Skip Changelog" label. +# Optional: The change log or logs in which this entry should be included. +# e.g. '[user]' or '[user, api]' +# Include 'user' if the change is relevant to end users. +# Include 'api' if there is a change to a library API. +# Default: '[user]' +change_logs: [api] diff --git a/pkg/ottl/expression.go b/pkg/ottl/expression.go index 8a1d74e04b36c..faba15ce431e7 100644 --- a/pkg/ottl/expression.go +++ b/pkg/ottl/expression.go @@ -248,6 +248,39 @@ func (m *mapGetter[K]) Get(ctx context.Context, tCtx K) (any, error) { return result, nil } +// PSliceGetSetter is a GetSetter that must interact with a pcommon.Slice +type PSliceGetSetter[K any] interface { + Get(ctx context.Context, tCtx K) (pcommon.Slice, error) + Set(ctx context.Context, tCtx K, val pcommon.Slice) error +} + +// StandardPSliceGetSetter is a basic implementation of PSliceGetSetter +type StandardPSliceGetSetter[K any] struct { + Getter func(ctx context.Context, tCtx K) (pcommon.Slice, error) + Setter func(ctx context.Context, tCtx K, val any) error +} + +func (path StandardPSliceGetSetter[K]) Get(ctx context.Context, tCtx K) (pcommon.Slice, error) { + return path.Getter(ctx, tCtx) +} + +func (path StandardPSliceGetSetter[K]) Set(ctx context.Context, tCtx K, val pcommon.Slice) error { + return path.Setter(ctx, tCtx, val) +} + +// newStandardPSliceGetSetter creates a new StandardPSliceGetSetter from a GetSetter[K], +// also validates getter on each use and checks if the GetSetter is a literalGetter. +func newStandardPSliceGetSetter[K any](getSetter GetSetter[K]) (PSliceGetSetter[K], error) { + g, err := newStandardPSliceGetter[K](getSetter) + if err != nil { + return nil, err + } + return StandardPSliceGetSetter[K]{ + Getter: g.Get, + Setter: getSetter.Set, + }, nil +} + // PSliceGetter is a Getter that must return a pcommon.Slice. type PSliceGetter[K any] interface { Get(ctx context.Context, tCtx K) (pcommon.Slice, error) diff --git a/pkg/ottl/functions.go b/pkg/ottl/functions.go index b0d4793f96c70..4f059106da4c1 100644 --- a/pkg/ottl/functions.go +++ b/pkg/ottl/functions.go @@ -615,6 +615,19 @@ func (p *Parser[K]) buildArg(argVal value, argType reflect.Type) (any, error) { return nil, err } return newStandardPMapGetter[K](arg) + case strings.HasPrefix(name, "PSliceGetSetter"): + if argVal.Literal == nil || argVal.Literal.Path == nil { + return nil, errors.New("must be a path") + } + pathGetSetter, err := p.buildGetSetterFromPath(argVal.Literal.Path) + if err != nil { + return nil, err + } + stdSliceGetSetter, err := newStandardPSliceGetSetter[K](pathGetSetter) + if err != nil { + return nil, err + } + return stdSliceGetSetter, nil case strings.HasPrefix(name, "PSliceGetter"): arg, err := p.newGetter(argVal) if err != nil { diff --git a/pkg/ottl/functions_test.go b/pkg/ottl/functions_test.go index c64fa9dfa0ace..7e20ed8e25b0f 100644 --- a/pkg/ottl/functions_test.go +++ b/pkg/ottl/functions_test.go @@ -30,6 +30,11 @@ func Test_NewFunctionCall_invalid(t *testing.T) { &pMapGetSetterArguments{}, functionWithPMapGetSetter, ), + createFactory[any]( + "testing_pslicegetsetter", + &pSliceGetSetterArguments{}, + functionWithPSliceGetSetter, + ), createFactory[any]( "testing_getsetter", &getSetterArguments{}, @@ -131,6 +136,19 @@ func Test_NewFunctionCall_invalid(t *testing.T) { }, }, }, + { + name: "not accessor (pslice)", + inv: editor{ + Function: "testing_pslicegetsetter", + Arguments: []argument{ + { + Value: value{ + String: ottltest.Strp("not path"), + }, + }, + }, + }, + }, { name: "not accessor", inv: editor{ @@ -434,6 +452,30 @@ func Test_NewFunctionCall_invalid(t *testing.T) { }, }, }, + { + name: "path parts not all used (pslice)", + inv: editor{ + Function: "testing_pslicegetsetter", + Arguments: []argument{ + { + Value: value{ + Literal: &mathExprLiteral{ + Path: &path{ + Fields: []field{ + { + Name: "name", + }, + { + Name: "not-used", + }, + }, + }, + }, + }, + }, + }, + }, + }, { name: "path parts not all used", inv: editor{ @@ -487,6 +529,35 @@ func Test_NewFunctionCall_invalid(t *testing.T) { }, }, }, + { + name: "Keys not allowed (pslice)", + inv: editor{ + Function: "testing_pslicegetsetter", + Arguments: []argument{ + { + Value: value{ + Literal: &mathExprLiteral{ + Path: &path{ + Fields: []field{ + { + Name: "name", + Keys: []key{ + { + String: ottltest.Strp("foo"), + }, + { + String: ottltest.Strp("bar"), + }, + }, + }, + }, + }, + }, + }, + }, + }, + }, + }, { name: "Keys not allowed", inv: editor{ @@ -947,6 +1018,27 @@ func Test_NewFunctionCall(t *testing.T) { }, want: 2, }, + { + name: "pslicegetsetter arg", + inv: editor{ + Function: "testing_pslicegetsetter", + Arguments: []argument{ + { + Value: value{ + Literal: &mathExprLiteral{ + Path: &path{ + Fields: []field{ + { + Name: "name", + }, + }, + }, + }, + }, + }, + }, + }, + }, { name: "pslicegetter slice arg", inv: editor{ @@ -1674,6 +1766,36 @@ func Test_NewFunctionCall(t *testing.T) { }, want: nil, }, + { + name: "Complex Indexing (pslice)", + inv: editor{ + Function: "testing_pslicegetsetter", + Arguments: []argument{ + { + Value: value{ + Literal: &mathExprLiteral{ + Path: &path{ + Fields: []field{ + { + Name: "attributes", + Keys: []key{ + { + Int: ottltest.Intp(0), + }, + { + String: ottltest.Strp("bar"), + }, + }, + }, + }, + }, + }, + }, + }, + }, + }, + want: nil, + }, { name: "Complex Indexing", inv: editor{ @@ -1726,6 +1848,28 @@ func Test_NewFunctionCall(t *testing.T) { }, want: nil, }, + { + name: "path that allows keys but none have been specified (pslice)", + inv: editor{ + Function: "testing_pslicegetsetter", + Arguments: []argument{ + { + Value: value{ + Literal: &mathExprLiteral{ + Path: &path{ + Fields: []field{ + { + Name: "attributes", + }, + }, + }, + }, + }, + }, + }, + }, + want: nil, + }, { name: "path that allows keys but none have been specified", inv: editor{ @@ -2178,6 +2322,16 @@ func functionWithPMapGetter(PMapGetter[any]) (ExprFunc[any], error) { }, nil } +type pSliceGetSetterArguments struct { + PSliceGetSetterArg PSliceGetSetter[any] +} + +func functionWithPSliceGetSetter(PSliceGetSetter[any]) (ExprFunc[any], error) { + return func(context.Context, any) (any, error) { + return "anything", nil + }, nil +} + type pSliceGetterArguments struct { PSliceArg PSliceGetter[any] } @@ -2409,6 +2563,11 @@ func defaultFunctionsForTests() map[string]Factory[any] { &pMapGetSetterArguments{}, functionWithPMapGetSetter, ), + createFactory[any]( + "testing_pslicegetsetter", + &pSliceGetSetterArguments{}, + functionWithPSliceGetSetter, + ), createFactory[any]( "testing_getsetter", &getSetterArguments{},