Skip to content

Commit

Permalink
Coerce maps to map[string]interface{} values
Browse files Browse the repository at this point in the history
This address Issue #48.
  • Loading branch information
Charles Banning committed Apr 18, 2018
1 parent 4e06f4e commit 1f00e0b
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions xml.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"errors"
"fmt"
"io"
"reflect"
"sort"
"strconv"
"strings"
Expand Down Expand Up @@ -811,6 +812,20 @@ func mapToXmlIndent(doIndent bool, s *string, key string, value interface{}, pp
var elen int
p := &pretty{pp.indent, pp.cnt, pp.padding, pp.mapDepth, pp.start}

// per issue #48, 18apr18 - try and coerce maps to map[string]interface{}
// Don't need for mapToXmlSeqIndent, since maps there are decoded by NewMapXmlSeq().
if reflect.ValueOf(value).Kind() == reflect.Map {
if _, ok := value.(map[string]interface{}); !ok {
val := make(map[string]interface{})
keys := reflect.ValueOf(value).MapKeys()
vv := reflect.ValueOf(value)
for _, k := range keys {
val[fmt.Sprint(k)] = vv.MapIndex(k).Interface()
}
value = val
}
}

switch value.(type) {
// special handling of []interface{} values when len(value) == 0
case map[string]interface{}, []byte, string, float64, bool, int, int32, int64, float32, json.Number:
Expand Down

0 comments on commit 1f00e0b

Please sign in to comment.