Skip to content

Commit 697130f

Browse files
committed
Implement rename_enum option (Fix #2129)
1 parent b807fe9 commit 697130f

File tree

4 files changed

+29
-4
lines changed

4 files changed

+29
-4
lines changed

internal/codegen/golang/enum.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package golang
33
import (
44
"strings"
55
"unicode"
6+
7+
"github.com/sqlc-dev/sqlc/internal/codegen/golang/opts"
68
)
79

810
type Constant struct {
@@ -47,9 +49,16 @@ func EnumReplace(value string) string {
4749

4850
// EnumValueName removes all non ident symbols (all but letters, numbers and
4951
// underscore) and converts snake case ident to camel case.
50-
func EnumValueName(value string) string {
52+
func EnumValueName(value string, options *opts.Options) string {
53+
if rename := options.RenameEnum[value]; rename != "" {
54+
return rename
55+
}
5156
parts := strings.Split(EnumReplace(value), "_")
5257
for i, part := range parts {
58+
if _, found := options.InitialismsMap[part]; found {
59+
parts[i] = strings.ToUpper(part)
60+
continue
61+
}
5362
parts[i] = titleFirst(part)
5463
}
5564

internal/codegen/golang/opts/options.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ type Options struct {
3030
Out string `json:"out" yaml:"out"`
3131
Overrides []Override `json:"overrides,omitempty" yaml:"overrides"`
3232
Rename map[string]string `json:"rename,omitempty" yaml:"rename"`
33+
RenameEnum map[string]string `json:"rename_enum,omitempty" yaml:"rename_enum"`
3334
SqlPackage string `json:"sql_package" yaml:"sql_package"`
3435
SqlDriver string `json:"sql_driver" yaml:"sql_driver"`
3536
OutputBatchFileName string `json:"output_batch_file_name,omitempty" yaml:"output_batch_file_name"`
@@ -50,8 +51,9 @@ type Options struct {
5051
}
5152

5253
type GlobalOptions struct {
53-
Overrides []Override `json:"overrides,omitempty" yaml:"overrides"`
54-
Rename map[string]string `json:"rename,omitempty" yaml:"rename"`
54+
Overrides []Override `json:"overrides,omitempty" yaml:"overrides"`
55+
Rename map[string]string `json:"rename,omitempty" yaml:"rename"`
56+
RenameEnum map[string]string `json:"rename_enum,omitempty" yaml:"rename_enum"`
5557
}
5658

5759
func Parse(req *plugin.GenerateRequest) (*Options, error) {
@@ -72,6 +74,12 @@ func Parse(req *plugin.GenerateRequest) (*Options, error) {
7274
}
7375
maps.Copy(options.Rename, global.Rename)
7476
}
77+
if len(global.RenameEnum) > 0 {
78+
if options.RenameEnum == nil {
79+
options.RenameEnum = map[string]string{}
80+
}
81+
maps.Copy(options.RenameEnum, global.RenameEnum)
82+
}
7583
return options, nil
7684
}
7785

internal/codegen/golang/result.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func buildEnums(req *plugin.GenerateRequest, options *opts.Options) []Enum {
4040

4141
seen := make(map[string]struct{}, len(enum.Vals))
4242
for i, v := range enum.Vals {
43-
value := EnumReplace(v)
43+
value := EnumValueName(v, options)
4444
if _, found := seen[value]; found || value == "" {
4545
value = fmt.Sprintf("value_%d", i)
4646
}

internal/config/v_two.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,14 @@
227227
}
228228
}
229229
},
230+
"rename_enum": {
231+
"type": "object",
232+
"patternProperties": {
233+
".*": {
234+
"type": "string"
235+
}
236+
}
237+
},
230238
"sql_package": {
231239
"type": "string"
232240
},

0 commit comments

Comments
 (0)