mirror of
https://github.com/go-ini/ini.git
synced 2026-06-19 21:46:45 +02:00
file: customize output delimiter (#226)
* 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>
This commit is contained in:
co-authored by
Chaliy Roman
parent
dd614b817b
commit
8e0f5b3a15
@@ -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.
|
||||
|
||||
@@ -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() {
|
||||
|
||||
@@ -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).
|
||||
|
||||
Reference in New Issue
Block a user