aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/reflect
diff options
context:
space:
mode:
authorDamian Gryski <[email protected]>2023-04-01 09:39:25 -0700
committerAyke <[email protected]>2023-04-01 22:46:46 +0200
commit60bb832c894c4e62b22dd126c9bd5dc776443c05 (patch)
treec0b1c97583aa7b44b0c4473841e4d935faa8eb3e /src/reflect
parent6eda52a289839c977c78bb84e32ea7bda9b7ae52 (diff)
downloadtinygo-60bb832c894c4e62b22dd126c9bd5dc776443c05.tar.gz
tinygo-60bb832c894c4e62b22dd126c9bd5dc776443c05.zip
reflect: handle Convert'ing between identical underlying types
Needed for go-jose/v3
Diffstat (limited to 'src/reflect')
-rw-r--r--src/reflect/value.go11
-rw-r--r--src/reflect/value_test.go6
2 files changed, 16 insertions, 1 deletions
diff --git a/src/reflect/value.go b/src/reflect/value.go
index dd2691bee..51c69cd5f 100644
--- a/src/reflect/value.go
+++ b/src/reflect/value.go
@@ -1224,6 +1224,16 @@ func (v Value) Convert(t Type) Value {
}
func convertOp(src Value, typ Type) (Value, bool) {
+
+ // Easy check first. Do we even need to do anything?
+ if src.typecode.underlying() == typ.(*rawType).underlying() {
+ return Value{
+ typecode: typ.(*rawType),
+ value: src.value,
+ flags: src.flags,
+ }, true
+ }
+
switch src.Kind() {
case Int, Int8, Int16, Int32, Int64:
switch rtype := typ.(*rawType); rtype.Kind() {
@@ -1289,7 +1299,6 @@ func convertOp(src Value, typ Type) (Value, bool) {
// TODO(dgryski): Unimplemented:
// Chan
- // Identical underlying types
// Non-defined pointers types with same underlying base type
// Interface <-> Type conversions
diff --git a/src/reflect/value_test.go b/src/reflect/value_test.go
index 64b1cf0ef..b6c177050 100644
--- a/src/reflect/value_test.go
+++ b/src/reflect/value_test.go
@@ -587,6 +587,12 @@ func TestConvert(t *testing.T) {
t.Errorf("Convert(string -> []byte")
}
+ type namedString string
+
+ c = v.Convert(TypeOf(namedString("")))
+ if c.Type().Kind() != String || c.Type().Name() != "namedString" {
+ t.Errorf("Convert(string -> namedString")
+ }
}
func equal[T comparable](a, b []T) bool {