Skip to content

Commit

Permalink
Fix problems with other strcase
Browse files Browse the repository at this point in the history
  • Loading branch information
robinlindhout committed Jan 25, 2024
1 parent a4c94db commit 6d9c138
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 21 deletions.
7 changes: 4 additions & 3 deletions cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,7 @@ func getModelsFromSchema(schema *ast.Schema, boilerModels []*structs.BoilerModel
// silent continue
continue
}
// log.Warn().Str("model", modelName).Msg("skipped because no database model found")
log.Debug().Str("model", modelName).Msg("skipped because no database model found")
continue
}
}
Expand All @@ -541,6 +541,7 @@ func getModelsFromSchema(schema *ast.Schema, boilerModels []*structs.BoilerModel
}
m := &structs.Model{
Name: modelName,
JSONName: strcase.ToCamel(modelName),
Description: schemaType.Description,
PluralName: Plural(modelName),
BoilerModel: boilerModel,
Expand Down Expand Up @@ -654,7 +655,7 @@ func safeTrim(v string, trimSuffix string) string {
}

func foreignKeyToRel(v string) string {
return strings.TrimSuffix(strings.TrimSuffix(strcase.ToCamel(v), "Id"), "ID")
return strings.TrimSuffix(v, "ID")
}

func isStruct(t types.Type) bool {
Expand Down Expand Up @@ -814,7 +815,7 @@ func getGraphTypeAsText(graphType string) string {

func FindBoilerModel(models []*structs.BoilerModel, modelName string) *structs.BoilerModel {
for _, m := range models {
if m.Name == modelName {
if strings.ToLower(m.Name) == strings.ToLower(modelName) {
return m
}
}
Expand Down
19 changes: 13 additions & 6 deletions cache/sqlboiler_parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,19 +53,26 @@ func GetBoilerModels(dir string) ([]*structs.BoilerModel, []*structs.BoilerEnum)
splitted := strings.Split(boiler.Name, ".")
// result in e.g. User
modelName := splitted[0]

// result in e.g. ID
boilerFieldName := splitted[1]
somethingFcm := strings.Contains(strings.ToLower(modelName), "fcm")

// handle names with lowercase e.g. userR, userL or other sqlboiler extra's
if IsFirstCharacterLowerCase(modelName) {
// It's the relations of the model
// let's add them so we can use them later
if strings.HasSuffix(modelName, "R") {
modelName = strcase.ToCamel(strings.TrimSuffix(modelName, "R"))

modelNameBefore := strings.TrimSuffix(modelName, "R")
modelName = strings.ToUpper(string(modelNameBefore[0])) + modelNameBefore[1:]
isArray := strings.HasSuffix(boiler.Type, "Slice")
boilerType := strings.TrimSuffix(boiler.Type, "Slice")

if somethingFcm {
fmt.Println("boilerType", boilerType)
fmt.Println("boilerFieldName", boilerFieldName)
}

relationField := &structs.BoilerField{
Name: boilerFieldName,
RelationshipName: strings.TrimSuffix(boilerFieldName, "ID"),
Expand Down Expand Up @@ -165,10 +172,10 @@ func GetBoilerModels(dir string) ([]*structs.BoilerModel, []*structs.BoilerEnum)
}

if field.IsRelation && field.Relationship == nil {
// log.Debug().Str("model", model.Name).Str("field", field.Name).Msg(
// "We could not find the relationship in the generated " +
// "boiler structs this could result in unexpected behavior, we marked this field as " +
// "non-relational \n")
log.Debug().Str("model", model.Name).Str("field", field.Name).Msg(
"We could not find the relationship in the generated " +
"boiler structs this could result in unexpected behavior, we marked this field as " +
"non-relational \n")
field.IsRelation = false
}

Expand Down
2 changes: 1 addition & 1 deletion plugin_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ func findModelOrEmpty(models []*structs.Model, modelName string) structs.Model {
return structs.Model{}
}
for _, m := range models {
if m.Name == modelName {
if strings.ToLower(m.Name) == strings.ToLower(modelName) {
return *m
}
}
Expand Down
1 change: 0 additions & 1 deletion sqlboiler_graphql_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,6 @@ func SchemaGet(
w.br()

for _, model := range models {

// enum UserSort { FIRST_NAME, LAST_NAME }
w.l("enum " + model.Name + "Sort {")
for _, v := range fieldAsEnumStrings(model.Fields) {
Expand Down
1 change: 1 addition & 0 deletions structs/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type Preload struct {

type Model struct { //nolint:maligned
Name string
JSONName string
PluralName string
BoilerModel *BoilerModel
HasBoilerModel bool
Expand Down
8 changes: 4 additions & 4 deletions template_files/generated_convert.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -229,8 +229,8 @@ import (
{{- if $field.IsRelation }}

{{- if $field.IsPlural }}
if m.R != nil && m.R.{{ $field.Name }} != nil {
r.{{ $field.Name }} = {{ $field.BoilerField.Relationship.PluralName }}ToGraphQL(m.R.{{ $field.Name }})
if m.R != nil && m.R.{{ $field.BoilerField.Name }} != nil {
r.{{ $field.Name }} = {{ $field.BoilerField.Relationship.PluralName }}ToGraphQL(m.R.{{ $field.BoilerField.Name }})
}
{{- else }}
{{- if $field.BoilerField.IsForeignKey }}
Expand All @@ -242,8 +242,8 @@ import (
}
}
{{- else }}
if m.R != nil && m.R.{{ $field.Name }} != nil {
r.{{ $field.Name }} = {{ $field.BoilerField.Relationship.Name }}ToGraphQL(m.R.{{ $field.Name }})
if m.R != nil && m.R.{{ $field.BoilerField.Name }} != nil {
r.{{ $field.Name }} = {{ $field.BoilerField.Relationship.Name }}ToGraphQL(m.R.{{ $field.BoilerField.Name }})
}
{{- end -}}
{{- end -}}
Expand Down
10 changes: 5 additions & 5 deletions template_files/generated_resolver.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -136,15 +136,15 @@ const inputKey = "input"
}

// resolve requested fields after creating
mods := Get{{ .Model.Name }}PreloadModsWithLevel(ctx, {{ .Model.Name }}PayloadPreloadLevels.{{ .Model.Name }})
mods := Get{{ .Model.Name }}PreloadModsWithLevel(ctx, {{ .Model.Name }}PayloadPreloadLevels.{{ .Model.JSONName }})
mods = append(mods, dm.{{ .Model.Name }}Where.ID.EQ(m.ID))
pM, err := dm.{{ .Model.PluralName }}(mods...).One(ctx, r.db)
if err != nil {
log.Error().Err(err).Msg({{ $resolver.PublicErrorKey }})
return nil, errors.New({{ $resolver.PublicErrorKey }})
}
return &fm.{{ .Model.Name }}Payload{
{{ .Model.Name }}: {{ .Model.Name }}ToGraphQL(pM),
{{ .Model.JSONName }}: {{ .Model.Name }}ToGraphQL(pM),
}, nil

{{- end -}}
Expand Down Expand Up @@ -194,7 +194,7 @@ const inputKey = "input"
}

// resolve requested fields after updating
mods := Get{{ .Model.Name }}PreloadModsWithLevel(ctx, {{ .Model.Name }}PayloadPreloadLevels.{{ .Model.Name }})
mods := Get{{ .Model.Name }}PreloadModsWithLevel(ctx, {{ .Model.Name }}PayloadPreloadLevels.{{ .Model.JSONName }})
mods = append(mods, dm.{{ .Model.Name }}Where.ID.EQ(dbID))
{{ range $scope := $.AuthorizationScopes -}}
{{- if (call $scope.AddHook $resolver.Model.BoilerModel $resolver "updateAfterWhere") }}
Expand All @@ -208,7 +208,7 @@ const inputKey = "input"
return nil, errors.New({{ $resolver.PublicErrorKey }})
}
return &fm.{{ .Model.Name }}Payload{
{{ .Model.Name }}: {{ .Model.Name }}ToGraphQL(pM),
{{ .Model.JSONName }}: {{ .Model.Name }}ToGraphQL(pM),
}, nil

{{- end -}}
Expand Down Expand Up @@ -308,7 +308,7 @@ func (r *queryResolver) Node(ctx context.Context, globalGraphID string) (fm.Node
{{ range $model := .Models -}}
{{ if .IsNormal -}}
case "{{$model.Name}}":
return r.{{$model.Name}}(ctx, globalGraphID)
return r.{{$model.JSONName}}(ctx, globalGraphID)
{{ end -}}
{{ end -}}

Expand Down
2 changes: 1 addition & 1 deletion template_files/generated_sort.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ import (
{{- if eq $field.Name "Sort" -}}
var {{ $field.Enum.Name }}Column = map[{{ $.Frontend.PackageName }}.{{$field.Enum.Name}}]string{
{{- range $value := $field.Enum.Values}}
{{ $.Frontend.PackageName }}.{{ $field.Enum.Name|go }}{{ .Name|go }}: {{ $.Backend.PackageName }}.{{ $model.BoilerModel.Name }}Columns.{{ $value.BoilerEnumValue.Name|go }},
{{ $.Frontend.PackageName }}.{{ $field.Enum.Name|go }}{{ .Name|go }}: {{ $.Backend.PackageName }}.{{ $model.BoilerModel.Name }}Columns.{{ $value.Name|go }},
{{- end }}
}

Expand Down

0 comments on commit 6d9c138

Please sign in to comment.