8000 file: customize output delimiter by aligator · Pull Request #226 · go-ini/ini · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

file: customize output delimiter #226

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 7, 2020
Merged
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
7 changes: 5 additions & 2 deletions file.go
8000
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