From b334b6a03f00305aed1a072fd3c28ec550d5cd4b Mon Sep 17 00:00:00 2001 From: smallfish Date: Tue, 5 Aug 2014 21:43:46 +0800 Subject: [PATCH 1/3] support custom struct's field tag name --- structure.go | 18 ++++++++++++++++-- structure_test.go | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 2 deletions(-) diff --git a/structure.go b/structure.go index 9b9a16d..563f70a 100644 --- a/structure.go +++ b/structure.go @@ -3,6 +3,10 @@ package structure import "reflect" +var ( + tagName = "structure" // struct's field default tag name +) + // Map converts the given s struct to a map[string]interface{}, where the keys // of the map are the field names and the values of the map the associated // values of the fields. The default key string is the struct field name but @@ -40,7 +44,7 @@ func Map(s interface{}) map[string]interface{} { // override if the user passed a structure tag value // ignore if the user passed the "-" value - if tag := field.Tag.Get("structure"); tag != "" { + if tag := field.Tag.Get(tagName); tag != "" { name = tag } @@ -194,6 +198,16 @@ func Has(s interface{}, fieldName string) bool { return false } +// Set struct's field tag name +func SetTagName(n string) { + tagName = n +} + +// Reset struct's field tag name, default: "structure" +func ResetTagName() { + tagName = "structure" +} + // strctInfo returns the struct value and the exported struct fields for a // given s struct. This is a convenient helper method to avoid duplicate code // in some of the functions. @@ -211,7 +225,7 @@ func strctInfo(s interface{}) (reflect.Value, []reflect.StructField) { } // don't check if it's omitted - if tag := field.Tag.Get("structure"); tag == "-" { + if tag := field.Tag.Get(tagName); tag == "-" { continue } diff --git a/structure_test.go b/structure_test.go index 94f03dc..1554723 100644 --- a/structure_test.go +++ b/structure_test.go @@ -89,6 +89,38 @@ func TestMap_Tag(t *testing.T) { } +func TestMap_CustomTag(t *testing.T) { + var T = struct { + A string `dd:"x"` + B int `dd:"y"` + C bool `dd:"z"` + }{ + A: "a-value", + B: 2, + C: true, + } + + SetTagName("dd") + defer ResetTagName() + a := Map(T) + + inMap := func(key interface{}) bool { + for k := range a { + if reflect.DeepEqual(k, key) { + return true + } + } + return false + } + + for _, key := range []string{"x", "y", "z"} { + if !inMap(key) { + t.Errorf("Map should have the key %v", key) + } + } + +} + func TestMap_Nested(t *testing.T) { type A struct { Name string From 13b709511562c90791b907fee66602fb1d711734 Mon Sep 17 00:00:00 2001 From: Fatih Arslan Date: Tue, 5 Aug 2014 17:01:25 +0300 Subject: [PATCH 2/3] Update README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index f61d1b9..c379f29 100644 --- a/README.md +++ b/README.md @@ -51,12 +51,12 @@ n := structure.Name(s) // => true h := structure.Has(s, "Enabled") -// Check if a field of a struct is initialized or not. +// Check if any field of a struct is initialized or not. if structure.HasZero(s) { fmt.Println("s has a zero value field") } -// Check if all field of a struct is initialized or not. +// Check if all fields of a struct is initialized or not. if structure.IsZero(s) { fmt.Println("all fields of s is zero value") } From b673565392b8b1e109b0ddd7b5e9f777d9dd7145 Mon Sep 17 00:00:00 2001 From: smallfish Date: Tue, 5 Aug 2014 22:45:42 +0800 Subject: [PATCH 3/3] update custom struct's field tag name use package var, remove setter/reset func --- structure.go | 16 +++------------- structure_test.go | 7 +++++-- 2 files changed, 8 insertions(+), 15 deletions(-) diff --git a/structure.go b/structure.go index 563f70a..571ce5c 100644 --- a/structure.go +++ b/structure.go @@ -4,7 +4,7 @@ package structure import "reflect" var ( - tagName = "structure" // struct's field default tag name + DefaultTagName = "structure" // struct's field default tag name ) // Map converts the given s struct to a map[string]interface{}, where the keys @@ -44,7 +44,7 @@ func Map(s interface{}) map[string]interface{} { // override if the user passed a structure tag value // ignore if the user passed the "-" value - if tag := field.Tag.Get(tagName); tag != "" { + if tag := field.Tag.Get(DefaultTagName); tag != "" { name = tag } @@ -198,16 +198,6 @@ func Has(s interface{}, fieldName string) bool { return false } -// Set struct's field tag name -func SetTagName(n string) { - tagName = n -} - -// Reset struct's field tag name, default: "structure" -func ResetTagName() { - tagName = "structure" -} - // strctInfo returns the struct value and the exported struct fields for a // given s struct. This is a convenient helper method to avoid duplicate code // in some of the functions. @@ -225,7 +215,7 @@ func strctInfo(s interface{}) (reflect.Value, []reflect.StructField) { } // don't check if it's omitted - if tag := field.Tag.Get(tagName); tag == "-" { + if tag := field.Tag.Get(DefaultTagName); tag == "-" { continue } diff --git a/structure_test.go b/structure_test.go index 1554723..0cf3548 100644 --- a/structure_test.go +++ b/structure_test.go @@ -100,8 +100,11 @@ func TestMap_CustomTag(t *testing.T) { C: true, } - SetTagName("dd") - defer ResetTagName() + defaultName := DefaultTagName + DefaultTagName = "dd" + defer func() { + DefaultTagName = defaultName + }() a := Map(T) inMap := func(key interface{}) bool {