Skip to content

Commit

Permalink
fix uppercasing and lowercasing issues
Browse files Browse the repository at this point in the history
  • Loading branch information
RichardLindhout committed Apr 9, 2021
1 parent 6607ad8 commit 740c6f2
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 23 deletions.
44 changes: 38 additions & 6 deletions convert_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"runtime"
"sort"
"strings"
"unicode"

"github.com/web-ridge/gqlgen-sqlboiler/v3/customization"

Expand Down Expand Up @@ -61,11 +62,11 @@ type ModelBuild struct {

func (t ModelBuild) Imports() []Import {
return []Import{
Import{
{
Alias: t.Frontend.PackageName,
ImportPath: t.Frontend.Directory,
},
Import{
{
Alias: t.Backend.PackageName,
ImportPath: t.Backend.Directory,
},
Expand Down Expand Up @@ -507,7 +508,7 @@ func enhanceModelsWithFields(enums []*Enum, schema *ast.Schema, cfg *config.Conf
IsOr: strings.EqualFold(name, "or"),
IsAnd: strings.EqualFold(name, "and"),
IsPlural: IsPlural(name),
PluralName: strmangle.Plural(name),
PluralName: Plural(name),
OriginalType: typ,
Description: field.Description,
Enum: enum,
Expand All @@ -528,12 +529,41 @@ func enhanceModelsWithFields(enums []*Enum, schema *ast.Schema, cfg *config.Conf

var ignoreTypePrefixes = []string{"graphql_models", "models", "boilergql"} //nolint:gochecknoglobals

// TaskBlockedBies -> TaskBlockedBy
// People -> Person
func Singular(s string) string {
singular := strmangle.Singular(strcase.ToSnake(s))

singularTitle := strmangle.TitleCase(singular)
if isFirstCharacterLowerCase(s) {
a := []rune(singularTitle)
a[0] = unicode.ToLower(a[0])
return string(a)
}
return singularTitle
}

// TaskBlockedBy -> TaskBlockedBies
// Person -> Persons
// Person -> People
func Plural(s string) string {
plural := strmangle.Plural(strcase.ToSnake(s))

pluralTitle := strmangle.TitleCase(plural)
if isFirstCharacterLowerCase(s) {
a := []rune(pluralTitle)
a[0] = unicode.ToLower(a[0])
return string(a)
}
return pluralTitle
}

func IsPlural(s string) bool {
return s != strmangle.Plural(s)
return s == Plural(s)
}

func IsSingular(s string) bool {
return s != strmangle.Singular(s)
return s == Singular(s)
}

func getShortType(longType string) string {
Expand Down Expand Up @@ -681,6 +711,7 @@ func getModelsFromSchema(schema *ast.Schema, boilerModels []*BoilerModel) (model
}

// if no boiler model is found

if boilerModel == nil || boilerModel.Name == "" {
if isInput || isWhere || isFilter || isPayload || isPageInfo || isPagination {
// silent continue
Expand All @@ -692,10 +723,11 @@ func getModelsFromSchema(schema *ast.Schema, boilerModels []*BoilerModel) (model

isNormalInput := isInput && !isCreateInput && !isUpdateInput
isNormal := !isInput && !isWhere && !isFilter && !isPayload && !isEdge && !isConnection && !isOrdering

m := &Model{
Name: modelName,
Description: schemaType.Description,
PluralName: strmangle.Plural(modelName),
PluralName: Plural(modelName),
BoilerModel: boilerModel,
IsInput: isInput,
IsFilter: isFilter,
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.14

require (
github.com/99designs/gqlgen v0.11.3
github.com/friendsofgo/errors v0.9.2 // indirect
github.com/iancoleman/strcase v0.1.3-0.20201122234759-77cf97e1f9dc
github.com/rs/zerolog v1.20.0
github.com/vektah/gqlparser/v2 v2.0.1
Expand Down
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dgryski/trifles v0.0.0-20190318185328-a8d75aae118c h1:TUuUh0Xgj97tLMNtWtNvI9mIV6isjEb9lBMNv+77IGM=
github.com/dgryski/trifles v0.0.0-20190318185328-a8d75aae118c/go.mod h1:if7Fbed8SFyPtHLHbg49SI7NAdJiC5WIA09pe59rfAA=
github.com/friendsofgo/errors v0.9.2 h1:X6NYxef4efCBdwI7BgS820zFaN7Cphrmb+Pljdzjtgk=
github.com/friendsofgo/errors v0.9.2/go.mod h1:yCvFW5AkDIL9qn7suHVLiI/gH228n7PC4Pn44IGoTOI=
github.com/go-chi/chi v3.3.2+incompatible/go.mod h1:eB3wogJHnLi3x/kFX2A+IbTBlXxmMeXJVKy9tTv1XzQ=
github.com/gogo/protobuf v1.0.0/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
github.com/gorilla/context v0.0.0-20160226214623-1ea25387ff6f/go.mod h1:kBGZzfjB9CEq2AlWe17Uuf7NDRt0dE0s8S51q0aT7Yg=
Expand Down Expand Up @@ -58,10 +60,10 @@ github.com/urfave/cli/v2 v2.1.1/go.mod h1:SE9GqnLQmjVa0iPEY0f1w3ygNIYcIJ0OKPMoW2
github.com/vektah/dataloaden v0.2.1-0.20190515034641-a19b9a6e7c9e/go.mod h1:/HUdMve7rvxZma+2ZELQeNh88+003LL7Pf/CZ089j8U=
github.com/vektah/gqlparser/v2 v2.0.1 h1:xgl5abVnsd4hkN9rk65OJID9bfcLSMuTaTcZj777q1o=
github.com/vektah/gqlparser/v2 v2.0.1/go.mod h1:SyUiHgLATUR8BiYURfTirrTcGpcE+4XkV2se04Px1Ms=
github.com/volatiletech/inflect v0.0.1 h1:2a6FcMQyhmPZcLa+uet3VJ8gLn/9svWhJxJYwvE8KsU=
github.com/volatiletech/inflect v0.0.1/go.mod h1:IBti31tG6phkHitLlr5j7shC5SOo//x0AjDzaJU1PLA=
github.com/volatiletech/strmangle v0.0.1 h1:UKQoHmY6be/R3tSvD2nQYrH41k43OJkidwEiC74KIzk=
github.com/volatiletech/strmangle v0.0.1/go.mod h1:F6RA6IkB5vq0yTG4GQ0UsbbRcl3ni9P76i+JrTBKFFg=
github.com/web-ridge/go-pluralize v0.1.5 h1:P6msW3rPYufi2HfQKMA/EHv6ZozBTt0nDlAIEAWgEOw=
github.com/web-ridge/go-pluralize v0.1.5/go.mod h1:Gx0NuzKc+RpUrcbR4wwcJt3R1JxwdtIvKbHKuRZykUc=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
Expand Down
8 changes: 3 additions & 5 deletions resolver_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import (
"path"
"strings"

"github.com/volatiletech/strmangle"

"github.com/rs/zerolog/log"

"github.com/iancoleman/strcase"
Expand Down Expand Up @@ -91,7 +89,7 @@ func (m *ResolverPlugin) generateSingleFile(data *codegen.Data, models []*Model,

file.Imports = append(file.Imports, Import{
Alias: "fm",
ImportPath: path.Join(m.rootImportPath, m.frontend.Directory),
ImportPath: path.Join(m.rootImportPath, m.frontend.Directory),
})

file.Imports = append(file.Imports, Import{
Expand Down Expand Up @@ -339,9 +337,9 @@ func getModelNames(v string, plural bool) (modelName, inputModelName string) {
}
var s string
if plural {
s = strmangle.Plural(v)
s = Plural(v)
} else {
s = strmangle.Singular(v)
s = Singular(v)
}

if isInputType {
Expand Down
8 changes: 3 additions & 5 deletions sqlboiler_graphql_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import (
"path"
"strings"

"github.com/volatiletech/strmangle"

"github.com/rs/zerolog/log"

"github.com/iancoleman/strcase"
Expand Down Expand Up @@ -249,7 +247,7 @@ func SchemaGet(
w.tl(strcase.ToLowerCamel(model.Name) + "(id: ID!): " + model.Name + "!" + joinedDirectives)

// lists
modelPluralName := strmangle.Plural(model.Name)
modelPluralName := Plural(model.Name)

arguments := []string{
"first: Int!",
Expand All @@ -270,7 +268,7 @@ func SchemaGet(
for _, model := range models {
filteredFields := fieldsWithout(model.Fields, config.SkipInputFields)

modelPluralName := strmangle.Plural(model.Name)
modelPluralName := Plural(model.Name)
// input UserCreateInput {
// firstName: String!
// lastName: String
Expand Down Expand Up @@ -394,7 +392,7 @@ func SchemaGet(
w.l("type Mutation {")

for _, model := range models {
modelPluralName := strmangle.Plural(model.Name)
modelPluralName := Plural(model.Name)

// create single
// e.g createUser(input: UserInput!): UserPayload!
Expand Down
9 changes: 4 additions & 5 deletions sqlboiler_parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (

"github.com/iancoleman/strcase"
"github.com/rs/zerolog/log"
"github.com/volatiletech/strmangle"
)

type BoilerModel struct {
Expand Down Expand Up @@ -89,7 +88,7 @@ func GetBoilerModels(dir string) []*BoilerModel { //nolint:gocognit,gocyclo
relationField := &BoilerField{
Name: boilerFieldName,
RelationshipName: strings.TrimSuffix(boilerFieldName, "ID"),
PluralName: strmangle.Plural(boilerFieldName),
PluralName: Plural(boilerFieldName),
Type: boilerType,
IsRelation: true,
IsRequired: false,
Expand All @@ -110,7 +109,7 @@ func GetBoilerModels(dir string) []*BoilerModel { //nolint:gocognit,gocyclo

addFieldToMap(fieldsPerModelName, modelName, &BoilerField{
Name: boilerFieldName,
PluralName: strmangle.Plural(boilerFieldName),
PluralName: Plural(boilerFieldName),
Type: boiler.Type,
IsRelation: isRelation,
IsRequired: isRequired(boiler.Type),
Expand All @@ -135,7 +134,7 @@ func GetBoilerModels(dir string) []*BoilerModel { //nolint:gocognit,gocyclo
models[i] = &BoilerModel{
Name: modelName,
TableName: tableName,
PluralName: strmangle.Plural(modelName),
PluralName: Plural(modelName),
Fields: fields,

HasPrimaryStringID: hasPrimaryStringID,
Expand Down Expand Up @@ -194,7 +193,7 @@ func findTableName(tableNames []string, modelName string) string {

// if database name is plural
for _, tableName := range tableNames {
if strmangle.Plural(modelName) == tableName {
if Plural(modelName) == tableName {
return tableName
}
}
Expand Down

0 comments on commit 740c6f2

Please sign in to comment.