// 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" "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 FileMod interface { Apply(*FileTemplate) } type FileModFunc func(*FileTemplate) func (f FileModFunc) Apply(n *FileTemplate) { f(n) } type FileModSlice []FileMod func (mods FileModSlice) Apply(n *FileTemplate) { for _, f := range mods { f.Apply(n) } } // FileTemplate is an object representing the database table. // all columns are optional and should be set by mods type FileTemplate struct { ID func() int32 Name func() null.Val[string] Data func() null.Val[[]byte] UserID func() null.Val[int32] r fileR f *Factory } type fileR struct { User *fileRUserR } type fileRUserR struct { o *UserTemplate } // Apply mods to the FileTemplate func (o *FileTemplate) Apply(mods ...FileMod) { for _, mod := range mods { mod.Apply(o) } } // toModel returns an *models.File // this does nothing with the relationship templates func (o FileTemplate) toModel() *models.File { m := &models.File{} if o.ID != nil { m.ID = o.ID() } if o.Name != nil { m.Name = o.Name() } if o.Data != nil { m.Data = o.Data() } if o.UserID != nil { m.UserID = o.UserID() } return m } // toModels returns an models.FileSlice // this does nothing with the relationship templates func (o FileTemplate) toModels(number int) models.FileSlice { m := make(models.FileSlice, number) for i := range m { m[i] = o.toModel() } return m } // setModelRels creates and sets the relationships on *models.File // according to the relationships in the template. Nothing is inserted into the db func (t FileTemplate) setModelRels(o *models.File) { if t.r.User != nil { rel := t.r.User.o.toModel() rel.R.Files = append(rel.R.Files, o) o.UserID = null.From(rel.ID) o.R.User = rel } } // BuildSetter returns an *models.FileSetter // this does nothing with the relationship templates func (o FileTemplate) BuildSetter() *models.FileSetter { m := &models.FileSetter{} if o.ID != nil { m.ID = omit.From(o.ID()) } if o.Name != nil { m.Name = omitnull.FromNull(o.Name()) } if o.Data != nil { m.Data = omitnull.FromNull(o.Data()) } if o.UserID != nil { m.UserID = omitnull.FromNull(o.UserID()) } return m } // BuildManySetter returns an []*models.FileSetter // this does nothing with the relationship templates func (o FileTemplate) BuildManySetter(number int) []*models.FileSetter { m := make([]*models.FileSetter, number) for i := range m { m[i] = o.BuildSetter() } return m } // Build returns an *models.File // Related objects are also created and placed in the .R field // NOTE: Objects are not inserted into the database. Use FileTemplate.Create func (o FileTemplate) Build() *models.File { m := o.toModel() o.setModelRels(m) return m } // BuildMany returns an models.FileSlice // Related objects are also created and placed in the .R field // NOTE: Objects are not inserted into the database. Use FileTemplate.CreateMany func (o FileTemplate) BuildMany(number int) models.FileSlice { m := make(models.FileSlice, number) for i := range m { m[i] = o.Build() } return m } func ensureCreatableFile(m *models.FileSetter) { } // insertOptRels creates and inserts any optional the relationships on *models.File // according to the relationships in the template. // any required relationship should have already exist on the model func (o *FileTemplate) insertOptRels(ctx context.Context, exec bob.Executor, m *models.File) (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 file and inserts it into the database // Relations objects are also inserted and placed in the .R field func (o *FileTemplate) Create(ctx context.Context, exec bob.Executor) (*models.File, error) { _, m, err := o.create(ctx, exec) return m, err } // MustCreate builds a file and inserts it into the database // Relations objects are also inserted and placed in the .R field // panics if an error occurs func (o *FileTemplate) MustCreate(ctx context.Context, exec bob.Executor) *models.File { _, m, err := o.create(ctx, exec) if err != nil { panic(err) } return m } // CreateOrFail builds a file 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 *FileTemplate) CreateOrFail(ctx context.Context, tb testing.TB, exec bob.Executor) *models.File { tb.Helper() _, m, err := o.create(ctx, exec) if err != nil { tb.Fatal(err) return nil } return m } // create builds a file 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 *FileTemplate) create(ctx context.Context, exec bob.Executor) (context.Context, *models.File, error) { var err error opt := o.BuildSetter() ensureCreatableFile(opt) m, err := models.Files.Insert(opt).One(ctx, exec) if err != nil { return ctx, nil, err } ctx = fileCtx.WithValue(ctx, m) ctx, err = o.insertOptRels(ctx, exec, m) return ctx, m, err } // CreateMany builds multiple files and inserts them into the database // Relations objects are also inserted and placed in the .R field func (o FileTemplate) CreateMany(ctx context.Context, exec bob.Executor, number int) (models.FileSlice, error) { _, m, err := o.createMany(ctx, exec, number) return m, err } // MustCreateMany builds multiple files and inserts them into the database // Relations objects are also inserted and placed in the .R field // panics if an error occurs func (o FileTemplate) MustCreateMany(ctx context.Context, exec bob.Executor, number int) models.FileSlice { _, m, err := o.createMany(ctx, exec, number) if err != nil { panic(err) } return m } // CreateManyOrFail builds multiple files 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 FileTemplate) CreateManyOrFail(ctx context.Context, tb testing.TB, exec bob.Executor, number int) models.FileSlice { tb.Helper() _, m, err := o.createMany(ctx, exec, number) if err != nil { tb.Fatal(err) return nil } return m } // createMany builds multiple files 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 FileTemplate) createMany(ctx context.Context, exec bob.Executor, number int) (context.Context, models.FileSlice, error) { var err error m := make(models.FileSlice, number) for i := range m { ctx, m[i], err = o.create(ctx, exec) if err != nil { return ctx, nil, err } } return ctx, m, nil } // File has methods that act as mods for the FileTemplate var FileMods fileMods type fileMods struct{} func (m fileMods) RandomizeAllColumns(f *faker.Faker) FileMod { return FileModSlice{ FileMods.RandomID(f), FileMods.RandomName(f), FileMods.RandomData(f), FileMods.RandomUserID(f), } } // Set the model columns to this value func (m fileMods) ID(val int32) FileMod { return FileModFunc(func(o *FileTemplate) { o.ID = func() int32 { return val } }) } // Set the Column from the function func (m fileMods) IDFunc(f func() int32) FileMod { return FileModFunc(func(o *FileTemplate) { o.ID = f }) } // Clear any values for the column func (m fileMods) UnsetID() FileMod { return FileModFunc(func(o *FileTemplate) { 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 fileMods) RandomID(f *faker.Faker) FileMod { return FileModFunc(func(o *FileTemplate) { o.ID = func() int32 { return random_int32(f) } }) } // Set the model columns to this value func (m fileMods) Name(val null.Val[string]) FileMod { return FileModFunc(func(o *FileTemplate) { o.Name = func() null.Val[string] { return val } }) } // Set the Column from the function func (m fileMods) NameFunc(f func() null.Val[string]) FileMod { return FileModFunc(func(o *FileTemplate) { o.Name = f }) } // Clear any values for the column func (m fileMods) UnsetName() FileMod { return FileModFunc(func(o *FileTemplate) { 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 fileMods) RandomName(f *faker.Faker) FileMod { return FileModFunc(func(o *FileTemplate) { 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 fileMods) Data(val null.Val[[]byte]) FileMod { return FileModFunc(func(o *FileTemplate) { o.Data = func() null.Val[[]byte] { return val } }) } // Set the Column from the function func (m fileMods) DataFunc(f func() null.Val[[]byte]) FileMod { return FileModFunc(func(o *FileTemplate) { o.Data = f }) } // Clear any values for the column func (m fileMods) UnsetData() FileMod { return FileModFunc(func(o *FileTemplate) { o.Data = nil }) } // Generates a random value for the column using the given faker // if faker is nil, a default faker is used func (m fileMods) RandomData(f *faker.Faker) FileMod { return FileModFunc(func(o *FileTemplate) { o.Data = func() null.Val[[]byte] { if f == nil { f = &defaultFaker } if f.Bool() { return null.FromPtr[[]byte](nil) } return null.From(random___byte(f)) } }) } // Set the model columns to this value func (m fileMods) UserID(val null.Val[int32]) FileMod { return FileModFunc(func(o *FileTemplate) { o.UserID = func() null.Val[int32] { return val } }) } // Set the Column from the function func (m fileMods) UserIDFunc(f func() null.Val[int32]) FileMod { return FileModFunc(func(o *FileTemplate) { o.UserID = f }) } // Clear any values for the column func (m fileMods) UnsetUserID() FileMod { return FileModFunc(func(o *FileTemplate) { 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 fileMods) RandomUserID(f *faker.Faker) FileMod { return FileModFunc(func(o *FileTemplate) { 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 fileMods) WithUser(rel *UserTemplate) FileMod { return FileModFunc(func(o *FileTemplate) { o.r.User = &fileRUserR{ o: rel, } }) } func (m fileMods) WithNewUser(mods ...UserMod) FileMod { return FileModFunc(func(o *FileTemplate) { related := o.f.NewUser(mods...) m.WithUser(related).Apply(o) }) } func (m fileMods) WithoutUser() FileMod { return FileModFunc(func(o *FileTemplate) { o.r.User = nil }) }