Skip to content

fix(parser): quote encoded values the parser corrupts on reload - #106

Merged
inhere merged 1 commit into
gookit:masterfrom
binggao1230:fix-encoder-value-quoting
Jul 30, 2026
Merged

inhere merged 1 commit into
gookit:masterfrom
binggao1230:fix-encoder-value-quoting

Conversation

@binggao1230

Copy link
Copy Markdown
Contributor

WriteTo/Encode write values verbatim, but the parser trims surrounding whitespace, strips one matching quote pair, and treats a trailing \ or a """/''' prefix as a multi-line marker. So parse(encode(x)) != x for a whole class of ordinary values, and a trailing backslash aborts the entire load.

c := ini.New()
c.SetSection("s", map[string]string{"pw": "  s3cr3t ", "path": `C:\dir\`})
var b bytes.Buffer
c.WriteTo(&b)
c2 := ini.New(); c2.LoadStrings(b.String())
c2.Get("s.pw")   // "s3cr3t"  — surrounding spaces lost
c2.Get("s.path") // ""        — LoadStrings errors on the trailing `\`

The encoder now wraps only the values the parser would otherwise mangle (leading/trailing whitespace, matching surrounding quotes, trailing backslash, triple-quote prefix) in a quote style the existing parser strips back off, so no parser change is needed. Values that already round-trip are emitted byte-for-byte unchanged. Applied at every encoder site: lite and full section k-v and array items, plus default-section k-v and arrays.

Values containing a real newline still need a symmetric escape to round-trip (the parser only turns \n back into a newline when ReplaceNl is set), so those are left as-is.

Added round-trip tests for the affected value classes and a check that normal values are emitted unchanged. go test ./... passes.

The encoder wrote values verbatim, but the parser trims surrounding
whitespace, strips one matching quote pair, and reads a trailing backslash
or triple-quote prefix as a multi-line marker. So WriteTo/Encode followed by
LoadStrings/Parse silently dropped data from space-padded, surround-quoted
and backslash-terminated values, and hard-errored on a trailing backslash.

Wrap just the affected values in a non-colliding quote style the parser
strips back off; values that already round-trip are emitted unchanged.
Covers every encoder site: lite and full section k-v and array items, plus
default-section k-v and arrays.
@inhere
inhere merged commit 8a6642d into gookit:master Jul 30, 2026
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants