1 // Copyright 2010 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // This file contains the test for canonical struct tags.
14 type StructTagTest struct {
15 A int "hello" // want "`hello` not compatible with reflect.StructTag.Get: bad syntax for struct tag pair"
16 B int "\tx:\"y\"" // want "not compatible with reflect.StructTag.Get: bad syntax for struct tag key"
17 C int "x:\"y\"\tx:\"y\"" // want "not compatible with reflect.StructTag.Get"
18 D int "x:`y`" // want "not compatible with reflect.StructTag.Get: bad syntax for struct tag value"
19 E int "ct\brl:\"char\"" // want "not compatible with reflect.StructTag.Get: bad syntax for struct tag pair"
20 F int `:"emptykey"` // want "not compatible with reflect.StructTag.Get: bad syntax for struct tag key"
21 G int `x:"noEndQuote` // want "not compatible with reflect.StructTag.Get: bad syntax for struct tag value"
22 H int `x:"trunc\x0"` // want "not compatible with reflect.StructTag.Get: bad syntax for struct tag value"
23 I int `x:"foo",y:"bar"` // want "not compatible with reflect.StructTag.Get: key:.value. pairs not separated by spaces"
24 J int `x:"foo"y:"bar"` // want "not compatible with reflect.StructTag.Get: key:.value. pairs not separated by spaces"
25 OK0 int `x:"y" u:"v" w:""`
26 OK1 int `x:"y:z" u:"v" w:""` // note multiple colons.
27 OK2 int "k0:\"values contain spaces\" k1:\"literal\ttabs\" k2:\"and\\tescaped\\tabs\""
28 OK3 int `under_scores:"and" CAPS:"ARE_OK"`
31 type UnexportedEncodingTagTest struct {
32 x int `json:"xx"` // want "struct field x has json tag but is not exported"
33 y int `xml:"yy"` // want "struct field y has xml tag but is not exported"
35 A int `json:"aa" xml:"bb"`
42 type JSONEmbeddedField struct {
43 UnexportedEncodingTagTest `is:"embedded"`
44 unexp `is:"embedded,notexported" json:"unexp"` // OK for now, see issue 7363
47 type AnonymousJSON struct{}
48 type AnonymousXML struct{}
50 type AnonymousJSONField struct {
51 DuplicateAnonJSON int `json:"a"`
53 A int "hello" // want "`hello` not compatible with reflect.StructTag.Get: bad syntax for struct tag pair"
56 // With different names to allow using as anonymous fields multiple times.
58 type AnonymousJSONField2 struct {
59 DuplicateAnonJSON int `json:"a"`
61 type AnonymousJSONField3 struct {
62 DuplicateAnonJSON int `json:"a"`
65 type DuplicateJSONFields struct {
67 DuplicateJSON int `json:"a"` // want "struct field DuplicateJSON repeats json tag .a. also at a.go:66"
68 IgnoredJSON int `json:"-"`
69 OtherIgnoredJSON int `json:"-"`
70 OmitJSON int `json:",omitempty"`
71 OtherOmitJSON int `json:",omitempty"`
72 DuplicateOmitJSON int `json:"a,omitempty"` // want "struct field DuplicateOmitJSON repeats json tag .a. also at a.go:66"
74 DuplicateNonJSON int `foo:"a"`
76 DuplicateJSON int `json:"a"` // OK because it's not in the same struct type
78 AnonymousJSON `json:"a"` // want "struct field AnonymousJSON repeats json tag .a. also at a.go:66"
81 DuplicateXML int `xml:"a"` // want "struct field DuplicateXML repeats xml tag .a. also at a.go:80"
82 IgnoredXML int `xml:"-"`
83 OtherIgnoredXML int `xml:"-"`
84 OmitXML int `xml:",omitempty"`
85 OtherOmitXML int `xml:",omitempty"`
86 DuplicateOmitXML int `xml:"a,omitempty"` // want "struct field DuplicateOmitXML repeats xml tag .a. also at a.go:80"
88 DuplicateNonXML int `foo:"a"`
90 DuplicateXML int `xml:"a"` // OK because it's not in the same struct type
92 AnonymousXML `xml:"a"` // want "struct field AnonymousXML repeats xml tag .a. also at a.go:80"
94 XMLName xml.Name `xml:"b"`
95 NoDup int `xml:"b"` // OK because XMLName above affects enclosing struct.
96 Attr int `xml:"b,attr"` // OK because <b b="0"><b>0</b></b> is valid.
97 DupAttr int `xml:"b,attr"` // want "struct field DupAttr repeats xml attribute tag .b. also at a.go:96"
98 DupOmitAttr int `xml:"b,omitempty,attr"` // want "struct field DupOmitAttr repeats xml attribute tag .b. also at a.go:96"
100 AnonymousXML `xml:"b,attr"` // want "struct field AnonymousXML repeats xml attribute tag .b. also at a.go:96"
103 AnonymousJSONField2 `json:"not_anon"` // ok; fields aren't embedded in JSON
104 AnonymousJSONField3 `json:"-"` // ok; entire field is ignored in JSON
107 type UnexpectedSpacetest struct {
108 A int `json:"a,omitempty"`
109 B int `json:"b, omitempty"` // want "suspicious space in struct tag value"
110 C int `json:"c ,omitempty"`
111 D int `json:"d,omitempty, string"` // want "suspicious space in struct tag value"
112 E int `xml:"e local"`
113 F int `xml:"f "` // want "suspicious space in struct tag value"
114 G int `xml:" g"` // want "suspicious space in struct tag value"
115 H int `xml:"h ,omitempty"` // want "suspicious space in struct tag value"
116 I int `xml:"i, omitempty"` // want "suspicious space in struct tag value"
117 J int `xml:"j local ,omitempty"` // want "suspicious space in struct tag value"
118 K int `xml:"k local, omitempty"` // want "suspicious space in struct tag value"
119 L int `xml:" l local,omitempty"` // want "suspicious space in struct tag value"
120 M int `xml:"m local,omitempty"` // want "suspicious space in struct tag value"
121 N int `xml:" "` // want "suspicious space in struct tag value"
124 Q int `foo:" doesn't care "`
127 // Nested fiels can be shadowed by fields further up. For example,
128 // ShadowingAnonJSON replaces the json:"a" field in AnonymousJSONField.
129 // However, if the two conflicting fields appear at the same level like in
130 // DuplicateWithAnotherPackage, we should error.
132 type ShadowingJsonFieldName struct {
134 ShadowingAnonJSON int `json:"a"`
137 type DuplicateWithAnotherPackage struct {
139 AnonymousJSONField2 // want "struct field DuplicateAnonJSON repeats json tag .a. also at b.b.go:8"