github.com/dannin/go@v0.0.0-20161031215817-d35dfd405eaa/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  type StructTagTest struct {
    10  	A   int "hello"            // ERROR "not compatible with reflect.StructTag.Get: bad syntax for struct tag pair"
    11  	B   int "\tx:\"y\""        // ERROR "not compatible with reflect.StructTag.Get: bad syntax for struct tag key"
    12  	C   int "x:\"y\"\tx:\"y\"" // ERROR "not compatible with reflect.StructTag.Get"
    13  	D   int "x:`y`"            // ERROR "not compatible with reflect.StructTag.Get: bad syntax for struct tag value"
    14  	E   int "ct\brl:\"char\""  // ERROR "not compatible with reflect.StructTag.Get: bad syntax for struct tag pair"
    15  	F   int `:"emptykey"`      // ERROR "not compatible with reflect.StructTag.Get: bad syntax for struct tag key"
    16  	G   int `x:"noEndQuote`    // ERROR "not compatible with reflect.StructTag.Get: bad syntax for struct tag value"
    17  	H   int `x:"trunc\x0"`     // ERROR "not compatible with reflect.StructTag.Get: bad syntax for struct tag value"
    18  	I   int `x:"foo",y:"bar"`  // ERROR "not compatible with reflect.StructTag.Get: key:.value. pairs not separated by spaces"
    19  	J   int `x:"foo"y:"bar"`   // ERROR "not compatible with reflect.StructTag.Get: key:.value. pairs not separated by spaces"
    20  	OK0 int `x:"y" u:"v" w:""`
    21  	OK1 int `x:"y:z" u:"v" w:""` // note multiple colons.
    22  	OK2 int "k0:\"values contain spaces\" k1:\"literal\ttabs\" k2:\"and\\tescaped\\tabs\""
    23  	OK3 int `under_scores:"and" CAPS:"ARE_OK"`
    24  }
    25  
    26  type UnexportedEncodingTagTest struct {
    27  	x int `json:"xx"` // ERROR "struct field x has json tag but is not exported"
    28  	y int `xml:"yy"`  // ERROR "struct field y has xml tag but is not exported"
    29  	z int
    30  	A int `json:"aa" xml:"bb"`
    31  }
    32  
    33  type unexp struct{}
    34  
    35  type JSONEmbeddedField struct {
    36  	UnexportedEncodingTagTest `is:"embedded"`
    37  	unexp                     `is:"embedded,notexported" json:"unexp"` // OK for now, see issue 7363
    38  }
    39  
    40  type DuplicateJSONFields struct {
    41  	JSON              int `json:"a"`
    42  	DuplicateJSON     int `json:"a"` // ERROR "struct field DuplicateJSON repeats json tag .a. also at testdata/structtag.go:41"
    43  	IgnoredJSON       int `json:"-"`
    44  	OtherIgnoredJSON  int `json:"-"`
    45  	OmitJSON          int `json:",omitempty"`
    46  	OtherOmitJSON     int `json:",omitempty"`
    47  	DuplicateOmitJSON int `json:"a,omitempty"` // ERROR "struct field DuplicateOmitJSON repeats json tag .a. also at testdata/structtag.go:41"
    48  	NonJSON           int `foo:"a"`
    49  	DuplicateNonJSON  int `foo:"a"`
    50  	Embedded          struct {
    51  		DuplicateJSON int `json:"a"` // OK because its not in the same struct type
    52  	}
    53  
    54  	XML              int `xml:"a"`
    55  	DuplicateXML     int `xml:"a"` // ERROR "struct field DuplicateXML repeats xml tag .a. also at testdata/structtag.go:54"
    56  	IgnoredXML       int `xml:"-"`
    57  	OtherIgnoredXML  int `xml:"-"`
    58  	OmitXML          int `xml:",omitempty"`
    59  	OtherOmitXML     int `xml:",omitempty"`
    60  	DuplicateOmitXML int `xml:"a,omitempty"` // ERROR "struct field DuplicateOmitXML repeats xml tag .a. also at testdata/structtag.go:54"
    61  	NonXML           int `foo:"a"`
    62  	DuplicateNonXML  int `foo:"a"`
    63  	Embedded         struct {
    64  		DuplicateXML int `xml:"a"` // OK because its not in the same struct type
    65  	}
    66  }