Skip to content

Commit 13994d2

Browse files
committed
Fix deleteKey
1 parent d144d99 commit 13994d2

File tree

2 files changed

+37
-9
lines changed

2 files changed

+37
-9
lines changed

pkg/ottl/ottlfuncs/func_delete_key.go

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,15 @@ package ottlfuncs // import "github.com/open-telemetry/opentelemetry-collector-c
66
import (
77
"context"
88
"errors"
9+
"fmt"
10+
11+
"go.opentelemetry.io/collector/pdata/pcommon"
912

1013
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl"
1114
)
1215

1316
type DeleteKeyArguments[K any] struct {
14-
Target ottl.PMapGetter[K]
17+
Target ottl.GetSetter[K]
1518
Key string
1619
}
1720

@@ -29,13 +32,30 @@ func createDeleteKeyFunction[K any](_ ottl.FunctionContext, oArgs ottl.Arguments
2932
return deleteKey(args.Target, args.Key), nil
3033
}
3134

32-
func deleteKey[K any](target ottl.PMapGetter[K], key string) ottl.ExprFunc[K] {
35+
func deleteKey[K any](target ottl.GetSetter[K], key string) ottl.ExprFunc[K] {
3336
return func(ctx context.Context, tCtx K) (any, error) {
34-
val, err := target.Get(ctx, tCtx)
37+
anyVal, err := target.Get(ctx, tCtx)
3538
if err != nil {
3639
return nil, err
3740
}
38-
val.Remove(key)
39-
return nil, nil
41+
var m pcommon.Map
42+
switch v := anyVal.(type) {
43+
case pcommon.Map:
44+
m = v
45+
case pcommon.Value:
46+
if v.Type() != pcommon.ValueTypeMap {
47+
return nil, ottl.TypeError(fmt.Sprintf("expected pcommon.Map but got %v", v.Type()))
48+
}
49+
m = v.Map()
50+
case map[string]any:
51+
m = pcommon.NewMap()
52+
if err = m.FromRaw(v); err != nil {
53+
return nil, err
54+
}
55+
default:
56+
return nil, ottl.TypeError(fmt.Sprintf("expected pcommon.Map but got %T", v))
57+
}
58+
m.Remove(key)
59+
return nil, target.Set(ctx, tCtx, m)
4060
}
4161
}

pkg/ottl/ottlfuncs/func_delete_key_test.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package ottlfuncs
55

66
import (
77
"context"
8+
"errors"
89
"testing"
910

1011
"github.com/stretchr/testify/assert"
@@ -19,15 +20,22 @@ func Test_deleteKey(t *testing.T) {
1920
input.PutInt("test2", 3)
2021
input.PutBool("test3", true)
2122

22-
target := &ottl.StandardPMapGetter[pcommon.Map]{
23+
target := &ottl.StandardGetSetter[pcommon.Map]{
2324
Getter: func(_ context.Context, tCtx pcommon.Map) (any, error) {
2425
return tCtx, nil
2526
},
27+
Setter: func(_ context.Context, tCtx pcommon.Map, val any) error {
28+
if v, ok := val.(pcommon.Map); ok {
29+
v.CopyTo(tCtx)
30+
return nil
31+
}
32+
return errors.New("expected pcommon.Map")
33+
},
2634
}
2735

2836
tests := []struct {
2937
name string
30-
target ottl.PMapGetter[pcommon.Map]
38+
target ottl.GetSetter[pcommon.Map]
3139
key string
3240
want func(pcommon.Map)
3341
}{
@@ -80,7 +88,7 @@ func Test_deleteKey(t *testing.T) {
8088

8189
func Test_deleteKey_bad_input(t *testing.T) {
8290
input := pcommon.NewValueStr("not a map")
83-
target := &ottl.StandardPMapGetter[any]{
91+
target := &ottl.StandardGetSetter[any]{
8492
Getter: func(_ context.Context, tCtx any) (any, error) {
8593
return tCtx, nil
8694
},
@@ -94,7 +102,7 @@ func Test_deleteKey_bad_input(t *testing.T) {
94102
}
95103

96104
func Test_deleteKey_get_nil(t *testing.T) {
97-
target := &ottl.StandardPMapGetter[any]{
105+
target := &ottl.StandardGetSetter[any]{
98106
Getter: func(_ context.Context, tCtx any) (any, error) {
99107
return tCtx, nil
100108
},

0 commit comments

Comments
 (0)