From d00d2cca6edb4ea8c1292d474008134687f703c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Wei=C3=9Fe?= <66256922+daniel-weisse@users.noreply.github.com> Date: Fri, 12 Apr 2024 16:42:12 +0200 Subject: [PATCH] Fix indentation of custom type arrays (#944) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Daniel Weiße --- marshaler.go | 4 ++++ marshaler_test.go | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/marshaler.go b/marshaler.go index ffc99272..7f4e20c1 100644 --- a/marshaler.go +++ b/marshaler.go @@ -1025,6 +1025,10 @@ func (enc *Encoder) encodeSliceAsArrayTable(b []byte, ctx encoderCtx, v reflect. scratch = enc.commented(ctx.commented, scratch) + if enc.indentTables { + scratch = enc.indent(ctx.indent, scratch) + } + scratch = append(scratch, "[["...) for i, k := range ctx.parentKey { diff --git a/marshaler_test.go b/marshaler_test.go index 23b0efcd..b3473e38 100644 --- a/marshaler_test.go +++ b/marshaler_test.go @@ -1499,6 +1499,43 @@ func TestMarshalCommented(t *testing.T) { require.Equal(t, expected, string(out)) } +func TestMarshalIndentedCustomTypeArray(t *testing.T) { + c := struct { + Nested struct { + NestedArray []struct { + Value int + } + } + }{ + Nested: struct { + NestedArray []struct { + Value int + } + }{ + NestedArray: []struct { + Value int + }{ + {Value: 1}, + {Value: 2}, + }, + }, + } + + expected := `[Nested] + [[Nested.NestedArray]] + Value = 1 + + [[Nested.NestedArray]] + Value = 2 +` + + var buf bytes.Buffer + enc := toml.NewEncoder(&buf) + enc.SetIndentTables(true) + require.NoError(t, enc.Encode(c)) + require.Equal(t, expected, buf.String()) +} + func ExampleMarshal() { type MyConfig struct { Version int