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

autogenerate enum validation methods #1607

Closed
josharian opened this issue May 10, 2022 · 4 comments · Fixed by #1613
Closed

autogenerate enum validation methods #1607

josharian opened this issue May 10, 2022 · 4 comments · Fixed by #1613
Labels
enhancement New feature or request 🔧 golang

Comments

@josharian
Copy link
Contributor

What do you want to change?

The codegen for enums generates a list of constants containing valid values:

type EnumType string

const (
	EnumTypeA EnumType = "A"
	EnumTypeB EnumType = "B"
)

When writing developer tools, it can be helpful to convert from a string value to an enum type. However, there is no exhaustive list of valid enum values, so we have to wait until a query fails to discover that an invalid enum value has been provided.

I'd like the codegen to also emit something like:

func (e EnumType) Valid() bool {
  switch e {
  case EnumTypeA, EnumTypeB:
    return true
  default:
    return false
  }
}

Then I can convert from string to enum type and check valid early on.

I know that this applies to PostgreSQL and Go. I'm not sure about MySQL and the other languages.

What database engines need to be changed?

PostgreSQL

What programming language backends need to be changed?

Go

@josharian josharian added enhancement New feature or request triage New issues that hasn't been reviewed labels May 10, 2022
@josharian
Copy link
Contributor Author

On a related note, I find myself wanting to be able to list all available enum values.

@kevinburke1
Copy link
Contributor

kevinburke1 commented May 11, 2022 via email

josharian added a commit to josharian/sqlc that referenced this issue May 11, 2022
This commit adds a Valid method for enum types
and an All...Values function to get all enum values.

This makes it easier to work with enums.

Fixes sqlc-dev#1607
josharian added a commit to josharian/sqlc that referenced this issue May 11, 2022
This commit adds a Valid method for enum types
and an All...Values function to get all enum values.

This makes it easier to work with enums.

Fixes sqlc-dev#1607
@josharian
Copy link
Contributor Author

The fix was easy: #1613. Feedback welcome.

I hope we can get it reviewed.

@kyleconroy kyleconroy added 🔧 golang and removed triage New issues that hasn't been reviewed labels May 12, 2022
@kevinburke
Copy link

Ah, previously here: #447

I still think this is a good idea.

josharian added a commit to josharian/sqlc that referenced this issue May 12, 2022
This commit adds a Valid method for enum types
and an All...Values function to get all enum values.

This makes it easier to work with enums.

These are gated by new config parameters
emit_enum_valid_method and emit_all_enum_values.

Fixes sqlc-dev#1607
josharian added a commit to josharian/sqlc that referenced this issue May 12, 2022
This commit adds a Valid method for enum types
and an All...Values function to get all enum values.

This makes it easier to work with enums.

These are gated by new config parameters
emit_enum_valid_method and emit_all_enum_values.

Fixes sqlc-dev#1607
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request 🔧 golang
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants