struct_test.go 3.7 KB

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