Skip to content

Commit

Permalink
DRY for Marshaller
Browse files Browse the repository at this point in the history
  • Loading branch information
khaf committed Jun 9, 2015
1 parent 3a4f98b commit a355e95
Showing 1 changed file with 5 additions and 16 deletions.
21 changes: 5 additions & 16 deletions marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func valueToInterface(f reflect.Value) interface{} {
newMap[valueToInterface(mk)] = valueToInterface(f.MapIndex(mk))
}

return f.Interface()
return newMap
case reflect.Slice, reflect.Array:
if f.Kind() == reflect.Slice && f.IsNil() {
return nil
Expand Down Expand Up @@ -137,7 +137,6 @@ func structToMap(s reflect.Value) map[string]interface{} {

func marshal(v interface{}) []*Bin {
s := reflect.Indirect(reflect.ValueOf(v).Elem())
typeOfT := s.Type()

// map tags
cacheObjectTags(s)
Expand All @@ -146,21 +145,11 @@ func marshal(v interface{}) []*Bin {
bins := binPool.Get(numFields).([]*Bin)

binCount := 0
for i := 0; i < numFields; i++ {
// skip unexported fields
if typeOfT.Field(i).PkgPath != "" {
continue
}

binValue := valueToInterface(s.Field(i))

alias := fieldAlias(typeOfT.Field(i))
if alias == "" {
continue
}
n := structToMap(s)
for k, v := range n {
bins[binCount].Name = k

bins[binCount].Name = alias
bins[binCount].Value = NewValue(binValue)
bins[binCount].Value = NewValue(v)
binCount++
}

Expand Down

0 comments on commit a355e95

Please sign in to comment.