reflect: handle Convert'ing between identical underlying types
Needed for go-jose/v3
Этот коммит содержится в:
родитель
6eda52a289
коммит
60bb832c89
2 изменённых файлов: 16 добавлений и 1 удалений
|
@ -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
|
||||
|
||||
|
|
|
@ -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 {
|
||||
|
|
Загрузка…
Создание таблицы
Сослаться в новой задаче