Skip to content

Commit af4d9b3

Browse files
committed
Convert <entity-name> to upper camel case.
Previous, we only capitalized the names which worked great unless they contained underscores. Was became `ListZipcode_imports` now becomes `ListZipcodeImports` which improves function and type names generates by sqlc.
1 parent cedd8c1 commit af4d9b3

File tree

3 files changed

+20
-7
lines changed

3 files changed

+20
-7
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
## Installation
66

77
```
8-
$ go install github.com/ngrash/sqlcup/cmd/[email protected].1
8+
$ go install github.com/ngrash/sqlcup/cmd/[email protected].2
99
```
1010

1111
## Usage
@@ -19,8 +19,8 @@ Synopsis:
1919
2020
Description:
2121
sqlcup prints SQL statements to stdout. The <entity-name> argument must be
22-
of the form <singular-name>/<plural-name>. sqlcup capitalizes those names
23-
where necessary.
22+
of the form <singular-name>/<plural-name>. sqlcup converts those names to
23+
upper camel case where necessary.
2424
2525
Each column argument given to sqlcup defines a database column and must
2626
be either a <plain-column> or a <smart-column>:

cmd/sqlcup/main.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,8 +259,8 @@ func parseScaffoldCommandArgs(args []string) (*scaffoldCommandArgs, error) {
259259

260260
sca := &scaffoldCommandArgs{
261261
Table: tableParts[1],
262-
SingularEntity: capitalize(tableParts[0]),
263-
PluralEntity: capitalize(tableParts[1]),
262+
SingularEntity: upperCamelCase(tableParts[0]),
263+
PluralEntity: upperCamelCase(tableParts[1]),
264264
NoExistsClause: *noExistsClauseFlag,
265265
NoReturningClause: *noReturningClauseFlag,
266266
OrderBy: *orderByFlag,
@@ -446,6 +446,19 @@ func writeUpdateQuery(w io.Writer, args *scaffoldCommandArgs) {
446446
}
447447
}
448448

449+
// upperCamelCase converts a string like "zipcode_imports" to "ZipcodeImports".
450+
func upperCamelCase(s string) string {
451+
parts := strings.Split(s, "_")
452+
if len(parts) == 1 {
453+
return capitalize(s)
454+
}
455+
b := strings.Builder{}
456+
for _, p := range parts {
457+
b.WriteString(capitalize(p))
458+
}
459+
return b.String()
460+
}
461+
449462
func capitalize(s string) string {
450463
r := []rune(s)
451464
r[0] = unicode.ToUpper(r[0])

cmd/sqlcup/usage.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ Synopsis:
55

66
Description:
77
sqlcup prints SQL statements to stdout. The <entity-name> argument must be
8-
of the form <singular-name>/<plural-name>. sqlcup capitalizes those names
9-
where necessary.
8+
of the form <singular-name>/<plural-name>. sqlcup converts those names to
9+
upper camel case where necessary.
1010

1111
Each column argument given to sqlcup defines a database column and must
1212
be either a <plain-column> or a <smart-column>:

0 commit comments

Comments
 (0)