Skip to content

Commit

Permalink
cover error cases with invalid index types
Browse files Browse the repository at this point in the history
Signed-off-by: odubajDT <[email protected]>
  • Loading branch information
odubajDT committed Jan 8, 2025
1 parent 551f125 commit 8b59bb7
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .chloggen/indexing-pkg-ottl.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: enhancement
change_type: breaking

# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
component: pkg/ottl
Expand Down
7 changes: 5 additions & 2 deletions pkg/ottl/contexts/internal/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func GetMapValue[K any](ctx context.Context, tCtx K, m pcommon.Map, keys []ottl.
if s == nil {
resString, err := FetchValueFromExpression[K, string](ctx, tCtx, keys[0])
if err != nil {
return nil, fmt.Errorf("unable to resolve a string index: %w", err)
return nil, fmt.Errorf("unable to resolve a string index in map: %w", err)
}
s = resString
}
Expand All @@ -49,7 +49,7 @@ func SetMapValue[K any](ctx context.Context, tCtx K, m pcommon.Map, keys []ottl.
if s == nil {
resString, err := FetchValueFromExpression[K, string](ctx, tCtx, keys[0])
if err != nil {
return fmt.Errorf("unable to resolve a string index: %w", err)
return fmt.Errorf("unable to resolve a string index in map: %w", err)
}
s = resString
}
Expand All @@ -66,6 +66,9 @@ func FetchValueFromExpression[K any, T int64 | string](ctx context.Context, tCtx
if err != nil {
return nil, err
}
if p == nil {
return nil, fmt.Errorf("invalid key type")
}
res, err := p.Get(ctx, tCtx)
if err != nil {
return nil, err
Expand Down
4 changes: 2 additions & 2 deletions pkg/ottl/contexts/internal/map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func Test_GetMapValue_Invalid(t *testing.T) {
G: getSetter,
},
},
err: fmt.Errorf("unable to resolve a string index: could not resolve key for map/slice, expecting 'string' but got '<nil>'"),
err: fmt.Errorf("unable to resolve a string index in map: could not resolve key for map/slice, expecting 'string' but got '<nil>'"),
},
{
name: "index map with int",
Expand Down Expand Up @@ -169,7 +169,7 @@ func Test_SetMapValue_Invalid(t *testing.T) {
G: getSetter,
},
},
err: fmt.Errorf("unable to resolve a string index: could not resolve key for map/slice, expecting 'string' but got '<nil>'"),
err: fmt.Errorf("unable to resolve a string index in map: could not resolve key for map/slice, expecting 'string' but got '<nil>'"),
},
{
name: "index map with int",
Expand Down
4 changes: 2 additions & 2 deletions pkg/ottl/contexts/internal/slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func GetSliceValue[K any](ctx context.Context, tCtx K, s pcommon.Slice, keys []o
if i == nil {
resInt, err := FetchValueFromExpression[K, int64](ctx, tCtx, keys[0])
if err != nil {
return nil, fmt.Errorf("unable to resolve an integer index: %w", err)
return nil, fmt.Errorf("unable to resolve an integer index in slice: %w", err)
}
i = resInt
}
Expand All @@ -50,7 +50,7 @@ func SetSliceValue[K any](ctx context.Context, tCtx K, s pcommon.Slice, keys []o
if i == nil {
resInt, err := FetchValueFromExpression[K, int64](ctx, tCtx, keys[0])
if err != nil {
return fmt.Errorf("unable to resolve an integer index: %w", err)
return fmt.Errorf("unable to resolve an integer index in slice: %w", err)
}
i = resInt
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/ottl/contexts/internal/slice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func Test_GetSliceValue_Invalid(t *testing.T) {
G: getSetter,
},
},
err: fmt.Errorf(`unable to resolve an integer index: could not resolve key for map/slice, expecting 'int64' but got '<nil>'`),
err: fmt.Errorf(`unable to resolve an integer index in slice: could not resolve key for map/slice, expecting 'int64' but got '<nil>'`),
},
{
name: "index too large",
Expand Down Expand Up @@ -113,7 +113,7 @@ func Test_SetSliceValue_Invalid(t *testing.T) {
G: getSetter,
},
},
err: fmt.Errorf(`unable to resolve an integer index: could not resolve key for map/slice, expecting 'int64' but got '<nil>'`),
err: fmt.Errorf(`unable to resolve an integer index in slice: could not resolve key for map/slice, expecting 'int64' but got '<nil>'`),
},
{
name: "index too large",
Expand Down
19 changes: 18 additions & 1 deletion pkg/ottl/e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,19 @@ func Test_e2e_converters(t *testing.T) {
tests := []struct {
statement string
want func(tCtx ottllog.TransformContext)
wantErr bool
errMsg string
}{
{
statement: `set(attributes["newOne"], attributes[1])`,
want: func(tCtx ottllog.TransformContext) {},
errMsg: "unable to resolve a string index in map: invalid key type",
},
{
statement: `set(attributes["array"][0.0], "bar")`,
want: func(tCtx ottllog.TransformContext) {},
errMsg: "unable to resolve an integer index in slice: invalid key type",
},
{
statement: `set(attributes[ConvertCase(attributes["A|B|C"], "upper")], "myvalue")`,
want: func(tCtx ottllog.TransformContext) {
Expand Down Expand Up @@ -1029,7 +1041,12 @@ func Test_e2e_converters(t *testing.T) {
assert.NoError(t, err)

tCtx := constructLogTransformContext()
_, _, _ = logStatements.Execute(context.Background(), tCtx)
_, _, err = logStatements.Execute(context.Background(), tCtx)
if tt.errMsg == "" {
assert.NoError(t, err)
} else {
assert.Contains(t, err.Error(), tt.errMsg)
}

exTCtx := constructLogTransformContext()
tt.want(exTCtx)
Expand Down

0 comments on commit 8b59bb7

Please sign in to comment.