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.

195 lines
3.4 KiB

  1. package msgpack_test
  2. import (
  3. "bytes"
  4. "fmt"
  5. "github.com/vmihailenco/msgpack"
  6. )
  7. func ExampleMarshal() {
  8. type Item struct {
  9. Foo string
  10. }
  11. b, err := msgpack.Marshal(&Item{Foo: "bar"})
  12. if err != nil {
  13. panic(err)
  14. }
  15. var item Item
  16. err = msgpack.Unmarshal(b, &item)
  17. if err != nil {
  18. panic(err)
  19. }
  20. fmt.Println(item.Foo)
  21. // Output: bar
  22. }
  23. func ExampleMarshal_mapStringInterface() {
  24. in := map[string]interface{}{"foo": 1, "hello": "world"}
  25. b, err := msgpack.Marshal(in)
  26. if err != nil {
  27. panic(err)
  28. }
  29. var out map[string]interface{}
  30. err = msgpack.Unmarshal(b, &out)
  31. if err != nil {
  32. panic(err)
  33. }
  34. fmt.Println("foo =", out["foo"])
  35. fmt.Println("hello =", out["hello"])
  36. // Output:
  37. // foo = 1
  38. // hello = world
  39. }
  40. func ExampleDecoder_SetDecodeMapFunc() {
  41. buf := new(bytes.Buffer)
  42. enc := msgpack.NewEncoder(buf)
  43. in := map[string]string{"hello": "world"}
  44. err := enc.Encode(in)
  45. if err != nil {
  46. panic(err)
  47. }
  48. dec := msgpack.NewDecoder(buf)
  49. dec.SetDecodeMapFunc(func(d *msgpack.Decoder) (interface{}, error) {
  50. n, err := d.DecodeMapLen()
  51. if err != nil {
  52. return nil, err
  53. }
  54. m := make(map[string]string, n)
  55. for i := 0; i < n; i++ {
  56. mk, err := d.DecodeString()
  57. if err != nil {
  58. return nil, err
  59. }
  60. mv, err := d.DecodeString()
  61. if err != nil {
  62. return nil, err
  63. }
  64. m[mk] = mv
  65. }
  66. return m, nil
  67. })
  68. out, err := dec.DecodeInterface()
  69. if err != nil {
  70. panic(err)
  71. }
  72. fmt.Println(out)
  73. // Output: map[hello:world]
  74. }
  75. func ExampleDecoder_Query() {
  76. b, err := msgpack.Marshal([]map[string]interface{}{
  77. {"id": 1, "attrs": map[string]interface{}{"phone": 12345}},
  78. {"id": 2, "attrs": map[string]interface{}{"phone": 54321}},
  79. })
  80. if err != nil {
  81. panic(err)
  82. }
  83. dec := msgpack.NewDecoder(bytes.NewBuffer(b))
  84. values, err := dec.Query("*.attrs.phone")
  85. if err != nil {
  86. panic(err)
  87. }
  88. fmt.Println("phones are", values)
  89. dec.Reset(bytes.NewBuffer(b))
  90. values, err = dec.Query("1.attrs.phone")
  91. if err != nil {
  92. panic(err)
  93. }
  94. fmt.Println("2nd phone is", values[0])
  95. // Output: phones are [12345 54321]
  96. // 2nd phone is 54321
  97. }
  98. func ExampleEncoder_StructAsArray() {
  99. type Item struct {
  100. Foo string
  101. Bar string
  102. }
  103. var buf bytes.Buffer
  104. enc := msgpack.NewEncoder(&buf).StructAsArray(true)
  105. err := enc.Encode(&Item{Foo: "foo", Bar: "bar"})
  106. if err != nil {
  107. panic(err)
  108. }
  109. dec := msgpack.NewDecoder(&buf)
  110. v, err := dec.DecodeInterface()
  111. if err != nil {
  112. panic(err)
  113. }
  114. fmt.Println(v)
  115. // Output: [foo bar]
  116. }
  117. func ExampleMarshal_asArray() {
  118. type Item struct {
  119. _msgpack struct{} `msgpack:",asArray"`
  120. Foo string
  121. Bar string
  122. }
  123. var buf bytes.Buffer
  124. enc := msgpack.NewEncoder(&buf)
  125. err := enc.Encode(&Item{Foo: "foo", Bar: "bar"})
  126. if err != nil {
  127. panic(err)
  128. }
  129. dec := msgpack.NewDecoder(&buf)
  130. v, err := dec.DecodeInterface()
  131. if err != nil {
  132. panic(err)
  133. }
  134. fmt.Println(v)
  135. // Output: [foo bar]
  136. }
  137. func ExampleMarshal_omitEmpty() {
  138. type Item struct {
  139. Foo string
  140. Bar string
  141. }
  142. item := &Item{
  143. Foo: "hello",
  144. }
  145. b, err := msgpack.Marshal(item)
  146. if err != nil {
  147. panic(err)
  148. }
  149. fmt.Printf("item: %q\n", b)
  150. type ItemOmitEmpty struct {
  151. _msgpack struct{} `msgpack:",omitempty"`
  152. Foo string
  153. Bar string
  154. }
  155. itemOmitEmpty := &ItemOmitEmpty{
  156. Foo: "hello",
  157. }
  158. b, err = msgpack.Marshal(itemOmitEmpty)
  159. if err != nil {
  160. panic(err)
  161. }
  162. fmt.Printf("item2: %q\n", b)
  163. // Output: item: "\x82\xa3Foo\xa5hello\xa3Bar\xa0"
  164. // item2: "\x81\xa3Foo\xa5hello"
  165. }