github.com/hikaru7719/go@v0.0.0-20181025140707-c8b2ac68906a/src/cmd/vet/testdata/structtag.go (about) 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. 4 5 // This file contains the test for canonical struct tags. 6 7 package testdata 8 9 import "encoding/xml" 10 11 type StructTagTest struct { 12 A int "hello" // ERROR "`hello` not compatible with reflect.StructTag.Get: bad syntax for struct tag pair" 13 B int "\tx:\"y\"" // ERROR "not compatible with reflect.StructTag.Get: bad syntax for struct tag key" 14 C int "x:\"y\"\tx:\"y\"" // ERROR "not compatible with reflect.StructTag.Get" 15 D int "x:`y`" // ERROR "not compatible with reflect.StructTag.Get: bad syntax for struct tag value" 16 E int "ct\brl:\"char\"" // ERROR "not compatible with reflect.StructTag.Get: bad syntax for struct tag pair" 17 F int `:"emptykey"` // ERROR "not compatible with reflect.StructTag.Get: bad syntax for struct tag key" 18 G int `x:"noEndQuote` // ERROR "not compatible with reflect.StructTag.Get: bad syntax for struct tag value" 19 H int `x:"trunc\x0"` // ERROR "not compatible with reflect.StructTag.Get: bad syntax for struct tag value" 20 I int `x:"foo",y:"bar"` // ERROR "not compatible with reflect.StructTag.Get: key:.value. pairs not separated by spaces" 21 J int `x:"foo"y:"bar"` // ERROR "not compatible with reflect.StructTag.Get: key:.value. pairs not separated by spaces" 22 OK0 int `x:"y" u:"v" w:""` 23 OK1 int `x:"y:z" u:"v" w:""` // note multiple colons. 24 OK2 int "k0:\"values contain spaces\" k1:\"literal\ttabs\" k2:\"and\\tescaped\\tabs\"" 25 OK3 int `under_scores:"and" CAPS:"ARE_OK"` 26 } 27 28 type UnexportedEncodingTagTest struct { 29 x int `json:"xx"` // ERROR "struct field x has json tag but is not exported" 30 y int `xml:"yy"` // ERROR "struct field y has xml tag but is not exported" 31 z int 32 A int `json:"aa" xml:"bb"` 33 } 34 35 type unexp struct{} 36 37 type JSONEmbeddedField struct { 38 UnexportedEncodingTagTest `is:"embedded"` 39 unexp `is:"embedded,notexported" json:"unexp"` // OK for now, see issue 7363 40 } 41 42 type AnonymousJSON struct{} 43 type AnonymousXML struct{} 44 45 type AnonymousJSONField struct { 46 DuplicateAnonJSON int `json:"a"` 47 48 A int "hello" // ERROR "`hello` not compatible with reflect.StructTag.Get: bad syntax for struct tag pair" 49 } 50 51 type DuplicateJSONFields struct { 52 JSON int `json:"a"` 53 DuplicateJSON int `json:"a"` // ERROR "struct field DuplicateJSON repeats json tag .a. also at structtag.go:52" 54 IgnoredJSON int `json:"-"` 55 OtherIgnoredJSON int `json:"-"` 56 OmitJSON int `json:",omitempty"` 57 OtherOmitJSON int `json:",omitempty"` 58 DuplicateOmitJSON int `json:"a,omitempty"` // ERROR "struct field DuplicateOmitJSON repeats json tag .a. also at structtag.go:52" 59 NonJSON int `foo:"a"` 60 DuplicateNonJSON int `foo:"a"` 61 Embedded struct { 62 DuplicateJSON int `json:"a"` // OK because it's not in the same struct type 63 } 64 AnonymousJSON `json:"a"` // ERROR "struct field AnonymousJSON repeats json tag .a. also at structtag.go:52" 65 66 AnonymousJSONField // ERROR "struct field DuplicateAnonJSON repeats json tag .a. also at structtag.go:52" 67 68 XML int `xml:"a"` 69 DuplicateXML int `xml:"a"` // ERROR "struct field DuplicateXML repeats xml tag .a. also at structtag.go:68" 70 IgnoredXML int `xml:"-"` 71 OtherIgnoredXML int `xml:"-"` 72 OmitXML int `xml:",omitempty"` 73 OtherOmitXML int `xml:",omitempty"` 74 DuplicateOmitXML int `xml:"a,omitempty"` // ERROR "struct field DuplicateOmitXML repeats xml tag .a. also at structtag.go:68" 75 NonXML int `foo:"a"` 76 DuplicateNonXML int `foo:"a"` 77 Embedded2 struct { 78 DuplicateXML int `xml:"a"` // OK because it's not in the same struct type 79 } 80 AnonymousXML `xml:"a"` // ERROR "struct field AnonymousXML repeats xml tag .a. also at structtag.go:68" 81 Attribute struct { 82 XMLName xml.Name `xml:"b"` 83 NoDup int `xml:"b"` // OK because XMLName above affects enclosing struct. 84 Attr int `xml:"b,attr"` // OK because <b b="0"><b>0</b></b> is valid. 85 DupAttr int `xml:"b,attr"` // ERROR "struct field DupAttr repeats xml attribute tag .b. also at structtag.go:84" 86 DupOmitAttr int `xml:"b,omitempty,attr"` // ERROR "struct field DupOmitAttr repeats xml attribute tag .b. also at structtag.go:84" 87 88 AnonymousXML `xml:"b,attr"` // ERROR "struct field AnonymousXML repeats xml attribute tag .b. also at structtag.go:84" 89 } 90 91 AnonymousJSONField `json:"not_anon"` // ok; fields aren't embedded in JSON 92 AnonymousJSONField `json:"-"` // ok; entire field is ignored in JSON 93 } 94 95 type UnexpectedSpacetest struct { 96 A int `json:"a,omitempty"` 97 B int `json:"b, omitempty"` // ERROR "suspicious space in struct tag value" 98 C int `json:"c ,omitempty"` 99 D int `json:"d,omitempty, string"` // ERROR "suspicious space in struct tag value" 100 E int `xml:"e local"` 101 F int `xml:"f "` // ERROR "suspicious space in struct tag value" 102 G int `xml:" g"` // ERROR "suspicious space in struct tag value" 103 H int `xml:"h ,omitempty"` // ERROR "suspicious space in struct tag value" 104 I int `xml:"i, omitempty"` // ERROR "suspicious space in struct tag value" 105 J int `xml:"j local ,omitempty"` // ERROR "suspicious space in struct tag value" 106 K int `xml:"k local, omitempty"` // ERROR "suspicious space in struct tag value" 107 L int `xml:" l local,omitempty"` // ERROR "suspicious space in struct tag value" 108 M int `xml:"m local,omitempty"` // ERROR "suspicious space in struct tag value" 109 N int `xml:" "` // ERROR "suspicious space in struct tag value" 110 O int `xml:""` 111 P int `xml:","` 112 Q int `foo:" doesn't care "` 113 }