struct_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. // TODO: fix me
  124. //structRecursive{},
  125. struct {
  126. *CacheItem
  127. // Omit bad keys
  128. OmitMaxAge omit `json:"cacheAge,omitempty"`
  129. // Add nice keys
  130. MaxAge int `json:"max_age"`
  131. }{
  132. CacheItem: &CacheItem{
  133. Key: "value",
  134. MaxAge: 100,
  135. },
  136. MaxAge: 20,
  137. },
  138. structOrder{},
  139. struct {
  140. Field1 *string
  141. Field2 *string
  142. }{Field2: pString("world")},
  143. struct {
  144. a int
  145. b <-chan int
  146. C int
  147. d *time.Timer
  148. }{
  149. a: 42,
  150. b: make(<-chan int, 10),
  151. C: 21,
  152. d: time.NewTimer(10 * time.Second),
  153. },
  154. )
  155. }
  156. type StructVarious struct {
  157. Field0 string
  158. Field1 []string
  159. Field2 map[string]interface{}
  160. }
  161. type structRecursive struct {
  162. Field1 string
  163. Me *structRecursive
  164. }
  165. type omit *struct{}
  166. type CacheItem struct {
  167. Key string `json:"key"`
  168. MaxAge int `json:"cacheAge"`
  169. }
  170. type orderA struct {
  171. Field2 string
  172. }
  173. type orderC struct {
  174. Field5 string
  175. }
  176. type orderB struct {
  177. Field4 string
  178. orderC
  179. Field6 string
  180. }
  181. type structOrder struct {
  182. Field1 string
  183. orderA
  184. Field3 string
  185. orderB
  186. Field7 string
  187. }