You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

276 lines
5.5 KiB

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