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

Add option to prefix enum values with their typename #662

Merged
merged 1 commit into from Jul 25, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 8 additions & 7 deletions pkg/codegen/codegen.go
Expand Up @@ -621,9 +621,10 @@ func GenerateEnums(t *template.Template, types []TypeDefinition) (string, error)
wrapper = `"`
}
enums = append(enums, EnumDefinition{
Schema: tp.Schema,
TypeName: tp.TypeName,
ValueWrapper: wrapper,
Schema: tp.Schema,
TypeName: tp.TypeName,
ValueWrapper: wrapper,
PrefixTypeName: globalState.options.Compatibility.AlwaysPrefixEnumValues,
})
}
}
Expand All @@ -640,8 +641,8 @@ func GenerateEnums(t *template.Template, types []TypeDefinition) (string, error)
for e1key := range e1.GetValues() {
_, found := e2.GetValues()[e1key]
if found {
e1.Conflicts = true
e2.Conflicts = true
e1.PrefixTypeName = true
e2.PrefixTypeName = true
enums[i] = e1
enums[j] = e2
break
Expand All @@ -657,7 +658,7 @@ func GenerateEnums(t *template.Template, types []TypeDefinition) (string, error)
}
_, found := e1.Schema.EnumValues[tp.TypeName]
if found {
e1.Conflicts = true
e1.PrefixTypeName = true
enums[i] = e1
}
}
Expand All @@ -666,7 +667,7 @@ func GenerateEnums(t *template.Template, types []TypeDefinition) (string, error)
// type name.
_, found := e1.GetValues()[e1.TypeName]
if found {
e1.Conflicts = true
e1.PrefixTypeName = true
enums[i] = e1
}
}
Expand Down
5 changes: 4 additions & 1 deletion pkg/codegen/configuration.go
Expand Up @@ -26,7 +26,7 @@ type GenerateOptions struct {
EchoServer bool `yaml:"echo-server,omitempty"` // EchoServer specifies whether to generate echo server boilerplate
GinServer bool `yaml:"gin-server,omitempty"` // GinServer specifies whether to generate gin server boilerplate
GorillaServer bool `yaml:"gorilla-server,omitempty"` // GorillaServer specifies whether to generate Gorilla server boilerplate
Strict bool `yaml:"strict-server,omitempty"` // Strict specifies whether to generate strict server wrapper
Strict bool `yaml:"strict-server,omitempty"` // Strict specifies whether to generate strict server wrapper
Client bool `yaml:"client,omitempty"` // Client specifies whether to generate client boilerplate
Models bool `yaml:"models,omitempty"` // Models specifies whether to generate type definitions
EmbeddedSpec bool `yaml:"embedded-spec,omitempty"` // Whether to embed the swagger spec in the generated code
Expand Down Expand Up @@ -57,6 +57,9 @@ type CompatibilityOptions struct {
// When an object contains no members, and only an additionalProperties specification,
// it is flattened to a map. Set
DisableFlattenAdditionalProperties bool `yaml:"disable-flatten-additional-properties,omitempty"`
// When set to true, always prefix enum values with their type name instead of only
// when typenames would be conflicting.
AlwaysPrefixEnumValues bool `yaml:"always-prefix-enum-values,omitempty"`
}

// OutputOptions are used to modify the output code in some way.
Expand Down
10 changes: 6 additions & 4 deletions pkg/codegen/schema.go
Expand Up @@ -111,15 +111,17 @@ type EnumDefinition struct {
// ValueWrapper wraps the value. It's used to conditionally apply quotes
// around strings.
ValueWrapper string
// Conflicts is set to true when this enum conflicts with another in
// terms of TypeNames
Conflicts bool
// PrefixTypeName determines if the enum value is prefixed with its TypeName.
// This is set to true when this enum conflicts with another in terms of
// TypeNames or when explicitly requested via the
// `compatibility.always-prefix-enum-values` option.
PrefixTypeName bool
}

// GetValues generates enum names in a way to minimize global conflicts
func (e *EnumDefinition) GetValues() map[string]string {
// in case there are no conflicts, it's safe to use the values as-is
if !e.Conflicts {
if !e.PrefixTypeName {
return e.Schema.EnumValues
}
// If we do have conflicts, we will prefix the enum's typename to the values.
Expand Down