Handle alias types for map keys in toTyped conversion (#1232)

## Changes
Handle alias types for map keys in toTyped conversion

## Tests
Added an unit test
This commit is contained in:
Andrew Nester 2024-02-22 16:17:43 +01:00 committed by GitHub
parent 1b4a774609
commit f69b70782d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 18 additions and 1 deletions

View File

@ -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:

View File

@ -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"])
}