committed by
Thomas Pelletier
parent
fee7787d3f
commit
62e2d802ed
+7
-4
@@ -48,6 +48,8 @@ func simpleValueCoercion(object interface{}) (interface{}, error) {
|
|||||||
return uint64(original), nil
|
return uint64(original), nil
|
||||||
case float32:
|
case float32:
|
||||||
return float64(original), nil
|
return float64(original), nil
|
||||||
|
case fmt.Stringer:
|
||||||
|
return original.String(), nil
|
||||||
default:
|
default:
|
||||||
return nil, fmt.Errorf("cannot convert type %T to TomlTree", object)
|
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{}{}
|
values := map[string]interface{}{}
|
||||||
keys := value.MapKeys()
|
keys := value.MapKeys()
|
||||||
for _, key := range keys {
|
for _, key := range keys {
|
||||||
k, ok := key.Interface().(string)
|
if key.Kind() != reflect.String {
|
||||||
if !ok {
|
if _, ok := key.Interface().(string); !ok {
|
||||||
return nil, fmt.Errorf("map key needs to be a string, not %T", key.Interface())
|
return nil, fmt.Errorf("map key needs to be a string, not %T (%v)", key.Interface(), key.Kind())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
v := value.MapIndex(key)
|
v := value.MapIndex(key)
|
||||||
@@ -112,7 +115,7 @@ func toTree(object interface{}) (interface{}, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
values[k] = newValue
|
values[key.String()] = newValue
|
||||||
}
|
}
|
||||||
return &TomlTree{values, Position{}}, nil
|
return &TomlTree{values, Position{}}, nil
|
||||||
}
|
}
|
||||||
|
|||||||
+16
-6
@@ -5,6 +5,14 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type customString string
|
||||||
|
|
||||||
|
type stringer struct{}
|
||||||
|
|
||||||
|
func (s stringer) String() string {
|
||||||
|
return "stringer"
|
||||||
|
}
|
||||||
|
|
||||||
func validate(t *testing.T, path string, object interface{}) {
|
func validate(t *testing.T, path string, object interface{}) {
|
||||||
switch o := object.(type) {
|
switch o := object.(type) {
|
||||||
case *TomlTree:
|
case *TomlTree:
|
||||||
@@ -46,14 +54,16 @@ func TestTomlTreeCreateToTree(t *testing.T) {
|
|||||||
"uint32": uint32(2),
|
"uint32": uint32(2),
|
||||||
"float32": float32(2),
|
"float32": float32(2),
|
||||||
"a_bool": false,
|
"a_bool": false,
|
||||||
|
"stringer": stringer{},
|
||||||
"nested": map[string]interface{}{
|
"nested": map[string]interface{}{
|
||||||
"foo": "bar",
|
"foo": "bar",
|
||||||
},
|
},
|
||||||
"array": []string{"a", "b", "c"},
|
"array": []string{"a", "b", "c"},
|
||||||
"array_uint": []uint{uint(1), uint(2)},
|
"array_uint": []uint{uint(1), uint(2)},
|
||||||
"array_table": []map[string]interface{}{map[string]interface{}{"sub_map": 52}},
|
"array_table": []map[string]interface{}{map[string]interface{}{"sub_map": 52}},
|
||||||
"array_times": []time.Time{time.Now(), time.Now()},
|
"array_times": []time.Time{time.Now(), time.Now()},
|
||||||
"map_times": map[string]time.Time{"now": time.Now()},
|
"map_times": map[string]time.Time{"now": time.Now()},
|
||||||
|
"custom_string_map_key": map[customString]interface{}{customString("custom"): "custom"},
|
||||||
}
|
}
|
||||||
tree, err := TreeFromMap(data)
|
tree, err := TreeFromMap(data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -72,7 +82,7 @@ func TestTomlTreeCreateToTreeInvalidLeafType(t *testing.T) {
|
|||||||
|
|
||||||
func TestTomlTreeCreateToTreeInvalidMapKeyType(t *testing.T) {
|
func TestTomlTreeCreateToTreeInvalidMapKeyType(t *testing.T) {
|
||||||
_, err := TreeFromMap(map[string]interface{}{"foo": map[int]interface{}{2: 1}})
|
_, err := TreeFromMap(map[string]interface{}{"foo": map[int]interface{}{2: 1}})
|
||||||
expected := "map key needs to be a string, not int"
|
expected := "map key needs to be a string, not int (int)"
|
||||||
if err.Error() != expected {
|
if err.Error() != expected {
|
||||||
t.Fatalf("expected error %s, got %s", expected, err.Error())
|
t.Fatalf("expected error %s, got %s", expected, err.Error())
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user