Skip to content

fix(map): ensure string-to-string flags maintain stable pointers#457

Merged
tomasaschan merged 1 commit intospf13:masterfrom
brandon1024:string-to-string-fix
Mar 5, 2026
Merged

fix(map): ensure string-to-string flags maintain stable pointers#457
tomasaschan merged 1 commit intospf13:masterfrom
brandon1024:string-to-string-fix

Conversation

@brandon1024
Copy link

@brandon1024 brandon1024 commented Nov 29, 2025

This revision tweaks the internals of the StringToString flag type to ensure that pointers are consistent across invocations of FlagSet.Parse(), Flag.Set(), FlagSet.GetStringToString().

Prior to this change, each call to GetStringToString would allocate and return a new map. Although this provides nice encapsulation, it means that users cannot manipulate the map (in tests, for instance). To address this, GetStringToString now returns the map directly from Flag.Value. Set() now avoids new allocations as well, updating the existing map over allocating new ones.

Documentation for all StringToString flags have been reworked for improved clarity. The tests for this flag type have been completely reworked. A small example demonstrating this flag type was also added.

Fixes #455
Fixes #457

@CLAassistant
Copy link

CLAassistant commented Nov 29, 2025

CLA assistant check
All committers have signed the CLA.

@brandon1024
Copy link
Author

/cc @tomasaschan 🚀

@brandon1024
Copy link
Author

brandon1024 commented Nov 29, 2025

Agh I just noticed that the other StringTo* and StringSlice flag types will also need to be patched. Perhaps I can address them in a seperate PR to avoid this ballooning into a huge patch.

Copy link
Collaborator

@tomasaschan tomasaschan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While I agree that the tests you removed were incredibly hard to read and understand what they were testing - and the new ones are much better, thanks! - I think there's value in keeping the old ones around if only to show that they're still passing.

Would that be OK with you (even if they test the same thing as the new ones)? If you feel like cleaning them up in the follow-up PR you mentioned, that's fine of course.

@brandon1024
Copy link
Author

While I agree that the tests you removed were incredibly hard to read and understand what they were testing - and the new ones are much better, thanks! - I think there's value in keeping the old ones around if only to show that they're still passing.

No problem at all 👍

This revision tweaks the internals of the StringToString flag type to
ensure that pointers are consistent across invocations of
FlagSet.Parse(), Flag.Set(), and FlagSet.GetStringToString().

Prior to this change, each call to GetStringToString would allocate and
return a new map. Although this provides nice encapsulation, it means
that users cannot manipulate the map (in tests, for instance). To
address this, GetStringToString now returns the map directly from
Flag.Value. Set() now avoids new allocations as well, updating the
existing map over allocating new ones.

Documentation for all StringToString flags have been reworked for
improved clarity. The tests for this flag type have been completely
reworked. A small example demonstrating this flag type was also added.
@brandon1024 brandon1024 force-pushed the string-to-string-fix branch from a758f15 to 4b23295 Compare March 5, 2026 07:46
@brandon1024
Copy link
Author

This should be ready for final review :-)

Copy link
Collaborator

@tomasaschan tomasaschan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice, thank you for your contribution!

@tomasaschan tomasaschan merged commit a165e4b into spf13:master Mar 5, 2026
6 checks passed
@brandon1024 brandon1024 deleted the string-to-string-fix branch March 5, 2026 11:02
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.

resetting string-to-string flag to default state is not possible

3 participants