Skip to content

Commit

Permalink
no-relationship-funcs flag added
Browse files Browse the repository at this point in the history
  • Loading branch information
Roman A. Grigorovich committed Nov 22, 2023
1 parent 73f2bf0 commit 0be697e
Show file tree
Hide file tree
Showing 26 changed files with 338 additions and 291 deletions.
38 changes: 21 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -372,23 +372,24 @@ blacklist = ["migrations", "addresses.name", "*.secret_col"]
You can also pass in these top level configuration values if you would prefer
not to pass them through the command line or environment variables:

| Name | Defaults |
| ------------------- | --------- |
| pkgname | "models" |
| output | "models" |
| tag | [] |
| debug | false |
| add-global-variants | false |
| add-panic-variants | false |
| add-enum-types | false |
| enum-null-prefix | "Null" |
| no-context | false |
| no-hooks | false |
| no-tests | false |
| no-auto-timestamps | false |
| no-rows-affected | false |
| no-driver-templates | false |
| tag-ignore | [] |
| Name | Defaults |
| --------------------- | -------- |
| pkgname | "models" |
| output | "models" |
| tag | [] |
| debug | false |
| add-global-variants | false |
| add-panic-variants | false |
| add-enum-types | false |
| enum-null-prefix | "Null" |
| no-context | false |
| no-hooks | false |
| no-relationship-funcs | false |
| no-tests | false |
| no-auto-timestamps | false |
| no-rows-affected | false |
| no-driver-templates | false |
| tag-ignore | [] |

##### Full Example

Expand Down Expand Up @@ -454,6 +455,7 @@ Flags:
--no-context Disable context.Context usage in the generated code
--no-driver-templates Disable parsing of templates defined by the database driver
--no-hooks Disable hooks feature for your models
--no-relationship-funcs Disable relationship loaders and setters feature for your models
--no-rows-affected Disable rows affected in the generated API
--no-tests Disable generated go test files
-o, --output string The name of the folder to output to (default "models")
Expand Down Expand Up @@ -1404,6 +1406,8 @@ type CoolObject struct {
Helper methods will be generated for every to one and to many relationship structure
you have defined in your database by using foreign keys.

You can disable this feature with "no-relationship-funcs" flag.

We attach these helpers directly to your model struct, for example:

```go
Expand Down
53 changes: 26 additions & 27 deletions boilingcore/boilingcore.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,31 +132,32 @@ func New(config *Config) (*State, error) {
// state given.
func (s *State) Run() error {
data := &templateData{
Tables: s.Tables,
Aliases: s.Config.Aliases,
DriverName: s.Config.DriverName,
PkgName: s.Config.PkgName,
AddGlobal: s.Config.AddGlobal,
AddPanic: s.Config.AddPanic,
AddSoftDeletes: s.Config.AddSoftDeletes,
AddEnumTypes: s.Config.AddEnumTypes,
EnumNullPrefix: s.Config.EnumNullPrefix,
NoContext: s.Config.NoContext,
NoHooks: s.Config.NoHooks,
NoAutoTimestamps: s.Config.NoAutoTimestamps,
NoRowsAffected: s.Config.NoRowsAffected,
NoDriverTemplates: s.Config.NoDriverTemplates,
NoBackReferencing: s.Config.NoBackReferencing,
AlwaysWrapErrors: s.Config.AlwaysWrapErrors,
StructTagCasing: s.Config.StructTagCasing,
TagIgnore: make(map[string]struct{}),
Tags: s.Config.Tags,
RelationTag: s.Config.RelationTag,
Dialect: s.Dialect,
Schema: s.Schema,
LQ: strmangle.QuoteCharacter(s.Dialect.LQ),
RQ: strmangle.QuoteCharacter(s.Dialect.RQ),
OutputDirDepth: s.Config.OutputDirDepth(),
Tables: s.Tables,
Aliases: s.Config.Aliases,
DriverName: s.Config.DriverName,
PkgName: s.Config.PkgName,
AddGlobal: s.Config.AddGlobal,
AddPanic: s.Config.AddPanic,
AddSoftDeletes: s.Config.AddSoftDeletes,
AddEnumTypes: s.Config.AddEnumTypes,
EnumNullPrefix: s.Config.EnumNullPrefix,
NoContext: s.Config.NoContext,
NoHooks: s.Config.NoHooks,
NoRelationshipFuncs: s.Config.NoRelationshipFuncs,
NoAutoTimestamps: s.Config.NoAutoTimestamps,
NoRowsAffected: s.Config.NoRowsAffected,
NoDriverTemplates: s.Config.NoDriverTemplates,
NoBackReferencing: s.Config.NoBackReferencing,
AlwaysWrapErrors: s.Config.AlwaysWrapErrors,
StructTagCasing: s.Config.StructTagCasing,
TagIgnore: make(map[string]struct{}),
Tags: s.Config.Tags,
RelationTag: s.Config.RelationTag,
Dialect: s.Dialect,
Schema: s.Schema,
LQ: strmangle.QuoteCharacter(s.Dialect.LQ),
RQ: strmangle.QuoteCharacter(s.Dialect.RQ),
OutputDirDepth: s.Config.OutputDirDepth(),

DBTypes: make(once),
StringFuncs: templateStringMappers,
Expand Down Expand Up @@ -395,7 +396,6 @@ func findTemplates(root, base string) (map[string]templateLoader, error) {
templates[relative] = fileLoader(path)
return nil
})

if err != nil {
return nil, err
}
Expand Down Expand Up @@ -451,7 +451,6 @@ func (s *State) mergeEnumImports() {
// and performs them.
func (s *State) processTypeReplacements() error {
for _, r := range s.Config.TypeReplaces {

for i := range s.Tables {
t := s.Tables[i]

Expand Down
47 changes: 24 additions & 23 deletions boilingcore/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,30 @@ type Config struct {
DriverName string `toml:"driver_name,omitempty" json:"driver_name,omitempty"`
DriverConfig drivers.Config `toml:"driver_config,omitempty" json:"driver_config,omitempty"`

PkgName string `toml:"pkg_name,omitempty" json:"pkg_name,omitempty"`
OutFolder string `toml:"out_folder,omitempty" json:"out_folder,omitempty"`
TemplateDirs []string `toml:"template_dirs,omitempty" json:"template_dirs,omitempty"`
Tags []string `toml:"tags,omitempty" json:"tags,omitempty"`
Replacements []string `toml:"replacements,omitempty" json:"replacements,omitempty"`
Debug bool `toml:"debug,omitempty" json:"debug,omitempty"`
AddGlobal bool `toml:"add_global,omitempty" json:"add_global,omitempty"`
AddPanic bool `toml:"add_panic,omitempty" json:"add_panic,omitempty"`
AddSoftDeletes bool `toml:"add_soft_deletes,omitempty" json:"add_soft_deletes,omitempty"`
AddEnumTypes bool `toml:"add_enum_types,omitempty" json:"add_enum_types,omitempty"`
EnumNullPrefix string `toml:"enum_null_prefix,omitempty" json:"enum_null_prefix,omitempty"`
NoContext bool `toml:"no_context,omitempty" json:"no_context,omitempty"`
NoTests bool `toml:"no_tests,omitempty" json:"no_tests,omitempty"`
NoHooks bool `toml:"no_hooks,omitempty" json:"no_hooks,omitempty"`
NoAutoTimestamps bool `toml:"no_auto_timestamps,omitempty" json:"no_auto_timestamps,omitempty"`
NoRowsAffected bool `toml:"no_rows_affected,omitempty" json:"no_rows_affected,omitempty"`
NoDriverTemplates bool `toml:"no_driver_templates,omitempty" json:"no_driver_templates,omitempty"`
NoBackReferencing bool `toml:"no_back_reference,omitempty" json:"no_back_reference,omitempty"`
AlwaysWrapErrors bool `toml:"always_wrap_errors,omitempty" json:"always_wrap_errors,omitempty"`
Wipe bool `toml:"wipe,omitempty" json:"wipe,omitempty"`
StructTagCasing string `toml:"struct_tag_casing,omitempty" json:"struct_tag_casing,omitempty"`
RelationTag string `toml:"relation_tag,omitempty" json:"relation_tag,omitempty"`
TagIgnore []string `toml:"tag_ignore,omitempty" json:"tag_ignore,omitempty"`
PkgName string `toml:"pkg_name,omitempty" json:"pkg_name,omitempty"`
OutFolder string `toml:"out_folder,omitempty" json:"out_folder,omitempty"`
TemplateDirs []string `toml:"template_dirs,omitempty" json:"template_dirs,omitempty"`
Tags []string `toml:"tags,omitempty" json:"tags,omitempty"`
Replacements []string `toml:"replacements,omitempty" json:"replacements,omitempty"`
Debug bool `toml:"debug,omitempty" json:"debug,omitempty"`
AddGlobal bool `toml:"add_global,omitempty" json:"add_global,omitempty"`
AddPanic bool `toml:"add_panic,omitempty" json:"add_panic,omitempty"`
AddSoftDeletes bool `toml:"add_soft_deletes,omitempty" json:"add_soft_deletes,omitempty"`
AddEnumTypes bool `toml:"add_enum_types,omitempty" json:"add_enum_types,omitempty"`
EnumNullPrefix string `toml:"enum_null_prefix,omitempty" json:"enum_null_prefix,omitempty"`
NoContext bool `toml:"no_context,omitempty" json:"no_context,omitempty"`
NoTests bool `toml:"no_tests,omitempty" json:"no_tests,omitempty"`
NoHooks bool `toml:"no_hooks,omitempty" json:"no_hooks,omitempty"`
NoRelationshipFuncs bool `toml:"no_relationship_funcs,omitempty" json:"no_relationship_funcs,omitempty"`
NoAutoTimestamps bool `toml:"no_auto_timestamps,omitempty" json:"no_auto_timestamps,omitempty"`
NoRowsAffected bool `toml:"no_rows_affected,omitempty" json:"no_rows_affected,omitempty"`
NoDriverTemplates bool `toml:"no_driver_templates,omitempty" json:"no_driver_templates,omitempty"`
NoBackReferencing bool `toml:"no_back_reference,omitempty" json:"no_back_reference,omitempty"`
AlwaysWrapErrors bool `toml:"always_wrap_errors,omitempty" json:"always_wrap_errors,omitempty"`
Wipe bool `toml:"wipe,omitempty" json:"wipe,omitempty"`
StructTagCasing string `toml:"struct_tag_casing,omitempty" json:"struct_tag_casing,omitempty"`
RelationTag string `toml:"relation_tag,omitempty" json:"relation_tag,omitempty"`
TagIgnore []string `toml:"tag_ignore,omitempty" json:"tag_ignore,omitempty"`

Imports importers.Collection `toml:"imports,omitempty" json:"imports,omitempty"`

Expand Down
2 changes: 1 addition & 1 deletion boilingcore/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ func writeFile(outFolder string, fileName string, input *bytes.Buffer, format bo
}

path := filepath.Join(outFolder, fileName)
if err := testHarnessWriteFile(path, byt, 0664); err != nil {
if err := testHarnessWriteFile(path, byt, 0o664); err != nil {
return errors.Wrapf(err, "failed to write output file %s", path)
}

Expand Down
25 changes: 13 additions & 12 deletions boilingcore/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,19 @@ type templateData struct {
RQ string

// Control various generation features
AddGlobal bool
AddPanic bool
AddSoftDeletes bool
AddEnumTypes bool
EnumNullPrefix string
NoContext bool
NoHooks bool
NoAutoTimestamps bool
NoRowsAffected bool
NoDriverTemplates bool
NoBackReferencing bool
AlwaysWrapErrors bool
AddGlobal bool
AddPanic bool
AddSoftDeletes bool
AddEnumTypes bool
EnumNullPrefix string
NoContext bool
NoHooks bool
NoRelationshipFuncs bool
NoAutoTimestamps bool
NoRowsAffected bool
NoDriverTemplates bool
NoBackReferencing bool
AlwaysWrapErrors bool

// Tags control which tags are added to the struct
Tags []string
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ require (
github.com/huandu/xstrings v1.3.2 // indirect
github.com/imdario/mergo v0.3.13 // indirect
github.com/inconshreveable/mousetrap v1.0.1 // indirect
github.com/kat-co/vala v0.0.0-20170210184112-42e1d8b61f12
github.com/lib/pq v1.10.6
github.com/microsoft/go-mssqldb v0.17.0
github.com/mitchellh/copystructure v1.2.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,8 @@ github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1
github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk=
github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM=
github.com/kat-co/vala v0.0.0-20170210184112-42e1d8b61f12 h1:DQVOxR9qdYEybJUr/c7ku34r3PfajaMYXZwgDM7KuSk=
github.com/kat-co/vala v0.0.0-20170210184112-42e1d8b61f12/go.mod h1:u9MdXq/QageOOSGp7qG4XAQsYUMP+V5zEel/Vrl6OOc=
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 h1:Z9n2FFNUXsshfwJMBgNA0RU6/i7WVaAegv3PtuIHPMs=
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51/go.mod h1:CzGEWj7cYgsdH8dAjBGEr58BoE7ScuLd+fwFZ44+/x8=
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
Expand Down
6 changes: 5 additions & 1 deletion importers/imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,6 @@ func MapFromInterface(intf interface{}) (Map, error) {
m[name] = s
return nil
})

if err != nil {
return nil, err
}
Expand Down Expand Up @@ -282,6 +281,11 @@ func NewDefaultImports() Collection {
`"testing"`,
},
},
"boil_relationship_test": {
Standard: List{
`"testing"`,
},
},
}

return col
Expand Down
54 changes: 28 additions & 26 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ func main() {
rootCmd.PersistentFlags().BoolP("no-context", "", false, "Disable context.Context usage in the generated code")
rootCmd.PersistentFlags().BoolP("no-tests", "", false, "Disable generated go test files")
rootCmd.PersistentFlags().BoolP("no-hooks", "", false, "Disable hooks feature for your models")
rootCmd.PersistentFlags().BoolP("no-relationship-funcs", "", false, "Disable relationship loaders and setters feature for your models")
rootCmd.PersistentFlags().BoolP("no-rows-affected", "", false, "Disable rows affected in the generated API")
rootCmd.PersistentFlags().BoolP("no-auto-timestamps", "", false, "Disable automatic timestamps for created_at/updated_at")
rootCmd.PersistentFlags().BoolP("no-driver-templates", "", false, "Disable parsing of templates defined by the database driver")
Expand Down Expand Up @@ -153,32 +154,33 @@ func preRun(cmd *cobra.Command, args []string) error {
}

cmdConfig = &boilingcore.Config{
DriverName: driverName,
OutFolder: viper.GetString("output"),
PkgName: viper.GetString("pkgname"),
Debug: viper.GetBool("debug"),
AddGlobal: viper.GetBool("add-global-variants"),
AddPanic: viper.GetBool("add-panic-variants"),
AddSoftDeletes: viper.GetBool("add-soft-deletes"),
AddEnumTypes: viper.GetBool("add-enum-types"),
EnumNullPrefix: viper.GetString("enum-null-prefix"),
NoContext: viper.GetBool("no-context"),
NoTests: viper.GetBool("no-tests"),
NoHooks: viper.GetBool("no-hooks"),
NoRowsAffected: viper.GetBool("no-rows-affected"),
NoAutoTimestamps: viper.GetBool("no-auto-timestamps"),
NoDriverTemplates: viper.GetBool("no-driver-templates"),
NoBackReferencing: viper.GetBool("no-back-referencing"),
AlwaysWrapErrors: viper.GetBool("always-wrap-errors"),
Wipe: viper.GetBool("wipe"),
StructTagCasing: strings.ToLower(viper.GetString("struct-tag-casing")), // camel | snake | title
TagIgnore: viper.GetStringSlice("tag-ignore"),
RelationTag: viper.GetString("relation-tag"),
TemplateDirs: viper.GetStringSlice("templates"),
Tags: viper.GetStringSlice("tag"),
Replacements: viper.GetStringSlice("replace"),
Aliases: boilingcore.ConvertAliases(viper.Get("aliases")),
TypeReplaces: boilingcore.ConvertTypeReplace(viper.Get("types")),
DriverName: driverName,
OutFolder: viper.GetString("output"),
PkgName: viper.GetString("pkgname"),
Debug: viper.GetBool("debug"),
AddGlobal: viper.GetBool("add-global-variants"),
AddPanic: viper.GetBool("add-panic-variants"),
AddSoftDeletes: viper.GetBool("add-soft-deletes"),
AddEnumTypes: viper.GetBool("add-enum-types"),
EnumNullPrefix: viper.GetString("enum-null-prefix"),
NoContext: viper.GetBool("no-context"),
NoTests: viper.GetBool("no-tests"),
NoHooks: viper.GetBool("no-hooks"),
NoRelationshipFuncs: viper.GetBool("no-relationship-funcs"),
NoRowsAffected: viper.GetBool("no-rows-affected"),
NoAutoTimestamps: viper.GetBool("no-auto-timestamps"),
NoDriverTemplates: viper.GetBool("no-driver-templates"),
NoBackReferencing: viper.GetBool("no-back-referencing"),
AlwaysWrapErrors: viper.GetBool("always-wrap-errors"),
Wipe: viper.GetBool("wipe"),
StructTagCasing: strings.ToLower(viper.GetString("struct-tag-casing")), // camel | snake | title
TagIgnore: viper.GetStringSlice("tag-ignore"),
RelationTag: viper.GetString("relation-tag"),
TemplateDirs: viper.GetStringSlice("templates"),
Tags: viper.GetStringSlice("tag"),
Replacements: viper.GetStringSlice("replace"),
Aliases: boilingcore.ConvertAliases(viper.Get("aliases")),
TypeReplaces: boilingcore.ConvertTypeReplace(viper.Get("types")),
AutoColumns: boilingcore.AutoColumns{
Created: viper.GetString("auto-columns.created"),
Updated: viper.GetString("auto-columns.updated"),
Expand Down
2 changes: 2 additions & 0 deletions templates/main/04_relationship_to_one.go.tpl
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{{- if not .NoRelationshipFuncs -}}
{{- if or .Table.IsJoinTable .Table.IsView -}}
{{- else -}}
{{- range $fkey := .Table.FKeys -}}
Expand All @@ -17,3 +18,4 @@ func (o *{{$ltable.UpSingular}}) {{$rel.Foreign}}(mods ...qm.QueryMod) ({{$ftabl
}
{{- end -}}
{{- end -}}
{{- end -}}{{/* no relationship */}}
2 changes: 2 additions & 0 deletions templates/main/05_relationship_one_to_one.go.tpl
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{{- if not .NoRelationshipFuncs -}}
{{- if or .Table.IsJoinTable .Table.IsView -}}
{{- else -}}
{{- range $rel := .Table.ToOneRelationships -}}
Expand All @@ -17,3 +18,4 @@ func (o *{{$ltable.UpSingular}}) {{$relAlias.Local}}(mods ...qm.QueryMod) ({{$ft
}
{{- end -}}
{{- end -}}
{{- end -}}{{/* no relationship */}}
2 changes: 2 additions & 0 deletions templates/main/06_relationship_to_many.go.tpl
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{{- if not .NoRelationshipFuncs -}}
{{- if or .Table.IsJoinTable .Table.IsView -}}
{{- else -}}
{{- range $rel := .Table.ToManyRelationships -}}
Expand Down Expand Up @@ -31,3 +32,4 @@ func (o *{{$ltable.UpSingular}}) {{$relAlias.Local}}(mods ...qm.QueryMod) {{$fta

{{end -}}{{- /* range relationships */ -}}
{{- end -}}{{- /* if isJoinTable */ -}}
{{- end -}}{{/* no relationship */}}
14 changes: 8 additions & 6 deletions templates/main/07_relationship_to_one_eager.go.tpl
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{{- if not .NoRelationshipFuncs -}}
{{- if or .Table.IsJoinTable .Table.IsView -}}
{{- else -}}
{{- range $fkey := .Table.FKeys -}}
Expand Down Expand Up @@ -81,12 +82,12 @@ func ({{$ltable.DownSingular}}L) Load{{$rel.Foreign}}({{if $.NoContext}}e boil.E
}

query := NewQuery(
qm.From(`{{if $.Dialect.UseSchema}}{{$.Schema}}.{{end}}{{.ForeignTable}}`),
qm.WhereIn(`{{if $.Dialect.UseSchema}}{{$.Schema}}.{{end}}{{.ForeignTable}}.{{.ForeignColumn}} in ?`, args...),
{{if and $.AddSoftDeletes $canSoftDelete -}}
qmhelper.WhereIsNull(`{{if $.Dialect.UseSchema}}{{$.Schema}}.{{end}}{{.ForeignTable}}.{{or $.AutoColumns.Deleted "deleted_at"}}`),
{{- end}}
)
qm.From(`{{if $.Dialect.UseSchema}}{{$.Schema}}.{{end}}{{.ForeignTable}}`),
qm.WhereIn(`{{if $.Dialect.UseSchema}}{{$.Schema}}.{{end}}{{.ForeignTable}}.{{.ForeignColumn}} in ?`, args...),
{{if and $.AddSoftDeletes $canSoftDelete -}}
qmhelper.WhereIsNull(`{{if $.Dialect.UseSchema}}{{$.Schema}}.{{end}}{{.ForeignTable}}.{{or $.AutoColumns.Deleted "deleted_at"}}`),
{{- end}}
)
if mods != nil {
mods.Apply(query)
}
Expand Down Expand Up @@ -169,3 +170,4 @@ func ({{$ltable.DownSingular}}L) Load{{$rel.Foreign}}({{if $.NoContext}}e boil.E
}
{{end -}}{{/* range */}}
{{end}}{{/* join table */}}
{{- end -}}{{/* no relationship */}}
Loading

0 comments on commit 0be697e

Please sign in to comment.