8000 file: indent subsequent shadow keys on write by team-humaki · Pull Request #403 · go-ini/ini · GitHub
[go: up one dir, main page]

Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion file.go
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,13 @@ func (f *File) writeToBuffer(indent string) (*bytes.Buffer, error) {
}
}

for _, val := range shadows {
for i, val := range shadows {
// Indent is written once before the key. Repeat it for
// subsequent shadow keys so they keep the same indent
// (https://github.com/go-ini/ini/issues/367).
if i > 0 && len(indent) > 0 && sname != DefaultSection {
buf.WriteString(indent)
}
exitLoop, err := writeKeyValue(val)
if err != nil {
return nil, err
Expand Down
25 changes: 25 additions & 0 deletions file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,31 @@ bar3 = " val ue3 "
bar1 = " val ue1 "
bar2 = " val ue2 "
bar3 = " val ue3 "
`, buf.String())
})

t.Run("indent shadow keys the same as the first key", func(t *testing.T) {
f, err := LoadSources(LoadOptions{
KeyValueDelimiters: " ",
KeyValueDelimiterOnWrite: " ",
AllowShadows: true,
}, []byte(`[FILTER]
whitelist_key _CMDLINE
whitelist_key _EXE
whitelist_key _HOSTNAME
record filename systemd
`))
require.NoError(t, err)
require.NotNil(t, f)

var buf bytes.Buffer
_, err = f.WriteToIndent(&buf, " ")
require.NoError(t, err)
assert.Equal(t, `[FILTER]
whitelist_key _CMDLINE
whitelist_key _EXE
whitelist_key _HOSTNAME
record filename systemd
`, buf.String())
})
}
Expand Down
0