Skip to content

Commit

Permalink
support converters as attribute keys
Browse files Browse the repository at this point in the history
Signed-off-by: odubajDT <[email protected]>
  • Loading branch information
odubajDT committed Dec 19, 2024
1 parent 2301072 commit 8f9ce11
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 17 deletions.
4 changes: 2 additions & 2 deletions pkg/ottl/contexts/internal/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ var _ ottl.Key[any] = &TestKey[any]{}
type TestKey[K any] struct {
S *string
I *int64
P ottl.GetSetter[K]
P ottl.Getter[K]
}

func (k *TestKey[K]) String(_ context.Context, _ K) (*string, error) {
Expand All @@ -57,6 +57,6 @@ func (k *TestKey[K]) Int(_ context.Context, _ K) (*int64, error) {
return k.I, nil
}

func (k *TestKey[K]) PathGetter(_ context.Context, _ K) (ottl.GetSetter[K], error) {
func (k *TestKey[K]) PathGetter(_ context.Context, _ K) (ottl.Getter[K], error) {
return k.P, nil
}
12 changes: 6 additions & 6 deletions pkg/ottl/e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,12 +329,12 @@ func Test_e2e_converters(t *testing.T) {
statement string
want func(tCtx ottllog.TransformContext)
}{
// {
// statement: `set(attributes[ConvertCase(attributes["A|B|C"], "upper")], "myvalue")`,
// want: func(tCtx ottllog.TransformContext) {
// tCtx.GetLogRecord().Attributes().PutStr("SOMETHING", "myvalue")
// },
// },
{
statement: `set(attributes[ConvertCase(attributes["A|B|C"], "upper")], "myvalue")`,
want: func(tCtx ottllog.TransformContext) {
tCtx.GetLogRecord().Attributes().PutStr("SOMETHING", "myvalue")
},
},
{
statement: `set(attributes[attributes["flags"]], "something33")`,
want: func(tCtx ottllog.TransformContext) {
Expand Down
33 changes: 24 additions & 9 deletions pkg/ottl/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,13 +216,28 @@ func (p *Parser[K]) newKeys(keys []key) ([]Key[K], error) {
}
ks := make([]Key[K], len(keys))
for i := range keys {
var par GetSetter[K]
if keys[i].Expression != nil && keys[i].Expression.Path != nil {
arg, err := p.buildGetSetterFromPath(keys[i].Expression.Path)
if err != nil {
return nil, err
var par Getter[K]
if keys[i].Expression != nil {
if keys[i].Expression.Path != nil {
arg, err := p.buildGetSetterFromPath(keys[i].Expression.Path)
if err != nil {
return nil, err
}
par = arg
}
if f := keys[i].Expression.Float; f != nil {
par = literal[K]{value: *f}
}
if i := keys[i].Expression.Int; i != nil {
par = literal[K]{value: *i}
}
if keys[i].Expression.Converter != nil {
g, err := p.newGetterFromConverter(*keys[i].Expression.Converter)
if err != nil {
return nil, err
}
par = g
}
par = arg
}
ks[i] = &baseKey[K]{
s: keys[i].String,
Expand All @@ -247,15 +262,15 @@ type Key[K any] interface {
// If Key experiences an error retrieving the value it is returned.
Int(context.Context, K) (*int64, error)

PathGetter(context.Context, K) (GetSetter[K], error)
PathGetter(context.Context, K) (Getter[K], error)
}

var _ Key[any] = &baseKey[any]{}

type baseKey[K any] struct {
s *string
i *int64
p GetSetter[K]
p Getter[K]
}

func (k *baseKey[K]) String(_ context.Context, _ K) (*string, error) {
Expand All @@ -266,7 +281,7 @@ func (k *baseKey[K]) Int(_ context.Context, _ K) (*int64, error) {
return k.i, nil
}

func (k *baseKey[K]) PathGetter(_ context.Context, _ K) (GetSetter[K], error) {
func (k *baseKey[K]) PathGetter(_ context.Context, _ K) (Getter[K], error) {
return k.p, nil
}

Expand Down

0 comments on commit 8f9ce11

Please sign in to comment.