Skip to content

Commit

Permalink
Handle nil, map[string]string, and map[interface{}]interface{} input (#…
Browse files Browse the repository at this point in the history
…103)

* Handle map[string]string and map[interface{}]interface{} input
* Handle nil values

Fixes #99
  • Loading branch information
moorereason authored and pelletier committed Sep 20, 2016
1 parent 67b7b94 commit 45932ad
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
23 changes: 23 additions & 0 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -647,6 +647,7 @@ func TestToTomlValue(t *testing.T) {
"1979-05-27T07:32:00Z"},
{[]interface{}{"gamma", "delta"},
"[\n \"gamma\",\n \"delta\",\n]"},
{nil, ""},
} {
result := toTomlValue(item.Value, 0)
if result != item.Expect {
Expand All @@ -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) {
tree, err := Load(text)
if err != nil {
Expand Down
32 changes: 32 additions & 0 deletions tomltree_conversions.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ func toTomlValue(item interface{}, indent int) string {
result += toTomlValue(item, indent+2) + ",\n"
}
return result + tab + "]"
case nil:
return ""
default:
panic(fmt.Sprintf("unsupported value type %T: %v", value, value))
}
Expand Down Expand Up @@ -116,6 +118,20 @@ func (t *TomlTree) toToml(indent, keyspace string) string {
case map[string]interface{}:
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 {
result += fmt.Sprintf("\n%s[%s]\n", indent, combinedKey)
}
Expand All @@ -129,6 +145,22 @@ func (t *TomlTree) toToml(indent, keyspace string) string {
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
func (t *TomlTree) ToString() string {
return t.String()
Expand Down

0 comments on commit 45932ad

Please sign in to comment.