Skip to content

Commit

Permalink
Merge pull request #43 from fraenky8/replace-strings-title
Browse files Browse the repository at this point in the history
internal/cli: replace strings.Title with case.Caser
  • Loading branch information
fraenky8 authored Jun 13, 2022
2 parents 39b3c02 + addcb7e commit 28b301b
Show file tree
Hide file tree
Showing 86 changed files with 65,192 additions and 196 deletions.
9 changes: 5 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@ go 1.17
require (
github.com/go-sql-driver/mysql v1.6.0
github.com/iancoleman/strcase v0.2.0
github.com/jmoiron/sqlx v1.3.4
github.com/lib/pq v1.10.4
github.com/jmoiron/sqlx v1.3.5
github.com/lib/pq v1.10.6
github.com/mattn/go-sqlite3 v2.0.3+incompatible
github.com/stretchr/testify v1.7.0
github.com/stretchr/testify v1.7.2
golang.org/x/text v0.3.7
)

require (
github.com/davecgh/go-spew v1.1.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/objx v0.1.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
20 changes: 11 additions & 9 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE=
github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
github.com/iancoleman/strcase v0.2.0 h1:05I4QRnGpI0m37iZQRuskXh+w77mr6Z41lwQzuHLwW0=
github.com/iancoleman/strcase v0.2.0/go.mod h1:iwCmte+B7n89clKwxIoIXy/HfoL7AsD47ZCWhYzw7ho=
github.com/jmoiron/sqlx v1.3.4 h1:wv+0IJZfL5z0uZoUjlpKgHkgaFSYD+r9CfrXjEXsO7w=
github.com/jmoiron/sqlx v1.3.4/go.mod h1:2BljVx/86SuTyjE+aPYlHCTNvZrnJXghYGpNiXLBMCQ=
github.com/jmoiron/sqlx v1.3.5 h1:vFFPA71p1o5gAeqtEAwLU4dnX2napprKtHr7PYIcN3g=
github.com/jmoiron/sqlx v1.3.5/go.mod h1:nRVWtLre0KfCLJvgxzCsLVMogSvQ1zNJtpYr2Ccp0mQ=
github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/lib/pq v1.10.4 h1:SO9z7FRPzA03QhHKJrH5BXA6HU1rS4V2nIVrrNC1iYk=
github.com/lib/pq v1.10.4/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
github.com/lib/pq v1.10.6 h1:jbk+ZieJ0D7EVGJYpL9QTz7/YW6UHbmdnZWYyK5cdBs=
github.com/lib/pq v1.10.6/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
github.com/mattn/go-sqlite3 v1.14.6/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
github.com/mattn/go-sqlite3 v2.0.3+incompatible h1:gXHsfypPkaMZrKbD5209QV9jbUTJKjyR5WD3HYQSd+U=
github.com/mattn/go-sqlite3 v2.0.3+incompatible/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.2 h1:4jaiDzPyXQvSd7D0EjG45355tLlV3VOECpq10pLC+8s=
github.com/stretchr/testify v1.7.2/go.mod h1:R6va5+xMeoiuVRoj+gSkQ7d3FALtqAAGI1FQKckRals=
golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
21 changes: 16 additions & 5 deletions internal/cli/tables-to-go-cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"unicode"

"github.com/iancoleman/strcase"
"golang.org/x/text/cases"
"golang.org/x/text/language"

"github.com/fraenky8/tables-to-go/pkg/database"
"github.com/fraenky8/tables-to-go/pkg/output"
Expand All @@ -15,6 +17,7 @@ import (

var (
taggers tagger.Tagger
caser = cases.Title(language.English, cases.NoLower)

// some strings for idiomatic go in column names
// see https://github.com/golang/go/wiki/CodeReviewComments#initialisms
Expand Down Expand Up @@ -100,7 +103,7 @@ func (c columnInfo) isNullableOrTemporal() bool {
func createTableStructString(settings *settings.Settings, db database.Database, table *database.Table) (string, string, error) {

var structFields strings.Builder
tableName := strings.Title(settings.Prefix + table.Name + settings.Suffix)
tableName := caser.String(settings.Prefix + table.Name + settings.Suffix)
// Replace any whitespace with underscores
tableName = strings.Map(replaceSpace, tableName)
if settings.IsOutputFormatCamelCase() {
Expand Down Expand Up @@ -252,12 +255,12 @@ func camelCaseString(s string) string {
splitted := strings.Split(s, "_")

if len(splitted) == 1 {
return strings.Title(s)
return caser.String(s)
}

var cc string
for _, part := range splitted {
cc += strings.Title(strings.ToLower(part))
cc += caser.String(strings.ToLower(part))
}
return cc
}
Expand Down Expand Up @@ -312,7 +315,7 @@ func formatColumnName(settings *settings.Settings, column, table string) (string

// Replace any whitespace with underscores
columnName := strings.Map(replaceSpace, column)
columnName = strings.Title(columnName)
columnName = caser.String(columnName)

if settings.IsOutputFormatCamelCase() {
columnName = camelCaseString(columnName)
Expand All @@ -325,13 +328,21 @@ func formatColumnName(settings *settings.Settings, column, table string) (string
if !validVariableName(columnName) {
return "", fmt.Errorf("column name %q in table %q contains invalid characters", column, table)
}

// First character of an identifier in Go must be letter or _
// We want it to be an uppercase letter to be a public field
if !unicode.IsLetter([]rune(columnName)[0]) {
if !unicode.IsLetter(rune(columnName[0])) {
prefix := "X_"
if settings.IsOutputFormatCamelCase() {
prefix = "X"
}
if settings.ShouldInitialism() {
// Note we use the original passed in name of the column here to
// avoid the Title'izing of the first non-digit character as done
// by cases.Caser. Eg: `1fish2fish` gets transformed to `X1Fish2fish`
// but we want `X1fish2fish`.
columnName = toInitialisms(column)
}
if settings.Verbose {
fmt.Printf("\t\t>column %q in table %q doesn't start with a letter; prepending with %q\n", column, table, prefix)
}
Expand Down
9 changes: 5 additions & 4 deletions internal/cli/tables-to-go-cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1676,7 +1676,6 @@ func TestReplaceSpace(t *testing.T) {
}

func TestFormatColumnName(t *testing.T) {
// success and failure subtests
t.Run("pass", func(t *testing.T) {
type testCase struct {
name string
Expand All @@ -1694,11 +1693,9 @@ func TestFormatColumnName(t *testing.T) {
{"nonEnglish", "火", "火", "火"},
{"nonEnglishUpper", "Λλ", "Λλ", "Λλ"},
}
// subtests for camelCase and original settings

camelSettings := settings.New()
camelSettings.OutputFormat = settings.OutputFormatCamelCase
originalSettings := settings.New()
originalSettings.OutputFormat = settings.OutputFormatOriginal
t.Run("camelcase", func(t *testing.T) {
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
Expand All @@ -1711,6 +1708,9 @@ func TestFormatColumnName(t *testing.T) {
})
}
})

originalSettings := settings.New()
originalSettings.OutputFormat = settings.OutputFormatOriginal
t.Run("original", func(t *testing.T) {
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
Expand All @@ -1724,6 +1724,7 @@ func TestFormatColumnName(t *testing.T) {
}
})
})

t.Run("fail", func(t *testing.T) {
type testCase struct {
name string
Expand Down
8 changes: 5 additions & 3 deletions vendor/github.com/jmoiron/sqlx/sqlx.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 5 additions & 3 deletions vendor/github.com/lib/pq/conn.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions vendor/github.com/lib/pq/connector.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 38 additions & 0 deletions vendor/github.com/lib/pq/copy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions vendor/github.com/lib/pq/encode.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions vendor/github.com/lib/pq/error.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 28b301b

Please sign in to comment.