Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
27 changes: 27 additions & 0 deletions .chloggen/add-PSliceGetSetter.yaml
Original file line number Diff line number Diff line change
@@ -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]
33 changes: 33 additions & 0 deletions pkg/ottl/expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
13 changes: 13 additions & 0 deletions pkg/ottl/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
159 changes: 159 additions & 0 deletions pkg/ottl/functions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ func Test_NewFunctionCall_invalid(t *testing.T) {
&pMapGetSetterArguments{},
functionWithPMapGetSetter,
),
createFactory[any](
"testing_pslicegetsetter",
&pSliceGetSetterArguments{},
functionWithPSliceGetSetter,
),
createFactory[any](
"testing_getsetter",
&getSetterArguments{},
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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]
}
Expand Down Expand Up @@ -2409,6 +2563,11 @@ func defaultFunctionsForTests() map[string]Factory[any] {
&pMapGetSetterArguments{},
functionWithPMapGetSetter,
),
createFactory[any](
"testing_pslicegetsetter",
&pSliceGetSetterArguments{},
functionWithPSliceGetSetter,
),
createFactory[any](
"testing_getsetter",
&getSetterArguments{},
Expand Down