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

Added enum custom field names option #1015

Closed
wants to merge 4 commits into from
Closed
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
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,35 @@ type Node struct {
}
```

### Custom enum field names

Optionally, enum fields name can be specified by `x-ogen-enum-naming`, for example:

```yaml
components:
schemas:
NodeWeight:
type: integer
enum: [ 1, 3, 5 ]
x-ogen-enum-naming:
1: Low
3: Medium
5: High
```

The generated source code looks like:

```go
// Ref: #/components/schemas/NodeWeight
type NodeWeight int

const (
Low NodeWeight = 1
Medium NodeWeight = 3
High NodeWeight = 35
)
```

### Extra struct field tags

Optionally, additional Go struct field tags can be specified by `x-oapi-codegen-extra-tags`, for example:
Expand Down
20 changes: 20 additions & 0 deletions _testdata/positive/enum_naming.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ paths:
- PascalStrat
- PascalSpecialStrat
- PascalExceptionStrat
- CustomNamingStr
- CustomNamingInt
properties:
VeryBadEnum:
$ref: '#/components/schemas/VeryBadEnum'
Expand All @@ -28,6 +30,10 @@ paths:
$ref: '#/components/schemas/PascalSpecialStrat'
PascalExceptionStrat:
$ref: '#/components/schemas/PascalExceptionStrat'
CustomNamingStr:
$ref: '#/components/schemas/CustomNamingStr'
CustomNamingInt:
$ref: '#/components/schemas/CustomNamingInt'
components:
schemas:
VeryBadEnum:
Expand Down Expand Up @@ -158,3 +164,17 @@ components:
enum:
- '1'
- '-2'
CustomNamingStr:
type: string
enum: [ "a", "b", "e" ]
x-ogen-enum-naming:
a: Alice
b: Bob
e: Eve
CustomNamingInt:
type: integer
enum: [ 1, 2, 3 ]
x-ogen-enum-naming:
1: One
2: Two
3: Three
14 changes: 14 additions & 0 deletions gen/_template/schema/enum.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,20 @@ func (s *{{ $.Name }}) UnmarshalText(data []byte) error {
return errors.Errorf("invalid value: %q", data)
}
}

{{ else -}}

func (s {{ $.Name }}) String() string {
switch *s {
{{- range $variant := $.EnumVariants }}
case {{ $variant.Name }}:
return "{{ $variant.Name }}"
{{- end }}
default:
return fmt.Sprintf("UNKNOWN<%v>", *s)
}
}

{{- end }}

{{ end }}
13 changes: 11 additions & 2 deletions gen/schema_gen_primitive.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ func (g *schemaGen) enum(name string, t *ir.Type, schema *jsonschema.Schema) (*i

type namingStrategy int
const (
pascalName namingStrategy = iota
customMapping namingStrategy = iota
pascalName
pascalSpecialName
cleanSuffix
indexSuffix
Expand All @@ -50,6 +51,14 @@ func (g *schemaGen) enum(name string, t *ir.Type, schema *jsonschema.Schema) (*i
}

switch s {
case customMapping:
if schema.XOgenEnumNaming == nil {
return "", fmt.Errorf("no custom mapping for enum %q", name)
}
if mappedName, ok := schema.XOgenEnumNaming[vstr]; ok {
return mappedName, nil
}
return "", fmt.Errorf("name '%v' not found in custom mapping for enum %q", vstr, name)
case pascalName:
return pascal(name, vstr)
case pascalSpecialName:
Expand Down Expand Up @@ -98,7 +107,7 @@ func (g *schemaGen) enum(name string, t *ir.Type, schema *jsonschema.Schema) (*i

chosenStrategy, err := func() (namingStrategy, error) {
nextStrategy:
for strategy := pascalName; strategy < _lastStrategy; strategy++ {
for strategy := customMapping; strategy < _lastStrategy; strategy++ {
// Treat enum type name as duplicate to prevent collisions.
names := map[string]struct{}{
name: {},
Expand Down
12 changes: 12 additions & 0 deletions internal/integration/sample_api/oas_schemas_gen.go

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

12 changes: 12 additions & 0 deletions internal/integration/sample_api_nc/oas_schemas_gen.go

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

12 changes: 12 additions & 0 deletions internal/integration/sample_api_ns/oas_schemas_gen.go

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

12 changes: 12 additions & 0 deletions internal/integration/sample_api_nsnc/oas_schemas_gen.go

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

108 changes: 106 additions & 2 deletions internal/integration/test_enum_naming/oas_json_gen.go

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

Loading