struct_test.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. package test
  2. import (
  3. "time"
  4. "encoding/json"
  5. "bytes"
  6. )
  7. func init() {
  8. unmarshalCases = append(unmarshalCases, unmarshalCase{
  9. ptr: (*struct {
  10. Field interface{}
  11. })(nil),
  12. input: `{"Field": "hello"}`,
  13. }, unmarshalCase{
  14. ptr: (*struct {
  15. Field int `json:"field"`
  16. })(nil),
  17. input: `{"field": null}`,
  18. }, unmarshalCase{
  19. ptr: (*struct {
  20. ID int `json:"id"`
  21. Payload map[string]interface{} `json:"payload"`
  22. buf *bytes.Buffer
  23. })(nil),
  24. input: ` {"id":1, "payload":{"account":"123","password":"456"}}`,
  25. }, unmarshalCase{
  26. ptr: (*struct {
  27. Field1 string
  28. })(nil),
  29. input: `{"Field\"1":"hello"}`,
  30. }, unmarshalCase{
  31. ptr: (*struct {
  32. Field1 string
  33. })(nil),
  34. input: `{"\u0046ield1":"hello"}`,
  35. })
  36. marshalCases = append(marshalCases,
  37. struct {
  38. Field map[string]interface{}
  39. }{
  40. map[string]interface{}{"hello": "world"},
  41. },
  42. struct {
  43. Field map[string]interface{}
  44. Field2 string
  45. }{
  46. map[string]interface{}{"hello": "world"}, "",
  47. },
  48. struct {
  49. Field interface{}
  50. }{
  51. 1024,
  52. },
  53. struct {
  54. Field MyInterface
  55. }{
  56. MyString("hello"),
  57. },
  58. struct {
  59. F *float64
  60. }{},
  61. // TODO: fix this
  62. //struct {
  63. // *time.Time
  64. //}{},
  65. struct {
  66. *time.Time
  67. }{&time.Time{}},
  68. struct {
  69. *StructVarious
  70. }{&StructVarious{}},
  71. struct {
  72. *StructVarious
  73. }{},
  74. struct {
  75. Field1 int
  76. Field2 [1]*float64
  77. }{},
  78. struct {
  79. Field interface{} `json:"field,omitempty"`
  80. }{},
  81. struct {
  82. Field MyInterface `json:"field,omitempty"`
  83. }{},
  84. struct {
  85. Field MyInterface `json:"field,omitempty"`
  86. }{MyString("hello")},
  87. struct {
  88. Field json.Marshaler `json:"field"`
  89. }{},
  90. struct {
  91. Field MyInterface `json:"field"`
  92. }{},
  93. struct {
  94. Field MyInterface `json:"field"`
  95. }{MyString("hello")},
  96. struct {
  97. Field1 string `json:"field-1,omitempty"`
  98. Field2 func() `json:"-"`
  99. }{},
  100. structRecursive{},
  101. struct {
  102. *CacheItem
  103. // Omit bad keys
  104. OmitMaxAge omit `json:"cacheAge,omitempty"`
  105. // Add nice keys
  106. MaxAge int `json:"max_age"`
  107. }{
  108. CacheItem: &CacheItem{
  109. Key: "value",
  110. MaxAge: 100,
  111. },
  112. MaxAge: 20,
  113. },
  114. structOrder{},
  115. )
  116. }
  117. type StructVarious struct {
  118. Field0 string
  119. Field1 []string
  120. Field2 map[string]interface{}
  121. }
  122. type structRecursive struct {
  123. Field1 string
  124. Me *structRecursive
  125. }
  126. type omit *struct{}
  127. type CacheItem struct {
  128. Key string `json:"key"`
  129. MaxAge int `json:"cacheAge"`
  130. }
  131. type orderA struct {
  132. Field2 string
  133. }
  134. type orderC struct {
  135. Field5 string
  136. }
  137. type orderB struct {
  138. Field4 string
  139. orderC
  140. Field6 string
  141. }
  142. type structOrder struct {
  143. Field1 string
  144. orderA
  145. Field3 string
  146. orderB
  147. Field7 string
  148. }