From b50fa592863acdf04e844a4abb849a8ae718fe52 Mon Sep 17 00:00:00 2001 From: Richard Lindhout Date: Thu, 23 Dec 2021 21:14:42 +0100 Subject: [PATCH] more support for models without boilerModel to analyze and genrate things for + fix for enum without filter --- convert_plugin.go | 86 +++++++++++++++++---------- resolver_plugin.go | 6 +- template_files/generated_filter.gotpl | 56 ++++++++--------- 3 files changed, 86 insertions(+), 62 deletions(-) diff --git a/convert_plugin.go b/convert_plugin.go index 239c4b3..37d6196 100755 --- a/convert_plugin.go +++ b/convert_plugin.go @@ -87,6 +87,7 @@ type Model struct { //nolint:maligned Name string PluralName string BoilerModel *BoilerModel + HasBoilerModel bool PrimaryKeyType string Fields []*Field IsNormal bool @@ -152,6 +153,7 @@ type Enum struct { PluralName string Values []*EnumValue HasBoilerEnum bool + HasFilter bool BoilerEnum *BoilerEnum } @@ -209,15 +211,13 @@ func copyConfig(cfg config.Config) *config.Config { return &cfg } -func GetModelsWithInformation( +func EnhanceModelsWithInformation( backend Config, enums []*Enum, cfg *config.Config, boilerModels []*BoilerModel, + models []*Model, ignoreTypePrefixes []string) []*Model { - // get models based on the schema and sqlboiler structs - models := getModelsFromSchema(cfg.Schema, boilerModels) - // always sort enums the same way to prevent merge conflicts in generated code sort.Slice(enums, func(i, j int) bool { return enums[i].Name < enums[j].Name @@ -264,11 +264,15 @@ func (m *ConvertPlugin) MutateConfig(originalCfg *config.Config) error { log.Debug().Msg("[convert] get boiler models") boilerModels, boilerEnums := GetBoilerModels(m.Backend.Directory) + // get models based on the schema and sqlboiler structs + log.Debug().Msg("[convert] enhance model with information") + baseModels := getModelsFromSchema(cfg.Schema, boilerModels) + log.Debug().Msg("[convert] get extra's from schema") - interfaces, enums, scalars := getExtrasFromSchema(cfg.Schema, boilerEnums) + interfaces, enums, scalars := getExtrasFromSchema(cfg.Schema, boilerEnums, baseModels) - log.Debug().Msg("[convert] get model with information") - models := GetModelsWithInformation(b.Backend, enums, originalCfg, boilerModels, []string{m.Frontend.PackageName, m.Backend.PackageName, "boilergql"}) + log.Debug().Msg("[convert] enhance model with information") + models := EnhanceModelsWithInformation(b.Backend, enums, originalCfg, boilerModels, baseModels, []string{m.Frontend.PackageName, m.Backend.PackageName, "boilergql"}) b.Models = models b.Interfaces = interfaces @@ -437,6 +441,9 @@ func enhanceModelsWithFields(enums []*Enum, schema *ast.Schema, cfg *config.Conf // Generate the basic of the fields for _, m := range models { + if !m.HasBoilerModel { + continue + } // Let's convert the pure ast fields to something usable for our templates for _, field := range m.PureFields { fieldDef := schema.Types[field.Type.Name()] @@ -469,7 +476,7 @@ func enhanceModelsWithFields(enums []*Enum, schema *ast.Schema, cfg *config.Conf isPrimaryID := strings.EqualFold(name, "id") // get sqlboiler information of the field - boilerField := findBoilerFieldOrForeignKey(m.BoilerModel.Fields, name, isObject) + boilerField := findBoilerFieldOrForeignKey(m.BoilerModel, name, isObject) isString := strings.Contains(strings.ToLower(boilerField.Type), "string") isNumberID := strings.HasSuffix(name, "ID") && !isString isPrimaryNumberID := isPrimaryID && !isString @@ -638,9 +645,13 @@ func findModel(models []*Model, search string) *Model { // return nil //} -func findBoilerFieldOrForeignKey(fields []*BoilerField, golangGraphQLName string, isObject bool) BoilerField { +func findBoilerFieldOrForeignKey(boilerModel *BoilerModel, golangGraphQLName string, isObject bool) BoilerField { + if boilerModel == nil { + return BoilerField{} + } + // get database friendly struct for this model - for _, field := range fields { + for _, field := range boilerModel.Fields { if isObject { // If it a relation check to see if a foreign key is available if strings.EqualFold(field.Name, golangGraphQLName+"ID") { @@ -654,7 +665,7 @@ func findBoilerFieldOrForeignKey(fields []*BoilerField, golangGraphQLName string return BoilerField{} } -func getExtrasFromSchema(schema *ast.Schema, boilerEnums []*BoilerEnum) (interfaces []*Interface, enums []*Enum, scalars []string) { +func getExtrasFromSchema(schema *ast.Schema, boilerEnums []*BoilerEnum, models []*Model) (interfaces []*Interface, enums []*Enum, scalars []string) { for _, schemaType := range schema.Types { switch schemaType.Kind { case ast.Interface, ast.Union: @@ -664,12 +675,14 @@ func getExtrasFromSchema(schema *ast.Schema, boilerEnums []*BoilerEnum) (interfa }) case ast.Enum: boilerEnum := findBoilerEnum(boilerEnums, schemaType.Name) + fmt.Println(schemaType.Name, len(models)) it := &Enum{ Name: schemaType.Name, PluralName: Plural(schemaType.Name), Description: schemaType.Description, HasBoilerEnum: boilerEnum != nil, BoilerEnum: boilerEnum, + HasFilter: findModel(models, schemaType.Name+"Filter") != nil, } for _, v := range schemaType.EnumValues { it.Values = append(it.Values, &EnumValue{ @@ -745,36 +758,43 @@ 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 + hasEmptyBoilerModel := boilerModel == nil || boilerModel.Name == "" + // TODO: make this cleaner and support custom structs + if !isFilter { + if hasEmptyBoilerModel { + if isInput || isWhere || isPayload || isPageInfo || isPagination { + // silent continue + continue + } + log.Warn().Str("model", modelName).Msg("skipped because no database model found") continue } - log.Warn().Str("model", modelName).Msg("skipped because no database model found") - continue } isNormalInput := isInput && !isCreateInput && !isUpdateInput isNormal := !isInput && !isWhere && !isFilter && !isPayload && !isEdge && !isConnection && !isOrdering + hasBoilerModel := !hasEmptyBoilerModel + hasDeletedAt := hasBoilerModel && boilerModel.HasDeletedAt m := &Model{ - Name: modelName, - Description: schemaType.Description, - PluralName: Plural(modelName), - BoilerModel: boilerModel, - IsInput: isInput, - IsFilter: isFilter, - IsWhere: isWhere, - IsUpdateInput: isUpdateInput, - IsCreateInput: isCreateInput, - IsNormalInput: isNormalInput, - IsConnection: isConnection, - IsEdge: isEdge, - IsPayload: isPayload, - IsOrdering: isOrdering, - IsNormal: isNormal, - IsPreloadable: isNormal, - HasDeletedAt: boilerModel.HasDeletedAt, + Name: modelName, + Description: schemaType.Description, + PluralName: Plural(modelName), + BoilerModel: boilerModel, + HasBoilerModel: hasBoilerModel, + IsInput: isInput, + IsFilter: isFilter, + IsWhere: isWhere, + IsUpdateInput: isUpdateInput, + IsCreateInput: isCreateInput, + IsNormalInput: isNormalInput, + IsConnection: isConnection, + IsEdge: isEdge, + IsPayload: isPayload, + IsOrdering: isOrdering, + IsNormal: isNormal, + IsPreloadable: isNormal, + HasDeletedAt: hasDeletedAt, } for _, implementor := range schema.GetImplements(schemaType) { diff --git a/resolver_plugin.go b/resolver_plugin.go index aa356b9..51cb8b6 100644 --- a/resolver_plugin.go +++ b/resolver_plugin.go @@ -61,8 +61,10 @@ func (m *ResolverPlugin) GenerateCode(data *codegen.Data) error { // Get all models information log.Debug().Msg("[resolver] get boiler models") boilerModels, _ := GetBoilerModels(m.backend.Directory) - log.Debug().Msg("[resolver] get models with information") - models := GetModelsWithInformation(m.backend, nil, data.Config, boilerModels, nil) + log.Debug().Msg("[resolver] get models") + baseModels := getModelsFromSchema(data.Schema, boilerModels) + log.Debug().Msg("[resolver] enhance models with information") + models := EnhanceModelsWithInformation(m.backend, nil, data.Config, boilerModels, baseModels, nil) log.Debug().Msg("[resolver] generate file") // switch data.Config.Resolver.Layout { // case config.LayoutSingleFile: diff --git a/template_files/generated_filter.gotpl b/template_files/generated_filter.gotpl index 4f6a2df..c26fd94 100644 --- a/template_files/generated_filter.gotpl +++ b/template_files/generated_filter.gotpl @@ -316,38 +316,40 @@ func TimeUnixFilterToMods(m *{{ $.Frontend.PackageName }}.TimeUnixFilter, c stri } {{ range $enum := .Enums }} - func {{ .Name }}FilterToMods(m *{{ $.Frontend.PackageName }}.{{ .Name }}Filter, column string) []qm.QueryMod { - if m == nil { - return nil - } + {{ if $enum.HasFilter }} + func {{ .Name }}FilterToMods(m *{{ $.Frontend.PackageName }}.{{ .Name }}Filter, column string) []qm.QueryMod { + if m == nil { + return nil + } - var queryMods []qm.QueryMod - if m.IsNull != nil { - queryMods = append(queryMods, qmhelper.WhereIsNull(column)) - } - if m.NotNull != nil { - queryMods = append(queryMods, qmhelper.WhereIsNotNull(column)) - } - if m.EqualTo != nil { - queryMods = append(queryMods, qmhelper.Where(column, qmhelper.EQ, *m.EqualTo)) - } - if m.NotEqualTo != nil { - queryMods = append(queryMods, qmhelper.Where(column, qmhelper.NEQ, *m.NotEqualTo)) - } - if len(m.In) > 0 { - queryMods = append(queryMods, qm.WhereIn(column+in, {{ .PluralName }}ToInterfaceArray(m.In)...)) - } - if len(m.NotIn) > 0 { - queryMods = append(queryMods, qm.WhereIn(column+notIn, {{ .PluralName }}ToInterfaceArray(m.NotIn)...)) - } + var queryMods []qm.QueryMod + if m.IsNull != nil { + queryMods = append(queryMods, qmhelper.WhereIsNull(column)) + } + if m.NotNull != nil { + queryMods = append(queryMods, qmhelper.WhereIsNotNull(column)) + } + if m.EqualTo != nil { + queryMods = append(queryMods, qmhelper.Where(column, qmhelper.EQ, *m.EqualTo)) + } + if m.NotEqualTo != nil { + queryMods = append(queryMods, qmhelper.Where(column, qmhelper.NEQ, *m.NotEqualTo)) + } + if len(m.In) > 0 { + queryMods = append(queryMods, qm.WhereIn(column+in, {{ .PluralName }}ToInterfaceArray(m.In)...)) + } + if len(m.NotIn) > 0 { + queryMods = append(queryMods, qm.WhereIn(column+notIn, {{ .PluralName }}ToInterfaceArray(m.NotIn)...)) + } - return queryMods - } + return queryMods + } + {{ end }} {{- end }} {{ range $model := .Models }} - {{- if .IsFilter -}} + {{- if and .IsFilter .HasBoilerModel -}} func {{ .Name }}ToMods(m *{{ $.Frontend.PackageName }}.{{ .Name }}) []qm.QueryMod { if m == nil { return nil @@ -378,7 +380,7 @@ func TimeUnixFilterToMods(m *{{ $.Frontend.PackageName }}.TimeUnixFilter, c stri return nil } {{ end }} - {{- if .IsWhere -}} + {{- if and .IsWhere .HasBoilerModel -}} func {{ .Name }}SubqueryToMods(m *{{ $.Frontend.PackageName }}.{{ .Name }}, foreignColumn string, parentTable string) []qm.QueryMod { if m == nil { return nil