Repository navigation
Make Section.Key stop adding missing keys - #429
Merged
Merged
Conversation
Section.Key returns a key that reads as empty for a name the section doesn't have, and SetValue, SetComment, AddShadow or AddNestedValue adds it. Until then, reads and changes through it follow any key of that name the section gains, so two such keys for one name stay in step. Reading a missing key no longer writes to the file, so HasKey, Keys and Write don't see it, a raw section read for a key stays writable, and concurrent reads are safe with Locking(false).
A missing key returned by Section.Key expanded its values with itself, so a reference to its own name, once the section gained the key, was expanded twice. Also narrow the docs: the migration guide no longer claims concurrent reads are safe with Locking(false), which Section still breaks by adding missing sections. AddShadow and AddNestedValue add a key only when the file allows them, and a missing key isn't added once its section gains a key of its name.
unknwon
force-pushed
the
v2-key-read-only
branch
from
November 11, 2026 15:34
275c1e6 to
42df5a4
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Section.Keyno longer adds a key when the section has none. In v1 (and v2 so far),sec.Key("PORT").MustInt(3000)added an emptyPORTkey, soHasKey,KeysandWritesaw it after a mere read.Behavior
Keyreturns a key that reads as empty and isn't in the section.SetValueandSetCommentadd it, and so doAddShadowandAddNestedValuewhen the file allows shadows or nested values, sosec.Key(name).SetValue(v)works as before.NewKey,Append,Reloador anotherKey(name)that was set. Two keys returned for one missing name stay in step.Key("")still returns a key that is never added.Why
Must*, which already stopped storing its default.Writefail.Cost
Key.Valueon a lock-free file takes about 0.2 ns more (2.03 → 2.26 ns) for the missing-key check. Other key benchmarks are within noise.Tests
TestSection_Keygains five subtests: reading doesn't add, each change adds, a missing key follows the key its section gains, it expands as that key, and an empty name is never added. The first fails on the old code.