refactor Struct to have own TagName field

This commit is contained in:
jaemin 2015-03-04 18:55:52 +09:00
parent bc210cbd8c
commit a093a86a06
3 changed files with 19 additions and 19 deletions

View File

@ -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 // It panics if field is not exported or if field's kind is not struct
func (f *Field) Fields() []*Field { 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 // Field returns the field from a nested struct. It panics if the nested struct

View File

@ -15,6 +15,7 @@ var (
type Struct struct { type Struct struct {
raw interface{} raw interface{}
value reflect.Value value reflect.Value
TagName string
} }
// New returns a new *Struct with the struct s. It panics if the s's kind is // New returns a new *Struct with the struct s. It panics if the s's kind is
@ -23,6 +24,7 @@ func New(s interface{}) *Struct {
return &Struct{ return &Struct{
raw: s, raw: s,
value: strctVal(s), value: strctVal(s),
TagName: DefaultTagName,
} }
} }
@ -71,7 +73,7 @@ func (s *Struct) Map() map[string]interface{} {
var finalVal interface{} var finalVal interface{}
tagName, tagOpts := parseTag(field.Tag.Get(DefaultTagName)) tagName, tagOpts := parseTag(field.Tag.Get(s.TagName))
if tagName != "" { if tagName != "" {
name = tagName name = tagName
} }
@ -131,7 +133,7 @@ func (s *Struct) Values() []interface{} {
for _, field := range fields { for _, field := range fields {
val := s.value.FieldByName(field.Name) 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 // if the value is a zero value and the field is marked as omitempty do
// not include // not include
@ -166,10 +168,10 @@ func (s *Struct) Values() []interface{} {
// //
// It panics if s's kind is not struct. // It panics if s's kind is not struct.
func (s *Struct) Fields() []*Field { 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 { if v.Kind() == reflect.Ptr {
v = v.Elem() v = v.Elem()
} }
@ -181,7 +183,7 @@ func getFields(v reflect.Value) []*Field {
for i := 0; i < t.NumField(); i++ { for i := 0; i < t.NumField(); i++ {
field := t.Field(i) field := t.Field(i)
if tag := field.Tag.Get(DefaultTagName); tag == "-" { if tag := field.Tag.Get(tagName); tag == "-" {
continue continue
} }
@ -247,7 +249,7 @@ func (s *Struct) IsZero() bool {
for _, field := range fields { for _, field := range fields {
val := s.value.FieldByName(field.Name) 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") { if IsStruct(val.Interface()) && !tagOpts.Has("omitnested") {
ok := IsZero(val.Interface()) ok := IsZero(val.Interface())
@ -294,7 +296,7 @@ func (s *Struct) HasZero() bool {
for _, field := range fields { for _, field := range fields {
val := s.value.FieldByName(field.Name) 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") { if IsStruct(val.Interface()) && !tagOpts.Has("omitnested") {
ok := HasZero(val.Interface()) ok := HasZero(val.Interface())
@ -341,7 +343,7 @@ func (s *Struct) structFields() []reflect.StructField {
} }
// don't check if it's omitted // don't check if it's omitted
if tag := field.Tag.Get(DefaultTagName); tag == "-" { if tag := field.Tag.Get(s.TagName); tag == "-" {
continue continue
} }

View File

@ -124,12 +124,10 @@ func TestMap_CustomTag(t *testing.T) {
C: true, C: true,
} }
defaultName := DefaultTagName s := New(T)
DefaultTagName = "dd" s.TagName = "dd"
defer func() {
DefaultTagName = defaultName a := s.Map()
}()
a := Map(T)
inMap := func(key interface{}) bool { inMap := func(key interface{}) bool {
for k := range a { for k := range a {