aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorDamian Gryski <[email protected]>2023-03-18 09:41:19 -0700
committerRon Evans <[email protected]>2023-03-25 22:32:29 +0100
commitbedd27b20e84d3db0941f829125d210c16e7fefc (patch)
treecdcb96b98cfa2a8ba0d31323c25bbb579c2adab8
parent3612b7749e88d8b835d963418e6dee9f5c0536e6 (diff)
downloadtinygo-bedd27b20e84d3db0941f829125d210c16e7fefc.tar.gz
tinygo-bedd27b20e84d3db0941f829125d210c16e7fefc.zip
reflect: handle map-keys-as-interfaces for MapIter()
-rw-r--r--src/reflect/value.go17
1 files changed, 14 insertions, 3 deletions
diff --git a/src/reflect/value.go b/src/reflect/value.go
index a5f6782f7..025d8618a 100644
--- a/src/reflect/value.go
+++ b/src/reflect/value.go
@@ -846,9 +846,13 @@ func (v Value) MapRange() *MapIter {
panic(&ValueError{Method: "MapRange", Kind: v.Kind()})
}
+ keyType := v.typecode.Key().(*rawType)
+ isKeyStoredAsInterface := keyType.Kind() != String && !keyType.isBinary()
+
return &MapIter{
- m: v,
- it: hashmapNewIterator(),
+ m: v,
+ it: hashmapNewIterator(),
+ keyInterface: isKeyStoredAsInterface,
}
}
@@ -858,7 +862,8 @@ type MapIter struct {
key Value
val Value
- valid bool
+ valid bool
+ keyInterface bool
}
func (it *MapIter) Key() Value {
@@ -866,6 +871,12 @@ func (it *MapIter) Key() Value {
panic("reflect.MapIter.Key called on invalid iterator")
}
+ if it.keyInterface {
+ intf := *(*interface{})(it.key.value)
+ v := ValueOf(intf)
+ return v
+ }
+
return it.key.Elem()
}