Add TextFlag for encoding.TextMarshaler/TextUnmarshaler values - #2415
Open
ChrisJr404 wants to merge 2 commits into
Open
Add TextFlag for encoding.TextMarshaler/TextUnmarshaler values#2415ChrisJr404 wants to merge 2 commits into
ChrisJr404 wants to merge 2 commits into
Conversation
Adds TextFlag, mirroring the standard library's flag.TextVar, so any type implementing encoding.TextMarshaler and encoding.TextUnmarshaler (such as *slog.LevelVar) can be set from a flag. Follows the existing FlagBase value pattern and reuses StringConfig for TrimSpace. Adds Command.Text lookup. Closes urfave#2051
Exercise the previously-uncovered branches in flag_text.go: the MarshalText error paths in ToString and String, String's nil and successful cases, Set with a nil destination, and Command.Text when the named flag is absent or not a TextMarshalUnmarshaler.
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.
What type of PR is this?
What this does
Adds
TextFlag, the cli equivalent of the standard library'sflag.TextVar(added in Go 1.19). It lets any value implementing bothencoding.TextMarshalerandencoding.TextUnmarshalerbe populated directly from a flag.The motivating case from #2051 is log-level configuration:
*slog.LevelVarsatisfies both interfaces, so you can wire the level straight into a flag and have the logger ready (with a sensible default) before parsing even finishes:Other stdlib types such as
*netip.Addrand*time.Timework the same way, as do third-party types likezap's atomic level.Implementation notes
FlagBase[T, C, VC]value pattern, mirroringGenericFlag/StringFlag.StringConfigsoTrimSpaceis available.Command.Text(name)lookup, consistent with the other typed getters.Createonly overwrites the target when a non-nil defaultValueis provided, so aDestinationpointing at an existing target isn't clobbered by the nil interface default.Testing
New
flag_text_test.gocovers set-from-arg, default value,Destination,TrimSpace, env source, invalid-value error propagation, andCommand.Text.godoc-current.txt/testdata/godoc-v3.x.txtregenerated.Closes #2051