Skip to content

Commit

Permalink
file: customize output delimiter (#226)
Browse files Browse the repository at this point in the history
* customization output delimiter

* rename KeyValueDelimitersOutput to KeyValueDelimiterOnWrite

* add test for writing with KeyValueDelimiterOnWrite

* bugfix: use '=' as the default delimiter on write

Co-authored-by: Chaliy Roman <cerebrum.ch@gmail.com>
  • Loading branch information
aligator and lasiar committed Mar 7, 2020
1 parent dd614b8 commit 8e0f5b3
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
7 changes: 5 additions & 2 deletions file.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ func newFile(dataSources []dataSource, opts LoadOptions) *File {
if len(opts.KeyValueDelimiters) == 0 {
opts.KeyValueDelimiters = "=:"
}
if len(opts.KeyValueDelimiterOnWrite) == 0 {
opts.KeyValueDelimiterOnWrite = "="
}
return &File{
BlockMode: true,
dataSources: dataSources,
Expand Down Expand Up @@ -230,10 +233,10 @@ func (f *File) Append(source interface{}, others ...interface{}) error {
}

func (f *File) writeToBuffer(indent string) (*bytes.Buffer, error) {
equalSign := DefaultFormatLeft + "=" + DefaultFormatRight
equalSign := DefaultFormatLeft + f.options.KeyValueDelimiterOnWrite + DefaultFormatRight

if PrettyFormat || PrettyEqual {
equalSign = " = "
equalSign = fmt.Sprintf(" %s ", f.options.KeyValueDelimiterOnWrite)
}

// Use buffer to make sure target is safe until finish encoding.
Expand Down
37 changes: 37 additions & 0 deletions file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,43 @@ func TestFile_SaveTo(t *testing.T) {
})
}

func TestFile_WriteToWithOutputDelimiter(t *testing.T) {
Convey("Write content to somewhere using a custom output delimiter", t, func() {
f, err := ini.LoadSources(ini.LoadOptions{
KeyValueDelimiterOnWrite: "->",
}, []byte(`[Others]
Cities = HangZhou|Boston
Visits = 1993-10-07T20:17:05Z, 1993-10-07T20:17:05Z
Years = 1993,1994
Numbers = 10010,10086
Ages = 18,19
Populations = 12345678,98765432
Coordinates = 192.168,10.11
Flags = true,false
Note = Hello world!`))
So(err, ShouldBeNil)
So(f, ShouldNotBeNil)

var actual bytes.Buffer
var expected = []byte(`[Others]
Cities -> HangZhou|Boston
Visits -> 1993-10-07T20:17:05Z, 1993-10-07T20:17:05Z
Years -> 1993,1994
Numbers -> 10010,10086
Ages -> 18,19
Populations -> 12345678,98765432
Coordinates -> 192.168,10.11
Flags -> true,false
Note -> Hello world!
`)
_, err = f.WriteTo(&actual)
So(err, ShouldBeNil)

So(bytes.Equal(expected, actual.Bytes()), ShouldBeTrue)
})
}

// Inspired by https://github.com/go-ini/ini/issues/207
func TestReloadAfterShadowLoad(t *testing.T) {
Convey("Reload file after ShadowLoad", t, func() {
Expand Down
2 changes: 2 additions & 0 deletions ini.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ type LoadOptions struct {
UnparseableSections []string
// KeyValueDelimiters is the sequence of delimiters that are used to separate key and value. By default, it is "=:".
KeyValueDelimiters string
// KeyValueDelimiters is the delimiter that are used to separate key and value output. By default, it is "=".
KeyValueDelimiterOnWrite string
// PreserveSurroundedQuote indicates whether to preserve surrounded quote (single and double quotes).
PreserveSurroundedQuote bool
// DebugFunc is called to collect debug information (currently only useful to debug parsing Python-style multiline values).
Expand Down

0 comments on commit 8e0f5b3

Please sign in to comment.