Skip to content

Commit

Permalink
Add option to prefix enum values with their typename (#657)
Browse files Browse the repository at this point in the history
  • Loading branch information
corani committed Jul 7, 2022
1 parent 2cf7fcf commit 34b47da
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 12 deletions.
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

0 comments on commit 34b47da

Please sign in to comment.