Handle nil, map[string]string, and map[interface{}]interface{} input (#103)
* Handle map[string]string and map[interface{}]interface{} input
* Handle nil values
Fixes #99
This commit is contained in:
committed by
Thomas Pelletier
parent
67b7b944a8
commit
45932ad32d
@@ -647,6 +647,7 @@ func TestToTomlValue(t *testing.T) {
|
|||||||
"1979-05-27T07:32:00Z"},
|
"1979-05-27T07:32:00Z"},
|
||||||
{[]interface{}{"gamma", "delta"},
|
{[]interface{}{"gamma", "delta"},
|
||||||
"[\n \"gamma\",\n \"delta\",\n]"},
|
"[\n \"gamma\",\n \"delta\",\n]"},
|
||||||
|
{nil, ""},
|
||||||
} {
|
} {
|
||||||
result := toTomlValue(item.Value, 0)
|
result := toTomlValue(item.Value, 0)
|
||||||
if result != item.Expect {
|
if result != item.Expect {
|
||||||
@@ -668,6 +669,28 @@ func TestToString(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestToStringMapStringString(t *testing.T) {
|
||||||
|
in := map[string]interface{}{"m": map[string]string{"v": "abc"}}
|
||||||
|
want := "\n[m]\n v = \"abc\"\n"
|
||||||
|
tree := TreeFromMap(in)
|
||||||
|
got := tree.String()
|
||||||
|
|
||||||
|
if got != want {
|
||||||
|
t.Errorf("want:\n%q\ngot:\n%q", want, got)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestToStringMapInterfaceInterface(t *testing.T) {
|
||||||
|
in := map[string]interface{}{"m": map[interface{}]interface{}{"v": "abc"}}
|
||||||
|
want := "\n[m]\n v = \"abc\"\n"
|
||||||
|
tree := TreeFromMap(in)
|
||||||
|
got := tree.String()
|
||||||
|
|
||||||
|
if got != want {
|
||||||
|
t.Errorf("want:\n%q\ngot:\n%q", want, got)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func assertPosition(t *testing.T, text string, ref map[string]Position) {
|
func assertPosition(t *testing.T, text string, ref map[string]Position) {
|
||||||
tree, err := Load(text)
|
tree, err := Load(text)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -84,6 +84,8 @@ func toTomlValue(item interface{}, indent int) string {
|
|||||||
result += toTomlValue(item, indent+2) + ",\n"
|
result += toTomlValue(item, indent+2) + ",\n"
|
||||||
}
|
}
|
||||||
return result + tab + "]"
|
return result + tab + "]"
|
||||||
|
case nil:
|
||||||
|
return ""
|
||||||
default:
|
default:
|
||||||
panic(fmt.Sprintf("unsupported value type %T: %v", value, value))
|
panic(fmt.Sprintf("unsupported value type %T: %v", value, value))
|
||||||
}
|
}
|
||||||
@@ -116,6 +118,20 @@ func (t *TomlTree) toToml(indent, keyspace string) string {
|
|||||||
case map[string]interface{}:
|
case map[string]interface{}:
|
||||||
sub := TreeFromMap(node)
|
sub := TreeFromMap(node)
|
||||||
|
|
||||||
|
if len(sub.Keys()) > 0 {
|
||||||
|
result += fmt.Sprintf("\n%s[%s]\n", indent, combinedKey)
|
||||||
|
}
|
||||||
|
result += sub.toToml(indent+" ", combinedKey)
|
||||||
|
case map[string]string:
|
||||||
|
sub := TreeFromMap(convertMapStringString(node))
|
||||||
|
|
||||||
|
if len(sub.Keys()) > 0 {
|
||||||
|
result += fmt.Sprintf("\n%s[%s]\n", indent, combinedKey)
|
||||||
|
}
|
||||||
|
result += sub.toToml(indent+" ", combinedKey)
|
||||||
|
case map[interface{}]interface{}:
|
||||||
|
sub := TreeFromMap(convertMapInterfaceInterface(node))
|
||||||
|
|
||||||
if len(sub.Keys()) > 0 {
|
if len(sub.Keys()) > 0 {
|
||||||
result += fmt.Sprintf("\n%s[%s]\n", indent, combinedKey)
|
result += fmt.Sprintf("\n%s[%s]\n", indent, combinedKey)
|
||||||
}
|
}
|
||||||
@@ -129,6 +145,22 @@ func (t *TomlTree) toToml(indent, keyspace string) string {
|
|||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func convertMapStringString(in map[string]string) map[string]interface{} {
|
||||||
|
result := make(map[string]interface{}, len(in))
|
||||||
|
for k, v := range in {
|
||||||
|
result[k] = v
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
func convertMapInterfaceInterface(in map[interface{}]interface{}) map[string]interface{} {
|
||||||
|
result := make(map[string]interface{}, len(in))
|
||||||
|
for k, v := range in {
|
||||||
|
result[k.(string)] = v
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
// ToString is an alias for String
|
// ToString is an alias for String
|
||||||
func (t *TomlTree) ToString() string {
|
func (t *TomlTree) ToString() string {
|
||||||
return t.String()
|
return t.String()
|
||||||
|
|||||||
Reference in New Issue
Block a user