diff --git a/file.go b/file.go index 017b77c..2c84d2b 100644 --- a/file.go +++ b/file.go @@ -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, @@ -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. diff --git a/file_test.go b/file_test.go index 65b92bf..9ec9c22 100644 --- a/file_test.go +++ b/file_test.go @@ -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() { diff --git a/ini.go b/ini.go index e927118..05a12ee 100644 --- a/ini.go +++ b/ini.go @@ -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).