aboutsummaryrefslogtreecommitdiff
path: root/vendor/github.com/pelletier/go-toml/tomltree_conversions.go
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/github.com/pelletier/go-toml/tomltree_conversions.go')
-rw-r--r--vendor/github.com/pelletier/go-toml/tomltree_conversions.go32
1 files changed, 26 insertions, 6 deletions
diff --git a/vendor/github.com/pelletier/go-toml/tomltree_conversions.go b/vendor/github.com/pelletier/go-toml/tomltree_conversions.go
index db3da0d..fc8f22b 100644
--- a/vendor/github.com/pelletier/go-toml/tomltree_conversions.go
+++ b/vendor/github.com/pelletier/go-toml/tomltree_conversions.go
@@ -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{}.