struct_test.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. package test
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "time"
  6. )
  7. func init() {
  8. var pString = func(val string) *string {
  9. return &val
  10. }
  11. epoch := time.Unix(0, 0)
  12. unmarshalCases = append(unmarshalCases, unmarshalCase{
  13. ptr: (*struct {
  14. Field interface{}
  15. })(nil),
  16. input: `{"Field": "hello"}`,
  17. }, unmarshalCase{
  18. ptr: (*struct {
  19. Field interface{}
  20. })(nil),
  21. input: `{"Field": "hello"} `,
  22. }, unmarshalCase{
  23. ptr: (*struct {
  24. Field int `json:"field"`
  25. })(nil),
  26. input: `{"field": null}`,
  27. }, unmarshalCase{
  28. ptr: (*struct {
  29. Field int `json:"field,string"`
  30. })(nil),
  31. input: `{"field": null}`,
  32. }, unmarshalCase{
  33. ptr: (*struct {
  34. ID int `json:"id"`
  35. Payload map[string]interface{} `json:"payload"`
  36. buf *bytes.Buffer
  37. })(nil),
  38. input: ` {"id":1, "payload":{"account":"123","password":"456"}}`,
  39. }, unmarshalCase{
  40. ptr: (*struct {
  41. Field1 string
  42. })(nil),
  43. input: `{"Field\"1":"hello"}`,
  44. }, unmarshalCase{
  45. ptr: (*struct {
  46. Field1 string
  47. })(nil),
  48. input: `{"\u0046ield1":"hello"}`,
  49. }, unmarshalCase{
  50. ptr: (*struct {
  51. Field1 *string
  52. Field2 *string
  53. })(nil),
  54. input: `{"field1": null, "field2": "world"}`,
  55. }, unmarshalCase{
  56. ptr: (*struct {
  57. Field1 string
  58. Field2 json.RawMessage
  59. })(nil),
  60. input: `{"field1": "hello", "field2":[1,2,3]}`,
  61. }, unmarshalCase{
  62. ptr: (*struct {
  63. a int
  64. b <-chan int
  65. C int
  66. d *time.Timer
  67. })(nil),
  68. input: `{"a": 444, "b":"bad", "C":256, "d":{"not":"a timer"}}`,
  69. }, unmarshalCase{
  70. ptr: (*struct {
  71. A string
  72. B string
  73. C string
  74. D string
  75. E string
  76. F string
  77. G string
  78. H string
  79. I string
  80. J string
  81. K string
  82. })(nil),
  83. input: `{"a":"1","b":"2","c":"3","d":"4","e":"5","f":"6","g":"7","h":"8","i":"9","j":"10","k":"11"}`,
  84. }, unmarshalCase{
  85. ptr: (*struct {
  86. T float64 `json:"T"`
  87. })(nil),
  88. input: `{"t":10.0}`,
  89. }, unmarshalCase{
  90. ptr: (*struct {
  91. T float64 `json:"T"`
  92. })(nil),
  93. input: `{"T":10.0}`,
  94. }, unmarshalCase{
  95. ptr: (*struct {
  96. T float64 `json:"t"`
  97. })(nil),
  98. input: `{"T":10.0}`,
  99. }, unmarshalCase{
  100. ptr: (*struct {
  101. KeyString string `json:"key_string"`
  102. Type string `json:"type"`
  103. Asks [][2]float64 `json:"asks"`
  104. })(nil),
  105. input: `{"key_string": "KEYSTRING","type": "TYPE","asks": [[1e+66,1]]}`,
  106. })
  107. marshalCases = append(marshalCases,
  108. struct {
  109. Field map[string]interface{}
  110. }{
  111. map[string]interface{}{"hello": "world"},
  112. },
  113. struct {
  114. Field map[string]interface{}
  115. Field2 string
  116. }{
  117. map[string]interface{}{"hello": "world"}, "",
  118. },
  119. struct {
  120. Field interface{}
  121. }{
  122. 1024,
  123. },
  124. struct {
  125. Field MyInterface
  126. }{
  127. MyString("hello"),
  128. },
  129. struct {
  130. F *float64
  131. }{},
  132. struct {
  133. *time.Time
  134. }{&epoch},
  135. struct {
  136. *StructVarious
  137. }{&StructVarious{}},
  138. struct {
  139. *StructVarious
  140. Field int
  141. }{nil, 10},
  142. struct {
  143. Field1 int
  144. Field2 [1]*float64
  145. }{},
  146. struct {
  147. Field interface{} `json:"field,omitempty"`
  148. }{},
  149. struct {
  150. Field MyInterface `json:"field,omitempty"`
  151. }{},
  152. struct {
  153. Field MyInterface `json:"field,omitempty"`
  154. }{MyString("hello")},
  155. struct {
  156. Field json.Marshaler `json:"field"`
  157. }{},
  158. struct {
  159. Field MyInterface `json:"field"`
  160. }{},
  161. struct {
  162. Field MyInterface `json:"field"`
  163. }{MyString("hello")},
  164. struct {
  165. Field1 string `json:"field-1,omitempty"`
  166. Field2 func() `json:"-"`
  167. }{},
  168. structRecursive{},
  169. struct {
  170. *CacheItem
  171. // Omit bad keys
  172. OmitMaxAge omit `json:"cacheAge,omitempty"`
  173. // Add nice keys
  174. MaxAge int `json:"max_age"`
  175. }{
  176. CacheItem: &CacheItem{
  177. Key: "value",
  178. MaxAge: 100,
  179. },
  180. MaxAge: 20,
  181. },
  182. structOrder{},
  183. struct {
  184. Field1 *string
  185. Field2 *string
  186. }{Field2: pString("world")},
  187. struct {
  188. a int
  189. b <-chan int
  190. C int
  191. d *time.Timer
  192. }{
  193. a: 42,
  194. b: make(<-chan int, 10),
  195. C: 21,
  196. d: time.NewTimer(10 * time.Second),
  197. },
  198. struct {
  199. _UnderscoreField string
  200. }{
  201. "should not marshal",
  202. },
  203. )
  204. }
  205. type StructVarious struct {
  206. Field0 string
  207. Field1 []string
  208. Field2 map[string]interface{}
  209. }
  210. type structRecursive struct {
  211. Field1 string
  212. Me *structRecursive
  213. }
  214. type omit *struct{}
  215. type CacheItem struct {
  216. Key string `json:"key"`
  217. MaxAge int `json:"cacheAge"`
  218. }
  219. type orderA struct {
  220. Field2 string
  221. }
  222. type orderC struct {
  223. Field5 string
  224. }
  225. type orderB struct {
  226. Field4 string
  227. orderC
  228. Field6 string
  229. }
  230. type structOrder struct {
  231. Field1 string
  232. orderA
  233. Field3 string
  234. orderB
  235. Field7 string
  236. }