Skip to content

Commit 98c952d

Browse files
committed
Implement rename_enum option (Fix #2129)
1 parent b807fe9 commit 98c952d

File tree

12 files changed

+65
-20
lines changed

12 files changed

+65
-20
lines changed

internal/codegen/golang/enum.go

Lines changed: 14 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,16 +49,27 @@ 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

5665
return strings.Join(parts, "")
5766
}
5867

5968
func titleFirst(s string) string {
69+
if len(s) == 0 {
70+
return s
71+
}
72+
6073
r := []rune(s)
6174
r[0] = unicode.ToUpper(r[0])
6275

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
@@ -45,7 +45,7 @@ func buildEnums(req *plugin.GenerateRequest, options *opts.Options) []Enum {
4545
value = fmt.Sprintf("value_%d", i)
4646
}
4747
e.Constants = append(e.Constants, Constant{
48-
Name: StructName(enumName+"_"+value, options),
48+
Name: EnumValueName(enumName+"_"+value, options),
4949
Value: v,
5050
Type: e.Name,
5151
})

internal/config/v_one.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,13 @@ import (
1111
)
1212

1313
type V1GenerateSettings struct {
14-
Version string `json:"version" yaml:"version"`
15-
Cloud Cloud `json:"cloud" yaml:"cloud"`
16-
Packages []v1PackageSettings `json:"packages" yaml:"packages"`
17-
Overrides []golang.Override `json:"overrides,omitempty" yaml:"overrides,omitempty"`
18-
Rename map[string]string `json:"rename,omitempty" yaml:"rename,omitempty"`
19-
Rules []Rule `json:"rules" yaml:"rules"`
14+
Version string `json:"version" yaml:"version"`
15+
Cloud Cloud `json:"cloud" yaml:"cloud"`
16+
Packages []v1PackageSettings `json:"packages" yaml:"packages"`
17+
Overrides []golang.Override `json:"overrides,omitempty" yaml:"overrides,omitempty"`
18+
Rename map[string]string `json:"rename,omitempty" yaml:"rename,omitempty"`
19+
RenameEnum map[string]string `json:"rename_enum,omitempty" yaml:"rename_enum,omitempty"`
20+
Rules []Rule `json:"rules" yaml:"rules"`
2021
}
2122

2223
type v1PackageSettings struct {
@@ -175,10 +176,11 @@ func (c *V1GenerateSettings) Translate() Config {
175176
})
176177
}
177178

178-
if len(c.Overrides) > 0 || len(c.Rename) > 0 {
179+
if len(c.Overrides) > 0 || len(c.Rename) > 0 || len(c.RenameEnum) > 0 {
179180
conf.Overrides.Go = &golang.GlobalOptions{
180-
Overrides: c.Overrides,
181-
Rename: c.Rename,
181+
Overrides: c.Overrides,
182+
Rename: c.Rename,
183+
RenameEnum: c.RenameEnum,
182184
}
183185
}
184186

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
},

internal/endtoend/testdata/emit_enum_valid_and_values/sqlc.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
"id_old": "IDNew",
1717
"bar_old": "BarNew",
1818
"foo_old": "FooNew",
19-
"ip_protocol": "IPProtocol",
19+
"ip_protocol": "IPProtocol"
20+
},
21+
"rename_enum": {
2022
"ip_protocol_tcp": "IPProtocolTCP"
2123
}
2224
}

internal/endtoend/testdata/rename/v1/pgx/v4/sqlc.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
"id_old": "IDNew",
1515
"bar_old": "BarNew",
1616
"foo_old": "FooNew",
17-
"ip_protocol": "IPProtocol",
17+
"ip_protocol": "IPProtocol"
18+
},
19+
"rename_enum": {
1820
"ip_protocol_tcp": "IPProtocolTCP"
1921
}
2022
}

internal/endtoend/testdata/rename/v1/pgx/v5/sqlc.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
"id_old": "IDNew",
1515
"bar_old": "BarNew",
1616
"foo_old": "FooNew",
17-
"ip_protocol": "IPProtocol",
17+
"ip_protocol": "IPProtocol"
18+
},
19+
"rename_enum": {
1820
"ip_protocol_tcp": "IPProtocolTCP"
1921
}
2022
}

internal/endtoend/testdata/rename/v1/stdlib/sqlc.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
"id_old": "IDNew",
1313
"bar_old": "BarNew",
1414
"foo_old": "FooNew",
15-
"ip_protocol": "IPProtocol",
15+
"ip_protocol": "IPProtocol"
16+
},
17+
"rename_enum": {
1618
"ip_protocol_tcp": "IPProtocolTCP"
1719
}
1820
}

internal/endtoend/testdata/rename/v2/pgx/v4/sqlc.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
"id_old": "IDNew",
1515
"bar_old": "BarNew",
1616
"foo_old": "FooNew",
17-
"ip_protocol": "IPProtocol",
17+
"ip_protocol": "IPProtocol"
18+
},
19+
"rename_enum": {
1820
"ip_protocol_tcp": "IPProtocolTCP"
1921
}
2022
}

0 commit comments

Comments
 (0)