Add Encoder opt to emit arrays on multiple lines (#203)
A new Encoder option emits arrays with more than one line on multiple lines. This is off by default and toggled with `ArraysWithOneElementPerLine`. For example: ``` A = [1,2,3] ``` Becomes: ``` A = [ 1, 2, 3 ] ``` Fixes #200
This commit is contained in:
@@ -721,6 +721,7 @@ func TestEncodeQuotedMapKeys(t *testing.T) {
|
||||
t.Errorf("Bad maps marshal: expected\n-----\n%s\n-----\ngot\n-----\n%s\n-----\n", expected, result)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDecodeQuotedMapKeys(t *testing.T) {
|
||||
result := mapsTestStruct{}
|
||||
err := NewDecoder(bytes.NewBuffer(mapsTestToml)).Decode(&result)
|
||||
@@ -732,3 +733,74 @@ func TestDecodeQuotedMapKeys(t *testing.T) {
|
||||
t.Errorf("Bad maps unmarshal: expected %v, got %v", expected, result)
|
||||
}
|
||||
}
|
||||
|
||||
type structArrayNoTag struct {
|
||||
A struct {
|
||||
B []int64
|
||||
C []int64
|
||||
}
|
||||
}
|
||||
|
||||
func TestMarshalArray(t *testing.T) {
|
||||
expected := []byte(`
|
||||
[A]
|
||||
B = [1,2,3]
|
||||
C = [1]
|
||||
`)
|
||||
|
||||
m := structArrayNoTag{
|
||||
A: struct {
|
||||
B []int64
|
||||
C []int64
|
||||
}{
|
||||
B: []int64{1, 2, 3},
|
||||
C: []int64{1},
|
||||
},
|
||||
}
|
||||
|
||||
b, err := Marshal(m)
|
||||
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if !bytes.Equal(b, expected) {
|
||||
t.Errorf("Bad arrays marshal: expected\n-----\n%s\n-----\ngot\n-----\n%s\n-----\n", expected, b)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMarshalArrayOnePerLine(t *testing.T) {
|
||||
expected := []byte(`
|
||||
[A]
|
||||
B = [
|
||||
1,
|
||||
2,
|
||||
3
|
||||
]
|
||||
C = [1]
|
||||
`)
|
||||
|
||||
m := structArrayNoTag{
|
||||
A: struct {
|
||||
B []int64
|
||||
C []int64
|
||||
}{
|
||||
B: []int64{1, 2, 3},
|
||||
C: []int64{1},
|
||||
},
|
||||
}
|
||||
|
||||
var buf bytes.Buffer
|
||||
encoder := NewEncoder(&buf).ArraysWithOneElementPerLine(true)
|
||||
err := encoder.Encode(m)
|
||||
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
b := buf.Bytes()
|
||||
|
||||
if !bytes.Equal(b, expected) {
|
||||
t.Errorf("Bad arrays marshal: expected\n-----\n%s\n-----\ngot\n-----\n%s\n-----\n", expected, b)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user