aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJohn Jeffery <jjeffery@sp.com.au>2016-03-20 20:24:12 +1000
committerBrad Fitzpatrick <bradfitz@golang.org>2016-03-25 04:54:19 +0000
commit5c8674a497724f509737a3523e7c2ddf94e3a72b (patch)
tree31bb815ce1beff55f7184db7690d0e901f8c99f3 /src
parent139fad21b937dc0b405d243e3abe33429890947f (diff)
downloadgo-5c8674a497724f509737a3523e7c2ddf94e3a72b.tar.xz
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>
Diffstat (limited to 'src')
-rw-r--r--src/reflect/example_test.go28
-rw-r--r--src/reflect/type.go18
2 files changed, 43 insertions, 3 deletions
diff --git a/src/reflect/example_test.go b/src/reflect/example_test.go
index 1147477955..9e2b9b3e97 100644
--- a/src/reflect/example_test.go
+++ b/src/reflect/example_test.go
@@ -67,6 +67,34 @@ func ExampleStructTag() {
// blue gopher
}
+func ExampleStructTag_Lookup() {
+ type S struct {
+ F0 string `alias:"field_0"`
+ F1 string `alias:""`
+ F2 string
+ }
+
+ s := S{}
+ st := reflect.TypeOf(s)
+ for i := 0; i < st.NumField(); i++ {
+ field := st.Field(i)
+ if alias, ok := field.Tag.Lookup("alias"); ok {
+ if alias == "" {
+ fmt.Println("(blank)")
+ } else {
+ fmt.Println(alias)
+ }
+ } else {
+ fmt.Println("(not specified)")
+ }
+ }
+
+ // Output:
+ // field_0
+ // (blank)
+ // (not specified)
+}
+
func ExampleTypeOf() {
// As interface types are only used for static typing, a
// common idiom to find the reflection Type for an interface
diff --git a/src/reflect/type.go b/src/reflect/type.go
index f75bfd379d..c7a0313809 100644
--- a/src/reflect/type.go
+++ b/src/reflect/type.go
@@ -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.