Skip to content

Commit

Permalink
Convert <entity-name> to upper camel case.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ngrash committed Feb 5, 2023
1 parent cedd8c1 commit af4d9b3
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
## Installation

```
$ go install github.com/ngrash/sqlcup/cmd/[email protected].1
$ go install github.com/ngrash/sqlcup/cmd/[email protected].2
```

## Usage
Expand All @@ -19,8 +19,8 @@ Synopsis:
Description:
sqlcup prints SQL statements to stdout. The <entity-name> argument must be
of the form <singular-name>/<plural-name>. sqlcup capitalizes those names
where necessary.
of the form <singular-name>/<plural-name>. sqlcup converts those names to
upper camel case where necessary.
Each column argument given to sqlcup defines a database column and must
be either a <plain-column> or a <smart-column>:
Expand Down
17 changes: 15 additions & 2 deletions cmd/sqlcup/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,8 @@ func parseScaffoldCommandArgs(args []string) (*scaffoldCommandArgs, error) {

sca := &scaffoldCommandArgs{
Table: tableParts[1],
SingularEntity: capitalize(tableParts[0]),
PluralEntity: capitalize(tableParts[1]),
SingularEntity: upperCamelCase(tableParts[0]),
PluralEntity: upperCamelCase(tableParts[1]),
NoExistsClause: *noExistsClauseFlag,
NoReturningClause: *noReturningClauseFlag,
OrderBy: *orderByFlag,
Expand Down Expand Up @@ -446,6 +446,19 @@ func writeUpdateQuery(w io.Writer, args *scaffoldCommandArgs) {
}
}

// upperCamelCase converts a string like "zipcode_imports" to "ZipcodeImports".
func upperCamelCase(s string) string {
parts := strings.Split(s, "_")
if len(parts) == 1 {
return capitalize(s)
}
b := strings.Builder{}
for _, p := range parts {
b.WriteString(capitalize(p))
}
return b.String()
}

func capitalize(s string) string {
r := []rune(s)
r[0] = unicode.ToUpper(r[0])
Expand Down
4 changes: 2 additions & 2 deletions cmd/sqlcup/usage.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ Synopsis:

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

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

0 comments on commit af4d9b3

Please sign in to comment.