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
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

View File

@ -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
}

View File

@ -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 {