Skip to content

Commit

Permalink
Handle alias types for map keys in toTyped conversion (#1232)
Browse files Browse the repository at this point in the history
## Changes
Handle alias types for map keys in toTyped conversion

## Tests
Added an unit test
  • Loading branch information
andrewnester authored Feb 22, 2024
1 parent 1b4a774 commit f69b707
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
3 changes: 2 additions & 1 deletion libs/dyn/convert/to_typed.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,13 @@ func toTypedMap(dst reflect.Value, src dyn.Value) error {
dst.Set(reflect.MakeMapWithSize(dst.Type(), len(m)))
for k, v := range m {
kv := reflect.ValueOf(k)
kt := dst.Type().Key()
vv := reflect.New(dst.Type().Elem())
err := ToTyped(vv.Interface(), v)
if err != nil {
return err
}
dst.SetMapIndex(kv, vv.Elem())
dst.SetMapIndex(kv.Convert(kt), vv.Elem())
}
return nil
case dyn.KindNil:
Expand Down
16 changes: 16 additions & 0 deletions libs/dyn/convert/to_typed_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -495,3 +495,19 @@ func TestToTypedFloat64FromStringVariableReference(t *testing.T) {
require.NoError(t, err)
assert.Equal(t, float64(0.0), out)
}

func TestToTypedWithAliasKeyType(t *testing.T) {
type custom string

var out map[custom]string
v := dyn.V(map[string]dyn.Value{
"foo": dyn.V("bar"),
"bar": dyn.V("baz"),
})

err := ToTyped(&out, v)
require.NoError(t, err)
assert.Len(t, out, 2)
assert.Equal(t, "bar", out["foo"])
assert.Equal(t, "baz", out["bar"])
}

0 comments on commit f69b707

Please sign in to comment.