Skip to content
Merged
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
4 changes: 4 additions & 0 deletions configuration-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,10 @@
"type": "boolean",
"description": "Whether to skip pruning unused components on the generated code"
},
"skip-enum-validate": {
"type": "boolean",
"description": "Whether to skip generation of the Valid() method on enum types"
},
"include-tags": {
"type": "array",
"description": "Only include operations that have one of these tags. Ignored when empty.",
Expand Down
5 changes: 4 additions & 1 deletion pkg/codegen/codegen.go
Original file line number Diff line number Diff line change
Expand Up @@ -1045,7 +1045,10 @@ func GenerateEnums(t *template.Template, types []TypeDefinition) (string, error)

// Now see if enums conflict with any non-enum typenames

return GenerateTemplates([]string{"constants.tmpl"}, t, Constants{EnumDefinitions: enums})
return GenerateTemplates([]string{"constants.tmpl"}, t, Constants{
EnumDefinitions: enums,
SkipEnumValidate: globalState.options.OutputOptions.SkipEnumValidate,
})
}

// GenerateImports generates our import statements and package definition.
Expand Down
6 changes: 6 additions & 0 deletions pkg/codegen/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,12 @@ type OutputOptions struct {
SkipFmt bool `yaml:"skip-fmt,omitempty"`
// Whether to skip pruning unused components on the generated code
SkipPrune bool `yaml:"skip-prune,omitempty"`
// SkipEnumValidate disables the generation of the `Valid()` method on
// enum types. By default each generated enum type includes a `Valid()
// bool` method which returns true when the value matches one of the
// defined constants; set this to true to suppress that method when it
// conflicts with user-defined methods of the same name.
SkipEnumValidate bool `yaml:"skip-enum-validate,omitempty"`
// Only include operations that have one of these tags. Ignored when empty.
IncludeTags []string `yaml:"include-tags,omitempty"`
// Exclude operations that have one of these tags. Ignored when empty.
Expand Down
3 changes: 3 additions & 0 deletions pkg/codegen/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,9 @@ type Constants struct {
SecuritySchemeProviderNames []string
// EnumDefinitions holds type and value information for all enums
EnumDefinitions []EnumDefinition
// SkipEnumValidate suppresses generation of the `Valid()` method on
// enum types. Mirrors OutputOptions.SkipEnumValidate.
SkipEnumValidate bool
}

// TypeDefinition describes a Go type definition in generated code.
Expand Down
3 changes: 2 additions & 1 deletion pkg/codegen/templates/constants.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const (
{{$name}} {{$Enum.TypeName}} = {{$Enum.ValueWrapper}}{{$value}}{{$Enum.ValueWrapper -}}
{{end}}
)

{{if not $.SkipEnumValidate}}
// Valid indicates whether the value is a known member of the {{$Enum.TypeName}} enum.
func (e {{$Enum.TypeName}}) Valid() bool {
switch e {
Expand All @@ -23,3 +23,4 @@ func (e {{$Enum.TypeName}}) Valid() bool {
}
}
{{end}}
{{end}}
Loading