Skip to content

fix: handle String() round-trip in Set() for stringToString#489

Closed
happysnaker wants to merge 1 commit into
spf13:masterfrom
happysnaker:fix-stringtostring-roundtrip
Closed

fix: handle String() round-trip in Set() for stringToString#489
happysnaker wants to merge 1 commit into
spf13:masterfrom
happysnaker:fix-stringtostring-roundtrip

Conversation

@happysnaker

Copy link
Copy Markdown

Summary

Fixes #413

String() wraps the output in [...], but Set() was not stripping those brackets before parsing. This caused round-trip failures when the output of String() (e.g., [x=[]]) was passed back to Set(), resulting in keys like '[x' instead of 'x'.

Reproduction

fs := pflag.NewFlagSet("test", pflag.PanicOnError)
fs.StringToString("testflag", map[string]string{}, "")
fs.Set("testflag", "x=[]")
result, _ := fs.GetStringToString("testflag")
// Before: map[[x:[]]  (incorrect)
// After:  map[x:[]]     (correct)

Changes

  • Added [...] bracket stripping in Set() at the start of parsing, so the method can handle its own String() output

String() wraps the output in [...], but Set() was not stripping
those brackets before parsing. This caused round-trip failures
when the output of String() (e.g. [x=[]]) was passed back to Set(),
resulting in keys like '[x' instead of 'x'.

Fixes spf13#413
@happysnaker

Copy link
Copy Markdown
Author

Superseded by existing #414 which addresses the same issue. Closing in favor of the earlier PR.

@happysnaker happysnaker closed this Jul 3, 2026
@happysnaker happysnaker deleted the fix-stringtostring-roundtrip branch July 3, 2026 08:52
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.

FlagSet.GetStringToString() drops brackets from values

1 participant