struct_test.go 3.4 KB

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