mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
reflect: add method StructTag.Lookup
The Lookup method provides a way to extract a tag value, while determining whether the tag key exists in the struct field's tag. Fixes #14883 Change-Id: I7460cb68f0ca1aaa025935050b9e182efcb64db3 Reviewed-on: https://go-review.googlesource.com/20864 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
This commit is contained in:
parent
139fad21b9
commit
5c8674a497
2 changed files with 43 additions and 3 deletions
|
|
@ -976,8 +976,20 @@ type StructTag string
|
|||
// Get returns the value associated with key in the tag string.
|
||||
// If there is no such key in the tag, Get returns the empty string.
|
||||
// If the tag does not have the conventional format, the value
|
||||
// returned by Get is unspecified.
|
||||
// returned by Get is unspecified. To determine whether a tag is
|
||||
// explicitly set to the empty string, use Lookup.
|
||||
func (tag StructTag) Get(key string) string {
|
||||
v, _ := tag.Lookup(key)
|
||||
return v
|
||||
}
|
||||
|
||||
// Lookup returns the value associated with key in the tag string.
|
||||
// If the key is present in the tag the value (which may be empty)
|
||||
// is returned. Otherwise the returned value will be the empty string.
|
||||
// The ok return value reports whether the value was explicitly set in
|
||||
// the tag string. If the tag does not have the conventional format,
|
||||
// the value returned by Lookup is unspecified.
|
||||
func (tag StructTag) Lookup(key string) (value string, ok bool) {
|
||||
// When modifying this code, also update the validateStructTag code
|
||||
// in golang.org/x/tools/cmd/vet/structtag.go.
|
||||
|
||||
|
|
@ -1025,10 +1037,10 @@ func (tag StructTag) Get(key string) string {
|
|||
if err != nil {
|
||||
break
|
||||
}
|
||||
return value
|
||||
return value, true
|
||||
}
|
||||
}
|
||||
return ""
|
||||
return "", false
|
||||
}
|
||||
|
||||
// Field returns the i'th struct field.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue