2025-04-05 14:27:36 -04:00

609 lines
15 KiB
Go

// Code generated by BobGen sqlite (devel). 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/aarondl/opt/null"
"github.com/aarondl/opt/omit"
"github.com/aarondl/opt/omitnull"
"github.com/jaswdr/faker/v2"
models "github.com/spotdemo4/trevstack/server/internal/models"
"github.com/stephenafamo/bob"
)
type ItemMod interface {
Apply(*ItemTemplate)
}
type ItemModFunc func(*ItemTemplate)
func (f ItemModFunc) Apply(n *ItemTemplate) {
f(n)
}
type ItemModSlice []ItemMod
func (mods ItemModSlice) Apply(n *ItemTemplate) {
for _, f := range mods {
f.Apply(n)
}
}
// ItemTemplate is an object representing the database table.
// all columns are optional and should be set by mods
type ItemTemplate struct {
ID func() int32
Name func() null.Val[string]
Description func() null.Val[string]
Price func() null.Val[float32]
Quantity func() null.Val[int32]
Added func() null.Val[time.Time]
UserID func() null.Val[int32]
r itemR
f *Factory
}
type itemR struct {
User *itemRUserR
}
type itemRUserR struct {
o *UserTemplate
}
// Apply mods to the ItemTemplate
func (o *ItemTemplate) Apply(mods ...ItemMod) {
for _, mod := range mods {
mod.Apply(o)
}
}
// toModel returns an *models.Item
// this does nothing with the relationship templates
func (o ItemTemplate) toModel() *models.Item {
m := &models.Item{}
if o.ID != nil {
m.ID = o.ID()
}
if o.Name != nil {
m.Name = o.Name()
}
if o.Description != nil {
m.Description = o.Description()
}
if o.Price != nil {
m.Price = o.Price()
}
if o.Quantity != nil {
m.Quantity = o.Quantity()
}
if o.Added != nil {
m.Added = o.Added()
}
if o.UserID != nil {
m.UserID = o.UserID()
}
return m
}
// toModels returns an models.ItemSlice
// this does nothing with the relationship templates
func (o ItemTemplate) toModels(number int) models.ItemSlice {
m := make(models.ItemSlice, number)
for i := range m {
m[i] = o.toModel()
}
return m
}
// setModelRels creates and sets the relationships on *models.Item
// according to the relationships in the template. Nothing is inserted into the db
func (t ItemTemplate) setModelRels(o *models.Item) {
if t.r.User != nil {
rel := t.r.User.o.toModel()
rel.R.Items = append(rel.R.Items, o)
o.UserID = null.From(rel.ID)
o.R.User = rel
}
}
// BuildSetter returns an *models.ItemSetter
// this does nothing with the relationship templates
func (o ItemTemplate) BuildSetter() *models.ItemSetter {
m := &models.ItemSetter{}
if o.ID != nil {
m.ID = omit.From(o.ID())
}
if o.Name != nil {
m.Name = omitnull.FromNull(o.Name())
}
if o.Description != nil {
m.Description = omitnull.FromNull(o.Description())
}
if o.Price != nil {
m.Price = omitnull.FromNull(o.Price())
}
if o.Quantity != nil {
m.Quantity = omitnull.FromNull(o.Quantity())
}
if o.Added != nil {
m.Added = omitnull.FromNull(o.Added())
}
if o.UserID != nil {
m.UserID = omitnull.FromNull(o.UserID())
}
return m
}
// BuildManySetter returns an []*models.ItemSetter
// this does nothing with the relationship templates
func (o ItemTemplate) BuildManySetter(number int) []*models.ItemSetter {
m := make([]*models.ItemSetter, number)
for i := range m {
m[i] = o.BuildSetter()
}
return m
}
// Build returns an *models.Item
// Related objects are also created and placed in the .R field
// NOTE: Objects are not inserted into the database. Use ItemTemplate.Create
func (o ItemTemplate) Build() *models.Item {
m := o.toModel()
o.setModelRels(m)
return m
}
// BuildMany returns an models.ItemSlice
// Related objects are also created and placed in the .R field
// NOTE: Objects are not inserted into the database. Use ItemTemplate.CreateMany
func (o ItemTemplate) BuildMany(number int) models.ItemSlice {
m := make(models.ItemSlice, number)
for i := range m {
m[i] = o.Build()
}
return m
}
func ensureCreatableItem(m *models.ItemSetter) {
}
// insertOptRels creates and inserts any optional the relationships on *models.Item
// according to the relationships in the template.
// any required relationship should have already exist on the model
func (o *ItemTemplate) insertOptRels(ctx context.Context, exec bob.Executor, m *models.Item) (context.Context, error) {
var err error
if o.r.User != nil {
var rel0 *models.User
ctx, rel0, err = o.r.User.o.create(ctx, exec)
if err != nil {
return ctx, err
}
err = m.AttachUser(ctx, exec, rel0)
if err != nil {
return ctx, err
}
}
return ctx, err
}
// Create builds a item and inserts it into the database
// Relations objects are also inserted and placed in the .R field
func (o *ItemTemplate) Create(ctx context.Context, exec bob.Executor) (*models.Item, error) {
_, m, err := o.create(ctx, exec)
return m, err
}
// MustCreate builds a item and inserts it into the database
// Relations objects are also inserted and placed in the .R field
// panics if an error occurs
func (o *ItemTemplate) MustCreate(ctx context.Context, exec bob.Executor) *models.Item {
_, m, err := o.create(ctx, exec)
if err != nil {
panic(err)
}
return m
}
// CreateOrFail builds a item 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 *ItemTemplate) CreateOrFail(ctx context.Context, tb testing.TB, exec bob.Executor) *models.Item {
tb.Helper()
_, m, err := o.create(ctx, exec)
if err != nil {
tb.Fatal(err)
return nil
}
return m
}
// create builds a item and inserts it into the database
// Relations objects are also inserted and placed in the .R field
// this returns a context that includes the newly inserted model
func (o *ItemTemplate) create(ctx context.Context, exec bob.Executor) (context.Context, *models.Item, error) {
var err error
opt := o.BuildSetter()
ensureCreatableItem(opt)
m, err := models.Items.Insert(opt).One(ctx, exec)
if err != nil {
return ctx, nil, err
}
ctx = itemCtx.WithValue(ctx, m)
ctx, err = o.insertOptRels(ctx, exec, m)
return ctx, m, err
}
// CreateMany builds multiple items and inserts them into the database
// Relations objects are also inserted and placed in the .R field
func (o ItemTemplate) CreateMany(ctx context.Context, exec bob.Executor, number int) (models.ItemSlice, error) {
_, m, err := o.createMany(ctx, exec, number)
return m, err
}
// MustCreateMany builds multiple items and inserts them into the database
// Relations objects are also inserted and placed in the .R field
// panics if an error occurs
func (o ItemTemplate) MustCreateMany(ctx context.Context, exec bob.Executor, number int) models.ItemSlice {
_, m, err := o.createMany(ctx, exec, number)
if err != nil {
panic(err)
}
return m
}
// CreateManyOrFail builds multiple items 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 ItemTemplate) CreateManyOrFail(ctx context.Context, tb testing.TB, exec bob.Executor, number int) models.ItemSlice {
tb.Helper()
_, m, err := o.createMany(ctx, exec, number)
if err != nil {
tb.Fatal(err)
return nil
}
return m
}
// createMany builds multiple items and inserts them into the database
// Relations objects are also inserted and placed in the .R field
// this returns a context that includes the newly inserted models
func (o ItemTemplate) createMany(ctx context.Context, exec bob.Executor, number int) (context.Context, models.ItemSlice, error) {
var err error
m := make(models.ItemSlice, number)
for i := range m {
ctx, m[i], err = o.create(ctx, exec)
if err != nil {
return ctx, nil, err
}
}
return ctx, m, nil
}
// Item has methods that act as mods for the ItemTemplate
var ItemMods itemMods
type itemMods struct{}
func (m itemMods) RandomizeAllColumns(f *faker.Faker) ItemMod {
return ItemModSlice{
ItemMods.RandomID(f),
ItemMods.RandomName(f),
ItemMods.RandomDescription(f),
ItemMods.RandomPrice(f),
ItemMods.RandomQuantity(f),
ItemMods.RandomAdded(f),
ItemMods.RandomUserID(f),
}
}
// Set the model columns to this value
func (m itemMods) ID(val int32) ItemMod {
return ItemModFunc(func(o *ItemTemplate) {
o.ID = func() int32 { return val }
})
}
// Set the Column from the function
func (m itemMods) IDFunc(f func() int32) ItemMod {
return ItemModFunc(func(o *ItemTemplate) {
o.ID = f
})
}
// Clear any values for the column
func (m itemMods) UnsetID() ItemMod {
return ItemModFunc(func(o *ItemTemplate) {
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 itemMods) RandomID(f *faker.Faker) ItemMod {
return ItemModFunc(func(o *ItemTemplate) {
o.ID = func() int32 {
return random_int32(f)
}
})
}
// Set the model columns to this value
func (m itemMods) Name(val null.Val[string]) ItemMod {
return ItemModFunc(func(o *ItemTemplate) {
o.Name = func() null.Val[string] { return val }
})
}
// Set the Column from the function
func (m itemMods) NameFunc(f func() null.Val[string]) ItemMod {
return ItemModFunc(func(o *ItemTemplate) {
o.Name = f
})
}
// Clear any values for the column
func (m itemMods) UnsetName() ItemMod {
return ItemModFunc(func(o *ItemTemplate) {
o.Name = nil
})
}
// Generates a random value for the column using the given faker
// if faker is nil, a default faker is used
func (m itemMods) RandomName(f *faker.Faker) ItemMod {
return ItemModFunc(func(o *ItemTemplate) {
o.Name = func() null.Val[string] {
if f == nil {
f = &defaultFaker
}
if f.Bool() {
return null.FromPtr[string](nil)
}
return null.From(random_string(f))
}
})
}
// Set the model columns to this value
func (m itemMods) Description(val null.Val[string]) ItemMod {
return ItemModFunc(func(o *ItemTemplate) {
o.Description = func() null.Val[string] { return val }
})
}
// Set the Column from the function
func (m itemMods) DescriptionFunc(f func() null.Val[string]) ItemMod {
return ItemModFunc(func(o *ItemTemplate) {
o.Description = f
})
}
// Clear any values for the column
func (m itemMods) UnsetDescription() ItemMod {
return ItemModFunc(func(o *ItemTemplate) {
o.Description = nil
})
}
// Generates a random value for the column using the given faker
// if faker is nil, a default faker is used
func (m itemMods) RandomDescription(f *faker.Faker) ItemMod {
return ItemModFunc(func(o *ItemTemplate) {
o.Description = func() null.Val[string] {
if f == nil {
f = &defaultFaker
}
if f.Bool() {
return null.FromPtr[string](nil)
}
return null.From(random_string(f))
}
})
}
// Set the model columns to this value
func (m itemMods) Price(val null.Val[float32]) ItemMod {
return ItemModFunc(func(o *ItemTemplate) {
o.Price = func() null.Val[float32] { return val }
})
}
// Set the Column from the function
func (m itemMods) PriceFunc(f func() null.Val[float32]) ItemMod {
return ItemModFunc(func(o *ItemTemplate) {
o.Price = f
})
}
// Clear any values for the column
func (m itemMods) UnsetPrice() ItemMod {
return ItemModFunc(func(o *ItemTemplate) {
o.Price = nil
})
}
// Generates a random value for the column using the given faker
// if faker is nil, a default faker is used
func (m itemMods) RandomPrice(f *faker.Faker) ItemMod {
return ItemModFunc(func(o *ItemTemplate) {
o.Price = func() null.Val[float32] {
if f == nil {
f = &defaultFaker
}
if f.Bool() {
return null.FromPtr[float32](nil)
}
return null.From(random_float32(f))
}
})
}
// Set the model columns to this value
func (m itemMods) Quantity(val null.Val[int32]) ItemMod {
return ItemModFunc(func(o *ItemTemplate) {
o.Quantity = func() null.Val[int32] { return val }
})
}
// Set the Column from the function
func (m itemMods) QuantityFunc(f func() null.Val[int32]) ItemMod {
return ItemModFunc(func(o *ItemTemplate) {
o.Quantity = f
})
}
// Clear any values for the column
func (m itemMods) UnsetQuantity() ItemMod {
return ItemModFunc(func(o *ItemTemplate) {
o.Quantity = nil
})
}
// Generates a random value for the column using the given faker
// if faker is nil, a default faker is used
func (m itemMods) RandomQuantity(f *faker.Faker) ItemMod {
return ItemModFunc(func(o *ItemTemplate) {
o.Quantity = func() null.Val[int32] {
if f == nil {
f = &defaultFaker
}
if f.Bool() {
return null.FromPtr[int32](nil)
}
return null.From(random_int32(f))
}
})
}
// Set the model columns to this value
func (m itemMods) Added(val null.Val[time.Time]) ItemMod {
return ItemModFunc(func(o *ItemTemplate) {
o.Added = func() null.Val[time.Time] { return val }
})
}
// Set the Column from the function
func (m itemMods) AddedFunc(f func() null.Val[time.Time]) ItemMod {
return ItemModFunc(func(o *ItemTemplate) {
o.Added = f
})
}
// Clear any values for the column
func (m itemMods) UnsetAdded() ItemMod {
return ItemModFunc(func(o *ItemTemplate) {
o.Added = nil
})
}
// Generates a random value for the column using the given faker
// if faker is nil, a default faker is used
func (m itemMods) RandomAdded(f *faker.Faker) ItemMod {
return ItemModFunc(func(o *ItemTemplate) {
o.Added = func() null.Val[time.Time] {
if f == nil {
f = &defaultFaker
}
if f.Bool() {
return null.FromPtr[time.Time](nil)
}
return null.From(random_time_Time(f))
}
})
}
// Set the model columns to this value
func (m itemMods) UserID(val null.Val[int32]) ItemMod {
return ItemModFunc(func(o *ItemTemplate) {
o.UserID = func() null.Val[int32] { return val }
})
}
// Set the Column from the function
func (m itemMods) UserIDFunc(f func() null.Val[int32]) ItemMod {
return ItemModFunc(func(o *ItemTemplate) {
o.UserID = f
})
}
// Clear any values for the column
func (m itemMods) UnsetUserID() ItemMod {
return ItemModFunc(func(o *ItemTemplate) {
o.UserID = nil
})
}
// Generates a random value for the column using the given faker
// if faker is nil, a default faker is used
func (m itemMods) RandomUserID(f *faker.Faker) ItemMod {
return ItemModFunc(func(o *ItemTemplate) {
o.UserID = func() null.Val[int32] {
if f == nil {
f = &defaultFaker
}
if f.Bool() {
return null.FromPtr[int32](nil)
}
return null.From(random_int32(f))
}
})
}
func (m itemMods) WithUser(rel *UserTemplate) ItemMod {
return ItemModFunc(func(o *ItemTemplate) {
o.r.User = &itemRUserR{
o: rel,
}
})
}
func (m itemMods) WithNewUser(mods ...UserMod) ItemMod {
return ItemModFunc(func(o *ItemTemplate) {
related := o.f.NewUser(mods...)
m.WithUser(related).Apply(o)
})
}
func (m itemMods) WithoutUser() ItemMod {
return ItemModFunc(func(o *ItemTemplate) {
o.r.User = nil
})
}