Skip to content

Commit

Permalink
Rever using reflect and update test
Browse files Browse the repository at this point in the history
  • Loading branch information
lkwronski committed May 19, 2024
1 parent 2809a2f commit 025157d
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 23 deletions.
7 changes: 7 additions & 0 deletions pkg/ottl/e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -732,6 +732,13 @@ func Test_e2e_ottl_features(t *testing.T) {
tCtx.GetLogRecord().Attributes().PutStr("test", "pass")
},
},
{
name: "where clause with Contains floats return value",
statement: []string{`set(attributes["test"], "pass") where Contains([1.1, 2.2, 3.3, 4.4], "4.4")`},
want: func(tCtx ottllog.TransformContext) {
tCtx.GetLogRecord().Attributes().PutStr("test", "pass")
},
},
{
name: `set attribute when tag "staging" is in tags attributes slice using Contains`,
statement: []string{
Expand Down
25 changes: 7 additions & 18 deletions pkg/ottl/expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,29 +170,18 @@ func (g StandardPSliceGetter[K]) Get(ctx context.Context, tCtx K) (pcommon.Slice
return v.Slice(), nil
}
return pcommon.Slice{}, TypeError(fmt.Sprintf("expected pcommon.Slice but got %v", v.Type()))
default:
if reflect.ValueOf(val).Kind() == reflect.Slice {
return convertToSlice(val)
case []any:
s := pcommon.NewSlice()
err := s.FromRaw(v)
if err != nil {
return pcommon.Slice{}, err
}
return s, nil
default:
return pcommon.Slice{}, TypeError(fmt.Sprintf("expected pcommon.Slice but got %T", val))
}
}

// converts a generic slice to pcommon.Slice. It uses reflection to handle any slice type, creating a pcommon.Slice from it.
func convertToSlice(val any) (pcommon.Slice, error) {
reflectV := reflect.ValueOf(val)
var output []any
for i := 0; i < reflectV.Len(); i++ {
output = append(output, reflectV.Index(i).Interface())
}
s := pcommon.NewSlice()
err := s.FromRaw(output)
if err != nil {
return pcommon.Slice{}, err
}
return s, nil
}

// TypeError represents that a value was not an expected type.
type TypeError string

Expand Down
1 change: 1 addition & 0 deletions pkg/ottl/ottlfuncs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,7 @@ Examples:

- `Contains(attributes["tags"], "staging")`
- `Contains([1, 2, 3, 4, 5], "3")`
- `Contains([1.1, 2.2, 3.3, 4.4], "4.4")`
- `Contains(["GET", "PUT", "POST"], "GET")`

### ConvertCase
Expand Down
10 changes: 5 additions & 5 deletions pkg/ottl/ottlfuncs/func_contains_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func Test_contains(t *testing.T) {
name: "find item in target",
target: ottl.StandardPSliceGetter[any]{
Getter: func(_ context.Context, _ any) (any, error) {
return []string{"hello", "world"}, nil
return []any{"hello", "world"}, nil
},
},
item: "hello",
Expand All @@ -34,7 +34,7 @@ func Test_contains(t *testing.T) {
name: "not find item in target",
target: ottl.StandardPSliceGetter[any]{
Getter: func(_ context.Context, _ any) (any, error) {
return []string{"hello", "world"}, nil
return []any{"hello", "world"}, nil
},
},
item: "unknow",
Expand All @@ -44,7 +44,7 @@ func Test_contains(t *testing.T) {
name: "find integers in target",
target: ottl.StandardPSliceGetter[any]{
Getter: func(_ context.Context, _ any) (any, error) {
return []int{0, 1}, nil
return []any{0, 1}, nil
},
},
item: "1",
Expand All @@ -54,7 +54,7 @@ func Test_contains(t *testing.T) {
name: "find floats in taget",
target: ottl.StandardPSliceGetter[any]{
Getter: func(_ context.Context, _ any) (any, error) {
return []float64{0, 3.14159}, nil
return []any{0, 3.14159}, nil
},
},
item: "3.14159",
Expand All @@ -64,7 +64,7 @@ func Test_contains(t *testing.T) {
name: "find booleans in target",
target: ottl.StandardPSliceGetter[any]{
Getter: func(_ context.Context, _ any) (any, error) {
return []bool{true, false}, nil
return []any{true, false}, nil
},
},
item: "true",
Expand Down

0 comments on commit 025157d

Please sign in to comment.