struct_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. package test
  2. import (
  3. "time"
  4. "encoding/json"
  5. "bytes"
  6. )
  7. func init() {
  8. var pString = func(val string) *string {
  9. return &val
  10. }
  11. unmarshalCases = append(unmarshalCases, unmarshalCase{
  12. ptr: (*struct {
  13. Field interface{}
  14. })(nil),
  15. input: `{"Field": "hello"}`,
  16. }, unmarshalCase{
  17. ptr: (*struct {
  18. Field int `json:"field"`
  19. })(nil),
  20. input: `{"field": null}`,
  21. }, unmarshalCase{
  22. ptr: (*struct {
  23. ID int `json:"id"`
  24. Payload map[string]interface{} `json:"payload"`
  25. buf *bytes.Buffer
  26. })(nil),
  27. input: ` {"id":1, "payload":{"account":"123","password":"456"}}`,
  28. }, unmarshalCase{
  29. ptr: (*struct {
  30. Field1 string
  31. })(nil),
  32. input: `{"Field\"1":"hello"}`,
  33. }, unmarshalCase{
  34. ptr: (*struct {
  35. Field1 string
  36. })(nil),
  37. input: `{"\u0046ield1":"hello"}`,
  38. }, unmarshalCase{
  39. ptr: (*struct {
  40. Field1 *string
  41. Field2 *string
  42. })(nil),
  43. input: `{"field1": null, "field2": "world"}`,
  44. }, unmarshalCase{
  45. ptr: (*struct {
  46. Field1 string
  47. Field2 json.RawMessage
  48. })(nil),
  49. input: `{"field1": "hello", "field2":[1,2,3]}`,
  50. }, unmarshalCase{
  51. ptr: (*struct {
  52. a int
  53. b <-chan int
  54. C int
  55. d *time.Timer
  56. })(nil),
  57. input: `{"a": 444, "b":"bad", "C":256, "d":{"not":"a timer"}}`,
  58. })
  59. marshalCases = append(marshalCases,
  60. struct {
  61. Field map[string]interface{}
  62. }{
  63. map[string]interface{}{"hello": "world"},
  64. },
  65. struct {
  66. Field map[string]interface{}
  67. Field2 string
  68. }{
  69. map[string]interface{}{"hello": "world"}, "",
  70. },
  71. struct {
  72. Field interface{}
  73. }{
  74. 1024,
  75. },
  76. struct {
  77. Field MyInterface
  78. }{
  79. MyString("hello"),
  80. },
  81. struct {
  82. F *float64
  83. }{},
  84. // TODO: fix this
  85. //struct {
  86. // *time.Time
  87. //}{},
  88. struct {
  89. *time.Time
  90. }{&time.Time{}},
  91. struct {
  92. *StructVarious
  93. }{&StructVarious{}},
  94. struct {
  95. *StructVarious
  96. }{},
  97. struct {
  98. Field1 int
  99. Field2 [1]*float64
  100. }{},
  101. struct {
  102. Field interface{} `json:"field,omitempty"`
  103. }{},
  104. struct {
  105. Field MyInterface `json:"field,omitempty"`
  106. }{},
  107. struct {
  108. Field MyInterface `json:"field,omitempty"`
  109. }{MyString("hello")},
  110. struct {
  111. Field json.Marshaler `json:"field"`
  112. }{},
  113. struct {
  114. Field MyInterface `json:"field"`
  115. }{},
  116. struct {
  117. Field MyInterface `json:"field"`
  118. }{MyString("hello")},
  119. struct {
  120. Field1 string `json:"field-1,omitempty"`
  121. Field2 func() `json:"-"`
  122. }{},
  123. structRecursive{},
  124. struct {
  125. *CacheItem
  126. // Omit bad keys
  127. OmitMaxAge omit `json:"cacheAge,omitempty"`
  128. // Add nice keys
  129. MaxAge int `json:"max_age"`
  130. }{
  131. CacheItem: &CacheItem{
  132. Key: "value",
  133. MaxAge: 100,
  134. },
  135. MaxAge: 20,
  136. },
  137. structOrder{},
  138. struct {
  139. Field1 *string
  140. Field2 *string
  141. }{Field2: pString("world")},
  142. struct {
  143. a int
  144. b <-chan int
  145. C int
  146. d *time.Timer
  147. }{
  148. a: 42,
  149. b: make(<-chan int, 10),
  150. C: 21,
  151. d: time.NewTimer(10 * time.Second),
  152. },
  153. )
  154. }
  155. type StructVarious struct {
  156. Field0 string
  157. Field1 []string
  158. Field2 map[string]interface{}
  159. }
  160. type structRecursive struct {
  161. Field1 string
  162. Me *structRecursive
  163. }
  164. type omit *struct{}
  165. type CacheItem struct {
  166. Key string `json:"key"`
  167. MaxAge int `json:"cacheAge"`
  168. }
  169. type orderA struct {
  170. Field2 string
  171. }
  172. type orderC struct {
  173. Field5 string
  174. }
  175. type orderB struct {
  176. Field4 string
  177. orderC
  178. Field6 string
  179. }
  180. type structOrder struct {
  181. Field1 string
  182. orderA
  183. Field3 string
  184. orderB
  185. Field7 string
  186. }