mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-20 12:29:03 +00:00
reflect: unpack interfaces in MapKeys() if needed
This commit is contained in:
+10
-1
@@ -779,8 +779,17 @@ func (v Value) MapKeys() []Value {
|
|||||||
k := New(v.typecode.Key())
|
k := New(v.typecode.Key())
|
||||||
e := New(v.typecode.Elem())
|
e := New(v.typecode.Elem())
|
||||||
|
|
||||||
|
keyType := v.typecode.Key().(*rawType)
|
||||||
|
isKeyStoredAsInterface := keyType.Kind() != String && !keyType.isBinary()
|
||||||
|
|
||||||
for hashmapNext(v.pointer(), it, k.value, e.value) {
|
for hashmapNext(v.pointer(), it, k.value, e.value) {
|
||||||
keys = append(keys, k.Elem())
|
if isKeyStoredAsInterface {
|
||||||
|
intf := *(*interface{})(k.value)
|
||||||
|
v := ValueOf(intf)
|
||||||
|
keys = append(keys, v)
|
||||||
|
} else {
|
||||||
|
keys = append(keys, k.Elem())
|
||||||
|
}
|
||||||
k = New(v.typecode.Key())
|
k = New(v.typecode.Key())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -134,6 +134,20 @@ func TestTinyMap(t *testing.T) {
|
|||||||
t.Errorf("m[hello, 4]=%v, want 6", six)
|
t.Errorf("m[hello, 4]=%v, want 6", six)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
keys := refsimap.MapKeys()
|
||||||
|
if len(keys) != 1 {
|
||||||
|
t.Errorf("refsimap: MapKeys()=%v, want 1", len(keys))
|
||||||
|
}
|
||||||
|
if keys[0].Type() != TypeOf(stringint{}) {
|
||||||
|
t.Errorf("keys[0] has wrong type: %v", keys[0].Type().String())
|
||||||
|
}
|
||||||
|
|
||||||
|
sikey := keys[0].Interface().(stringint)
|
||||||
|
|
||||||
|
if sikey != (stringint{"hello", 4}) {
|
||||||
|
t.Errorf("sikey has unexpected value: %#v", sikey)
|
||||||
|
}
|
||||||
|
|
||||||
// make sure we can look up interface keys with reflection
|
// make sure we can look up interface keys with reflection
|
||||||
type unmarshalerText struct {
|
type unmarshalerText struct {
|
||||||
A, B string
|
A, B string
|
||||||
|
|||||||
Reference in New Issue
Block a user