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

[WIP] allow empty string in enum set methods #724

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

mgyucht
Copy link
Contributor

@mgyucht mgyucht commented Nov 30, 2023

Changes

WIP PR with the idea to support empty string in Set() methods for enums. This allows simpler setting of optional enum values, bypassing validation for optional fields in other structures.

Tests

  • make test passing
  • make fmt applied
  • relevant integration tests applied

@@ -45,7 +45,7 @@ func (f *{{.PascalName}}) String() string {
// Set raw string value and validate it against allowed values
func (f *{{.PascalName}}) Set(v string) error {
switch v {
case {{range $i, $e := .Enum }}{{if $i}}, {{end}}`{{.Content}}`{{end}}:
case ``{{range $i, $e := .Enum }}, `{{.Content}}`{{end}}:
Copy link
Contributor

@nfx nfx Dec 4, 2023

Choose a reason for hiding this comment

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

why do we want to allow this?..

@hong-db was putting out a proposal for UNSPECIFIED enum value for "empty" values

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, I marked it WIP because I'm not sure whether we actually want this or not.

The idea is that optional enum fields are represented as empty strings in structures. When you know the value you want to set an enum field to, you can simply assign it. However, you can't just say req.EnumField.Set(x) without checking if x is empty. However, an enum may be optional in some contexts and required in others, because optional or not is not a type-level decision, it is a field-level one. So here I propose adding it just in the Set() method for all enums to support optional enum fields, basically that a user can use empty string in the Set() method to indicate that the field shouldn't be sent in the request without introducing an empty string enum value (which we definitely don't want).

Copy link
Contributor

Choose a reason for hiding this comment

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

empty string for enum feels wrong 🤷 it might not work in other languages consistently

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Python has None and Java has null, it's just Go with this problem I think. It's not that we're adding an enum value of empty string, but since Go doesn't have enums, and this is also the behavior or omitempty, it seems like it could be OK to do this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants