+2
-2
@@ -367,7 +367,7 @@ func (e *Encoder) PromoteAnonymous(promote bool) *Encoder {
|
|||||||
func (e *Encoder) marshal(v interface{}) ([]byte, error) {
|
func (e *Encoder) marshal(v interface{}) ([]byte, error) {
|
||||||
// Check if indentation is valid
|
// Check if indentation is valid
|
||||||
for _, char := range e.indentation {
|
for _, char := range e.indentation {
|
||||||
if !(char == ' ' || char == '\t') {
|
if !isSpace(char) {
|
||||||
return []byte{}, fmt.Errorf("invalid indentation: must only contains space or tab characters")
|
return []byte{}, fmt.Errorf("invalid indentation: must only contains space or tab characters")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -475,7 +475,7 @@ func (e *Encoder) valueToTree(mtype reflect.Type, mval reflect.Value) (*Tree, er
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if e.quoteMapKeys {
|
if e.quoteMapKeys {
|
||||||
keyStr, err := tomlValueStringRepresentation(key.String(), "", "", OrderPreserve, e.arraysOneElementPerLine)
|
keyStr, err := tomlValueStringRepresentation(key.String(), "", "", e.order, e.arraysOneElementPerLine)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1988,6 +1988,58 @@ func TestMarshalSlicePointer(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestMarshalNestedArrayInlineTables(t *testing.T) {
|
||||||
|
type table struct {
|
||||||
|
Value1 int `toml:"ZValue1"`
|
||||||
|
Value2 int `toml:"YValue2"`
|
||||||
|
Value3 int `toml:"XValue3"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type nestedTable struct {
|
||||||
|
Table table
|
||||||
|
}
|
||||||
|
|
||||||
|
nestedArray := struct {
|
||||||
|
Simple [][]table
|
||||||
|
SimplePointer *[]*[]table
|
||||||
|
Nested [][]nestedTable
|
||||||
|
NestedPointer *[]*[]nestedTable
|
||||||
|
}{
|
||||||
|
Simple: [][]table{{{Value1: 1}, {Value1: 10}}},
|
||||||
|
SimplePointer: &[]*[]table{{{Value2: 2}}},
|
||||||
|
Nested: [][]nestedTable{{{Table: table{Value3: 3}}}},
|
||||||
|
NestedPointer: &[]*[]nestedTable{{{Table: table{Value3: -3}}}},
|
||||||
|
}
|
||||||
|
|
||||||
|
expectedPreserve := `Simple = [[{ ZValue1 = 1, YValue2 = 0, XValue3 = 0 }, { ZValue1 = 10, YValue2 = 0, XValue3 = 0 }]]
|
||||||
|
SimplePointer = [[{ ZValue1 = 0, YValue2 = 2, XValue3 = 0 }]]
|
||||||
|
Nested = [[{ Table = { ZValue1 = 0, YValue2 = 0, XValue3 = 3 } }]]
|
||||||
|
NestedPointer = [[{ Table = { ZValue1 = 0, YValue2 = 0, XValue3 = -3 } }]]
|
||||||
|
`
|
||||||
|
|
||||||
|
expectedAlphabetical := `Nested = [[{ Table = { XValue3 = 3, YValue2 = 0, ZValue1 = 0 } }]]
|
||||||
|
NestedPointer = [[{ Table = { XValue3 = -3, YValue2 = 0, ZValue1 = 0 } }]]
|
||||||
|
Simple = [[{ XValue3 = 0, YValue2 = 0, ZValue1 = 1 }, { XValue3 = 0, YValue2 = 0, ZValue1 = 10 }]]
|
||||||
|
SimplePointer = [[{ XValue3 = 0, YValue2 = 2, ZValue1 = 0 }]]
|
||||||
|
`
|
||||||
|
|
||||||
|
var bufPreserve bytes.Buffer
|
||||||
|
if err := NewEncoder(&bufPreserve).Order(OrderPreserve).Encode(nestedArray); err != nil {
|
||||||
|
t.Fatalf("unexpected error: %s", err.Error())
|
||||||
|
}
|
||||||
|
if !bytes.Equal(bufPreserve.Bytes(), []byte(expectedPreserve)) {
|
||||||
|
t.Errorf("Bad marshal: expected\n-----\n%s\n-----\ngot\n-----\n%s\n-----\n", expectedPreserve, bufPreserve.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
var bufAlphabetical bytes.Buffer
|
||||||
|
if err := NewEncoder(&bufAlphabetical).Order(OrderAlphabetical).Encode(nestedArray); err != nil {
|
||||||
|
t.Fatalf("unexpected error: %s", err.Error())
|
||||||
|
}
|
||||||
|
if !bytes.Equal(bufAlphabetical.Bytes(), []byte(expectedAlphabetical)) {
|
||||||
|
t.Errorf("Bad marshal: expected\n-----\n%s\n-----\ngot\n-----\n%s\n-----\n", expectedAlphabetical, bufAlphabetical.String())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
type testDuration struct {
|
type testDuration struct {
|
||||||
Nanosec time.Duration `toml:"nanosec"`
|
Nanosec time.Duration `toml:"nanosec"`
|
||||||
Microsec1 time.Duration `toml:"microsec1"`
|
Microsec1 time.Duration `toml:"microsec1"`
|
||||||
|
|||||||
+1
-1
@@ -922,7 +922,7 @@ func TestTomlValueStringRepresentation(t *testing.T) {
|
|||||||
{[]interface{}{"gamma", "delta"}, "[\"gamma\", \"delta\"]"},
|
{[]interface{}{"gamma", "delta"}, "[\"gamma\", \"delta\"]"},
|
||||||
{nil, ""},
|
{nil, ""},
|
||||||
} {
|
} {
|
||||||
result, err := tomlValueStringRepresentation(item.Value, "", "", OrderPreserve, false)
|
result, err := tomlValueStringRepresentation(item.Value, "", "", OrderAlphabetical, false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Test %d - unexpected error: %s", idx, err)
|
t.Errorf("Test %d - unexpected error: %s", idx, err)
|
||||||
}
|
}
|
||||||
|
|||||||
+8
-20
@@ -105,7 +105,6 @@ func encodeTomlString(value string) string {
|
|||||||
|
|
||||||
func tomlTreeStringRepresentation(t *Tree, ord marshalOrder) (string, error) {
|
func tomlTreeStringRepresentation(t *Tree, ord marshalOrder) (string, error) {
|
||||||
var orderedVals []sortNode
|
var orderedVals []sortNode
|
||||||
|
|
||||||
switch ord {
|
switch ord {
|
||||||
case OrderPreserve:
|
case OrderPreserve:
|
||||||
orderedVals = sortByLines(t)
|
orderedVals = sortByLines(t)
|
||||||
@@ -113,29 +112,18 @@ func tomlTreeStringRepresentation(t *Tree, ord marshalOrder) (string, error) {
|
|||||||
orderedVals = sortAlphabetical(t)
|
orderedVals = sortAlphabetical(t)
|
||||||
}
|
}
|
||||||
|
|
||||||
stringBuffer := bytes.Buffer{}
|
var values []string
|
||||||
stringBuffer.WriteString(`{`)
|
for _, node := range orderedVals {
|
||||||
first := true
|
k := node.key
|
||||||
for i := range orderedVals {
|
v := t.values[k]
|
||||||
v := t.values[orderedVals[i].key]
|
|
||||||
quotedKey := quoteKeyIfNeeded(orderedVals[i].key)
|
repr, err := tomlValueStringRepresentation(v, "", "", ord, false)
|
||||||
valueStr, err := tomlValueStringRepresentation(v, "", "", ord, false)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
if first {
|
values = append(values, quoteKeyIfNeeded(k)+" = "+repr)
|
||||||
first = false
|
|
||||||
} else {
|
|
||||||
stringBuffer.WriteString(`,`)
|
|
||||||
}
|
}
|
||||||
|
return "{ " + strings.Join(values, ", ") + " }", nil
|
||||||
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) {
|
func tomlValueStringRepresentation(v interface{}, commented string, indent string, ord marshalOrder, arraysOneElementPerLine bool) (string, error) {
|
||||||
|
|||||||
Reference in New Issue
Block a user