Skip to content
Open
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
15 changes: 14 additions & 1 deletion internal/codegen/golang/enum.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package golang
import (
"strings"
"unicode"

"github.com/sqlc-dev/sqlc/internal/codegen/golang/opts"
)

type Constant struct {
Expand Down Expand Up @@ -47,16 +49,27 @@ func EnumReplace(value string) string {

// EnumValueName removes all non ident symbols (all but letters, numbers and
// underscore) and converts snake case ident to camel case.
func EnumValueName(value string) string {
func EnumValueName(value string, options *opts.Options) string {
if rename := options.RenameEnum[value]; rename != "" {
return rename
}
parts := strings.Split(EnumReplace(value), "_")
for i, part := range parts {
if _, found := options.InitialismsMap[part]; found {
parts[i] = strings.ToUpper(part)
continue
}
parts[i] = titleFirst(part)
}

return strings.Join(parts, "")
}

func titleFirst(s string) string {
if len(s) == 0 {
return s
}

r := []rune(s)
r[0] = unicode.ToUpper(r[0])

Expand Down
12 changes: 10 additions & 2 deletions internal/codegen/golang/opts/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type Options struct {
Out string `json:"out" yaml:"out"`
Overrides []Override `json:"overrides,omitempty" yaml:"overrides"`
Rename map[string]string `json:"rename,omitempty" yaml:"rename"`
RenameEnum map[string]string `json:"rename_enum,omitempty" yaml:"rename_enum"`
SqlPackage string `json:"sql_package" yaml:"sql_package"`
SqlDriver string `json:"sql_driver" yaml:"sql_driver"`
OutputBatchFileName string `json:"output_batch_file_name,omitempty" yaml:"output_batch_file_name"`
Expand All @@ -50,8 +51,9 @@ type Options struct {
}

type GlobalOptions struct {
Overrides []Override `json:"overrides,omitempty" yaml:"overrides"`
Rename map[string]string `json:"rename,omitempty" yaml:"rename"`
Overrides []Override `json:"overrides,omitempty" yaml:"overrides"`
Rename map[string]string `json:"rename,omitempty" yaml:"rename"`
RenameEnum map[string]string `json:"rename_enum,omitempty" yaml:"rename_enum"`
}

func Parse(req *plugin.GenerateRequest) (*Options, error) {
Expand All @@ -72,6 +74,12 @@ func Parse(req *plugin.GenerateRequest) (*Options, error) {
}
maps.Copy(options.Rename, global.Rename)
}
if len(global.RenameEnum) > 0 {
if options.RenameEnum == nil {
options.RenameEnum = map[string]string{}
}
maps.Copy(options.RenameEnum, global.RenameEnum)
}
return options, nil
}

Expand Down
2 changes: 1 addition & 1 deletion internal/codegen/golang/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func buildEnums(req *plugin.GenerateRequest, options *opts.Options) []Enum {
value = fmt.Sprintf("value_%d", i)
}
e.Constants = append(e.Constants, Constant{
Name: StructName(enumName+"_"+value, options),
Name: EnumValueName(enumName+"_"+value, options),
Value: v,
Type: e.Name,
})
Expand Down
20 changes: 11 additions & 9 deletions internal/config/v_one.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ import (
)

type V1GenerateSettings struct {
Version string `json:"version" yaml:"version"`
Cloud Cloud `json:"cloud" yaml:"cloud"`
Packages []v1PackageSettings `json:"packages" yaml:"packages"`
Overrides []golang.Override `json:"overrides,omitempty" yaml:"overrides,omitempty"`
Rename map[string]string `json:"rename,omitempty" yaml:"rename,omitempty"`
Rules []Rule `json:"rules" yaml:"rules"`
Version string `json:"version" yaml:"version"`
Cloud Cloud `json:"cloud" yaml:"cloud"`
Packages []v1PackageSettings `json:"packages" yaml:"packages"`
Overrides []golang.Override `json:"overrides,omitempty" yaml:"overrides,omitempty"`
Rename map[string]string `json:"rename,omitempty" yaml:"rename,omitempty"`
RenameEnum map[string]string `json:"rename_enum,omitempty" yaml:"rename_enum,omitempty"`
Rules []Rule `json:"rules" yaml:"rules"`
}

type v1PackageSettings struct {
Expand Down Expand Up @@ -175,10 +176,11 @@ func (c *V1GenerateSettings) Translate() Config {
})
}

if len(c.Overrides) > 0 || len(c.Rename) > 0 {
if len(c.Overrides) > 0 || len(c.Rename) > 0 || len(c.RenameEnum) > 0 {
conf.Overrides.Go = &golang.GlobalOptions{
Overrides: c.Overrides,
Rename: c.Rename,
Overrides: c.Overrides,
Rename: c.Rename,
RenameEnum: c.RenameEnum,
}
}

Expand Down
8 changes: 8 additions & 0 deletions internal/config/v_two.json
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,14 @@
}
}
},
"rename_enum": {
"type": "object",
"patternProperties": {
".*": {
"type": "string"
}
}
},
"sql_package": {
"type": "string"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
"id_old": "IDNew",
"bar_old": "BarNew",
"foo_old": "FooNew",
"ip_protocol": "IPProtocol",
"ip_protocol": "IPProtocol"
},
"rename_enum": {
"ip_protocol_tcp": "IPProtocolTCP"
}
}
4 changes: 3 additions & 1 deletion internal/endtoend/testdata/rename/v1/pgx/v4/sqlc.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
"id_old": "IDNew",
"bar_old": "BarNew",
"foo_old": "FooNew",
"ip_protocol": "IPProtocol",
"ip_protocol": "IPProtocol"
},
"rename_enum": {
"ip_protocol_tcp": "IPProtocolTCP"
}
}
4 changes: 3 additions & 1 deletion internal/endtoend/testdata/rename/v1/pgx/v5/sqlc.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
"id_old": "IDNew",
"bar_old": "BarNew",
"foo_old": "FooNew",
"ip_protocol": "IPProtocol",
"ip_protocol": "IPProtocol"
},
"rename_enum": {
"ip_protocol_tcp": "IPProtocolTCP"
}
}
4 changes: 3 additions & 1 deletion internal/endtoend/testdata/rename/v1/stdlib/sqlc.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
"id_old": "IDNew",
"bar_old": "BarNew",
"foo_old": "FooNew",
"ip_protocol": "IPProtocol",
"ip_protocol": "IPProtocol"
},
"rename_enum": {
"ip_protocol_tcp": "IPProtocolTCP"
}
}
4 changes: 3 additions & 1 deletion internal/endtoend/testdata/rename/v2/pgx/v4/sqlc.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
"id_old": "IDNew",
"bar_old": "BarNew",
"foo_old": "FooNew",
"ip_protocol": "IPProtocol",
"ip_protocol": "IPProtocol"
},
"rename_enum": {
"ip_protocol_tcp": "IPProtocolTCP"
}
}
Expand Down
4 changes: 3 additions & 1 deletion internal/endtoend/testdata/rename/v2/pgx/v5/sqlc.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
"id_old": "IDNew",
"bar_old": "BarNew",
"foo_old": "FooNew",
"ip_protocol": "IPProtocol",
"ip_protocol": "IPProtocol"
},
"rename_enum": {
"ip_protocol_tcp": "IPProtocolTCP"
}
}
Expand Down
4 changes: 3 additions & 1 deletion internal/endtoend/testdata/rename/v2/stdlib/sqlc.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
"id_old": "IDNew",
"bar_old": "BarNew",
"foo_old": "FooNew",
"ip_protocol": "IPProtocol",
"ip_protocol": "IPProtocol"
},
"rename_enum": {
"ip_protocol_tcp": "IPProtocolTCP"
}
}
Expand Down
Loading