Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rework flag tags to allow for easier extensibility #6246

Merged
merged 5 commits into from May 14, 2024
Merged

Conversation

tempoz
Copy link
Contributor

@tempoz tempoz commented Mar 27, 2024

Make tags extensible without needing to special-case each new tag type that needs to modify methods.

Go flag.Values need to be able to produce accurate string forms of their zero values even when the instance is nil because of how they are used in flagset.Usage:

https://cs.opensource.google/go/go/+/refs/tags/go1.22.1:src/flag/flag.go;l=642-644

This means if we are wrapping a value, we need to capture the type of the value in a type parameter of the wrapping type. Unfortunately, that means that if we want the Tag function of a tag we pass in to the New or Var functions would need to have a type parameter as well if we needed them to wrap the flag value themselves, resulting in the awkward and repetitive:

var foo = autoflags.New(flag.CommandLine, "foo", Foo{}, flagutil.SecretTag[Foo]{})

where SecretTag[T any] would have a Tag method that would wrap the flag in question in a SecretFlag[T].

To avoid this, I had been custom-recognizing the tag types that need to wrap flags inside of Var[T] and just ignoring their tag methods and doing the wrapping there. However, ideally creating a new tag doesn't require this weird special-casing. To that end, I have created TagFlag[T] which can be created by the Tag[T] function to wrap flag values and then be passed to the Tag method of a tag as a Tagged. The method can then freely modify the functions by using Tagged's interface to set functions without needing to parameterize the type of the tag or create flag.Values that cannot satisfy the requirement of producing valid strings when nil.

I did all this when I was adding a new Hidden flag tag for buildbuddy-io/buildbuddy-internal#2804 and then realized I was going to have to add yet another special case to the tag for loop in Var[T].

This also prevents duplication of code and boilerplate for every tag-flag-value-type, as can be seen with how it allows Secret and Deprecate to both be defined using TagFlag. It should, generally, also make it easier to define new tags without touching flagutil at all.

Related issues: N/A

@tempoz tempoz requested a review from bduffany March 27, 2024 18:27
Copy link
Member

@bduffany bduffany left a comment

Choose a reason for hiding this comment

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

mind updating the PR description with a bit more context / motivation as well as a high-level summary of the changes? There is a lot of code being moved around and I'm not familiar enough with this part of the code to grasp what's happening in this PR after a first pass :)

@tempoz
Copy link
Contributor Author

tempoz commented Mar 28, 2024

mind updating the PR description with a bit more context / motivation as well as a high-level summary of the changes? There is a lot of code being moved around and I'm not familiar enough with this part of the code to grasp what's happening in this PR after a first pass :)

Of, course, no problem!

server/util/flagutil/types/autoflags/tags/tags.go Outdated Show resolved Hide resolved
server/util/flagutil/types/autoflags/tags/tags.go Outdated Show resolved Hide resolved
server/util/flagutil/types/autoflags/tags/tags.go Outdated Show resolved Hide resolved
server/util/flagutil/flagutil.go Show resolved Hide resolved
server/util/flagutil/types/autoflags/autoflags.go Outdated Show resolved Hide resolved
@tempoz tempoz merged commit eecfe09 into master May 14, 2024
18 checks passed
@tempoz tempoz deleted the flag-tag-rework branch May 14, 2024 16:19
tempoz added a commit that referenced this pull request May 14, 2024
tempoz added a commit that referenced this pull request May 14, 2024
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.

None yet

2 participants