struct_test.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. })
  95. marshalCases = append(marshalCases,
  96. struct {
  97. Field map[string]interface{}
  98. }{
  99. map[string]interface{}{"hello": "world"},
  100. },
  101. struct {
  102. Field map[string]interface{}
  103. Field2 string
  104. }{
  105. map[string]interface{}{"hello": "world"}, "",
  106. },
  107. struct {
  108. Field interface{}
  109. }{
  110. 1024,
  111. },
  112. struct {
  113. Field MyInterface
  114. }{
  115. MyString("hello"),
  116. },
  117. struct {
  118. F *float64
  119. }{},
  120. struct {
  121. *time.Time
  122. }{&epoch},
  123. struct {
  124. *StructVarious
  125. }{&StructVarious{}},
  126. struct {
  127. *StructVarious
  128. Field int
  129. }{nil, 10},
  130. struct {
  131. Field1 int
  132. Field2 [1]*float64
  133. }{},
  134. struct {
  135. Field interface{} `json:"field,omitempty"`
  136. }{},
  137. struct {
  138. Field MyInterface `json:"field,omitempty"`
  139. }{},
  140. struct {
  141. Field MyInterface `json:"field,omitempty"`
  142. }{MyString("hello")},
  143. struct {
  144. Field json.Marshaler `json:"field"`
  145. }{},
  146. struct {
  147. Field MyInterface `json:"field"`
  148. }{},
  149. struct {
  150. Field MyInterface `json:"field"`
  151. }{MyString("hello")},
  152. struct {
  153. Field1 string `json:"field-1,omitempty"`
  154. Field2 func() `json:"-"`
  155. }{},
  156. structRecursive{},
  157. struct {
  158. *CacheItem
  159. // Omit bad keys
  160. OmitMaxAge omit `json:"cacheAge,omitempty"`
  161. // Add nice keys
  162. MaxAge int `json:"max_age"`
  163. }{
  164. CacheItem: &CacheItem{
  165. Key: "value",
  166. MaxAge: 100,
  167. },
  168. MaxAge: 20,
  169. },
  170. structOrder{},
  171. struct {
  172. Field1 *string
  173. Field2 *string
  174. }{Field2: pString("world")},
  175. struct {
  176. a int
  177. b <-chan int
  178. C int
  179. d *time.Timer
  180. }{
  181. a: 42,
  182. b: make(<-chan int, 10),
  183. C: 21,
  184. d: time.NewTimer(10 * time.Second),
  185. },
  186. )
  187. }
  188. type StructVarious struct {
  189. Field0 string
  190. Field1 []string
  191. Field2 map[string]interface{}
  192. }
  193. type structRecursive struct {
  194. Field1 string
  195. Me *structRecursive
  196. }
  197. type omit *struct{}
  198. type CacheItem struct {
  199. Key string `json:"key"`
  200. MaxAge int `json:"cacheAge"`
  201. }
  202. type orderA struct {
  203. Field2 string
  204. }
  205. type orderC struct {
  206. Field5 string
  207. }
  208. type orderB struct {
  209. Field4 string
  210. orderC
  211. Field6 string
  212. }
  213. type structOrder struct {
  214. Field1 string
  215. orderA
  216. Field3 string
  217. orderB
  218. Field7 string
  219. }