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.

166 lines
3.1 KiB

  1. package msgpack
  2. import (
  3. "reflect"
  4. "sort"
  5. "github.com/vmihailenco/msgpack/codes"
  6. )
  7. func encodeMapValue(e *Encoder, v reflect.Value) error {
  8. if v.IsNil() {
  9. return e.EncodeNil()
  10. }
  11. if err := e.EncodeMapLen(v.Len()); err != nil {
  12. return err
  13. }
  14. for _, key := range v.MapKeys() {
  15. if err := e.EncodeValue(key); err != nil {
  16. return err
  17. }
  18. if err := e.EncodeValue(v.MapIndex(key)); err != nil {
  19. return err
  20. }
  21. }
  22. return nil
  23. }
  24. func encodeMapStringStringValue(e *Encoder, v reflect.Value) error {
  25. if v.IsNil() {
  26. return e.EncodeNil()
  27. }
  28. if err := e.EncodeMapLen(v.Len()); err != nil {
  29. return err
  30. }
  31. m := v.Convert(mapStringStringType).Interface().(map[string]string)
  32. if e.sortMapKeys {
  33. return e.encodeSortedMapStringString(m)
  34. }
  35. for mk, mv := range m {
  36. if err := e.EncodeString(mk); err != nil {
  37. return err
  38. }
  39. if err := e.EncodeString(mv); err != nil {
  40. return err
  41. }
  42. }
  43. return nil
  44. }
  45. func encodeMapStringInterfaceValue(e *Encoder, v reflect.Value) error {
  46. if v.IsNil() {
  47. return e.EncodeNil()
  48. }
  49. if err := e.EncodeMapLen(v.Len()); err != nil {
  50. return err
  51. }
  52. m := v.Convert(mapStringInterfaceType).Interface().(map[string]interface{})
  53. if e.sortMapKeys {
  54. return e.encodeSortedMapStringInterface(m)
  55. }
  56. for mk, mv := range m {
  57. if err := e.EncodeString(mk); err != nil {
  58. return err
  59. }
  60. if err := e.Encode(mv); err != nil {
  61. return err
  62. }
  63. }
  64. return nil
  65. }
  66. func (e *Encoder) encodeSortedMapStringString(m map[string]string) error {
  67. keys := make([]string, 0, len(m))
  68. for k, _ := range m {
  69. keys = append(keys, k)
  70. }
  71. sort.Strings(keys)
  72. for _, k := range keys {
  73. err := e.EncodeString(k)
  74. if err != nil {
  75. return err
  76. }
  77. if err = e.EncodeString(m[k]); err != nil {
  78. return err
  79. }
  80. }
  81. return nil
  82. }
  83. func (e *Encoder) encodeSortedMapStringInterface(m map[string]interface{}) error {
  84. keys := make([]string, 0, len(m))
  85. for k, _ := range m {
  86. keys = append(keys, k)
  87. }
  88. sort.Strings(keys)
  89. for _, k := range keys {
  90. err := e.EncodeString(k)
  91. if err != nil {
  92. return err
  93. }
  94. if err = e.Encode(m[k]); err != nil {
  95. return err
  96. }
  97. }
  98. return nil
  99. }
  100. func (e *Encoder) EncodeMapLen(l int) error {
  101. if l < 16 {
  102. return e.writeCode(codes.FixedMapLow | codes.Code(l))
  103. }
  104. if l < 65536 {
  105. return e.write2(codes.Map16, uint64(l))
  106. }
  107. return e.write4(codes.Map32, uint32(l))
  108. }
  109. func encodeStructValue(e *Encoder, strct reflect.Value) error {
  110. structFields := structs.Fields(strct.Type())
  111. if e.structAsArray || structFields.asArray {
  112. return encodeStructValueAsArray(e, strct, structFields.List)
  113. }
  114. fields := structFields.OmitEmpty(strct)
  115. if err := e.EncodeMapLen(len(fields)); err != nil {
  116. return err
  117. }
  118. for _, f := range fields {
  119. if err := e.EncodeString(f.name); err != nil {
  120. return err
  121. }
  122. if err := f.EncodeValue(e, strct); err != nil {
  123. return err
  124. }
  125. }
  126. return nil
  127. }
  128. func encodeStructValueAsArray(e *Encoder, strct reflect.Value, fields []*field) error {
  129. if err := e.EncodeArrayLen(len(fields)); err != nil {
  130. return err
  131. }
  132. for _, f := range fields {
  133. if err := f.EncodeValue(e, strct); err != nil {
  134. return err
  135. }
  136. }
  137. return nil
  138. }