From ff1619b4a054b2ae53499a047192a598dc9c0306 Mon Sep 17 00:00:00 2001 From: Chaliy Roman Date: Fri, 6 Sep 2019 15:47:11 +0700 Subject: [PATCH 1/4] customization output delimiter --- file.go | 7 +++++-- ini.go | 2 ++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/file.go b/file.go index 017b77c..4a46ef7 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.KeyValueDelimitersOutput) == 0 { + opts.KeyValueDelimitersOutput = "" + } 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.KeyValueDelimitersOutput + DefaultFormatRight if PrettyFormat || PrettyEqual { - equalSign = " = " + equalSign = fmt.Sprintf(" %s ", f.options.KeyValueDelimitersOutput) } // Use buffer to make sure target is safe until finish encoding. diff --git a/ini.go b/ini.go index 420f3db..ba69c8f 100644 --- a/ini.go +++ b/ini.go @@ -109,6 +109,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 "=". + KeyValueDelimitersOutput string // PreserveSurroundedQuote indicates whether to preserve surrounded quote (single and double quotes). PreserveSurroundedQuote bool } From 913ae29c155611df8d6abc11d87d7aa0fbe4d392 Mon Sep 17 00:00:00 2001 From: aligator Date: Sat, 7 Mar 2020 15:04:36 +0100 Subject: [PATCH 2/4] rename KeyValueDelimitersOutput to KeyValueDelimiterOnWrite --- file.go | 8 ++++---- ini.go | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/file.go b/file.go index 4a46ef7..c7cb366 100644 --- a/file.go +++ b/file.go @@ -48,8 +48,8 @@ func newFile(dataSources []dataSource, opts LoadOptions) *File { if len(opts.KeyValueDelimiters) == 0 { opts.KeyValueDelimiters = "=:" } - if len(opts.KeyValueDelimitersOutput) == 0 { - opts.KeyValueDelimitersOutput = "" + if len(opts.KeyValueDelimiterOnWrite) == 0 { + opts.KeyValueDelimiterOnWrite = "" } return &File{ BlockMode: true, @@ -233,10 +233,10 @@ func (f *File) Append(source interface{}, others ...interface{}) error { } func (f *File) writeToBuffer(indent string) (*bytes.Buffer, error) { - equalSign := DefaultFormatLeft + f.options.KeyValueDelimitersOutput + DefaultFormatRight + equalSign := DefaultFormatLeft + f.options.KeyValueDelimiterOnWrite + DefaultFormatRight if PrettyFormat || PrettyEqual { - equalSign = fmt.Sprintf(" %s ", f.options.KeyValueDelimitersOutput) + equalSign = fmt.Sprintf(" %s ", f.options.KeyValueDelimiterOnWrite) } // Use buffer to make sure target is safe until finish encoding. diff --git a/ini.go b/ini.go index 986f8bf..05a12ee 100644 --- a/ini.go +++ b/ini.go @@ -104,7 +104,7 @@ type LoadOptions struct { // 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 "=". - KeyValueDelimitersOutput string + 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). From 2cd58a2032dfccd852d514d3e30efb057003c304 Mon Sep 17 00:00:00 2001 From: aligator Date: Sat, 7 Mar 2020 15:05:59 +0100 Subject: [PATCH 3/4] add test for writing with KeyValueDelimiterOnWrite --- file_test.go | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) 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() { From 5e7126ec62600589348487addb0c4ac882fc6fab Mon Sep 17 00:00:00 2001 From: aligator Date: Sat, 7 Mar 2020 15:06:55 +0100 Subject: [PATCH 4/4] bugfix: use '=' as the default delimiter on write --- file.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/file.go b/file.go index c7cb366..2c84d2b 100644 --- a/file.go +++ b/file.go @@ -49,7 +49,7 @@ func newFile(dataSources []dataSource, opts LoadOptions) *File { opts.KeyValueDelimiters = "=:" } if len(opts.KeyValueDelimiterOnWrite) == 0 { - opts.KeyValueDelimiterOnWrite = "" + opts.KeyValueDelimiterOnWrite = "=" } return &File{ BlockMode: true,