struct_test.go 3.3 KB

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