struct_test.go 4.2 KB

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