struct_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. })
  51. marshalCases = append(marshalCases,
  52. struct {
  53. Field map[string]interface{}
  54. }{
  55. map[string]interface{}{"hello": "world"},
  56. },
  57. struct {
  58. Field map[string]interface{}
  59. Field2 string
  60. }{
  61. map[string]interface{}{"hello": "world"}, "",
  62. },
  63. struct {
  64. Field interface{}
  65. }{
  66. 1024,
  67. },
  68. struct {
  69. Field MyInterface
  70. }{
  71. MyString("hello"),
  72. },
  73. struct {
  74. F *float64
  75. }{},
  76. // TODO: fix this
  77. //struct {
  78. // *time.Time
  79. //}{},
  80. struct {
  81. *time.Time
  82. }{&time.Time{}},
  83. struct {
  84. *StructVarious
  85. }{&StructVarious{}},
  86. struct {
  87. *StructVarious
  88. }{},
  89. struct {
  90. Field1 int
  91. Field2 [1]*float64
  92. }{},
  93. struct {
  94. Field interface{} `json:"field,omitempty"`
  95. }{},
  96. struct {
  97. Field MyInterface `json:"field,omitempty"`
  98. }{},
  99. struct {
  100. Field MyInterface `json:"field,omitempty"`
  101. }{MyString("hello")},
  102. struct {
  103. Field json.Marshaler `json:"field"`
  104. }{},
  105. struct {
  106. Field MyInterface `json:"field"`
  107. }{},
  108. struct {
  109. Field MyInterface `json:"field"`
  110. }{MyString("hello")},
  111. struct {
  112. Field1 string `json:"field-1,omitempty"`
  113. Field2 func() `json:"-"`
  114. }{},
  115. structRecursive{},
  116. struct {
  117. *CacheItem
  118. // Omit bad keys
  119. OmitMaxAge omit `json:"cacheAge,omitempty"`
  120. // Add nice keys
  121. MaxAge int `json:"max_age"`
  122. }{
  123. CacheItem: &CacheItem{
  124. Key: "value",
  125. MaxAge: 100,
  126. },
  127. MaxAge: 20,
  128. },
  129. structOrder{},
  130. struct {
  131. Field1 *string
  132. Field2 *string
  133. }{Field2: pString("world")},
  134. )
  135. }
  136. type StructVarious struct {
  137. Field0 string
  138. Field1 []string
  139. Field2 map[string]interface{}
  140. }
  141. type structRecursive struct {
  142. Field1 string
  143. Me *structRecursive
  144. }
  145. type omit *struct{}
  146. type CacheItem struct {
  147. Key string `json:"key"`
  148. MaxAge int `json:"cacheAge"`
  149. }
  150. type orderA struct {
  151. Field2 string
  152. }
  153. type orderC struct {
  154. Field5 string
  155. }
  156. type orderB struct {
  157. Field4 string
  158. orderC
  159. Field6 string
  160. }
  161. type structOrder struct {
  162. Field1 string
  163. orderA
  164. Field3 string
  165. orderB
  166. Field7 string
  167. }