feat: add Valid() method to generated enum types - #2227
Conversation
Generate a Valid() bool method on each enum type that returns true when the receiver matches one of the defined enum constants and false otherwise. This lets callers validate enum values at runtime with a simple method call instead of hand-writing switch statements. This is default-on because it only adds a new method to an already generated type -- existing code that does not call Valid() is completely unaffected, so this should be very unlikely to break anything. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
58e61de to
2615c6e
Compare
jamietanna
left a comment
There was a problem hiding this comment.
From #2181 I do wonder if it'd be useful to be able to get the list of valid enum names, though, i.e. as a slice
But maybe we can see if this solves the underlying need folks have
It turns out we don't need a sorted map of names, since we have the same thing already present on the template context in a different way. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
I think it does, because you can always validate in your own code with something like |
If this is default-on, how can we turn it off explicitly? |
|
Ha! You are right, @rkosegi , I forgot to add the flag. I will do so in a subsequent commit. Whenever I add something I think is useful, it inevitably clashes with someone's code. |
Add an `output-options.skip-enum-validate` flag that suppresses the `Valid()` method generated on enum types. The method is still emitted by default; users whose code defines its own `Valid()` on the same type can now opt out instead of seeing a compile-time conflict. Relates to PR #2227, which introduced the `Valid()` method. Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
* Regenerate client from latest public-api-schema Catch-up regen of the generated REST client against the current public-api-schema baseline (oapi-codegen v2.5.0). No tooling changes — this only picks up schema drift accumulated since the last regen. * Regenerate client with oapi-codegen v2.6.0 Regenerate the REST client with oapi-codegen v2.6.0. Each enum type now carries a generated Valid() bool method (oapi-codegen/oapi-codegen#2227), a native replacement for hand-maintained valid-value lists. v2.6.0-generated code uses runtime.StyleParamWithOptions, so bump github.com/oapi-codegen/runtime v1.1.1 -> v1.2.0. go build ./... passes. * Fix test failure GitOrigin-RevId: 59dd13da90f628f11a4cfc57c44f953817d1bc0c
* Regenerate SDKs from latest public-api-schema Catch-up regen of the Go, Python, and TypeScript SDKs against the current public-api-schema baseline (oapi-codegen v2.5.0 for Go; redocly + openapi-typescript + openapi-python-client for TS/Python), now that the oapi-sdk.yaml import-mapping points at the correct render-oss/sdk/go module path. The Go client gains several component subpackages it was missing (artifactsources, autodeploy, envvar, ...). * Regenerate Go SDK with oapi-codegen v2.6.0 Regenerate the Go SDK client with oapi-codegen v2.6.0. Each enum type now carries a generated Valid() bool method (oapi-codegen/oapi-codegen#2227), a native replacement for hand-maintained valid-value lists. Bump github.com/oapi-codegen/runtime v1.1.1 -> v1.2.0 (required by v2.6.0-generated code); go mod tidy also pulls testify to v1.9.0. Only the Go SDK changes: the TypeScript and Python SDKs are generated by openapi-typescript / openapi-python-client (unaffected by the Go codegen bump) and regenerate identically. * Python lint fixes Run tox -e format-fix to fix lint warnings in python generated code. Add E501 ignore rule to prevent noisy line-length warnings for long string literals that black/ruff-format do not wrap. GitOrigin-RevId: b888c72
* Regenerate client from latest public-api-schema Catch-up regen of the generated REST client against the current public-api-schema baseline (oapi-codegen v2.5.0). Pulls in several new component packages (artifactsources, autodeploy, envvar, eventstatuses, sandboxes, storage, workflows). * Fix hand-written code drift after client regen Resolve go vet failures from regenerating the client against the latest public-api-schema. These are hand-written call sites that referenced symbols the schema/codegen has since moved or retyped: - logs: LogDirectionParam -> LogDirection (parameter component renamed logDirectionParam -> directionParam upstream) - postgres/keyvalue: region create field is now the typed client.Region enum, not a string; cast via (*client.Region)(...) - service: EnvVarInput/EnvVarInputArray/EnvVarKeyValue moved into the client/envvar subpackage - service: web-service Plan field is now *client.Plan (compute-plan rename); convert from the PaidPlan-validated value and update the test * Regenerate client with oapi-codegen v2.6.0 Regenerate the REST client with oapi-codegen v2.6.0. Each enum type now carries a generated Valid() bool method (oapi-codegen/oapi-codegen#2227), a native replacement for hand-maintained valid-value lists. Bump github.com/oapi-codegen/runtime v1.1.1 -> v1.2.0, required by v2.6.0-generated code. GitOrigin-RevId: c23e1297d93838d242cbe66fd36ac38ae53472b3
Generate a Valid() bool method on each enum type that returns true when the receiver matches one of the defined enum constants and false otherwise. This lets callers validate enum values at runtime with a simple method call instead of hand-writing switch statements.
This is default-on because it only adds a new method to an already generated type -- existing code that does not call Valid() is completely unaffected, so this should be very unlikely to break anything.