Make ToString() return an error instead of panic (#117)

Fixes #100
This commit is contained in:
Thomas Pelletier
2017-01-15 18:49:11 -08:00
committed by GitHub
parent ee2c0b51cf
commit a1f048ba24
3 changed files with 47 additions and 9 deletions
+26 -6
View File
@@ -87,7 +87,7 @@ func toTomlValue(item interface{}, indent int) string {
case nil:
return ""
default:
panic(fmt.Sprintf("unsupported value type %T: %v", value, value))
panic(fmt.Errorf("unsupported value type %T: %v", value, value))
}
}
@@ -154,6 +154,23 @@ func (t *TomlTree) toToml(indent, keyspace string) string {
return strings.Join(resultChunks, "")
}
// Same as ToToml(), but does not panic and returns an error
func (t *TomlTree) toTomlSafe(indent, keyspace string) (result string, err error) {
defer func() {
if r := recover(); r != nil {
result = ""
switch x := r.(type) {
case error:
err = x
default:
err = fmt.Errorf("unknown panic: %s", r)
}
}
}()
result = t.toToml(indent, keyspace)
return
}
func convertMapStringString(in map[string]string) map[string]interface{} {
result := make(map[string]interface{}, len(in))
for k, v := range in {
@@ -170,15 +187,18 @@ func convertMapInterfaceInterface(in map[interface{}]interface{}) map[string]int
return result
}
// ToString is an alias for String
func (t *TomlTree) ToString() string {
return t.String()
// ToString generates a human-readable representation of the current tree.
// Output spans multiple lines, and is suitable for ingest by a TOML parser.
// If the conversion cannot be performed, ToString returns a non-nil error.
func (t *TomlTree) ToString() (string, error) {
return t.toTomlSafe("", "")
}
// String generates a human-readable representation of the current tree.
// Output spans multiple lines, and is suitable for ingest by a TOML parser
// Alias of ToString.
func (t *TomlTree) String() string {
return t.toToml("", "")
result, _ := t.ToString()
return result
}
// ToMap recursively generates a representation of the current tree using map[string]interface{}.