reflect: handle Convert'ing between identical underlying types

Needed for go-jose/v3
Этот коммит содержится в:
Damian Gryski 2023-04-01 09:39:25 -07:00 коммит произвёл Ayke
родитель 6eda52a289
коммит 60bb832c89
2 изменённых файлов: 16 добавлений и 1 удалений

Просмотреть файл

@ -1224,6 +1224,16 @@ func (v Value) Convert(t Type) Value {
} }
func convertOp(src Value, typ Type) (Value, bool) { 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() { switch src.Kind() {
case Int, Int8, Int16, Int32, Int64: case Int, Int8, Int16, Int32, Int64:
switch rtype := typ.(*rawType); rtype.Kind() { switch rtype := typ.(*rawType); rtype.Kind() {
@ -1289,7 +1299,6 @@ func convertOp(src Value, typ Type) (Value, bool) {
// TODO(dgryski): Unimplemented: // TODO(dgryski): Unimplemented:
// Chan // Chan
// Identical underlying types
// Non-defined pointers types with same underlying base type // Non-defined pointers types with same underlying base type
// Interface <-> Type conversions // Interface <-> Type conversions

Просмотреть файл

@ -587,6 +587,12 @@ func TestConvert(t *testing.T) {
t.Errorf("Convert(string -> []byte") 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 { func equal[T comparable](a, b []T) bool {