Skip to content

Commit bd3128e

Browse files
committed
PR feedback: add config for relation getters feature
1 parent abf0695 commit bd3128e

File tree

6 files changed

+22
-0
lines changed

6 files changed

+22
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,7 @@ not to pass them through the command line or environment variables:
415415
| no-auto-timestamps | false |
416416
| no-rows-affected | false |
417417
| no-driver-templates | false |
418+
| no-relation-getters | false |
418419
| tag-ignore | [] |
419420
| strict-verify-mod-version | false |
420421

@@ -485,6 +486,7 @@ Flags:
485486
--no-hooks Disable hooks feature for your models
486487
--no-rows-affected Disable rows affected in the generated API
487488
--no-tests Disable generated go test files
489+
--no-relation-getters Disable generating getters for relationship tables
488490
-o, --output string The name of the folder to output to (default "models")
489491
-p, --pkgname string The name you wish to assign to your generated package (default "models")
490492
--struct-tag-casing string Decides the casing for go structure tag names. camel, title, alias or snake (default "snake")

boilingcore/boilingcore.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ func (s *State) Run() error {
157157
NoRowsAffected: s.Config.NoRowsAffected,
158158
NoDriverTemplates: s.Config.NoDriverTemplates,
159159
NoBackReferencing: s.Config.NoBackReferencing,
160+
NoRelationGetters: s.Config.NoRelationGetters,
160161
AlwaysWrapErrors: s.Config.AlwaysWrapErrors,
161162
StructTagCasing: s.Config.StructTagCasing,
162163
StructTagCases: s.Config.StructTagCases,

boilingcore/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ type Config struct {
4747
NoRowsAffected bool `toml:"no_rows_affected,omitempty" json:"no_rows_affected,omitempty"`
4848
NoDriverTemplates bool `toml:"no_driver_templates,omitempty" json:"no_driver_templates,omitempty"`
4949
NoBackReferencing bool `toml:"no_back_reference,omitempty" json:"no_back_reference,omitempty"`
50+
NoRelationGetters bool `toml:"no_relation_getters,omitempty" json:"no_relation_getters,omitempty"`
5051
AlwaysWrapErrors bool `toml:"always_wrap_errors,omitempty" json:"always_wrap_errors,omitempty"`
5152
Wipe bool `toml:"wipe,omitempty" json:"wipe,omitempty"`
5253

boilingcore/templates.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ type templateData struct {
5252
NoRowsAffected bool
5353
NoDriverTemplates bool
5454
NoBackReferencing bool
55+
NoRelationGetters bool
5556
AlwaysWrapErrors bool
5657

5758
// Tags control which tags are added to the struct

main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ func main() {
102102
rootCmd.PersistentFlags().BoolP("no-driver-templates", "", false, "Disable parsing of templates defined by the database driver")
103103
rootCmd.PersistentFlags().BoolP("no-back-referencing", "", false, "Disable back referencing in the loaded relationship structs")
104104
rootCmd.PersistentFlags().BoolP("no-schema", "", false, "Disable generating a schema in the output")
105+
rootCmd.PersistentFlags().BoolP("no-relation-getters", "", false, "Disable generating getters for relationship tables")
105106
rootCmd.PersistentFlags().BoolP("always-wrap-errors", "", false, "Wrap all returned errors with stacktraces, also sql.ErrNoRows")
106107
rootCmd.PersistentFlags().BoolP("add-global-variants", "", false, "Enable generation for global variants")
107108
rootCmd.PersistentFlags().BoolP("add-panic-variants", "", false, "Enable generation for panic variants")
@@ -173,6 +174,7 @@ func preRun(cmd *cobra.Command, args []string) error {
173174
NoAutoTimestamps: viper.GetBool("no-auto-timestamps"),
174175
NoDriverTemplates: viper.GetBool("no-driver-templates"),
175176
NoBackReferencing: viper.GetBool("no-back-referencing"),
177+
NoRelationGetters: viper.GetBool("no-relation-getters"),
176178
AlwaysWrapErrors: viper.GetBool("always-wrap-errors"),
177179
Wipe: viper.GetBool("wipe"),
178180
StructTagCasing: strings.ToLower(viper.GetString("struct-tag-casing")), // camel | snake | title

templates/main/00_struct.go.tpl

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,9 @@ func (*{{$alias.DownSingular}}R) NewStruct() *{{$alias.DownSingular}}R {
197197
{{range .Table.FKeys -}}
198198
{{- $ftable := $.Aliases.Table .ForeignTable -}}
199199
{{- $relAlias := $alias.Relationship .Name -}}
200+
201+
{{- if not $.NoRelationGetters}}
202+
200203
func (o *{{$alias.UpSingular}}) Get{{$relAlias.Foreign}}() *{{$ftable.UpSingular}} {
201204
if (o == nil) {
202205
return nil
@@ -205,6 +208,8 @@ func (o *{{$alias.UpSingular}}) Get{{$relAlias.Foreign}}() *{{$ftable.UpSingular
205208
return o.R.Get{{$relAlias.Foreign}}()
206209
}
207210
211+
{{end -}}
212+
208213
func (r *{{$alias.DownSingular}}R) Get{{$relAlias.Foreign}}() *{{$ftable.UpSingular}} {
209214
if (r == nil) {
210215
return nil
@@ -218,6 +223,9 @@ func (r *{{$alias.DownSingular}}R) Get{{$relAlias.Foreign}}() *{{$ftable.UpSingu
218223
{{- range .Table.ToOneRelationships -}}
219224
{{- $ftable := $.Aliases.Table .ForeignTable -}}
220225
{{- $relAlias := $ftable.Relationship .Name -}}
226+
227+
{{- if not $.NoRelationGetters}}
228+
221229
func (o *{{$alias.UpSingular}}) Get{{$relAlias.Local}}() *{{$ftable.UpSingular}} {
222230
if (o == nil) {
223231
return nil
@@ -226,6 +234,8 @@ func (o *{{$alias.UpSingular}}) Get{{$relAlias.Local}}() *{{$ftable.UpSingular}}
226234
return o.R.Get{{$relAlias.Local}}()
227235
}
228236
237+
{{end -}}
238+
229239
func (r *{{$alias.DownSingular}}R) Get{{$relAlias.Local}}() *{{$ftable.UpSingular}} {
230240
if (r == nil) {
231241
return nil
@@ -239,6 +249,9 @@ func (r *{{$alias.DownSingular}}R) Get{{$relAlias.Local}}() *{{$ftable.UpSingula
239249
{{- range .Table.ToManyRelationships -}}
240250
{{- $ftable := $.Aliases.Table .ForeignTable -}}
241251
{{- $relAlias := $.Aliases.ManyRelationship .ForeignTable .Name .JoinTable .JoinLocalFKeyName -}}
252+
253+
{{- if not $.NoRelationGetters}}
254+
242255
func (o *{{$alias.UpSingular}}) Get{{$relAlias.Local}}() {{printf "%sSlice" $ftable.UpSingular}} {
243256
if (o == nil) {
244257
return nil
@@ -247,6 +260,8 @@ func (o *{{$alias.UpSingular}}) Get{{$relAlias.Local}}() {{printf "%sSlice" $fta
247260
return o.R.Get{{$relAlias.Local}}()
248261
}
249262
263+
{{end -}}
264+
250265
func (r *{{$alias.DownSingular}}R) Get{{$relAlias.Local}}() {{printf "%sSlice" $ftable.UpSingular}} {
251266
if (r == nil) {
252267
return nil

0 commit comments

Comments
 (0)