structs: add omitempty test

This commit is contained in:
Fatih Arslan 2014-09-14 20:43:08 +03:00
parent 9c8bc21e6a
commit 782d098eb6
2 changed files with 23 additions and 1 deletions

View File

@ -76,7 +76,8 @@ func (s *Struct) Map() map[string]interface{} {
name = tagName
}
// if the value is a zero value do not include
// if the value is a zero value and the field is marked as omitempty do
// not include
if tagOpts.Has("omitempty") {
zero := reflect.Zero(val.Type()).Interface()
current := val.Interface()

View File

@ -148,6 +148,27 @@ func TestMap_CustomTag(t *testing.T) {
}
func TestMap_OmitEmpty(t *testing.T) {
type A struct {
Name string
Value string `structs:",omitempty"`
Time time.Time `structs:",omitempty"`
}
a := A{}
m := Map(a)
_, ok := m["Value"].(map[string]interface{})
if ok {
t.Error("Map should not contain the Value field that is tagged as omitempty")
}
_, ok = m["Time"].(map[string]interface{})
if ok {
t.Error("Map should not contain the Time field that is tagged as omitempty")
}
}
func TestMap_OmitNested(t *testing.T) {
type A struct {
Name string