nidus-sync/db/factory/comms.email.bob.go
Eli Ribble 842e6cff43
Move comms work to background goroutine
This is a sort of random checkpoint of work
 * add schema for tracking messages sent to DB
 * add terms of service and privacy policy for RCS compliance
 * standardize some things about background workers
 * update some missing stuff from generated DB code
2026-01-20 17:10:22 +00:00

434 lines
12 KiB
Go

// Code generated by BobGen psql v0.42.1. DO NOT EDIT.
// This file is meant to be re-generated in place and/or deleted at any time.
package factory
import (
"context"
"testing"
models "github.com/Gleipnir-Technology/nidus-sync/db/models"
"github.com/aarondl/opt/omit"
"github.com/jaswdr/faker/v2"
"github.com/stephenafamo/bob"
)
type CommsEmailMod interface {
Apply(context.Context, *CommsEmailTemplate)
}
type CommsEmailModFunc func(context.Context, *CommsEmailTemplate)
func (f CommsEmailModFunc) Apply(ctx context.Context, n *CommsEmailTemplate) {
f(ctx, n)
}
type CommsEmailModSlice []CommsEmailMod
func (mods CommsEmailModSlice) Apply(ctx context.Context, n *CommsEmailTemplate) {
for _, f := range mods {
f.Apply(ctx, n)
}
}
// CommsEmailTemplate is an object representing the database table.
// all columns are optional and should be set by mods
type CommsEmailTemplate struct {
Address func() string
Confirmed func() bool
IsSubscribed func() bool
r commsEmailR
f *Factory
alreadyPersisted bool
}
type commsEmailR struct {
DestinationEmailLogs []*commsEmailRDestinationEmailLogsR
}
type commsEmailRDestinationEmailLogsR struct {
number int
o *CommsEmailLogTemplate
}
// Apply mods to the CommsEmailTemplate
func (o *CommsEmailTemplate) Apply(ctx context.Context, mods ...CommsEmailMod) {
for _, mod := range mods {
mod.Apply(ctx, o)
}
}
// setModelRels creates and sets the relationships on *models.CommsEmail
// according to the relationships in the template. Nothing is inserted into the db
func (t CommsEmailTemplate) setModelRels(o *models.CommsEmail) {
if t.r.DestinationEmailLogs != nil {
rel := models.CommsEmailLogSlice{}
for _, r := range t.r.DestinationEmailLogs {
related := r.o.BuildMany(r.number)
for _, rel := range related {
rel.Destination = o.Address // h2
rel.R.DestinationEmail = o
}
rel = append(rel, related...)
}
o.R.DestinationEmailLogs = rel
}
}
// BuildSetter returns an *models.CommsEmailSetter
// this does nothing with the relationship templates
func (o CommsEmailTemplate) BuildSetter() *models.CommsEmailSetter {
m := &models.CommsEmailSetter{}
if o.Address != nil {
val := o.Address()
m.Address = omit.From(val)
}
if o.Confirmed != nil {
val := o.Confirmed()
m.Confirmed = omit.From(val)
}
if o.IsSubscribed != nil {
val := o.IsSubscribed()
m.IsSubscribed = omit.From(val)
}
return m
}
// BuildManySetter returns an []*models.CommsEmailSetter
// this does nothing with the relationship templates
func (o CommsEmailTemplate) BuildManySetter(number int) []*models.CommsEmailSetter {
m := make([]*models.CommsEmailSetter, number)
for i := range m {
m[i] = o.BuildSetter()
}
return m
}
// Build returns an *models.CommsEmail
// Related objects are also created and placed in the .R field
// NOTE: Objects are not inserted into the database. Use CommsEmailTemplate.Create
func (o CommsEmailTemplate) Build() *models.CommsEmail {
m := &models.CommsEmail{}
if o.Address != nil {
m.Address = o.Address()
}
if o.Confirmed != nil {
m.Confirmed = o.Confirmed()
}
if o.IsSubscribed != nil {
m.IsSubscribed = o.IsSubscribed()
}
o.setModelRels(m)
return m
}
// BuildMany returns an models.CommsEmailSlice
// Related objects are also created and placed in the .R field
// NOTE: Objects are not inserted into the database. Use CommsEmailTemplate.CreateMany
func (o CommsEmailTemplate) BuildMany(number int) models.CommsEmailSlice {
m := make(models.CommsEmailSlice, number)
for i := range m {
m[i] = o.Build()
}
return m
}
func ensureCreatableCommsEmail(m *models.CommsEmailSetter) {
if !(m.Address.IsValue()) {
val := random_string(nil)
m.Address = omit.From(val)
}
if !(m.Confirmed.IsValue()) {
val := random_bool(nil)
m.Confirmed = omit.From(val)
}
if !(m.IsSubscribed.IsValue()) {
val := random_bool(nil)
m.IsSubscribed = omit.From(val)
}
}
// insertOptRels creates and inserts any optional the relationships on *models.CommsEmail
// according to the relationships in the template.
// any required relationship should have already exist on the model
func (o *CommsEmailTemplate) insertOptRels(ctx context.Context, exec bob.Executor, m *models.CommsEmail) error {
var err error
isDestinationEmailLogsDone, _ := commsEmailRelDestinationEmailLogsCtx.Value(ctx)
if !isDestinationEmailLogsDone && o.r.DestinationEmailLogs != nil {
ctx = commsEmailRelDestinationEmailLogsCtx.WithValue(ctx, true)
for _, r := range o.r.DestinationEmailLogs {
if r.o.alreadyPersisted {
m.R.DestinationEmailLogs = append(m.R.DestinationEmailLogs, r.o.Build())
} else {
rel0, err := r.o.CreateMany(ctx, exec, r.number)
if err != nil {
return err
}
err = m.AttachDestinationEmailLogs(ctx, exec, rel0...)
if err != nil {
return err
}
}
}
}
return err
}
// Create builds a commsEmail and inserts it into the database
// Relations objects are also inserted and placed in the .R field
func (o *CommsEmailTemplate) Create(ctx context.Context, exec bob.Executor) (*models.CommsEmail, error) {
var err error
opt := o.BuildSetter()
ensureCreatableCommsEmail(opt)
m, err := models.CommsEmails.Insert(opt).One(ctx, exec)
if err != nil {
return nil, err
}
if err := o.insertOptRels(ctx, exec, m); err != nil {
return nil, err
}
return m, err
}
// MustCreate builds a commsEmail and inserts it into the database
// Relations objects are also inserted and placed in the .R field
// panics if an error occurs
func (o *CommsEmailTemplate) MustCreate(ctx context.Context, exec bob.Executor) *models.CommsEmail {
m, err := o.Create(ctx, exec)
if err != nil {
panic(err)
}
return m
}
// CreateOrFail builds a commsEmail and inserts it into the database
// Relations objects are also inserted and placed in the .R field
// It calls `tb.Fatal(err)` on the test/benchmark if an error occurs
func (o *CommsEmailTemplate) CreateOrFail(ctx context.Context, tb testing.TB, exec bob.Executor) *models.CommsEmail {
tb.Helper()
m, err := o.Create(ctx, exec)
if err != nil {
tb.Fatal(err)
return nil
}
return m
}
// CreateMany builds multiple commsEmails and inserts them into the database
// Relations objects are also inserted and placed in the .R field
func (o CommsEmailTemplate) CreateMany(ctx context.Context, exec bob.Executor, number int) (models.CommsEmailSlice, error) {
var err error
m := make(models.CommsEmailSlice, number)
for i := range m {
m[i], err = o.Create(ctx, exec)
if err != nil {
return nil, err
}
}
return m, nil
}
// MustCreateMany builds multiple commsEmails and inserts them into the database
// Relations objects are also inserted and placed in the .R field
// panics if an error occurs
func (o CommsEmailTemplate) MustCreateMany(ctx context.Context, exec bob.Executor, number int) models.CommsEmailSlice {
m, err := o.CreateMany(ctx, exec, number)
if err != nil {
panic(err)
}
return m
}
// CreateManyOrFail builds multiple commsEmails and inserts them into the database
// Relations objects are also inserted and placed in the .R field
// It calls `tb.Fatal(err)` on the test/benchmark if an error occurs
func (o CommsEmailTemplate) CreateManyOrFail(ctx context.Context, tb testing.TB, exec bob.Executor, number int) models.CommsEmailSlice {
tb.Helper()
m, err := o.CreateMany(ctx, exec, number)
if err != nil {
tb.Fatal(err)
return nil
}
return m
}
// CommsEmail has methods that act as mods for the CommsEmailTemplate
var CommsEmailMods commsEmailMods
type commsEmailMods struct{}
func (m commsEmailMods) RandomizeAllColumns(f *faker.Faker) CommsEmailMod {
return CommsEmailModSlice{
CommsEmailMods.RandomAddress(f),
CommsEmailMods.RandomConfirmed(f),
CommsEmailMods.RandomIsSubscribed(f),
}
}
// Set the model columns to this value
func (m commsEmailMods) Address(val string) CommsEmailMod {
return CommsEmailModFunc(func(_ context.Context, o *CommsEmailTemplate) {
o.Address = func() string { return val }
})
}
// Set the Column from the function
func (m commsEmailMods) AddressFunc(f func() string) CommsEmailMod {
return CommsEmailModFunc(func(_ context.Context, o *CommsEmailTemplate) {
o.Address = f
})
}
// Clear any values for the column
func (m commsEmailMods) UnsetAddress() CommsEmailMod {
return CommsEmailModFunc(func(_ context.Context, o *CommsEmailTemplate) {
o.Address = nil
})
}
// Generates a random value for the column using the given faker
// if faker is nil, a default faker is used
func (m commsEmailMods) RandomAddress(f *faker.Faker) CommsEmailMod {
return CommsEmailModFunc(func(_ context.Context, o *CommsEmailTemplate) {
o.Address = func() string {
return random_string(f)
}
})
}
// Set the model columns to this value
func (m commsEmailMods) Confirmed(val bool) CommsEmailMod {
return CommsEmailModFunc(func(_ context.Context, o *CommsEmailTemplate) {
o.Confirmed = func() bool { return val }
})
}
// Set the Column from the function
func (m commsEmailMods) ConfirmedFunc(f func() bool) CommsEmailMod {
return CommsEmailModFunc(func(_ context.Context, o *CommsEmailTemplate) {
o.Confirmed = f
})
}
// Clear any values for the column
func (m commsEmailMods) UnsetConfirmed() CommsEmailMod {
return CommsEmailModFunc(func(_ context.Context, o *CommsEmailTemplate) {
o.Confirmed = nil
})
}
// Generates a random value for the column using the given faker
// if faker is nil, a default faker is used
func (m commsEmailMods) RandomConfirmed(f *faker.Faker) CommsEmailMod {
return CommsEmailModFunc(func(_ context.Context, o *CommsEmailTemplate) {
o.Confirmed = func() bool {
return random_bool(f)
}
})
}
// Set the model columns to this value
func (m commsEmailMods) IsSubscribed(val bool) CommsEmailMod {
return CommsEmailModFunc(func(_ context.Context, o *CommsEmailTemplate) {
o.IsSubscribed = func() bool { return val }
})
}
// Set the Column from the function
func (m commsEmailMods) IsSubscribedFunc(f func() bool) CommsEmailMod {
return CommsEmailModFunc(func(_ context.Context, o *CommsEmailTemplate) {
o.IsSubscribed = f
})
}
// Clear any values for the column
func (m commsEmailMods) UnsetIsSubscribed() CommsEmailMod {
return CommsEmailModFunc(func(_ context.Context, o *CommsEmailTemplate) {
o.IsSubscribed = nil
})
}
// Generates a random value for the column using the given faker
// if faker is nil, a default faker is used
func (m commsEmailMods) RandomIsSubscribed(f *faker.Faker) CommsEmailMod {
return CommsEmailModFunc(func(_ context.Context, o *CommsEmailTemplate) {
o.IsSubscribed = func() bool {
return random_bool(f)
}
})
}
func (m commsEmailMods) WithParentsCascading() CommsEmailMod {
return CommsEmailModFunc(func(ctx context.Context, o *CommsEmailTemplate) {
if isDone, _ := commsEmailWithParentsCascadingCtx.Value(ctx); isDone {
return
}
ctx = commsEmailWithParentsCascadingCtx.WithValue(ctx, true)
})
}
func (m commsEmailMods) WithDestinationEmailLogs(number int, related *CommsEmailLogTemplate) CommsEmailMod {
return CommsEmailModFunc(func(ctx context.Context, o *CommsEmailTemplate) {
o.r.DestinationEmailLogs = []*commsEmailRDestinationEmailLogsR{{
number: number,
o: related,
}}
})
}
func (m commsEmailMods) WithNewDestinationEmailLogs(number int, mods ...CommsEmailLogMod) CommsEmailMod {
return CommsEmailModFunc(func(ctx context.Context, o *CommsEmailTemplate) {
related := o.f.NewCommsEmailLogWithContext(ctx, mods...)
m.WithDestinationEmailLogs(number, related).Apply(ctx, o)
})
}
func (m commsEmailMods) AddDestinationEmailLogs(number int, related *CommsEmailLogTemplate) CommsEmailMod {
return CommsEmailModFunc(func(ctx context.Context, o *CommsEmailTemplate) {
o.r.DestinationEmailLogs = append(o.r.DestinationEmailLogs, &commsEmailRDestinationEmailLogsR{
number: number,
o: related,
})
})
}
func (m commsEmailMods) AddNewDestinationEmailLogs(number int, mods ...CommsEmailLogMod) CommsEmailMod {
return CommsEmailModFunc(func(ctx context.Context, o *CommsEmailTemplate) {
related := o.f.NewCommsEmailLogWithContext(ctx, mods...)
m.AddDestinationEmailLogs(number, related).Apply(ctx, o)
})
}
func (m commsEmailMods) AddExistingDestinationEmailLogs(existingModels ...*models.CommsEmailLog) CommsEmailMod {
return CommsEmailModFunc(func(ctx context.Context, o *CommsEmailTemplate) {
for _, em := range existingModels {
o.r.DestinationEmailLogs = append(o.r.DestinationEmailLogs, &commsEmailRDestinationEmailLogsR{
o: o.f.FromExistingCommsEmailLog(em),
})
}
})
}
func (m commsEmailMods) WithoutDestinationEmailLogs() CommsEmailMod {
return CommsEmailModFunc(func(ctx context.Context, o *CommsEmailTemplate) {
o.r.DestinationEmailLogs = nil
})
}