struct_tags_test.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. package test
  2. func init() {
  3. testCases = append(testCases,
  4. (*EmbeddedFieldName)(nil),
  5. (*StringFieldName)(nil),
  6. (*StructFieldName)(nil),
  7. (*struct {
  8. F1 bool `json:"F1"`
  9. F2 bool `json:"F2,omitempty"`
  10. })(nil),
  11. (*EmbeddedOmitEmpty)(nil),
  12. (*struct {
  13. F1 float32 `json:"F1"`
  14. F2 float32 `json:"F2,omitempty"`
  15. })(nil),
  16. (*struct {
  17. F1 int32 `json:"F1"`
  18. F2 int32 `json:"F2,omitempty"`
  19. })(nil),
  20. (*struct {
  21. F1 map[string]string `json:"F1"`
  22. F2 map[string]string `json:"F2,omitempty"`
  23. })(nil),
  24. (*struct {
  25. F1 *bool `json:"F1"`
  26. F2 *bool `json:"F2,omitempty"`
  27. })(nil),
  28. (*struct {
  29. F1 *float32 `json:"F1"`
  30. F2 *float32 `json:"F2,omitempty"`
  31. })(nil),
  32. (*struct {
  33. F1 *int32 `json:"F1"`
  34. F2 *int32 `json:"F2,omitempty"`
  35. })(nil),
  36. (*struct {
  37. F1 *map[string]string `json:"F1"`
  38. F2 *map[string]string `json:"F2,omitempty"`
  39. })(nil),
  40. (*struct {
  41. F1 *[]string `json:"F1"`
  42. F2 *[]string `json:"F2,omitempty"`
  43. })(nil),
  44. (*struct {
  45. F1 string `json:"F1"`
  46. F2 string `json:"F2,omitempty"`
  47. })(nil),
  48. (*struct {
  49. F1 *string `json:"F1"`
  50. F2 *string `json:"F2,omitempty"`
  51. })(nil),
  52. (*struct {
  53. F *jm `json:"f,omitempty"`
  54. })(nil),
  55. (*struct {
  56. F *tm `json:"f,omitempty"`
  57. })(nil),
  58. (*struct {
  59. F *sjm `json:"f,omitempty"`
  60. })(nil),
  61. (*struct {
  62. F *tm `json:"f,omitempty"`
  63. })(nil),
  64. (*struct {
  65. F1 *uint32 `json:"F1"`
  66. F2 *uint32 `json:"F2,omitempty"`
  67. })(nil),
  68. (*struct {
  69. F1 []string `json:"F1"`
  70. F2 []string `json:"F2,omitempty"`
  71. })(nil),
  72. (*struct {
  73. F1 string `json:"F1"`
  74. F2 string `json:"F2,omitempty"`
  75. })(nil),
  76. (*struct {
  77. F jm `json:"f,omitempty"`
  78. })(nil),
  79. (*struct {
  80. F tm `json:"f,omitempty"`
  81. })(nil),
  82. (*struct {
  83. F struct{} `json:"f,omitempty"` // omitempty is meaningless here
  84. })(nil),
  85. (*struct {
  86. F sjm `json:"f,omitempty"`
  87. })(nil),
  88. (*struct {
  89. F stm `json:"f,omitempty"`
  90. })(nil),
  91. (*struct {
  92. F1 uint32 `json:"F1"`
  93. F2 uint32 `json:"F2,omitempty"`
  94. })(nil),
  95. (*struct {
  96. F1 bool `json:"F1"`
  97. F2 bool `json:"F2,string"`
  98. })(nil),
  99. (*struct {
  100. F1 byte `json:"F1"`
  101. F2 byte `json:"F2,string"`
  102. })(nil),
  103. (*struct {
  104. F1 float32 `json:"F1"`
  105. F2 float32 `json:"F2,string"`
  106. })(nil),
  107. (*struct {
  108. F1 float64 `json:"F1"`
  109. F2 float64 `json:"F2,string"`
  110. })(nil),
  111. (*struct {
  112. F1 int8 `json:"F1"`
  113. F2 int8 `json:"F2,string"`
  114. })(nil),
  115. (*struct {
  116. F1 int16 `json:"F1"`
  117. F2 int16 `json:"F2,string"`
  118. })(nil),
  119. (*struct {
  120. F1 int32 `json:"F1"`
  121. F2 int32 `json:"F2,string"`
  122. })(nil),
  123. // remove temporarily until https://github.com/golang/go/issues/38126 is fixed
  124. // (*struct {
  125. // F1 string `json:"F1"`
  126. // F2 string `json:"F2,string"`
  127. // })(nil),
  128. (*struct {
  129. F1 uint8 `json:"F1"`
  130. F2 uint8 `json:"F2,string"`
  131. })(nil),
  132. (*struct {
  133. F1 uint16 `json:"F1"`
  134. F2 uint16 `json:"F2,string"`
  135. })(nil),
  136. (*struct {
  137. F1 uint32 `json:"F1"`
  138. F2 uint32 `json:"F2,string"`
  139. })(nil),
  140. (*struct {
  141. A string `json:"a,omitempty"`
  142. B string `json:"b,omitempty"`
  143. Annotations map[string]string `json:"annotations,omitempty"`
  144. })(nil),
  145. (*struct {
  146. Field bool `json:",omitempty,string"`
  147. })(nil),
  148. (*struct {
  149. Field bool `json:"中文"`
  150. })(nil),
  151. )
  152. }
  153. // S1 TEST ONLY
  154. type EmbeddedFieldNameS1 struct {
  155. S1F string
  156. }
  157. // S2 TEST ONLY
  158. type EmbeddedFieldNameS2 struct {
  159. S2F string
  160. }
  161. // S3 TEST ONLY
  162. type EmbeddedFieldNameS3 struct {
  163. S3F string
  164. }
  165. // S4 TEST ONLY
  166. type EmbeddedFieldNameS4 struct {
  167. S4F string
  168. }
  169. // S5 TEST ONLY
  170. type EmbeddedFieldNameS5 struct {
  171. S5F string
  172. }
  173. // S6 TEST ONLY
  174. type EmbeddedFieldNameS6 struct {
  175. S6F string
  176. }
  177. type EmbeddedFieldName struct {
  178. EmbeddedFieldNameS1 `json:"F1"`
  179. EmbeddedFieldNameS2 `json:"f2"`
  180. EmbeddedFieldNameS3 `json:"-"`
  181. EmbeddedFieldNameS4 `json:"-,"`
  182. EmbeddedFieldNameS5 `json:","`
  183. EmbeddedFieldNameS6 `json:""`
  184. }
  185. type StringFieldNameE struct {
  186. E1 string
  187. }
  188. type StringFieldName struct {
  189. F1 string `json:"F1"`
  190. F2 string `json:"f2"`
  191. F3 string `json:"-"`
  192. F4 string `json:"-,"`
  193. F5 string `json:","`
  194. F6 string `json:""`
  195. StringFieldNameE `json:"e"`
  196. }
  197. type StructFieldNameS1 struct {
  198. S1F string
  199. }
  200. type StructFieldNameS2 struct {
  201. S2F string
  202. }
  203. type StructFieldNameS3 struct {
  204. S3F string
  205. }
  206. type StructFieldNameS4 struct {
  207. S4F string
  208. }
  209. type StructFieldNameS5 struct {
  210. S5F string
  211. }
  212. type StructFieldNameS6 struct {
  213. S6F string
  214. }
  215. type StructFieldName struct {
  216. F1 StructFieldNameS1 `json:"F1"`
  217. F2 StructFieldNameS2 `json:"f2"`
  218. F3 StructFieldNameS3 `json:"-"`
  219. F4 StructFieldNameS4 `json:"-,"`
  220. F5 StructFieldNameS5 `json:","`
  221. F6 StructFieldNameS6 `json:""`
  222. }
  223. type EmbeddedOmitEmptyE struct {
  224. F string `json:"F,omitempty"`
  225. }
  226. type EmbeddedOmitEmpty struct {
  227. EmbeddedOmitEmptyE
  228. }
  229. type jm string
  230. func (t *jm) UnmarshalJSON(b []byte) error {
  231. return nil
  232. }
  233. func (t jm) MarshalJSON() ([]byte, error) {
  234. return []byte(`""`), nil
  235. }
  236. type tm string
  237. func (t *tm) UnmarshalText(b []byte) error {
  238. return nil
  239. }
  240. func (t tm) MarshalText() ([]byte, error) {
  241. return []byte(`""`), nil
  242. }
  243. type sjm struct{}
  244. func (t *sjm) UnmarshalJSON(b []byte) error {
  245. return nil
  246. }
  247. func (t sjm) MarshalJSON() ([]byte, error) {
  248. return []byte(`""`), nil
  249. }
  250. type stm struct{}
  251. func (t *stm) UnmarshalText(b []byte) error {
  252. return nil
  253. }
  254. func (t stm) MarshalText() ([]byte, error) {
  255. return []byte(`""`), nil
  256. }