decode_map.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. package msgpack
  2. import (
  3. "errors"
  4. "fmt"
  5. "reflect"
  6. "github.com/vmihailenco/msgpack/v5/msgpcode"
  7. )
  8. var errArrayStruct = errors.New("msgpack: number of fields in array-encoded struct has changed")
  9. var (
  10. mapStringStringPtrType = reflect.TypeOf((*map[string]string)(nil))
  11. mapStringStringType = mapStringStringPtrType.Elem()
  12. )
  13. var (
  14. mapStringInterfacePtrType = reflect.TypeOf((*map[string]interface{})(nil))
  15. mapStringInterfaceType = mapStringInterfacePtrType.Elem()
  16. )
  17. func decodeMapValue(d *Decoder, v reflect.Value) error {
  18. n, err := d.DecodeMapLen()
  19. if err != nil {
  20. return err
  21. }
  22. typ := v.Type()
  23. if n == -1 {
  24. v.Set(reflect.Zero(typ))
  25. return nil
  26. }
  27. if v.IsNil() {
  28. v.Set(reflect.MakeMap(typ))
  29. }
  30. if n == 0 {
  31. return nil
  32. }
  33. return d.decodeTypedMapValue(v, n)
  34. }
  35. func (d *Decoder) decodeMapDefault() (interface{}, error) {
  36. if d.mapDecoder != nil {
  37. return d.mapDecoder(d)
  38. }
  39. return d.DecodeMap()
  40. }
  41. // DecodeMapLen decodes map length. Length is -1 when map is nil.
  42. func (d *Decoder) DecodeMapLen() (int, error) {
  43. c, err := d.readCode()
  44. if err != nil {
  45. return 0, err
  46. }
  47. if msgpcode.IsExt(c) {
  48. if err = d.skipExtHeader(c); err != nil {
  49. return 0, err
  50. }
  51. c, err = d.readCode()
  52. if err != nil {
  53. return 0, err
  54. }
  55. }
  56. return d.mapLen(c)
  57. }
  58. func (d *Decoder) mapLen(c byte) (int, error) {
  59. if c == msgpcode.Nil {
  60. return -1, nil
  61. }
  62. if c >= msgpcode.FixedMapLow && c <= msgpcode.FixedMapHigh {
  63. return int(c & msgpcode.FixedMapMask), nil
  64. }
  65. if c == msgpcode.Map16 {
  66. size, err := d.uint16()
  67. return int(size), err
  68. }
  69. if c == msgpcode.Map32 {
  70. size, err := d.uint32()
  71. return int(size), err
  72. }
  73. return 0, unexpectedCodeError{code: c, hint: "map length"}
  74. }
  75. func decodeMapStringStringValue(d *Decoder, v reflect.Value) error {
  76. mptr := v.Addr().Convert(mapStringStringPtrType).Interface().(*map[string]string)
  77. return d.decodeMapStringStringPtr(mptr)
  78. }
  79. func (d *Decoder) decodeMapStringStringPtr(ptr *map[string]string) error {
  80. size, err := d.DecodeMapLen()
  81. if err != nil {
  82. return err
  83. }
  84. if size == -1 {
  85. *ptr = nil
  86. return nil
  87. }
  88. m := *ptr
  89. if m == nil {
  90. *ptr = make(map[string]string, min(size, maxMapSize))
  91. m = *ptr
  92. }
  93. for i := 0; i < size; i++ {
  94. mk, err := d.DecodeString()
  95. if err != nil {
  96. return err
  97. }
  98. mv, err := d.DecodeString()
  99. if err != nil {
  100. return err
  101. }
  102. m[mk] = mv
  103. }
  104. return nil
  105. }
  106. func decodeMapStringInterfaceValue(d *Decoder, v reflect.Value) error {
  107. ptr := v.Addr().Convert(mapStringInterfacePtrType).Interface().(*map[string]interface{})
  108. return d.decodeMapStringInterfacePtr(ptr)
  109. }
  110. func (d *Decoder) decodeMapStringInterfacePtr(ptr *map[string]interface{}) error {
  111. m, err := d.DecodeMap()
  112. if err != nil {
  113. return err
  114. }
  115. *ptr = m
  116. return nil
  117. }
  118. func (d *Decoder) DecodeMap() (map[string]interface{}, error) {
  119. n, err := d.DecodeMapLen()
  120. if err != nil {
  121. return nil, err
  122. }
  123. if n == -1 {
  124. return nil, nil
  125. }
  126. m := make(map[string]interface{}, min(n, maxMapSize))
  127. for i := 0; i < n; i++ {
  128. mk, err := d.DecodeString()
  129. if err != nil {
  130. return nil, err
  131. }
  132. mv, err := d.decodeInterfaceCond()
  133. if err != nil {
  134. return nil, err
  135. }
  136. m[mk] = mv
  137. }
  138. return m, nil
  139. }
  140. func (d *Decoder) DecodeUntypedMap() (map[interface{}]interface{}, error) {
  141. n, err := d.DecodeMapLen()
  142. if err != nil {
  143. return nil, err
  144. }
  145. if n == -1 {
  146. return nil, nil
  147. }
  148. m := make(map[interface{}]interface{}, min(n, maxMapSize))
  149. for i := 0; i < n; i++ {
  150. mk, err := d.decodeInterfaceCond()
  151. if err != nil {
  152. return nil, err
  153. }
  154. mv, err := d.decodeInterfaceCond()
  155. if err != nil {
  156. return nil, err
  157. }
  158. m[mk] = mv
  159. }
  160. return m, nil
  161. }
  162. // DecodeTypedMap decodes a typed map. Typed map is a map that has a fixed type for keys and values.
  163. // Key and value types may be different.
  164. func (d *Decoder) DecodeTypedMap() (interface{}, error) {
  165. n, err := d.DecodeMapLen()
  166. if err != nil {
  167. return nil, err
  168. }
  169. if n <= 0 {
  170. return nil, nil
  171. }
  172. key, err := d.decodeInterfaceCond()
  173. if err != nil {
  174. return nil, err
  175. }
  176. value, err := d.decodeInterfaceCond()
  177. if err != nil {
  178. return nil, err
  179. }
  180. keyType := reflect.TypeOf(key)
  181. valueType := reflect.TypeOf(value)
  182. if !keyType.Comparable() {
  183. return nil, fmt.Errorf("msgpack: unsupported map key: %s", keyType.String())
  184. }
  185. mapType := reflect.MapOf(keyType, valueType)
  186. mapValue := reflect.MakeMap(mapType)
  187. mapValue.SetMapIndex(reflect.ValueOf(key), reflect.ValueOf(value))
  188. n--
  189. if err := d.decodeTypedMapValue(mapValue, n); err != nil {
  190. return nil, err
  191. }
  192. return mapValue.Interface(), nil
  193. }
  194. func (d *Decoder) decodeTypedMapValue(v reflect.Value, n int) error {
  195. typ := v.Type()
  196. keyType := typ.Key()
  197. valueType := typ.Elem()
  198. for i := 0; i < n; i++ {
  199. mk := reflect.New(keyType).Elem()
  200. if err := d.DecodeValue(mk); err != nil {
  201. return err
  202. }
  203. mv := reflect.New(valueType).Elem()
  204. if err := d.DecodeValue(mv); err != nil {
  205. return err
  206. }
  207. v.SetMapIndex(mk, mv)
  208. }
  209. return nil
  210. }
  211. func (d *Decoder) skipMap(c byte) error {
  212. n, err := d.mapLen(c)
  213. if err != nil {
  214. return err
  215. }
  216. for i := 0; i < n; i++ {
  217. if err := d.Skip(); err != nil {
  218. return err
  219. }
  220. if err := d.Skip(); err != nil {
  221. return err
  222. }
  223. }
  224. return nil
  225. }
  226. func decodeStructValue(d *Decoder, v reflect.Value) error {
  227. c, err := d.readCode()
  228. if err != nil {
  229. return err
  230. }
  231. n, err := d.mapLen(c)
  232. if err == nil {
  233. return d.decodeStruct(v, n)
  234. }
  235. var err2 error
  236. n, err2 = d.arrayLen(c)
  237. if err2 != nil {
  238. return err
  239. }
  240. if n <= 0 {
  241. v.Set(reflect.Zero(v.Type()))
  242. return nil
  243. }
  244. fields := structs.Fields(v.Type(), d.structTag)
  245. if n != len(fields.List) {
  246. return errArrayStruct
  247. }
  248. for _, f := range fields.List {
  249. if err := f.DecodeValue(d, v); err != nil {
  250. return err
  251. }
  252. }
  253. return nil
  254. }
  255. func (d *Decoder) decodeStruct(v reflect.Value, n int) error {
  256. if n == -1 {
  257. v.Set(reflect.Zero(v.Type()))
  258. return nil
  259. }
  260. fields := structs.Fields(v.Type(), d.structTag)
  261. for i := 0; i < n; i++ {
  262. name, err := d.decodeStringTemp()
  263. if err != nil {
  264. return err
  265. }
  266. if f := fields.Map[name]; f != nil {
  267. if err := f.DecodeValue(d, v); err != nil {
  268. return err
  269. }
  270. continue
  271. }
  272. if d.flags&disallowUnknownFieldsFlag != 0 {
  273. return fmt.Errorf("msgpack: unknown field %q", name)
  274. }
  275. if err := d.Skip(); err != nil {
  276. return err
  277. }
  278. }
  279. return nil
  280. }