mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
reflect: support for struct tag use by multiple packages
Each package using struct field tags assumes that
it is the only package storing data in the tag.
This CL adds support in package reflect for sharing
tags between multiple packages. In this scheme, the
tags must be of the form
key:"value" key2:"value2"
(raw strings help when writing that tag in Go source).
reflect.StructField's Tag field now has type StructTag
(a string type), which has method Get(key string) string
that returns the associated value.
Clients of json and xml will need to be updated.
Code that says
type T struct {
X int "name"
}
should become
type T struct {
X int `json:"name"` // or `xml:"name"`
}
Use govet to identify struct tags that need to be changed
to use the new syntax.
R=r, r, dsymonds, bradfitz, kevlar, fvbommel, n13m3y3r
CC=golang-dev
https://golang.org/cl/4645069
This commit is contained in:
parent
f83609f642
commit
25733a94fd
24 changed files with 293 additions and 184 deletions
|
|
@ -42,8 +42,9 @@ var (
|
|||
|
||||
type badTag struct {
|
||||
X string
|
||||
Y string "y"
|
||||
Z string "@#*%(#@"
|
||||
Y string `json:"y"`
|
||||
Z string `x:"@#*%(#@"`
|
||||
W string `json:"@#$@#$"`
|
||||
}
|
||||
|
||||
type unmarshalTest struct {
|
||||
|
|
@ -68,7 +69,7 @@ var unmarshalTests = []unmarshalTest{
|
|||
{`{"x": 1}`, new(tx), tx{}, &UnmarshalFieldError{"x", txType, txType.Field(0)}},
|
||||
|
||||
// skip invalid tags
|
||||
{`{"X":"a", "y":"b", "Z":"c"}`, new(badTag), badTag{"a", "b", "c"}, nil},
|
||||
{`{"X":"a", "y":"b", "Z":"c", "W":"d"}`, new(badTag), badTag{"a", "b", "c", "d"}, nil},
|
||||
|
||||
// syntax errors
|
||||
{`{"X": "foo", "Y"}`, nil, nil, &SyntaxError{"invalid character '}' after object key", 17}},
|
||||
|
|
@ -250,7 +251,7 @@ type All struct {
|
|||
Float32 float32
|
||||
Float64 float64
|
||||
|
||||
Foo string "bar"
|
||||
Foo string `json:"bar"`
|
||||
|
||||
PBool *bool
|
||||
PInt *int
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue