diff --git a/field.go b/field.go index 093dd40..a7a134d 100644 --- a/field.go +++ b/field.go @@ -92,7 +92,7 @@ func (f *Field) Set(val interface{}) error { // // It panics if field is not exported or if field's kind is not struct func (f *Field) Fields() []*Field { - return getFields(f.value) + return getFields(f.value, DefaultTagName) } // Field returns the field from a nested struct. It panics if the nested struct diff --git a/structs.go b/structs.go index a75f523..0a1fd51 100644 --- a/structs.go +++ b/structs.go @@ -13,16 +13,18 @@ var ( // Struct encapsulates a struct type to provide several high level functions // around the struct. type Struct struct { - raw interface{} - value reflect.Value + raw interface{} + value reflect.Value + TagName string } // New returns a new *Struct with the struct s. It panics if the s's kind is // not struct. func New(s interface{}) *Struct { return &Struct{ - raw: s, - value: strctVal(s), + raw: s, + value: strctVal(s), + TagName: DefaultTagName, } } @@ -71,7 +73,7 @@ func (s *Struct) Map() map[string]interface{} { var finalVal interface{} - tagName, tagOpts := parseTag(field.Tag.Get(DefaultTagName)) + tagName, tagOpts := parseTag(field.Tag.Get(s.TagName)) if tagName != "" { name = tagName } @@ -131,7 +133,7 @@ func (s *Struct) Values() []interface{} { for _, field := range fields { val := s.value.FieldByName(field.Name) - _, tagOpts := parseTag(field.Tag.Get(DefaultTagName)) + _, tagOpts := parseTag(field.Tag.Get(s.TagName)) // if the value is a zero value and the field is marked as omitempty do // not include @@ -166,10 +168,10 @@ func (s *Struct) Values() []interface{} { // // It panics if s's kind is not struct. func (s *Struct) Fields() []*Field { - return getFields(s.value) + return getFields(s.value, s.TagName) } -func getFields(v reflect.Value) []*Field { +func getFields(v reflect.Value, tagName string) []*Field { if v.Kind() == reflect.Ptr { v = v.Elem() } @@ -181,7 +183,7 @@ func getFields(v reflect.Value) []*Field { for i := 0; i < t.NumField(); i++ { field := t.Field(i) - if tag := field.Tag.Get(DefaultTagName); tag == "-" { + if tag := field.Tag.Get(tagName); tag == "-" { continue } @@ -247,7 +249,7 @@ func (s *Struct) IsZero() bool { for _, field := range fields { val := s.value.FieldByName(field.Name) - _, tagOpts := parseTag(field.Tag.Get(DefaultTagName)) + _, tagOpts := parseTag(field.Tag.Get(s.TagName)) if IsStruct(val.Interface()) && !tagOpts.Has("omitnested") { ok := IsZero(val.Interface()) @@ -294,7 +296,7 @@ func (s *Struct) HasZero() bool { for _, field := range fields { val := s.value.FieldByName(field.Name) - _, tagOpts := parseTag(field.Tag.Get(DefaultTagName)) + _, tagOpts := parseTag(field.Tag.Get(s.TagName)) if IsStruct(val.Interface()) && !tagOpts.Has("omitnested") { ok := HasZero(val.Interface()) @@ -341,7 +343,7 @@ func (s *Struct) structFields() []reflect.StructField { } // don't check if it's omitted - if tag := field.Tag.Get(DefaultTagName); tag == "-" { + if tag := field.Tag.Get(s.TagName); tag == "-" { continue } diff --git a/structs_test.go b/structs_test.go index e37a337..4e00974 100644 --- a/structs_test.go +++ b/structs_test.go @@ -124,12 +124,10 @@ func TestMap_CustomTag(t *testing.T) { C: true, } - defaultName := DefaultTagName - DefaultTagName = "dd" - defer func() { - DefaultTagName = defaultName - }() - a := Map(T) + s := New(T) + s.TagName = "dd" + + a := s.Map() inMap := func(key interface{}) bool { for k := range a {