@@ -637,20 +637,29 @@ func (d *Decoder) convertValue(v reflect.Value, typ reflect.Type, src ast.Node)
637637 return v .Convert (typ ), nil
638638 }
639639 // cast value to string
640+ var strVal string
640641 switch v .Type ().Kind () {
641642 case reflect .Int , reflect .Int8 , reflect .Int16 , reflect .Int32 , reflect .Int64 :
642- return reflect . ValueOf ( strconv .FormatInt (v .Int (), 10 )), nil
643+ strVal = strconv .FormatInt (v .Int (), 10 )
643644 case reflect .Float32 , reflect .Float64 :
644- return reflect . ValueOf ( fmt .Sprint (v .Float ())), nil
645+ strVal = fmt .Sprint (v .Float ())
645646 case reflect .Uint , reflect .Uint8 , reflect .Uint16 , reflect .Uint32 , reflect .Uint64 , reflect .Uintptr :
646- return reflect . ValueOf ( strconv .FormatUint (v .Uint (), 10 )), nil
647+ strVal = strconv .FormatUint (v .Uint (), 10 )
647648 case reflect .Bool :
648- return reflect .ValueOf (strconv .FormatBool (v .Bool ())), nil
649+ strVal = strconv .FormatBool (v .Bool ())
650+ default :
651+ if ! v .Type ().ConvertibleTo (typ ) {
652+ return reflect .Zero (typ ), errors .ErrTypeMismatch (typ , v .Type (), src .GetToken ())
653+ }
654+ return v .Convert (typ ), nil
649655 }
650- if ! v .Type ().ConvertibleTo (typ ) {
651- return reflect .Zero (typ ), errors .ErrTypeMismatch (typ , v .Type (), src .GetToken ())
656+
657+ val := reflect .ValueOf (strVal )
658+ if val .Type () != typ {
659+ // Handle named types, e.g., `type MyString string`
660+ val = val .Convert (typ )
652661 }
653- return v . Convert ( typ ) , nil
662+ return val , nil
654663}
655664
656665func (d * Decoder ) deleteStructKeys (structType reflect.Type , unknownFields map [string ]ast.Node ) error {
0 commit comments