Skip to content

Commit

Permalink
Handle nil from Get
Browse files Browse the repository at this point in the history
  • Loading branch information
TylerHelmuth committed May 1, 2023
1 parent 19eed3b commit 702687a
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 2 deletions.
4 changes: 3 additions & 1 deletion pkg/ottl/contexts/internal/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,11 @@ func SetMapValue(m pcommon.Map, keys []ottl.Key, val interface{}) error {
if keys[i].String == nil {
return fmt.Errorf("map must be indexed by a string")
}
currentValue, ok = currentValue.Map().Get(*keys[i].String)
potentialValue, ok := currentValue.Map().Get(*keys[i].String)
if !ok {
currentValue = currentValue.Map().PutEmpty(*keys[i].String)
} else {
currentValue = potentialValue
}
case pcommon.ValueTypeSlice:
if keys[i].Int == nil {
Expand Down
49 changes: 48 additions & 1 deletion pkg/ottl/contexts/internal/map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,8 @@ func Test_GetMapValue_MissingKey(t *testing.T) {
},
}
result, err := GetMapValue(m, keys)
assert.Nil(t, result)
assert.Nil(t, err)
assert.Nil(t, result)
}

func Test_SetMapValue_Invalid(t *testing.T) {
Expand Down Expand Up @@ -234,3 +234,50 @@ func Test_SetMapValue_Invalid(t *testing.T) {
})
}
}

func Test_SetMapValue_AddingNewSubMap(t *testing.T) {
m := pcommon.NewMap()
m.PutEmptyMap("map1").PutStr("test", "test")
keys := []ottl.Key{
{
String: ottltest.Strp("map1"),
},
{
String: ottltest.Strp("map2"),
},
{
String: ottltest.Strp("foo"),
},
}
err := SetMapValue(m, keys, "bar")
assert.Nil(t, err)

expected := pcommon.NewMap()
sub := expected.PutEmptyMap("map1")
sub.PutStr("test", "test")
sub.PutEmptyMap("map2").PutStr("foo", "bar")

assert.Equal(t, expected, m)
}

func Test_SetMapValue_EmptyMap(t *testing.T) {
m := pcommon.NewMap()
keys := []ottl.Key{
{
String: ottltest.Strp("map1"),
},
{
String: ottltest.Strp("map2"),
},
{
String: ottltest.Strp("foo"),
},
}
err := SetMapValue(m, keys, "bar")
assert.Nil(t, err)

expected := pcommon.NewMap()
expected.PutEmptyMap("map1").PutEmptyMap("map2").PutStr("foo", "bar")

assert.Equal(t, expected, m)
}
32 changes: 32 additions & 0 deletions pkg/ottl/expression_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,38 @@ func Test_exprGetter_Get_Invalid(t *testing.T) {
},
err: fmt.Errorf("index -1 out of bounds"),
},
{
name: "invalid int indexing type",
val: value{
Literal: &mathExprLiteral{
Converter: &converter{
Function: "Hello",
Keys: []Key{
{
Int: ottltest.Intp(-1),
},
},
},
},
},
err: fmt.Errorf("type, string, does not support int indexing"),
},
{
name: "invalid string indexing type",
val: value{
Literal: &mathExprLiteral{
Converter: &converter{
Function: "Hello",
Keys: []Key{
{
String: ottltest.Strp("test"),
},
},
},
},
},
err: fmt.Errorf("type, string, does not support string indexing"),
},
}

functions := map[string]interface{}{
Expand Down

0 comments on commit 702687a

Please sign in to comment.