* Use String() of key if it exists during TreeFromMap
This commit is contained in:
Albert Nigmatzianov
2017-03-21 14:01:44 +05:00
committed by Thomas Pelletier
parent fee7787d3f
commit 62e2d802ed
2 changed files with 23 additions and 10 deletions
+7 -4
View File
@@ -48,6 +48,8 @@ func simpleValueCoercion(object interface{}) (interface{}, error) {
return uint64(original), nil
case float32:
return float64(original), nil
case fmt.Stringer:
return original.String(), nil
default:
return nil, fmt.Errorf("cannot convert type %T to TomlTree", object)
}
@@ -102,9 +104,10 @@ func toTree(object interface{}) (interface{}, error) {
values := map[string]interface{}{}
keys := value.MapKeys()
for _, key := range keys {
k, ok := key.Interface().(string)
if !ok {
return nil, fmt.Errorf("map key needs to be a string, not %T", key.Interface())
if key.Kind() != reflect.String {
if _, ok := key.Interface().(string); !ok {
return nil, fmt.Errorf("map key needs to be a string, not %T (%v)", key.Interface(), key.Kind())
}
}
v := value.MapIndex(key)
@@ -112,7 +115,7 @@ func toTree(object interface{}) (interface{}, error) {
if err != nil {
return nil, err
}
values[k] = newValue
values[key.String()] = newValue
}
return &TomlTree{values, Position{}}, nil
}