Add FillMap(s interface{}, out map[string]interface{})

This commit is contained in:
polaris 2016-03-14 17:00:24 +08:00
parent 12ff68a6f4
commit 179aa9d3d4

View File

@ -74,6 +74,15 @@ func New(s interface{}) *Struct {
// fields will be neglected.
func (s *Struct) Map() map[string]interface{} {
out := make(map[string]interface{})
s.FillMap(out)
return out
}
// FillMap is the same as Map. Instead of returning the output, it fills the given input
func (s *Struct) FillMap(out map[string]interface{}) {
if out == nil {
return
}
fields := s.structFields()
@ -124,8 +133,6 @@ func (s *Struct) Map() map[string]interface{} {
out[name] = finalVal
}
return out
}
// Values converts the given s struct's field values to a []interface{}. A
@ -427,6 +434,11 @@ func Map(s interface{}) map[string]interface{} {
return New(s).Map()
}
// FillMap is the same as Map. Instead of returning the output, it fills the given input
func FillMap(s interface{}, out map[string]interface{}) {
New(s).FillMap(out)
}
// Values converts the given struct to a []interface{}. For more info refer to
// Struct types Values() method. It panics if s's kind is not struct.
func Values(s interface{}) []interface{} {