Home | History | Annotate | Download | only in testdata
      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 "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 DuplicateJSONFields struct {
     46 	JSON              int `json:"a"`
     47 	DuplicateJSON     int `json:"a"` // ERROR "struct field DuplicateJSON repeats json tag .a. also at testdata/structtag.go:46"
     48 	IgnoredJSON       int `json:"-"`
     49 	OtherIgnoredJSON  int `json:"-"`
     50 	OmitJSON          int `json:",omitempty"`
     51 	OtherOmitJSON     int `json:",omitempty"`
     52 	DuplicateOmitJSON int `json:"a,omitempty"` // ERROR "struct field DuplicateOmitJSON repeats json tag .a. also at testdata/structtag.go:46"
     53 	NonJSON           int `foo:"a"`
     54 	DuplicateNonJSON  int `foo:"a"`
     55 	Embedded          struct {
     56 		DuplicateJSON int `json:"a"` // OK because its not in the same struct type
     57 	}
     58 	AnonymousJSON `json:"a"` // ERROR "struct field AnonymousJSON repeats json tag .a. also at testdata/structtag.go:46"
     59 
     60 	XML              int `xml:"a"`
     61 	DuplicateXML     int `xml:"a"` // ERROR "struct field DuplicateXML repeats xml tag .a. also at testdata/structtag.go:60"
     62 	IgnoredXML       int `xml:"-"`
     63 	OtherIgnoredXML  int `xml:"-"`
     64 	OmitXML          int `xml:",omitempty"`
     65 	OtherOmitXML     int `xml:",omitempty"`
     66 	DuplicateOmitXML int `xml:"a,omitempty"` // ERROR "struct field DuplicateOmitXML repeats xml tag .a. also at testdata/structtag.go:60"
     67 	NonXML           int `foo:"a"`
     68 	DuplicateNonXML  int `foo:"a"`
     69 	Embedded         struct {
     70 		DuplicateXML int `xml:"a"` // OK because its not in the same struct type
     71 	}
     72 	AnonymousXML `xml:"a"` // ERROR "struct field AnonymousXML repeats xml tag .a. also at testdata/structtag.go:60"
     73 	Attribute    struct {
     74 		XMLName     xml.Name `xml:"b"`
     75 		NoDup       int      `xml:"b"`                // OK because XMLName above affects enclosing struct.
     76 		Attr        int      `xml:"b,attr"`           // OK because <b b="0"><b>0</b></b> is valid.
     77 		DupAttr     int      `xml:"b,attr"`           // ERROR "struct field DupAttr repeats xml attribute tag .b. also at testdata/structtag.go:76"
     78 		DupOmitAttr int      `xml:"b,omitempty,attr"` // ERROR "struct field DupOmitAttr repeats xml attribute tag .b. also at testdata/structtag.go:76"
     79 
     80 		AnonymousXML `xml:"b,attr"` // ERROR "struct field AnonymousXML repeats xml attribute tag .b. also at testdata/structtag.go:76"
     81 	}
     82 }
     83 
     84 type UnexpectedSpacetest struct {
     85 	A int `json:"a,omitempty"`
     86 	B int `json:"b, omitempty"` // ERROR "suspicious space in struct tag value"
     87 	C int `json:"c ,omitempty"`
     88 	D int `json:"d,omitempty, string"` // ERROR "suspicious space in struct tag value"
     89 	E int `xml:"e local"`
     90 	F int `xml:"f "`                 // ERROR "suspicious space in struct tag value"
     91 	G int `xml:" g"`                 // ERROR "suspicious space in struct tag value"
     92 	H int `xml:"h ,omitempty"`       // ERROR "suspicious space in struct tag value"
     93 	I int `xml:"i, omitempty"`       // ERROR "suspicious space in struct tag value"
     94 	J int `xml:"j local ,omitempty"` // ERROR "suspicious space in struct tag value"
     95 	K int `xml:"k local, omitempty"` // ERROR "suspicious space in struct tag value"
     96 	L int `xml:" l local,omitempty"` // ERROR "suspicious space in struct tag value"
     97 	M int `xml:"m  local,omitempty"` // ERROR "suspicious space in struct tag value"
     98 	N int `xml:" "`                  // ERROR "suspicious space in struct tag value"
     99 	O int `xml:""`
    100 	P int `xml:","`
    101 	Q int `foo:" doesn't care "`
    102 }
    103