decode_value.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. package msgpack
  2. import (
  3. "encoding"
  4. "errors"
  5. "fmt"
  6. "reflect"
  7. )
  8. var (
  9. interfaceType = reflect.TypeOf((*interface{})(nil)).Elem()
  10. stringType = reflect.TypeOf((*string)(nil)).Elem()
  11. )
  12. var valueDecoders []decoderFunc
  13. //nolint:gochecknoinits
  14. func init() {
  15. valueDecoders = []decoderFunc{
  16. reflect.Bool: decodeBoolValue,
  17. reflect.Int: decodeInt64Value,
  18. reflect.Int8: decodeInt64Value,
  19. reflect.Int16: decodeInt64Value,
  20. reflect.Int32: decodeInt64Value,
  21. reflect.Int64: decodeInt64Value,
  22. reflect.Uint: decodeUint64Value,
  23. reflect.Uint8: decodeUint64Value,
  24. reflect.Uint16: decodeUint64Value,
  25. reflect.Uint32: decodeUint64Value,
  26. reflect.Uint64: decodeUint64Value,
  27. reflect.Float32: decodeFloat32Value,
  28. reflect.Float64: decodeFloat64Value,
  29. reflect.Complex64: decodeUnsupportedValue,
  30. reflect.Complex128: decodeUnsupportedValue,
  31. reflect.Array: decodeArrayValue,
  32. reflect.Chan: decodeUnsupportedValue,
  33. reflect.Func: decodeUnsupportedValue,
  34. reflect.Interface: decodeInterfaceValue,
  35. reflect.Map: decodeMapValue,
  36. reflect.Ptr: decodeUnsupportedValue,
  37. reflect.Slice: decodeSliceValue,
  38. reflect.String: decodeStringValue,
  39. reflect.Struct: decodeStructValue,
  40. reflect.UnsafePointer: decodeUnsupportedValue,
  41. }
  42. }
  43. func getDecoder(typ reflect.Type) decoderFunc {
  44. if v, ok := typeDecMap.Load(typ); ok {
  45. return v.(decoderFunc)
  46. }
  47. fn := _getDecoder(typ)
  48. typeDecMap.Store(typ, fn)
  49. return fn
  50. }
  51. func _getDecoder(typ reflect.Type) decoderFunc {
  52. kind := typ.Kind()
  53. if kind == reflect.Ptr {
  54. if _, ok := typeDecMap.Load(typ.Elem()); ok {
  55. return ptrValueDecoder(typ)
  56. }
  57. }
  58. if typ.Implements(customDecoderType) {
  59. return nilAwareDecoder(typ, decodeCustomValue)
  60. }
  61. if typ.Implements(unmarshalerType) {
  62. return nilAwareDecoder(typ, unmarshalValue)
  63. }
  64. if typ.Implements(binaryUnmarshalerType) {
  65. return nilAwareDecoder(typ, unmarshalBinaryValue)
  66. }
  67. if typ.Implements(textUnmarshalerType) {
  68. return nilAwareDecoder(typ, unmarshalTextValue)
  69. }
  70. // Addressable struct field value.
  71. if kind != reflect.Ptr {
  72. ptr := reflect.PtrTo(typ)
  73. if ptr.Implements(customDecoderType) {
  74. return addrDecoder(nilAwareDecoder(typ, decodeCustomValue))
  75. }
  76. if ptr.Implements(unmarshalerType) {
  77. return addrDecoder(nilAwareDecoder(typ, unmarshalValue))
  78. }
  79. if ptr.Implements(binaryUnmarshalerType) {
  80. return addrDecoder(nilAwareDecoder(typ, unmarshalBinaryValue))
  81. }
  82. if ptr.Implements(textUnmarshalerType) {
  83. return addrDecoder(nilAwareDecoder(typ, unmarshalTextValue))
  84. }
  85. }
  86. switch kind {
  87. case reflect.Ptr:
  88. return ptrValueDecoder(typ)
  89. case reflect.Slice:
  90. elem := typ.Elem()
  91. if elem.Kind() == reflect.Uint8 {
  92. return decodeBytesValue
  93. }
  94. if elem == stringType {
  95. return decodeStringSliceValue
  96. }
  97. case reflect.Array:
  98. if typ.Elem().Kind() == reflect.Uint8 {
  99. return decodeByteArrayValue
  100. }
  101. case reflect.Map:
  102. if typ.Key() == stringType {
  103. switch typ.Elem() {
  104. case stringType:
  105. return decodeMapStringStringValue
  106. case interfaceType:
  107. return decodeMapStringInterfaceValue
  108. }
  109. }
  110. }
  111. return valueDecoders[kind]
  112. }
  113. func ptrValueDecoder(typ reflect.Type) decoderFunc {
  114. decoder := getDecoder(typ.Elem())
  115. return func(d *Decoder, v reflect.Value) error {
  116. if d.hasNilCode() {
  117. if !v.IsNil() {
  118. v.Set(reflect.Zero(v.Type()))
  119. }
  120. return d.DecodeNil()
  121. }
  122. if v.IsNil() {
  123. v.Set(reflect.New(v.Type().Elem()))
  124. }
  125. return decoder(d, v.Elem())
  126. }
  127. }
  128. func addrDecoder(fn decoderFunc) decoderFunc {
  129. return func(d *Decoder, v reflect.Value) error {
  130. if !v.CanAddr() {
  131. return fmt.Errorf("msgpack: Decode(nonaddressable %T)", v.Interface())
  132. }
  133. return fn(d, v.Addr())
  134. }
  135. }
  136. func nilAwareDecoder(typ reflect.Type, fn decoderFunc) decoderFunc {
  137. if nilable(typ.Kind()) {
  138. return func(d *Decoder, v reflect.Value) error {
  139. if d.hasNilCode() {
  140. return d.decodeNilValue(v)
  141. }
  142. if v.IsNil() {
  143. v.Set(reflect.New(v.Type().Elem()))
  144. }
  145. return fn(d, v)
  146. }
  147. }
  148. return func(d *Decoder, v reflect.Value) error {
  149. if d.hasNilCode() {
  150. return d.decodeNilValue(v)
  151. }
  152. return fn(d, v)
  153. }
  154. }
  155. func decodeBoolValue(d *Decoder, v reflect.Value) error {
  156. flag, err := d.DecodeBool()
  157. if err != nil {
  158. return err
  159. }
  160. v.SetBool(flag)
  161. return nil
  162. }
  163. func decodeInterfaceValue(d *Decoder, v reflect.Value) error {
  164. if v.IsNil() {
  165. return d.interfaceValue(v)
  166. }
  167. return d.DecodeValue(v.Elem())
  168. }
  169. func (d *Decoder) interfaceValue(v reflect.Value) error {
  170. vv, err := d.decodeInterfaceCond()
  171. if err != nil {
  172. return err
  173. }
  174. if vv != nil {
  175. if v.Type() == errorType {
  176. if vv, ok := vv.(string); ok {
  177. v.Set(reflect.ValueOf(errors.New(vv)))
  178. return nil
  179. }
  180. }
  181. v.Set(reflect.ValueOf(vv))
  182. }
  183. return nil
  184. }
  185. func decodeUnsupportedValue(d *Decoder, v reflect.Value) error {
  186. return fmt.Errorf("msgpack: Decode(unsupported %s)", v.Type())
  187. }
  188. //------------------------------------------------------------------------------
  189. func decodeCustomValue(d *Decoder, v reflect.Value) error {
  190. decoder := v.Interface().(CustomDecoder)
  191. return decoder.DecodeMsgpack(d)
  192. }
  193. func unmarshalValue(d *Decoder, v reflect.Value) error {
  194. var b []byte
  195. d.rec = make([]byte, 0, 64)
  196. if err := d.Skip(); err != nil {
  197. return err
  198. }
  199. b = d.rec
  200. d.rec = nil
  201. unmarshaler := v.Interface().(Unmarshaler)
  202. return unmarshaler.UnmarshalMsgpack(b)
  203. }
  204. func unmarshalBinaryValue(d *Decoder, v reflect.Value) error {
  205. data, err := d.DecodeBytes()
  206. if err != nil {
  207. return err
  208. }
  209. unmarshaler := v.Interface().(encoding.BinaryUnmarshaler)
  210. return unmarshaler.UnmarshalBinary(data)
  211. }
  212. func unmarshalTextValue(d *Decoder, v reflect.Value) error {
  213. data, err := d.DecodeBytes()
  214. if err != nil {
  215. return err
  216. }
  217. unmarshaler := v.Interface().(encoding.TextUnmarshaler)
  218. return unmarshaler.UnmarshalText(data)
  219. }