Skip to content

Commit ea56d78

Browse files
authored
Merge pull request #1439 from parnic/add-relation-getters-on-base-struct
Add relation getters on base model structs
2 parents 0561d17 + bd3128e commit ea56d78

File tree

6 files changed

+55
-6
lines changed

6 files changed

+55
-6
lines changed

README.md

+2
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

+1
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

+1
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

+1
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

+2
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

+48-6
Original file line numberDiff line numberDiff line change
@@ -197,35 +197,77 @@ 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+
203+
func (o *{{$alias.UpSingular}}) Get{{$relAlias.Foreign}}() *{{$ftable.UpSingular}} {
204+
if (o == nil) {
205+
return nil
206+
}
207+
208+
return o.R.Get{{$relAlias.Foreign}}()
209+
}
210+
211+
{{end -}}
212+
200213
func (r *{{$alias.DownSingular}}R) Get{{$relAlias.Foreign}}() *{{$ftable.UpSingular}} {
201214
if (r == nil) {
202-
return nil
215+
return nil
203216
}
204-
return r.{{$relAlias.Foreign}}
217+
218+
return r.{{$relAlias.Foreign}}
205219
}
206220
207221
{{end -}}
208222
209223
{{- range .Table.ToOneRelationships -}}
210224
{{- $ftable := $.Aliases.Table .ForeignTable -}}
211225
{{- $relAlias := $ftable.Relationship .Name -}}
226+
227+
{{- if not $.NoRelationGetters}}
228+
229+
func (o *{{$alias.UpSingular}}) Get{{$relAlias.Local}}() *{{$ftable.UpSingular}} {
230+
if (o == nil) {
231+
return nil
232+
}
233+
234+
return o.R.Get{{$relAlias.Local}}()
235+
}
236+
237+
{{end -}}
238+
212239
func (r *{{$alias.DownSingular}}R) Get{{$relAlias.Local}}() *{{$ftable.UpSingular}} {
213240
if (r == nil) {
214-
return nil
241+
return nil
215242
}
216-
return r.{{$relAlias.Local}}
243+
244+
return r.{{$relAlias.Local}}
217245
}
218246
219247
{{end -}}
220248
221249
{{- range .Table.ToManyRelationships -}}
222250
{{- $ftable := $.Aliases.Table .ForeignTable -}}
223251
{{- $relAlias := $.Aliases.ManyRelationship .ForeignTable .Name .JoinTable .JoinLocalFKeyName -}}
252+
253+
{{- if not $.NoRelationGetters}}
254+
255+
func (o *{{$alias.UpSingular}}) Get{{$relAlias.Local}}() {{printf "%sSlice" $ftable.UpSingular}} {
256+
if (o == nil) {
257+
return nil
258+
}
259+
260+
return o.R.Get{{$relAlias.Local}}()
261+
}
262+
263+
{{end -}}
264+
224265
func (r *{{$alias.DownSingular}}R) Get{{$relAlias.Local}}() {{printf "%sSlice" $ftable.UpSingular}} {
225266
if (r == nil) {
226-
return nil
267+
return nil
227268
}
228-
return r.{{$relAlias.Local}}
269+
270+
return r.{{$relAlias.Local}}
229271
}
230272
231273
{{end -}}

0 commit comments

Comments
 (0)