Skip to content

Commit ff900fe

Browse files
committed
feat(gen): Support creating templates without having to pass a context
Closes: #504
1 parent 5dec281 commit ff900fe

File tree

9 files changed

+31
-21
lines changed

9 files changed

+31
-21
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2929
- Updated documentation for readability, added code gen examples. (thanks @singhsays)
3030
- Columns are now matched in a case-insensitive manner in type replacements. (thanks @abdusco)
3131
- Columns can now be matched with as many conditions as needed in type replacements. This removes the previous requirement that boolean fields had to be specified in addition to a string field. (thanks @abdusco)
32+
- Factories now expose a `NewXWithContext` that accepts a context in addition to `NewX` that does not. This provides a cleaner API for the callers, while still allowing the use of context internally. (thanks @abdusco)
3233

3334
### Removed
3435

gen/language/go.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@ func findGoMod(path string) (string, error) {
237237
return "", fmt.Errorf("could not create destination folder %q: %w", path, err)
238238
}
239239

240+
//nolint:noctx
240241
c := exec.Command("go", "env", "GOMOD")
241242
c.Stdout = &outData
242243
c.Stderr = &errData

gen/templates/factory/bobfactory_main.bob.go.tpl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@ func New() *Factory {
1414

1515
{{range $table := .Tables}}
1616
{{ $tAlias := $.Aliases.Table $table.Key -}}
17-
func (f *Factory) New{{$tAlias.UpSingular}}(ctx context.Context, mods ...{{$tAlias.UpSingular}}Mod) *{{$tAlias.UpSingular}}Template {
17+
func (f *Factory) New{{$tAlias.UpSingular}}(mods ...{{$tAlias.UpSingular}}Mod) *{{$tAlias.UpSingular}}Template {
18+
return f.New{{$tAlias.UpSingular}}WithContext(context.Background(), mods...)
19+
}
20+
21+
func (f *Factory) New{{$tAlias.UpSingular}}WithContext(ctx context.Context, mods ...{{$tAlias.UpSingular}}Mod) *{{$tAlias.UpSingular}}Template {
1822
o := &{{$tAlias.UpSingular}}Template{f: f}
1923

2024
if f != nil {

gen/templates/factory/bobfactory_main.bob_test.go.tpl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ func TestCreate{{$tAlias.UpSingular}}(t *testing.T) {
88
t.Skip("skipping test, no DSN provided")
99
}
1010

11-
ctx, cancel := context.WithCancel(context.Background())
11+
ctx, cancel := context.WithCancel(t.Context())
1212
t.Cleanup(cancel)
1313

1414
tx, err := testDB.Begin(ctx)
@@ -22,7 +22,7 @@ func TestCreate{{$tAlias.UpSingular}}(t *testing.T) {
2222
}
2323
}()
2424

25-
if _, err := New().New{{$tAlias.UpSingular}}(ctx).Create(ctx, tx); err != nil {
25+
if _, err := New().New{{$tAlias.UpSingular}}WithContext(ctx).Create(ctx, tx); err != nil {
2626
t.Fatalf("Error creating {{$tAlias.UpSingular}}: %v", err)
2727
}
2828
}

gen/templates/factory/table/12_rel_to_one_mods.go.tpl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ func (m {{$tAlias.DownSingular}}Mods) WithParentsCascading() {{$tAlias.UpSingula
1414
{
1515
{{range $.Tables.NeededBridgeRels . -}}
1616
{{$alias := $.Aliases.Table .Table -}}
17-
{{$alias.DownSingular}}{{.Position}} := o.f.New{{$alias.UpSingular}}(ctx)
17+
{{$alias.DownSingular}}{{.Position}} := o.f.New{{$alias.UpSingular}}WithContext(ctx)
1818
{{end}}
19-
related := o.f.New{{$ftable.UpSingular}}(ctx, {{$ftable.UpSingular}}Mods.WithParentsCascading())
19+
related := o.f.New{{$ftable.UpSingular}}WithContext(ctx, {{$ftable.UpSingular}}Mods.WithParentsCascading())
2020
m.With{{$relAlias}}({{$.Tables.RelArgs $.Aliases .}} related).Apply(ctx, o)
2121
}
2222
{{end -}}
@@ -41,9 +41,9 @@ func (m {{$tAlias.DownSingular}}Mods) WithNew{{$relAlias}}(mods ...{{$ftable.UpS
4141
return {{$tAlias.UpSingular}}ModFunc(func (ctx context.Context, o *{{$tAlias.UpSingular}}Template) {
4242
{{range $.Tables.NeededBridgeRels . -}}
4343
{{$alias := $.Aliases.Table .Table -}}
44-
{{$alias.DownSingular}}{{.Position}} := o.f.New{{$alias.UpSingular}}(ctx)
44+
{{$alias.DownSingular}}{{.Position}} := o.f.New{{$alias.UpSingular}}WithContext(ctx)
4545
{{end}}
46-
related := o.f.New{{$ftable.UpSingular}}(ctx, mods...)
46+
related := o.f.New{{$ftable.UpSingular}}WithContext(ctx, mods...)
4747

4848
m.With{{$relAlias}}({{$.Tables.RelArgs $.Aliases .}} related).Apply(ctx, o)
4949
})

gen/templates/factory/table/13_rel_to_many_mods.go.tpl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ func (m {{$tAlias.DownSingular}}Mods) WithNew{{$relAlias}}(number int, mods ...{
2222
return {{$tAlias.UpSingular}}ModFunc(func (ctx context.Context, o *{{$tAlias.UpSingular}}Template) {
2323
{{range $.Tables.NeededBridgeRels . -}}
2424
{{$alias := $.Aliases.Table .Table -}}
25-
{{$alias.DownSingular}}{{.Position}} := o.f.New{{$alias.UpSingular}}(ctx)
25+
{{$alias.DownSingular}}{{.Position}} := o.f.New{{$alias.UpSingular}}WithContext(ctx)
2626
{{end}}
2727

28-
related := o.f.New{{$ftable.UpSingular}}(ctx, mods...)
28+
related := o.f.New{{$ftable.UpSingular}}WithContext(ctx, mods...)
2929
m.With{{$relAlias}}(number, {{$.Tables.RelArgs $.Aliases .}} related).Apply(ctx, o)
3030
})
3131
}
@@ -44,10 +44,10 @@ func (m {{$tAlias.DownSingular}}Mods) AddNew{{$relAlias}}(number int, mods ...{{
4444
return {{$tAlias.UpSingular}}ModFunc(func (ctx context.Context, o *{{$tAlias.UpSingular}}Template) {
4545
{{range $.Tables.NeededBridgeRels . -}}
4646
{{$alias := $.Aliases.Table .Table -}}
47-
{{$alias.DownSingular}}{{.Position}} := o.f.New{{$alias.UpSingular}}(ctx)
47+
{{$alias.DownSingular}}{{.Position}} := o.f.New{{$alias.UpSingular}}WithContext(ctx)
4848
{{end}}
4949

50-
related := o.f.New{{$ftable.UpSingular}}(ctx, mods...)
50+
related := o.f.New{{$ftable.UpSingular}}WithContext(ctx, mods...)
5151
m.Add{{$relAlias}}(number, {{$.Tables.RelArgs $.Aliases .}} related).Apply(ctx, o)
5252
})
5353
}

gen/templates/models/table/01_types_test.go.tpl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func Test{{$tAlias.UpSingular}}UniqueConstraintErrors(t *testing.T) {
4040
{{end}}
4141

4242
if shouldUpdate {
43-
if err := obj.Update(ctx, exec, f.New{{$tAlias.UpSingular}}(ctx, updateMods...).BuildSetter()); err != nil {
43+
if err := obj.Update(ctx, exec, f.New{{$tAlias.UpSingular}}WithContext(ctx, updateMods...).BuildSetter()); err != nil {
4444
t.Fatalf("Error updating object: %v", err)
4545
}
4646
}
@@ -59,7 +59,7 @@ func Test{{$tAlias.UpSingular}}UniqueConstraintErrors(t *testing.T) {
5959

6060
for _, tt := range tests {
6161
t.Run(tt.name, func(t *testing.T) {
62-
ctx, cancel := context.WithCancel(context.Background())
62+
ctx, cancel := context.WithCancel(t.Context())
6363
t.Cleanup(cancel)
6464
6565
tx, err := testDB.Begin(ctx)
@@ -75,17 +75,17 @@ func Test{{$tAlias.UpSingular}}UniqueConstraintErrors(t *testing.T) {
7575

7676
var exec bob.Executor = tx
7777

78-
obj, err := f.New{{$tAlias.UpSingular}}(ctx, factory.{{$tAlias.UpSingular}}Mods.WithParentsCascading()).Create(ctx, exec)
78+
obj, err := f.New{{$tAlias.UpSingular}}WithContext(ctx, factory.{{$tAlias.UpSingular}}Mods.WithParentsCascading()).Create(ctx, exec)
7979
if err != nil {
8080
t.Fatal(err)
8181
}
8282

83-
obj2, err := f.New{{$tAlias.UpSingular}}(ctx).Create(ctx, exec)
83+
obj2, err := f.New{{$tAlias.UpSingular}}WithContext(ctx).Create(ctx, exec)
8484
if err != nil {
8585
t.Fatal(err)
8686
}
8787

88-
err = obj2.Update(ctx, exec, f.New{{$tAlias.UpSingular}}(ctx, tt.conflictMods(ctx, exec, obj)...).BuildSetter())
88+
err = obj2.Update(ctx, exec, f.New{{$tAlias.UpSingular}}WithContext(ctx, tt.conflictMods(ctx, exec, obj)...).BuildSetter())
8989
if !errors.Is(models.ErrUniqueConstraint, err) {
9090
t.Fatalf("Expected: %s, Got: %v", tt.name, err)
9191
}

gen/templates/queries/query/01_query_test.go.tpl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func Test{{$upperName}} (t *testing.T) {
2525
2626
query := {{$upperName}}({{join ", " $args}})
2727

28-
if _, err := query.WriteQuery(context.Background(), &sb, 1); err != nil {
28+
if _, err := query.WriteQuery(t.Context(), &sb, 1); err != nil {
2929
t.Fatal(err)
3030
}
3131

@@ -40,7 +40,7 @@ func Test{{$upperName}} (t *testing.T) {
4040
4141
query := {{$upperName}}({{join ", " $args}})
4242

43-
if _, err := {{$.Dialect}}.{{$queryType}}(query).WriteQuery(context.Background(), &sb, 1); err != nil {
43+
if _, err := {{$.Dialect}}.{{$queryType}}(query).WriteQuery(t.Context(), &sb, 1); err != nil {
4444
t.Fatal(err)
4545
}
4646

@@ -60,7 +60,7 @@ func Test{{$upperName}} (t *testing.T) {
6060
t.Skip("skipping test, no DSN provided")
6161
}
6262

63-
ctxTx, cancel := context.WithCancel(context.Background())
63+
ctxTx, cancel := context.WithCancel(t.Context())
6464
defer cancel()
6565

6666
tx, err := testDB.Begin(ctxTx)
@@ -106,7 +106,7 @@ func Test{{$upperName}} (t *testing.T) {
106106
t.Skip("skipping test, no DSN provided")
107107
}
108108

109-
ctxTx, cancel := context.WithCancel(context.Background())
109+
ctxTx, cancel := context.WithCancel(t.Context())
110110
defer cancel()
111111

112112
tx, err := testDB.Begin(ctxTx)

test/gen/gen.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ func TestDriver[T, C, I any](t *testing.T, config DriverTestConfig[T, C, I]) {
138138
t.SkipNow()
139139
}
140140

141+
//nolint:noctx
141142
cmd := exec.Command("go", "env", "GOMOD")
142143
output, err := cmd.Output()
143144
if err != nil {
@@ -194,6 +195,7 @@ func testDriver[T, C, I any](t *testing.T, dst string, tpls *helpers.Templates,
194195
t.Helper()
195196
buf := &bytes.Buffer{}
196197

198+
//nolint:noctx
197199
cmd := exec.Command("go", "mod", "init", module)
198200
cmd.Dir = dst
199201
cmd.Stdout = buf
@@ -205,7 +207,7 @@ func testDriver[T, C, I any](t *testing.T, dst string, tpls *helpers.Templates,
205207
t.Fatalf("go mod init cmd execution failed: %s", err)
206208
}
207209

208-
//nolint:gosec
210+
//nolint:gosec,noctx
209211
cmd = exec.Command("go", "mod", "edit", fmt.Sprintf("-replace=github.com/stephenafamo/bob=%s", filepath.Dir(modPath)))
210212
cmd.Dir = dst
211213
cmd.Stdout = buf
@@ -228,6 +230,7 @@ func testDriver[T, C, I any](t *testing.T, dst string, tpls *helpers.Templates,
228230
}
229231

230232
// From go1.16 dependencies are not auto downloaded
233+
//nolint:noctx
231234
cmd = exec.Command("go", "mod", "tidy")
232235
cmd.Dir = dst
233236
cmd.Stdout = buf
@@ -239,6 +242,7 @@ func testDriver[T, C, I any](t *testing.T, dst string, tpls *helpers.Templates,
239242
t.Fatalf("go mod tidy cmd execution failed: %s", err)
240243
}
241244

245+
//nolint:noctx
242246
cmd = exec.Command("go", "test", "-v", "./...")
243247
cmd.Dir = dst
244248
cmd.Stdout = buf

0 commit comments

Comments
 (0)