Add support of mixed-type array (#376)

Fixes #357
This commit is contained in:
Allen
2020-05-07 11:07:57 +08:00
committed by GitHub
parent 9ccd9bbc7a
commit c5fbd3eba6
6 changed files with 122 additions and 50 deletions
+41 -4
View File
@@ -103,7 +103,42 @@ func encodeTomlString(value string) string {
return b.String()
}
func tomlValueStringRepresentation(v interface{}, commented string, indent string, arraysOneElementPerLine bool) (string, error) {
func tomlTreeStringRepresentation(t *Tree, ord marshalOrder) (string, error) {
var orderedVals []sortNode
switch ord {
case OrderPreserve:
orderedVals = sortByLines(t)
default:
orderedVals = sortAlphabetical(t)
}
stringBuffer := bytes.Buffer{}
stringBuffer.WriteString(`{`)
first := true
for i := range orderedVals {
v := t.values[orderedVals[i].key]
quotedKey := quoteKeyIfNeeded(orderedVals[i].key)
valueStr, err := tomlValueStringRepresentation(v, "", "", ord, false)
if err != nil {
return "", err
}
if first {
first = false
} else {
stringBuffer.WriteString(`,`)
}
stringBuffer.WriteString(quotedKey)
stringBuffer.WriteString(" = ")
stringBuffer.WriteString(valueStr)
}
stringBuffer.WriteString(`}`)
return stringBuffer.String(), nil
}
func tomlValueStringRepresentation(v interface{}, commented string, indent string, ord marshalOrder, arraysOneElementPerLine bool) (string, error) {
// this interface check is added to dereference the change made in the writeTo function.
// That change was made to allow this function to see formatting options.
tv, ok := v.(*tomlValue)
@@ -140,7 +175,7 @@ func tomlValueStringRepresentation(v interface{}, commented string, indent strin
return "\"" + encodeTomlString(value) + "\"", nil
case []byte:
b, _ := v.([]byte)
return tomlValueStringRepresentation(string(b), commented, indent, arraysOneElementPerLine)
return tomlValueStringRepresentation(string(b), commented, indent, ord, arraysOneElementPerLine)
case bool:
if value {
return "true", nil
@@ -154,6 +189,8 @@ func tomlValueStringRepresentation(v interface{}, commented string, indent strin
return value.String(), nil
case LocalTime:
return value.String(), nil
case *Tree:
return tomlTreeStringRepresentation(value, ord)
case nil:
return "", nil
}
@@ -164,7 +201,7 @@ func tomlValueStringRepresentation(v interface{}, commented string, indent strin
var values []string
for i := 0; i < rv.Len(); i++ {
item := rv.Index(i).Interface()
itemRepr, err := tomlValueStringRepresentation(item, commented, indent, arraysOneElementPerLine)
itemRepr, err := tomlValueStringRepresentation(item, commented, indent, ord, arraysOneElementPerLine)
if err != nil {
return "", err
}
@@ -368,7 +405,7 @@ func (t *Tree) writeToOrdered(w io.Writer, indent, keyspace string, bytesCount i
if parentCommented || t.commented || v.commented {
commented = "# "
}
repr, err := tomlValueStringRepresentation(v, commented, indent, arraysOneElementPerLine)
repr, err := tomlValueStringRepresentation(v, commented, indent, ord, arraysOneElementPerLine)
if err != nil {
return bytesCount, err
}