struct_test.go 3.9 KB

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