nidus-sync/db/factory/address.bob.go

719 lines
18 KiB
Go

// Code generated by BobGen psql v0.42.5. DO NOT EDIT.
// This file is meant to be re-generated in place and/or deleted at any time.
package factory
import (
"context"
"testing"
"time"
"github.com/Gleipnir-Technology/bob"
enums "github.com/Gleipnir-Technology/nidus-sync/db/enums"
models "github.com/Gleipnir-Technology/nidus-sync/db/models"
"github.com/aarondl/opt/omit"
"github.com/jaswdr/faker/v2"
)
type AddressMod interface {
Apply(context.Context, *AddressTemplate)
}
type AddressModFunc func(context.Context, *AddressTemplate)
func (f AddressModFunc) Apply(ctx context.Context, n *AddressTemplate) {
f(ctx, n)
}
type AddressModSlice []AddressMod
func (mods AddressModSlice) Apply(ctx context.Context, n *AddressTemplate) {
for _, f := range mods {
f.Apply(ctx, n)
}
}
// AddressTemplate is an object representing the database table.
// all columns are optional and should be set by mods
type AddressTemplate struct {
Country func() enums.Countrytype
Created func() time.Time
Geom func() string
H3cell func() string
ID func() int32
Locality func() string
Number func() int32
PostalCode func() string
Street func() string
Unit func() string
r addressR
f *Factory
alreadyPersisted bool
}
type addressR struct {
Site *addressRSiteR
}
type addressRSiteR struct {
o *SiteTemplate
}
// Apply mods to the AddressTemplate
func (o *AddressTemplate) Apply(ctx context.Context, mods ...AddressMod) {
for _, mod := range mods {
mod.Apply(ctx, o)
}
}
// setModelRels creates and sets the relationships on *models.Address
// according to the relationships in the template. Nothing is inserted into the db
func (t AddressTemplate) setModelRels(o *models.Address) {
if t.r.Site != nil {
rel := t.r.Site.o.Build()
rel.R.Address = o
rel.AddressID = o.ID // h2
o.R.Site = rel
}
}
// BuildSetter returns an *models.AddressSetter
// this does nothing with the relationship templates
func (o AddressTemplate) BuildSetter() *models.AddressSetter {
m := &models.AddressSetter{}
if o.Country != nil {
val := o.Country()
m.Country = omit.From(val)
}
if o.Created != nil {
val := o.Created()
m.Created = omit.From(val)
}
if o.Geom != nil {
val := o.Geom()
m.Geom = omit.From(val)
}
if o.H3cell != nil {
val := o.H3cell()
m.H3cell = omit.From(val)
}
if o.ID != nil {
val := o.ID()
m.ID = omit.From(val)
}
if o.Locality != nil {
val := o.Locality()
m.Locality = omit.From(val)
}
if o.Number != nil {
val := o.Number()
m.Number = omit.From(val)
}
if o.PostalCode != nil {
val := o.PostalCode()
m.PostalCode = omit.From(val)
}
if o.Street != nil {
val := o.Street()
m.Street = omit.From(val)
}
if o.Unit != nil {
val := o.Unit()
m.Unit = omit.From(val)
}
return m
}
// BuildManySetter returns an []*models.AddressSetter
// this does nothing with the relationship templates
func (o AddressTemplate) BuildManySetter(number int) []*models.AddressSetter {
m := make([]*models.AddressSetter, number)
for i := range m {
m[i] = o.BuildSetter()
}
return m
}
// Build returns an *models.Address
// Related objects are also created and placed in the .R field
// NOTE: Objects are not inserted into the database. Use AddressTemplate.Create
func (o AddressTemplate) Build() *models.Address {
m := &models.Address{}
if o.Country != nil {
m.Country = o.Country()
}
if o.Created != nil {
m.Created = o.Created()
}
if o.Geom != nil {
m.Geom = o.Geom()
}
if o.H3cell != nil {
m.H3cell = o.H3cell()
}
if o.ID != nil {
m.ID = o.ID()
}
if o.Locality != nil {
m.Locality = o.Locality()
}
if o.Number != nil {
m.Number = o.Number()
}
if o.PostalCode != nil {
m.PostalCode = o.PostalCode()
}
if o.Street != nil {
m.Street = o.Street()
}
if o.Unit != nil {
m.Unit = o.Unit()
}
o.setModelRels(m)
return m
}
// BuildMany returns an models.AddressSlice
// Related objects are also created and placed in the .R field
// NOTE: Objects are not inserted into the database. Use AddressTemplate.CreateMany
func (o AddressTemplate) BuildMany(number int) models.AddressSlice {
m := make(models.AddressSlice, number)
for i := range m {
m[i] = o.Build()
}
return m
}
func ensureCreatableAddress(m *models.AddressSetter) {
if !(m.Country.IsValue()) {
val := random_enums_Countrytype(nil)
m.Country = omit.From(val)
}
if !(m.Created.IsValue()) {
val := random_time_Time(nil)
m.Created = omit.From(val)
}
if !(m.Geom.IsValue()) {
val := random_string(nil)
m.Geom = omit.From(val)
}
if !(m.H3cell.IsValue()) {
val := random_string(nil)
m.H3cell = omit.From(val)
}
if !(m.Locality.IsValue()) {
val := random_string(nil)
m.Locality = omit.From(val)
}
if !(m.Number.IsValue()) {
val := random_int32(nil)
m.Number = omit.From(val)
}
if !(m.PostalCode.IsValue()) {
val := random_string(nil)
m.PostalCode = omit.From(val)
}
if !(m.Street.IsValue()) {
val := random_string(nil)
m.Street = omit.From(val)
}
if !(m.Unit.IsValue()) {
val := random_string(nil)
m.Unit = omit.From(val)
}
}
// insertOptRels creates and inserts any optional the relationships on *models.Address
// according to the relationships in the template.
// any required relationship should have already exist on the model
func (o *AddressTemplate) insertOptRels(ctx context.Context, exec bob.Executor, m *models.Address) error {
var err error
isSiteDone, _ := addressRelSiteCtx.Value(ctx)
if !isSiteDone && o.r.Site != nil {
ctx = addressRelSiteCtx.WithValue(ctx, true)
if o.r.Site.o.alreadyPersisted {
m.R.Site = o.r.Site.o.Build()
} else {
var rel0 *models.Site
rel0, err = o.r.Site.o.Create(ctx, exec)
if err != nil {
return err
}
err = m.AttachSite(ctx, exec, rel0)
if err != nil {
return err
}
}
}
return err
}
// Create builds a address and inserts it into the database
// Relations objects are also inserted and placed in the .R field
func (o *AddressTemplate) Create(ctx context.Context, exec bob.Executor) (*models.Address, error) {
var err error
opt := o.BuildSetter()
ensureCreatableAddress(opt)
m, err := models.Addresses.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 address and inserts it into the database
// Relations objects are also inserted and placed in the .R field
// panics if an error occurs
func (o *AddressTemplate) MustCreate(ctx context.Context, exec bob.Executor) *models.Address {
m, err := o.Create(ctx, exec)
if err != nil {
panic(err)
}
return m
}
// CreateOrFail builds a address 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 *AddressTemplate) CreateOrFail(ctx context.Context, tb testing.TB, exec bob.Executor) *models.Address {
tb.Helper()
m, err := o.Create(ctx, exec)
if err != nil {
tb.Fatal(err)
return nil
}
return m
}
// CreateMany builds multiple addresses and inserts them into the database
// Relations objects are also inserted and placed in the .R field
func (o AddressTemplate) CreateMany(ctx context.Context, exec bob.Executor, number int) (models.AddressSlice, error) {
var err error
m := make(models.AddressSlice, number)
for i := range m {
m[i], err = o.Create(ctx, exec)
if err != nil {
return nil, err
}
}
return m, nil
}
// MustCreateMany builds multiple addresses and inserts them into the database
// Relations objects are also inserted and placed in the .R field
// panics if an error occurs
func (o AddressTemplate) MustCreateMany(ctx context.Context, exec bob.Executor, number int) models.AddressSlice {
m, err := o.CreateMany(ctx, exec, number)
if err != nil {
panic(err)
}
return m
}
// CreateManyOrFail builds multiple addresses 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 AddressTemplate) CreateManyOrFail(ctx context.Context, tb testing.TB, exec bob.Executor, number int) models.AddressSlice {
tb.Helper()
m, err := o.CreateMany(ctx, exec, number)
if err != nil {
tb.Fatal(err)
return nil
}
return m
}
// Address has methods that act as mods for the AddressTemplate
var AddressMods addressMods
type addressMods struct{}
func (m addressMods) RandomizeAllColumns(f *faker.Faker) AddressMod {
return AddressModSlice{
AddressMods.RandomCountry(f),
AddressMods.RandomCreated(f),
AddressMods.RandomGeom(f),
AddressMods.RandomH3cell(f),
AddressMods.RandomID(f),
AddressMods.RandomLocality(f),
AddressMods.RandomNumber(f),
AddressMods.RandomPostalCode(f),
AddressMods.RandomStreet(f),
AddressMods.RandomUnit(f),
}
}
// Set the model columns to this value
func (m addressMods) Country(val enums.Countrytype) AddressMod {
return AddressModFunc(func(_ context.Context, o *AddressTemplate) {
o.Country = func() enums.Countrytype { return val }
})
}
// Set the Column from the function
func (m addressMods) CountryFunc(f func() enums.Countrytype) AddressMod {
return AddressModFunc(func(_ context.Context, o *AddressTemplate) {
o.Country = f
})
}
// Clear any values for the column
func (m addressMods) UnsetCountry() AddressMod {
return AddressModFunc(func(_ context.Context, o *AddressTemplate) {
o.Country = nil
})
}
// Generates a random value for the column using the given faker
// if faker is nil, a default faker is used
func (m addressMods) RandomCountry(f *faker.Faker) AddressMod {
return AddressModFunc(func(_ context.Context, o *AddressTemplate) {
o.Country = func() enums.Countrytype {
return random_enums_Countrytype(f)
}
})
}
// Set the model columns to this value
func (m addressMods) Created(val time.Time) AddressMod {
return AddressModFunc(func(_ context.Context, o *AddressTemplate) {
o.Created = func() time.Time { return val }
})
}
// Set the Column from the function
func (m addressMods) CreatedFunc(f func() time.Time) AddressMod {
return AddressModFunc(func(_ context.Context, o *AddressTemplate) {
o.Created = f
})
}
// Clear any values for the column
func (m addressMods) UnsetCreated() AddressMod {
return AddressModFunc(func(_ context.Context, o *AddressTemplate) {
o.Created = nil
})
}
// Generates a random value for the column using the given faker
// if faker is nil, a default faker is used
func (m addressMods) RandomCreated(f *faker.Faker) AddressMod {
return AddressModFunc(func(_ context.Context, o *AddressTemplate) {
o.Created = func() time.Time {
return random_time_Time(f)
}
})
}
// Set the model columns to this value
func (m addressMods) Geom(val string) AddressMod {
return AddressModFunc(func(_ context.Context, o *AddressTemplate) {
o.Geom = func() string { return val }
})
}
// Set the Column from the function
func (m addressMods) GeomFunc(f func() string) AddressMod {
return AddressModFunc(func(_ context.Context, o *AddressTemplate) {
o.Geom = f
})
}
// Clear any values for the column
func (m addressMods) UnsetGeom() AddressMod {
return AddressModFunc(func(_ context.Context, o *AddressTemplate) {
o.Geom = nil
})
}
// Generates a random value for the column using the given faker
// if faker is nil, a default faker is used
func (m addressMods) RandomGeom(f *faker.Faker) AddressMod {
return AddressModFunc(func(_ context.Context, o *AddressTemplate) {
o.Geom = func() string {
return random_string(f)
}
})
}
// Set the model columns to this value
func (m addressMods) H3cell(val string) AddressMod {
return AddressModFunc(func(_ context.Context, o *AddressTemplate) {
o.H3cell = func() string { return val }
})
}
// Set the Column from the function
func (m addressMods) H3cellFunc(f func() string) AddressMod {
return AddressModFunc(func(_ context.Context, o *AddressTemplate) {
o.H3cell = f
})
}
// Clear any values for the column
func (m addressMods) UnsetH3cell() AddressMod {
return AddressModFunc(func(_ context.Context, o *AddressTemplate) {
o.H3cell = nil
})
}
// Generates a random value for the column using the given faker
// if faker is nil, a default faker is used
func (m addressMods) RandomH3cell(f *faker.Faker) AddressMod {
return AddressModFunc(func(_ context.Context, o *AddressTemplate) {
o.H3cell = func() string {
return random_string(f)
}
})
}
// Set the model columns to this value
func (m addressMods) ID(val int32) AddressMod {
return AddressModFunc(func(_ context.Context, o *AddressTemplate) {
o.ID = func() int32 { return val }
})
}
// Set the Column from the function
func (m addressMods) IDFunc(f func() int32) AddressMod {
return AddressModFunc(func(_ context.Context, o *AddressTemplate) {
o.ID = f
})
}
// Clear any values for the column
func (m addressMods) UnsetID() AddressMod {
return AddressModFunc(func(_ context.Context, o *AddressTemplate) {
o.ID = nil
})
}
// Generates a random value for the column using the given faker
// if faker is nil, a default faker is used
func (m addressMods) RandomID(f *faker.Faker) AddressMod {
return AddressModFunc(func(_ context.Context, o *AddressTemplate) {
o.ID = func() int32 {
return random_int32(f)
}
})
}
// Set the model columns to this value
func (m addressMods) Locality(val string) AddressMod {
return AddressModFunc(func(_ context.Context, o *AddressTemplate) {
o.Locality = func() string { return val }
})
}
// Set the Column from the function
func (m addressMods) LocalityFunc(f func() string) AddressMod {
return AddressModFunc(func(_ context.Context, o *AddressTemplate) {
o.Locality = f
})
}
// Clear any values for the column
func (m addressMods) UnsetLocality() AddressMod {
return AddressModFunc(func(_ context.Context, o *AddressTemplate) {
o.Locality = nil
})
}
// Generates a random value for the column using the given faker
// if faker is nil, a default faker is used
func (m addressMods) RandomLocality(f *faker.Faker) AddressMod {
return AddressModFunc(func(_ context.Context, o *AddressTemplate) {
o.Locality = func() string {
return random_string(f)
}
})
}
// Set the model columns to this value
func (m addressMods) Number(val int32) AddressMod {
return AddressModFunc(func(_ context.Context, o *AddressTemplate) {
o.Number = func() int32 { return val }
})
}
// Set the Column from the function
func (m addressMods) NumberFunc(f func() int32) AddressMod {
return AddressModFunc(func(_ context.Context, o *AddressTemplate) {
o.Number = f
})
}
// Clear any values for the column
func (m addressMods) UnsetNumber() AddressMod {
return AddressModFunc(func(_ context.Context, o *AddressTemplate) {
o.Number = nil
})
}
// Generates a random value for the column using the given faker
// if faker is nil, a default faker is used
func (m addressMods) RandomNumber(f *faker.Faker) AddressMod {
return AddressModFunc(func(_ context.Context, o *AddressTemplate) {
o.Number = func() int32 {
return random_int32(f)
}
})
}
// Set the model columns to this value
func (m addressMods) PostalCode(val string) AddressMod {
return AddressModFunc(func(_ context.Context, o *AddressTemplate) {
o.PostalCode = func() string { return val }
})
}
// Set the Column from the function
func (m addressMods) PostalCodeFunc(f func() string) AddressMod {
return AddressModFunc(func(_ context.Context, o *AddressTemplate) {
o.PostalCode = f
})
}
// Clear any values for the column
func (m addressMods) UnsetPostalCode() AddressMod {
return AddressModFunc(func(_ context.Context, o *AddressTemplate) {
o.PostalCode = nil
})
}
// Generates a random value for the column using the given faker
// if faker is nil, a default faker is used
func (m addressMods) RandomPostalCode(f *faker.Faker) AddressMod {
return AddressModFunc(func(_ context.Context, o *AddressTemplate) {
o.PostalCode = func() string {
return random_string(f)
}
})
}
// Set the model columns to this value
func (m addressMods) Street(val string) AddressMod {
return AddressModFunc(func(_ context.Context, o *AddressTemplate) {
o.Street = func() string { return val }
})
}
// Set the Column from the function
func (m addressMods) StreetFunc(f func() string) AddressMod {
return AddressModFunc(func(_ context.Context, o *AddressTemplate) {
o.Street = f
})
}
// Clear any values for the column
func (m addressMods) UnsetStreet() AddressMod {
return AddressModFunc(func(_ context.Context, o *AddressTemplate) {
o.Street = nil
})
}
// Generates a random value for the column using the given faker
// if faker is nil, a default faker is used
func (m addressMods) RandomStreet(f *faker.Faker) AddressMod {
return AddressModFunc(func(_ context.Context, o *AddressTemplate) {
o.Street = func() string {
return random_string(f)
}
})
}
// Set the model columns to this value
func (m addressMods) Unit(val string) AddressMod {
return AddressModFunc(func(_ context.Context, o *AddressTemplate) {
o.Unit = func() string { return val }
})
}
// Set the Column from the function
func (m addressMods) UnitFunc(f func() string) AddressMod {
return AddressModFunc(func(_ context.Context, o *AddressTemplate) {
o.Unit = f
})
}
// Clear any values for the column
func (m addressMods) UnsetUnit() AddressMod {
return AddressModFunc(func(_ context.Context, o *AddressTemplate) {
o.Unit = nil
})
}
// Generates a random value for the column using the given faker
// if faker is nil, a default faker is used
func (m addressMods) RandomUnit(f *faker.Faker) AddressMod {
return AddressModFunc(func(_ context.Context, o *AddressTemplate) {
o.Unit = func() string {
return random_string(f)
}
})
}
func (m addressMods) WithParentsCascading() AddressMod {
return AddressModFunc(func(ctx context.Context, o *AddressTemplate) {
if isDone, _ := addressWithParentsCascadingCtx.Value(ctx); isDone {
return
}
ctx = addressWithParentsCascadingCtx.WithValue(ctx, true)
{
related := o.f.NewSiteWithContext(ctx, SiteMods.WithParentsCascading())
m.WithSite(related).Apply(ctx, o)
}
})
}
func (m addressMods) WithSite(rel *SiteTemplate) AddressMod {
return AddressModFunc(func(ctx context.Context, o *AddressTemplate) {
o.r.Site = &addressRSiteR{
o: rel,
}
})
}
func (m addressMods) WithNewSite(mods ...SiteMod) AddressMod {
return AddressModFunc(func(ctx context.Context, o *AddressTemplate) {
related := o.f.NewSiteWithContext(ctx, mods...)
m.WithSite(related).Apply(ctx, o)
})
}
func (m addressMods) WithExistingSite(em *models.Site) AddressMod {
return AddressModFunc(func(ctx context.Context, o *AddressTemplate) {
o.r.Site = &addressRSiteR{
o: o.f.FromExistingSite(em),
}
})
}
func (m addressMods) WithoutSite() AddressMod {
return AddressModFunc(func(ctx context.Context, o *AddressTemplate) {
o.r.Site = nil
})
}