Skip to content

Commit

Permalink
Fix an issue with title casing enum values
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenafamo committed May 26, 2024
1 parent 81cb7d8 commit df05fd7
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 28 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Use `netip.Prefix` instead of `netip.Addr` for postgres inet column type. This makes it possible to contain a subnet.
- Allow underscores in enum variable names.
- Fix an issue with title casing enum values

## [v0.26.0] - 2024-05-21

Expand Down
36 changes: 18 additions & 18 deletions gen/bobgen-psql/driver/psql.golden.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
"generated": false,
"autoincr": false,
"domain_name": "",
"type": "Workday"
"type": "UnicodeEnum"
},
{
"name": "enum_nullable",
Expand All @@ -92,7 +92,7 @@
"generated": false,
"autoincr": false,
"domain_name": "",
"type": "Workday"
"type": "UnicodeEnum"
},
{
"name": "bool_zero",
Expand Down Expand Up @@ -1746,25 +1746,25 @@
},
{
"name": "enumarr_null",
"db_type": "_workday[]",
"db_type": "_unicode_enum[]",
"default": "NULL",
"comment": "",
"nullable": true,
"generated": false,
"autoincr": false,
"domain_name": "",
"type": "parray.EnumArray[Workday]"
"type": "parray.EnumArray[UnicodeEnum]"
},
{
"name": "enumarr_nnull",
"db_type": "_workday[]",
"db_type": "_unicode_enum[]",
"default": "",
"comment": "",
"nullable": false,
"generated": false,
"autoincr": false,
"domain_name": "",
"type": "parray.EnumArray[Workday]"
"type": "parray.EnumArray[UnicodeEnum]"
},
{
"name": "customarr_null",
Expand Down Expand Up @@ -1880,7 +1880,7 @@
"generated": false,
"autoincr": false,
"domain_name": "",
"type": "Workday"
"type": "UnicodeEnum"
},
{
"name": "enum_nullable",
Expand All @@ -1891,7 +1891,7 @@
"generated": false,
"autoincr": false,
"domain_name": "",
"type": "Workday"
"type": "UnicodeEnum"
},
{
"name": "bool_zero",
Expand Down Expand Up @@ -3545,25 +3545,25 @@
},
{
"name": "enumarr_null",
"db_type": "_workday[]",
"db_type": "_unicode_enum[]",
"default": "NULL",
"comment": "",
"nullable": true,
"generated": false,
"autoincr": false,
"domain_name": "",
"type": "parray.EnumArray[Workday]"
"type": "parray.EnumArray[UnicodeEnum]"
},
{
"name": "enumarr_nnull",
"db_type": "_workday[]",
"db_type": "_unicode_enum[]",
"default": "NULL",
"comment": "",
"nullable": true,
"generated": false,
"autoincr": false,
"domain_name": "",
"type": "parray.EnumArray[Workday]"
"type": "parray.EnumArray[UnicodeEnum]"
},
{
"name": "customarr_null",
Expand Down Expand Up @@ -3952,13 +3952,13 @@
],
"enums": [
{
"Type": "Workday",
"Type": "UnicodeEnum",
"Values": [
"monday",
"tuesday",
"wednesday",
"thursday",
"friday"
"hello",
"привет",
"こんにちは",
"안녕하세요",
"hello_with_underscore"
]
}
],
Expand Down
5 changes: 2 additions & 3 deletions gen/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,6 @@ var templateFunctions = template.FuncMap{
"generateTags": strmangle.GenerateTags,
"generateIgnoreTags": strmangle.GenerateIgnoreTags,
"enumVal": func(val string) string {
val = strmangle.TitleCase(val)

var newval strings.Builder
for _, r := range val {
if r == '_' || unicode.IsLetter(r) || unicode.IsDigit(r) {
Expand All @@ -232,7 +230,8 @@ var templateFunctions = template.FuncMap{
newval.WriteString(fmt.Sprintf("U%x", r))
}

return newval.String()
// Title case after doing unicode replacements or they will be stripped
return strmangle.TitleCase(newval.String())
},
"dbTag": func(t drivers.Table, c drivers.Column) string {
tag := c.Name
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ require (
github.com/stephenafamo/sqlparser v0.0.0-20230326220333-c2adaf4c30e8
github.com/takuoki/gocase v1.0.0
github.com/urfave/cli/v2 v2.23.7
github.com/volatiletech/strmangle v0.0.4
github.com/volatiletech/strmangle v0.0.6
github.com/wasilibs/go-pgquery v0.0.0-20240319230125-b9b2e95c69a7
golang.org/x/mod v0.16.0
golang.org/x/tools v0.19.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,8 @@ github.com/volatiletech/inflect v0.0.1 h1:2a6FcMQyhmPZcLa+uet3VJ8gLn/9svWhJxJYwv
github.com/volatiletech/inflect v0.0.1/go.mod h1:IBti31tG6phkHitLlr5j7shC5SOo//x0AjDzaJU1PLA=
github.com/volatiletech/strmangle v0.0.4 h1:CxrEPhobZL/PCZOTDSH1aq7s4Kv76hQpRoTVVlUOim4=
github.com/volatiletech/strmangle v0.0.4/go.mod h1:ycDvbDkjDvhC0NUU8w3fWwl5JEMTV56vTKXzR3GeR+0=
github.com/volatiletech/strmangle v0.0.6 h1:AdOYE3B2ygRDq4rXDij/MMwq6KVK/pWAYxpC7CLrkKQ=
github.com/volatiletech/strmangle v0.0.6/go.mod h1:ycDvbDkjDvhC0NUU8w3fWwl5JEMTV56vTKXzR3GeR+0=
github.com/wasilibs/go-pgquery v0.0.0-20240319230125-b9b2e95c69a7 h1:sqqLVb63En4uTKFKBWSJ7c1aIFonhM1yn35/+KchOf4=
github.com/wasilibs/go-pgquery v0.0.0-20240319230125-b9b2e95c69a7/go.mod h1:ZAUjWnxivykc22k0TKFZylOV0WlVQ9nWMExfGFIBuF4=
github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8 h1:nIPpBwaJSVYIxUFsDv3M8ofmx9yWTog9BfvIu0q41lo=
Expand Down
12 changes: 6 additions & 6 deletions test/files/psql/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ drop view if exists user_videos;
drop view if exists type_monsters_v;
drop materialized view if exists type_monsters_mv;

drop type if exists workday;
create type workday as enum('monday', 'tuesday', 'wednesday', 'thursday', 'friday');
drop type if exists unicode_enum;
create type unicode_enum as enum ('hello', 'привет', 'こんにちは', '안녕하세요', 'hello_with_underscore');

drop domain if exists uint3;
create domain uint3 as numeric check(value >= 0 and value < power(2::numeric, 3::numeric));
Expand Down Expand Up @@ -70,8 +70,8 @@ create domain my_int_array as int[];
create table type_monsters (
id serial primary key not null,

enum_use workday not null,
enum_nullable workday,
enum_use unicode_enum not null,
enum_nullable unicode_enum,

bool_zero bool,
bool_one bool null,
Expand Down Expand Up @@ -252,8 +252,8 @@ create table type_monsters (
jsonarr_null json[] null,
jsonarr_nnull json[] not null,

enumarr_null workday[] null,
enumarr_nnull workday[] not null,
enumarr_null unicode_enum[] null,
enumarr_nnull unicode_enum[] not null,

customarr_null my_int_array null,
customarr_nnull my_int_array not null,
Expand Down

0 comments on commit df05fd7

Please sign in to comment.