Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .changesets/1795431518-150ae894.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
id: 1795431518-150ae894
features:
- enums
targets:
- go
- cli
- terraform
type: feat
bump: patch
description: add Values helpers that list every known value of a generated Go enum
author: AshGodfrey
date: "2026-11-23"
1 change: 1 addition & 0 deletions internal/features/tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ const (
TestCustomCodeRegionSdkMethodWithImports
TestCustomCodeRegionSubSdkMethodWithImports
TestCustomCodeRegionModelMethodWithImports
TestEnumValues
TestFlatteningComponentBodyAndParamNoConflict
TestFlatteningComponentBodyAndParamNoConflictFlattenRequests
TestFlatteningComponentBodyAndParamConflict
Expand Down
5 changes: 5 additions & 0 deletions internal/features/tests_generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions pkg/generate/snapshots/error_message_field_access_go_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,13 @@ const (
func (e KindRejection) ToPointer() *KindRejection {
return &e
}

// KindRejectionValues returns every known KindRejection value in schema order.
func KindRejectionValues() []KindRejection {
return []KindRejection{
KindRejectionRejection,
}
}
func (e *KindRejection) UnmarshalJSON(data []byte) error {
var v string
if err := json.Unmarshal(data, &v); err != nil {
Expand Down Expand Up @@ -199,6 +206,13 @@ const (
func (e KindFailure) ToPointer() *KindFailure {
return &e
}

// KindFailureValues returns every known KindFailure value in schema order.
func KindFailureValues() []KindFailure {
return []KindFailure{
KindFailureFailure,
}
}
func (e *KindFailure) UnmarshalJSON(data []byte) error {
var v string
if err := json.Unmarshal(data, &v); err != nil {
Expand Down
1 change: 1 addition & 0 deletions templates/templates/cli/features.ts
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,7 @@ function isTestSkipped(test: string): boolean {
"request-bodies-base64-file-input-raw-bytes",
"parameters-header-params-enum",
"parameters-header-params-string-formats",
"enum-values",
].includes(test);
}

Expand Down
1 change: 1 addition & 0 deletions templates/templates/csharp/features.ts
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,7 @@ function isTestSkipped(test: string): boolean {
"request-bodies-base64-file-input-raw-bytes",
"parameters-header-params-enum",
"parameters-header-params-string-formats",
"enum-values",
].includes(test);
}

Expand Down
8 changes: 8 additions & 0 deletions templates/templates/go/enum.go.stmpl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ func (e {{$enumType}}) ToPointer() *{{$enumType}} {
return &e
}

{{- $valuesFunc := templateEnumValuesFuncName .Local.Type $enumType }}
// {{$valuesFunc}} returns every known {{$enumType}} value in schema order.
func {{$valuesFunc}}() []{{$enumType}} {
return []{{$enumType}}{
{{templateEnumValueNames .Local.Type}}
}
}

{{- if $enum.Open }}
// IsExact returns true if the value matches a known enum value, false otherwise.
func (e *{{$enumType}}) IsExact() bool {
Expand Down
144 changes: 144 additions & 0 deletions templates/templates/go/includes/enums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,150 @@ function templateEnums(typeDef: TypeDef): string {

registerTemplateFunc("templateEnums", templateEnums);

// @ts-ignore
function templateEnumValueNames(typeDef: TypeDef): string {
return getEnumNames(typeDef)
.map((name) => `${name},`)
.join("\n");
}

registerTemplateFunc("templateEnumValueNames", templateEnumValueNames);

type PackageIdentifiers = {
taken: Set<string>;
helpers: Map<string, string>;
};

function allocateEnumValuesFuncName(
enumType: string,
taken: Set<string>,
): string {
let name = `${enumType}Values`;
if (taken.has(name)) {
name = `${enumType}EnumValues`;
for (let i = 2; taken.has(name); i++) {
name = `${enumType}EnumValues${i}`;
}
}
taken.add(name);
return name;
}

function unionPackageIdentifiers(
union: TypeDef,
className: string,
outputLocation: string,
): string[] {
const unionType = getUnionTypeName(className, outputLocation);
const variants: string[] = [];
const typeConstants: string[] = [];
if (union.Discriminator) {
for (const mapping of union.Discriminator.Mapping) {
variants.push(sanitizeClassName(getDiscriminatorDisplayName(mapping)));
}
typeConstants.push(
...getUnionEnumNamesFromDiscriminator(
unionType,
union.Discriminator.Mapping,
outputLocation,
),
);
} else {
for (const assocType of union.AssociatedTypes) {
variants.push(sanitizeClassName(sanitizeUnionTypeName(assocType)));
}
typeConstants.push(
...getUnionEnumNamesFromAssociatedTypes(
unionType,
union.AssociatedTypes,
outputLocation,
),
);
}
if (union.IsUnionOpen) {
variants.push("Unknown");
typeConstants.push(openUnionUnknownEnumName(unionType));
}
const names = [unionType, `New${className}`, ...typeConstants];
for (const v of variants) {
names.push(`New${className}${v}`, `Create${className}${v}`);
}
return names;
}

function packageIdentifiers(outputLocation: string): PackageIdentifiers {
if (!context.GlobalComputed) {
context.GlobalComputed = {};
}
if (!context.GlobalComputed._packageIdentifiers) {
const byLocation = new Map<string, PackageIdentifiers>();
for (const [location, models] of sequencedMapEntries(
context.Global.AST.BucketedTypes,
)) {
const ids: PackageIdentifiers = { taken: new Set(), helpers: new Map() };
const enums: string[] = [];
for (const [model, types] of sequencedMapEntries(models)) {
if (
types.length == 0 ||
types.some((t) => t.Scope.toString() == "operations")
) {
const servers = context.Global.AST.OperationServers.Get(model)[0];
if (servers) {
const modelName = sanitizeFieldName(model);
ids.taken.add(`${modelName}ServerList`);
if (servers.ServerMap) {
for (const s of servers.Servers) {
ids.taken.add(templateConstName(s.ID, `${modelName}Server`));
}
}
}
}
for (const t of types) {
const name = sanitizeClass(t, location, true, false);
ids.taken.add(name);
Comment thread
AshGodfrey marked this conversation as resolved.
Resolved
if (t.Type.toString() == "union") {
unionPackageIdentifiers(t, name, location).forEach((n) =>
ids.taken.add(n),
);
}
if (t.Type.toString() == "enum" && t.Enum) {
getEnumNames(t).forEach((c) => ids.taken.add(c));
enums.push(name);
}
}
}
for (const name of enums.sort()) {
ids.helpers.set(name, allocateEnumValuesFuncName(name, ids.taken));
}
byLocation.set(location, ids);
}
context.GlobalComputed._packageIdentifiers = byLocation;
}
let ids = context.GlobalComputed._packageIdentifiers.get(outputLocation);
if (!ids) {
ids = { taken: new Set(), helpers: new Map() };
context.GlobalComputed._packageIdentifiers.set(outputLocation, ids);
}
return ids;
}

// @ts-ignore
function templateEnumValuesFuncName(
typeDef: TypeDef,
enumType: string,
): string {
const ids = packageIdentifiers(typeDef.OutputLocation);
let name = ids.helpers.get(enumType);
if (!name) {
getEnumNames(typeDef).forEach((c) => ids.taken.add(c));
name = allocateEnumValuesFuncName(enumType, ids.taken);
ids.helpers.set(enumType, name);
}
return name;
}

registerTemplateFunc("templateEnumValuesFuncName", templateEnumValuesFuncName);

function formatGoEnumDescriptionComments(
enumName: string,
description: string,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,34 @@ func TestOpenEnumsRoundTrip(t *testing.T) {
wantHeroWidth = shared.HeroWidth(2160)
require.Equal(t, &wantHeroWidth, roundTripTheme.HeroWidth)
}

func TestEnumValues(t *testing.T) {
recordTest("enum-values")

require.Equal(t, []shared.PrototypeMemberNameEnum{
shared.PrototypeMemberNameEnumStandard,
shared.PrototypeMemberNameEnumConstructor,
shared.PrototypeMemberNameEnumToString,
shared.PrototypeMemberNameEnumValueOf,
shared.PrototypeMemberNameEnumHasOwnProperty,
}, shared.PrototypeMemberNameEnumValues())

require.Equal(t, []shared.ValuesMemberEnum{shared.ValuesMemberEnumValues, shared.ValuesMemberEnumCount}, shared.ValuesMemberEnumEnumValues())
require.Equal(t, []shared.Priority{shared.PriorityLow, shared.PriorityHigh}, shared.PriorityEnumValues())
require.Equal(t, []shared.PriorityEnum{shared.PriorityEnumUrgent, shared.PriorityEnumNormal}, shared.PriorityEnumEnumValues())

require.Equal(t, []shared.HeroWidth{480, 720, 1080}, shared.HeroWidthValues())

colors := shared.ColorValues()
Comment thread
cubic-dev-ai[bot] marked this conversation as resolved.
Resolved
require.Equal(t, []shared.Color{
shared.ColorRed,
shared.ColorGreen,
shared.ColorBlue,
shared.ColorReturn,
shared.ColorClass,
shared.ColorInt,
}, colors)
for _, c := range colors {
require.True(t, c.IsExact(), "ColorValues returned %q, which IsExact rejects", c)
}
}
1 change: 1 addition & 0 deletions templates/templates/javav2/features.ts
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,7 @@ function isTestSkipped(test: string): boolean {
"request-bodies-base64-file-input-raw-bytes",
"parameters-header-params-enum",
"parameters-header-params-string-formats",
"enum-values",
].includes(test);
}

Expand Down
1 change: 1 addition & 0 deletions templates/templates/mcp-typescript/features.ts
Original file line number Diff line number Diff line change
Expand Up @@ -706,6 +706,7 @@ function isTestSkipped(test: string): boolean {
"request-bodies-base64-file-input-raw-bytes",
"parameters-header-params-enum",
"parameters-header-params-string-formats",
"enum-values",
].includes(test);
}

Expand Down
1 change: 1 addition & 0 deletions templates/templates/php/features.ts
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,7 @@ function isTestSkipped(test: string): boolean {
"unions-body-variant-overloads-nested-union",
"unions-body-variant-overloads-open-enum",
"request-bodies-base64-file-input-raw-bytes",
"enum-values",
].includes(test);
}

Expand Down
1 change: 1 addition & 0 deletions templates/templates/pythonv2/features.ts
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,7 @@ function isTestSkipped(test: string): boolean {
"unions-body-variant-overloads-open-enum",
"parameters-header-params-enum",
"parameters-header-params-string-formats",
"enum-values",
].includes(test);
}

Expand Down
1 change: 1 addition & 0 deletions templates/templates/ruby/features.ts
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,7 @@ function isTestSkipped(test: string): boolean {
"request-bodies-base64-file-input-raw-bytes",
"parameters-header-params-enum",
"parameters-header-params-string-formats",
"enum-values",
].includes(test);
}

Expand Down
1 change: 1 addition & 0 deletions templates/templates/typescriptv2/features.ts
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,7 @@ function isTestSkipped(test: string): boolean {
"request-bodies-base64-file-input-raw-bytes",
"parameters-header-params-enum",
"parameters-header-params-string-formats",
"enum-values",
].includes(test);
}

Expand Down
1 change: 1 addition & 0 deletions templates/templates/unity/features.ts
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,7 @@ function isTestSkipped(test: string): boolean {
"request-bodies-base64-file-input-raw-bytes",
"parameters-header-params-enum",
"parameters-header-params-string-formats",
"enum-values",
].includes(test);
}

Expand Down
Loading
Loading