Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle nil, map[string]string, and map[interface{}]interface{} input #103

Merged
merged 2 commits into from
Sep 20, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,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 @@ -658,6 +659,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 @@ -64,6 +64,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: %v", value))
}
Expand Down Expand Up @@ -96,6 +98,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 @@ -109,6 +125,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