From 090cccf4bac30af63f82eaf7225077b47c85f8e9 Mon Sep 17 00:00:00 2001 From: Cuong Manh Le Date: Wed, 1 Feb 2023 18:00:09 +0700 Subject: [PATCH] Fix inline table first key value whitespace (#837) Co-authored-by: Cuong Manh Le --- marshaler.go | 2 +- marshaler_test.go | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/marshaler.go b/marshaler.go index 07aceb90..b3b177e7 100644 --- a/marshaler.go +++ b/marshaler.go @@ -357,9 +357,9 @@ func (enc *Encoder) encodeKv(b []byte, ctx encoderCtx, options valueOptions, v r if !ctx.inline { b = enc.encodeComment(ctx.indent, options.comment, b) + b = enc.indent(ctx.indent, b) } - b = enc.indent(ctx.indent, b) b = enc.encodeKey(b, ctx.key) b = append(b, " = "...) diff --git a/marshaler_test.go b/marshaler_test.go index d7b4e520..0a9d7445 100644 --- a/marshaler_test.go +++ b/marshaler_test.go @@ -1190,6 +1190,27 @@ func TestMarshalUint64Overflow(t *testing.T) { require.Error(t, err) } +func TestIndentWithInlineTable(t *testing.T) { + x := map[string][]map[string]string{ + "one": []map[string]string{ + {"0": "0"}, + {"1": "1"}, + }, + } + expected := `one = [ + {0 = '0'}, + {1 = '1'} +] +` + var buf bytes.Buffer + enc := toml.NewEncoder(&buf) + enc.SetIndentTables(true) + enc.SetTablesInline(true) + enc.SetArraysMultiline(true) + require.NoError(t, enc.Encode(x)) + assert.Equal(t, expected, buf.String()) +} + func ExampleMarshal() { type MyConfig struct { Version int