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