feat: bob
This commit is contained in:
@ -28,12 +28,12 @@ func random_float32(f *faker.Faker) float32 {
|
||||
return f.Float32(10, -1_000_000, 1_000_000)
|
||||
}
|
||||
|
||||
func random_int32(f *faker.Faker) int32 {
|
||||
func random_int64(f *faker.Faker) int64 {
|
||||
if f == nil {
|
||||
f = &defaultFaker
|
||||
}
|
||||
|
||||
return f.Int32()
|
||||
return f.Int64()
|
||||
}
|
||||
|
||||
func random_string(f *faker.Faker) string {
|
||||
|
@ -8,14 +8,14 @@ import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestRandom_int32(t *testing.T) {
|
||||
func TestRandom_int64(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
val1 := random_int32(nil)
|
||||
val2 := random_int32(nil)
|
||||
val1 := random_int64(nil)
|
||||
val2 := random_int64(nil)
|
||||
|
||||
if val1 == val2 {
|
||||
t.Fatalf("random_int32() returned the same value twice: %v", val1)
|
||||
t.Fatalf("random_int64() returned the same value twice: %v", val1)
|
||||
}
|
||||
}
|
||||
|
||||
@ -41,17 +41,6 @@ func TestRandom___byte(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestRandom_float32(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
val1 := random_float32(nil)
|
||||
val2 := random_float32(nil)
|
||||
|
||||
if val1 == val2 {
|
||||
t.Fatalf("random_float32() returned the same value twice: %v", val1)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRandom_time_Time(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
@ -62,3 +51,14 @@ func TestRandom_time_Time(t *testing.T) {
|
||||
t.Fatalf("random_time_Time() returned the same value twice: %v", val1)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRandom_float32(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
val1 := random_float32(nil)
|
||||
val2 := random_float32(nil)
|
||||
|
||||
if val1 == val2 {
|
||||
t.Fatalf("random_float32() returned the same value twice: %v", val1)
|
||||
}
|
||||
}
|
||||
|
@ -9,7 +9,6 @@ import (
|
||||
|
||||
"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"
|
||||
@ -36,22 +35,27 @@ func (mods FileModSlice) Apply(n *FileTemplate) {
|
||||
// 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]
|
||||
ID func() int64
|
||||
Name func() string
|
||||
Data func() []byte
|
||||
UserID func() int64
|
||||
|
||||
r fileR
|
||||
f *Factory
|
||||
}
|
||||
|
||||
type fileR struct {
|
||||
User *fileRUserR
|
||||
User *fileRUserR
|
||||
ProfilePictureUsers []*fileRProfilePictureUsersR
|
||||
}
|
||||
|
||||
type fileRUserR struct {
|
||||
o *UserTemplate
|
||||
}
|
||||
type fileRProfilePictureUsersR struct {
|
||||
number int
|
||||
o *UserTemplate
|
||||
}
|
||||
|
||||
// Apply mods to the FileTemplate
|
||||
func (o *FileTemplate) Apply(mods ...FileMod) {
|
||||
@ -99,9 +103,22 @@ 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.UserID = rel.ID
|
||||
o.R.User = rel
|
||||
}
|
||||
|
||||
if t.r.ProfilePictureUsers != nil {
|
||||
rel := models.UserSlice{}
|
||||
for _, r := range t.r.ProfilePictureUsers {
|
||||
related := r.o.toModels(r.number)
|
||||
for _, rel := range related {
|
||||
rel.ProfilePictureID = null.From(o.ID)
|
||||
rel.R.ProfilePictureFile = o
|
||||
}
|
||||
rel = append(rel, related...)
|
||||
}
|
||||
o.R.ProfilePictureUsers = rel
|
||||
}
|
||||
}
|
||||
|
||||
// BuildSetter returns an *models.FileSetter
|
||||
@ -113,13 +130,13 @@ func (o FileTemplate) BuildSetter() *models.FileSetter {
|
||||
m.ID = omit.From(o.ID())
|
||||
}
|
||||
if o.Name != nil {
|
||||
m.Name = omitnull.FromNull(o.Name())
|
||||
m.Name = omit.From(o.Name())
|
||||
}
|
||||
if o.Data != nil {
|
||||
m.Data = omitnull.FromNull(o.Data())
|
||||
m.Data = omit.From(o.Data())
|
||||
}
|
||||
if o.UserID != nil {
|
||||
m.UserID = omitnull.FromNull(o.UserID())
|
||||
m.UserID = omit.From(o.UserID())
|
||||
}
|
||||
|
||||
return m
|
||||
@ -161,6 +178,15 @@ func (o FileTemplate) BuildMany(number int) models.FileSlice {
|
||||
}
|
||||
|
||||
func ensureCreatableFile(m *models.FileSetter) {
|
||||
if m.Name.IsUnset() {
|
||||
m.Name = omit.From(random_string(nil))
|
||||
}
|
||||
if m.Data.IsUnset() {
|
||||
m.Data = omit.From(random___byte(nil))
|
||||
}
|
||||
if m.UserID.IsUnset() {
|
||||
m.UserID = omit.From(random_int64(nil))
|
||||
}
|
||||
}
|
||||
|
||||
// insertOptRels creates and inserts any optional the relationships on *models.File
|
||||
@ -169,15 +195,18 @@ func ensureCreatableFile(m *models.FileSetter) {
|
||||
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
|
||||
if o.r.ProfilePictureUsers != nil {
|
||||
for _, r := range o.r.ProfilePictureUsers {
|
||||
var rel1 models.UserSlice
|
||||
ctx, rel1, err = r.o.createMany(ctx, exec, r.number)
|
||||
if err != nil {
|
||||
return ctx, err
|
||||
}
|
||||
|
||||
err = m.AttachProfilePictureUsers(ctx, exec, rel1...)
|
||||
if err != nil {
|
||||
return ctx, err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -223,12 +252,30 @@ func (o *FileTemplate) create(ctx context.Context, exec bob.Executor) (context.C
|
||||
opt := o.BuildSetter()
|
||||
ensureCreatableFile(opt)
|
||||
|
||||
var rel0 *models.User
|
||||
if o.r.User == nil {
|
||||
var ok bool
|
||||
rel0, ok = userCtx.Value(ctx)
|
||||
if !ok {
|
||||
FileMods.WithNewUser().Apply(o)
|
||||
}
|
||||
}
|
||||
if o.r.User != nil {
|
||||
ctx, rel0, err = o.r.User.o.create(ctx, exec)
|
||||
if err != nil {
|
||||
return ctx, nil, err
|
||||
}
|
||||
}
|
||||
opt.UserID = omit.From(rel0.ID)
|
||||
|
||||
m, err := models.Files.Insert(opt).One(ctx, exec)
|
||||
if err != nil {
|
||||
return ctx, nil, err
|
||||
}
|
||||
ctx = fileCtx.WithValue(ctx, m)
|
||||
|
||||
m.R.User = rel0
|
||||
|
||||
ctx, err = o.insertOptRels(ctx, exec, m)
|
||||
return ctx, m, err
|
||||
}
|
||||
@ -296,14 +343,14 @@ func (m fileMods) RandomizeAllColumns(f *faker.Faker) FileMod {
|
||||
}
|
||||
|
||||
// Set the model columns to this value
|
||||
func (m fileMods) ID(val int32) FileMod {
|
||||
func (m fileMods) ID(val int64) FileMod {
|
||||
return FileModFunc(func(o *FileTemplate) {
|
||||
o.ID = func() int32 { return val }
|
||||
o.ID = func() int64 { return val }
|
||||
})
|
||||
}
|
||||
|
||||
// Set the Column from the function
|
||||
func (m fileMods) IDFunc(f func() int32) FileMod {
|
||||
func (m fileMods) IDFunc(f func() int64) FileMod {
|
||||
return FileModFunc(func(o *FileTemplate) {
|
||||
o.ID = f
|
||||
})
|
||||
@ -320,21 +367,21 @@ func (m fileMods) UnsetID() FileMod {
|
||||
// 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)
|
||||
o.ID = func() int64 {
|
||||
return random_int64(f)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// Set the model columns to this value
|
||||
func (m fileMods) Name(val null.Val[string]) FileMod {
|
||||
func (m fileMods) Name(val string) FileMod {
|
||||
return FileModFunc(func(o *FileTemplate) {
|
||||
o.Name = func() null.Val[string] { return val }
|
||||
o.Name = func() string { return val }
|
||||
})
|
||||
}
|
||||
|
||||
// Set the Column from the function
|
||||
func (m fileMods) NameFunc(f func() null.Val[string]) FileMod {
|
||||
func (m fileMods) NameFunc(f func() string) FileMod {
|
||||
return FileModFunc(func(o *FileTemplate) {
|
||||
o.Name = f
|
||||
})
|
||||
@ -351,29 +398,21 @@ func (m fileMods) UnsetName() FileMod {
|
||||
// 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))
|
||||
o.Name = func() string {
|
||||
return random_string(f)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// Set the model columns to this value
|
||||
func (m fileMods) Data(val null.Val[[]byte]) FileMod {
|
||||
func (m fileMods) Data(val []byte) FileMod {
|
||||
return FileModFunc(func(o *FileTemplate) {
|
||||
o.Data = func() null.Val[[]byte] { return val }
|
||||
o.Data = func() []byte { return val }
|
||||
})
|
||||
}
|
||||
|
||||
// Set the Column from the function
|
||||
func (m fileMods) DataFunc(f func() null.Val[[]byte]) FileMod {
|
||||
func (m fileMods) DataFunc(f func() []byte) FileMod {
|
||||
return FileModFunc(func(o *FileTemplate) {
|
||||
o.Data = f
|
||||
})
|
||||
@ -390,29 +429,21 @@ func (m fileMods) UnsetData() FileMod {
|
||||
// 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))
|
||||
o.Data = func() []byte {
|
||||
return random___byte(f)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// Set the model columns to this value
|
||||
func (m fileMods) UserID(val null.Val[int32]) FileMod {
|
||||
func (m fileMods) UserID(val int64) FileMod {
|
||||
return FileModFunc(func(o *FileTemplate) {
|
||||
o.UserID = func() null.Val[int32] { return val }
|
||||
o.UserID = func() int64 { return val }
|
||||
})
|
||||
}
|
||||
|
||||
// Set the Column from the function
|
||||
func (m fileMods) UserIDFunc(f func() null.Val[int32]) FileMod {
|
||||
func (m fileMods) UserIDFunc(f func() int64) FileMod {
|
||||
return FileModFunc(func(o *FileTemplate) {
|
||||
o.UserID = f
|
||||
})
|
||||
@ -429,16 +460,8 @@ func (m fileMods) UnsetUserID() FileMod {
|
||||
// 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))
|
||||
o.UserID = func() int64 {
|
||||
return random_int64(f)
|
||||
}
|
||||
})
|
||||
}
|
||||
@ -464,3 +487,41 @@ func (m fileMods) WithoutUser() FileMod {
|
||||
o.r.User = nil
|
||||
})
|
||||
}
|
||||
|
||||
func (m fileMods) WithProfilePictureUsers(number int, related *UserTemplate) FileMod {
|
||||
return FileModFunc(func(o *FileTemplate) {
|
||||
o.r.ProfilePictureUsers = []*fileRProfilePictureUsersR{{
|
||||
number: number,
|
||||
o: related,
|
||||
}}
|
||||
})
|
||||
}
|
||||
|
||||
func (m fileMods) WithNewProfilePictureUsers(number int, mods ...UserMod) FileMod {
|
||||
return FileModFunc(func(o *FileTemplate) {
|
||||
related := o.f.NewUser(mods...)
|
||||
m.WithProfilePictureUsers(number, related).Apply(o)
|
||||
})
|
||||
}
|
||||
|
||||
func (m fileMods) AddProfilePictureUsers(number int, related *UserTemplate) FileMod {
|
||||
return FileModFunc(func(o *FileTemplate) {
|
||||
o.r.ProfilePictureUsers = append(o.r.ProfilePictureUsers, &fileRProfilePictureUsersR{
|
||||
number: number,
|
||||
o: related,
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
func (m fileMods) AddNewProfilePictureUsers(number int, mods ...UserMod) FileMod {
|
||||
return FileModFunc(func(o *FileTemplate) {
|
||||
related := o.f.NewUser(mods...)
|
||||
m.AddProfilePictureUsers(number, related).Apply(o)
|
||||
})
|
||||
}
|
||||
|
||||
func (m fileMods) WithoutProfilePictureUsers() FileMod {
|
||||
return FileModFunc(func(o *FileTemplate) {
|
||||
o.r.ProfilePictureUsers = nil
|
||||
})
|
||||
}
|
@ -37,13 +37,13 @@ func (mods ItemModSlice) Apply(n *ItemTemplate) {
|
||||
// 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]
|
||||
ID func() int64
|
||||
Name func() string
|
||||
Added func() time.Time
|
||||
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]
|
||||
Quantity func() null.Val[int64]
|
||||
UserID func() int64
|
||||
|
||||
r itemR
|
||||
f *Factory
|
||||
@ -75,6 +75,9 @@ func (o ItemTemplate) toModel() *models.Item {
|
||||
if o.Name != nil {
|
||||
m.Name = o.Name()
|
||||
}
|
||||
if o.Added != nil {
|
||||
m.Added = o.Added()
|
||||
}
|
||||
if o.Description != nil {
|
||||
m.Description = o.Description()
|
||||
}
|
||||
@ -84,9 +87,6 @@ func (o ItemTemplate) toModel() *models.Item {
|
||||
if o.Quantity != nil {
|
||||
m.Quantity = o.Quantity()
|
||||
}
|
||||
if o.Added != nil {
|
||||
m.Added = o.Added()
|
||||
}
|
||||
if o.UserID != nil {
|
||||
m.UserID = o.UserID()
|
||||
}
|
||||
@ -112,7 +112,7 @@ 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.UserID = rel.ID
|
||||
o.R.User = rel
|
||||
}
|
||||
}
|
||||
@ -126,7 +126,10 @@ func (o ItemTemplate) BuildSetter() *models.ItemSetter {
|
||||
m.ID = omit.From(o.ID())
|
||||
}
|
||||
if o.Name != nil {
|
||||
m.Name = omitnull.FromNull(o.Name())
|
||||
m.Name = omit.From(o.Name())
|
||||
}
|
||||
if o.Added != nil {
|
||||
m.Added = omit.From(o.Added())
|
||||
}
|
||||
if o.Description != nil {
|
||||
m.Description = omitnull.FromNull(o.Description())
|
||||
@ -137,11 +140,8 @@ func (o ItemTemplate) BuildSetter() *models.ItemSetter {
|
||||
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())
|
||||
m.UserID = omit.From(o.UserID())
|
||||
}
|
||||
|
||||
return m
|
||||
@ -183,6 +183,15 @@ func (o ItemTemplate) BuildMany(number int) models.ItemSlice {
|
||||
}
|
||||
|
||||
func ensureCreatableItem(m *models.ItemSetter) {
|
||||
if m.Name.IsUnset() {
|
||||
m.Name = omit.From(random_string(nil))
|
||||
}
|
||||
if m.Added.IsUnset() {
|
||||
m.Added = omit.From(random_time_Time(nil))
|
||||
}
|
||||
if m.UserID.IsUnset() {
|
||||
m.UserID = omit.From(random_int64(nil))
|
||||
}
|
||||
}
|
||||
|
||||
// insertOptRels creates and inserts any optional the relationships on *models.Item
|
||||
@ -191,18 +200,6 @@ func ensureCreatableItem(m *models.ItemSetter) {
|
||||
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
|
||||
}
|
||||
|
||||
@ -245,12 +242,30 @@ func (o *ItemTemplate) create(ctx context.Context, exec bob.Executor) (context.C
|
||||
opt := o.BuildSetter()
|
||||
ensureCreatableItem(opt)
|
||||
|
||||
var rel0 *models.User
|
||||
if o.r.User == nil {
|
||||
var ok bool
|
||||
rel0, ok = userCtx.Value(ctx)
|
||||
if !ok {
|
||||
ItemMods.WithNewUser().Apply(o)
|
||||
}
|
||||
}
|
||||
if o.r.User != nil {
|
||||
ctx, rel0, err = o.r.User.o.create(ctx, exec)
|
||||
if err != nil {
|
||||
return ctx, nil, err
|
||||
}
|
||||
}
|
||||
opt.UserID = omit.From(rel0.ID)
|
||||
|
||||
m, err := models.Items.Insert(opt).One(ctx, exec)
|
||||
if err != nil {
|
||||
return ctx, nil, err
|
||||
}
|
||||
ctx = itemCtx.WithValue(ctx, m)
|
||||
|
||||
m.R.User = rel0
|
||||
|
||||
ctx, err = o.insertOptRels(ctx, exec, m)
|
||||
return ctx, m, err
|
||||
}
|
||||
@ -312,23 +327,23 @@ func (m itemMods) RandomizeAllColumns(f *faker.Faker) ItemMod {
|
||||
return ItemModSlice{
|
||||
ItemMods.RandomID(f),
|
||||
ItemMods.RandomName(f),
|
||||
ItemMods.RandomAdded(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 {
|
||||
func (m itemMods) ID(val int64) ItemMod {
|
||||
return ItemModFunc(func(o *ItemTemplate) {
|
||||
o.ID = func() int32 { return val }
|
||||
o.ID = func() int64 { return val }
|
||||
})
|
||||
}
|
||||
|
||||
// Set the Column from the function
|
||||
func (m itemMods) IDFunc(f func() int32) ItemMod {
|
||||
func (m itemMods) IDFunc(f func() int64) ItemMod {
|
||||
return ItemModFunc(func(o *ItemTemplate) {
|
||||
o.ID = f
|
||||
})
|
||||
@ -345,21 +360,21 @@ func (m itemMods) UnsetID() ItemMod {
|
||||
// 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)
|
||||
o.ID = func() int64 {
|
||||
return random_int64(f)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// Set the model columns to this value
|
||||
func (m itemMods) Name(val null.Val[string]) ItemMod {
|
||||
func (m itemMods) Name(val string) ItemMod {
|
||||
return ItemModFunc(func(o *ItemTemplate) {
|
||||
o.Name = func() null.Val[string] { return val }
|
||||
o.Name = func() string { return val }
|
||||
})
|
||||
}
|
||||
|
||||
// Set the Column from the function
|
||||
func (m itemMods) NameFunc(f func() null.Val[string]) ItemMod {
|
||||
func (m itemMods) NameFunc(f func() string) ItemMod {
|
||||
return ItemModFunc(func(o *ItemTemplate) {
|
||||
o.Name = f
|
||||
})
|
||||
@ -376,16 +391,39 @@ func (m itemMods) UnsetName() ItemMod {
|
||||
// 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
|
||||
}
|
||||
o.Name = func() string {
|
||||
return random_string(f)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
if f.Bool() {
|
||||
return null.FromPtr[string](nil)
|
||||
}
|
||||
// Set the model columns to this value
|
||||
func (m itemMods) Added(val time.Time) ItemMod {
|
||||
return ItemModFunc(func(o *ItemTemplate) {
|
||||
o.Added = func() time.Time { return val }
|
||||
})
|
||||
}
|
||||
|
||||
return null.From(random_string(f))
|
||||
// Set the Column from the function
|
||||
func (m itemMods) AddedFunc(f func() 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() time.Time {
|
||||
return random_time_Time(f)
|
||||
}
|
||||
})
|
||||
}
|
||||
@ -469,14 +507,14 @@ func (m itemMods) RandomPrice(f *faker.Faker) ItemMod {
|
||||
}
|
||||
|
||||
// Set the model columns to this value
|
||||
func (m itemMods) Quantity(val null.Val[int32]) ItemMod {
|
||||
func (m itemMods) Quantity(val null.Val[int64]) ItemMod {
|
||||
return ItemModFunc(func(o *ItemTemplate) {
|
||||
o.Quantity = func() null.Val[int32] { return val }
|
||||
o.Quantity = func() null.Val[int64] { return val }
|
||||
})
|
||||
}
|
||||
|
||||
// Set the Column from the function
|
||||
func (m itemMods) QuantityFunc(f func() null.Val[int32]) ItemMod {
|
||||
func (m itemMods) QuantityFunc(f func() null.Val[int64]) ItemMod {
|
||||
return ItemModFunc(func(o *ItemTemplate) {
|
||||
o.Quantity = f
|
||||
})
|
||||
@ -493,68 +531,29 @@ func (m itemMods) UnsetQuantity() ItemMod {
|
||||
// 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] {
|
||||
o.Quantity = func() null.Val[int64] {
|
||||
if f == nil {
|
||||
f = &defaultFaker
|
||||
}
|
||||
|
||||
if f.Bool() {
|
||||
return null.FromPtr[int32](nil)
|
||||
return null.FromPtr[int64](nil)
|
||||
}
|
||||
|
||||
return null.From(random_int32(f))
|
||||
return null.From(random_int64(f))
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// Set the model columns to this value
|
||||
func (m itemMods) Added(val null.Val[time.Time]) ItemMod {
|
||||
func (m itemMods) UserID(val int64) ItemMod {
|
||||
return ItemModFunc(func(o *ItemTemplate) {
|
||||
o.Added = func() null.Val[time.Time] { return val }
|
||||
o.UserID = func() int64 { 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 {
|
||||
func (m itemMods) UserIDFunc(f func() int64) ItemMod {
|
||||
return ItemModFunc(func(o *ItemTemplate) {
|
||||
o.UserID = f
|
||||
})
|
||||
@ -571,16 +570,8 @@ func (m itemMods) UnsetUserID() ItemMod {
|
||||
// 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))
|
||||
o.UserID = func() int64 {
|
||||
return random_int64(f)
|
||||
}
|
||||
})
|
||||
}
|
@ -36,19 +36,19 @@ func (mods UserModSlice) Apply(n *UserTemplate) {
|
||||
// UserTemplate is an object representing the database table.
|
||||
// all columns are optional and should be set by mods
|
||||
type UserTemplate struct {
|
||||
ID func() int32
|
||||
Username func() null.Val[string]
|
||||
Password func() null.Val[string]
|
||||
ProfilePictureID func() null.Val[int32]
|
||||
Challenge func() null.Val[string]
|
||||
ID func() int64
|
||||
Username func() string
|
||||
Password func() string
|
||||
ProfilePictureID func() null.Val[int64]
|
||||
|
||||
r userR
|
||||
f *Factory
|
||||
}
|
||||
|
||||
type userR struct {
|
||||
Files []*userRFilesR
|
||||
Items []*userRItemsR
|
||||
Files []*userRFilesR
|
||||
Items []*userRItemsR
|
||||
ProfilePictureFile *userRProfilePictureFileR
|
||||
}
|
||||
|
||||
type userRFilesR struct {
|
||||
@ -59,6 +59,9 @@ type userRItemsR struct {
|
||||
number int
|
||||
o *ItemTemplate
|
||||
}
|
||||
type userRProfilePictureFileR struct {
|
||||
o *FileTemplate
|
||||
}
|
||||
|
||||
// Apply mods to the UserTemplate
|
||||
func (o *UserTemplate) Apply(mods ...UserMod) {
|
||||
@ -84,9 +87,6 @@ func (o UserTemplate) toModel() *models.User {
|
||||
if o.ProfilePictureID != nil {
|
||||
m.ProfilePictureID = o.ProfilePictureID()
|
||||
}
|
||||
if o.Challenge != nil {
|
||||
m.Challenge = o.Challenge()
|
||||
}
|
||||
|
||||
return m
|
||||
}
|
||||
@ -111,7 +111,7 @@ func (t UserTemplate) setModelRels(o *models.User) {
|
||||
for _, r := range t.r.Files {
|
||||
related := r.o.toModels(r.number)
|
||||
for _, rel := range related {
|
||||
rel.UserID = null.From(o.ID)
|
||||
rel.UserID = o.ID
|
||||
rel.R.User = o
|
||||
}
|
||||
rel = append(rel, related...)
|
||||
@ -124,13 +124,20 @@ func (t UserTemplate) setModelRels(o *models.User) {
|
||||
for _, r := range t.r.Items {
|
||||
related := r.o.toModels(r.number)
|
||||
for _, rel := range related {
|
||||
rel.UserID = null.From(o.ID)
|
||||
rel.UserID = o.ID
|
||||
rel.R.User = o
|
||||
}
|
||||
rel = append(rel, related...)
|
||||
}
|
||||
o.R.Items = rel
|
||||
}
|
||||
|
||||
if t.r.ProfilePictureFile != nil {
|
||||
rel := t.r.ProfilePictureFile.o.toModel()
|
||||
rel.R.ProfilePictureUsers = append(rel.R.ProfilePictureUsers, o)
|
||||
o.ProfilePictureID = null.From(rel.ID)
|
||||
o.R.ProfilePictureFile = rel
|
||||
}
|
||||
}
|
||||
|
||||
// BuildSetter returns an *models.UserSetter
|
||||
@ -142,17 +149,14 @@ func (o UserTemplate) BuildSetter() *models.UserSetter {
|
||||
m.ID = omit.From(o.ID())
|
||||
}
|
||||
if o.Username != nil {
|
||||
m.Username = omitnull.FromNull(o.Username())
|
||||
m.Username = omit.From(o.Username())
|
||||
}
|
||||
if o.Password != nil {
|
||||
m.Password = omitnull.FromNull(o.Password())
|
||||
m.Password = omit.From(o.Password())
|
||||
}
|
||||
if o.ProfilePictureID != nil {
|
||||
m.ProfilePictureID = omitnull.FromNull(o.ProfilePictureID())
|
||||
}
|
||||
if o.Challenge != nil {
|
||||
m.Challenge = omitnull.FromNull(o.Challenge())
|
||||
}
|
||||
|
||||
return m
|
||||
}
|
||||
@ -193,6 +197,12 @@ func (o UserTemplate) BuildMany(number int) models.UserSlice {
|
||||
}
|
||||
|
||||
func ensureCreatableUser(m *models.UserSetter) {
|
||||
if m.Username.IsUnset() {
|
||||
m.Username = omit.From(random_string(nil))
|
||||
}
|
||||
if m.Password.IsUnset() {
|
||||
m.Password = omit.From(random_string(nil))
|
||||
}
|
||||
}
|
||||
|
||||
// insertOptRels creates and inserts any optional the relationships on *models.User
|
||||
@ -231,6 +241,18 @@ func (o *UserTemplate) insertOptRels(ctx context.Context, exec bob.Executor, m *
|
||||
}
|
||||
}
|
||||
|
||||
if o.r.ProfilePictureFile != nil {
|
||||
var rel2 *models.File
|
||||
ctx, rel2, err = o.r.ProfilePictureFile.o.create(ctx, exec)
|
||||
if err != nil {
|
||||
return ctx, err
|
||||
}
|
||||
err = m.AttachProfilePictureFile(ctx, exec, rel2)
|
||||
if err != nil {
|
||||
return ctx, err
|
||||
}
|
||||
}
|
||||
|
||||
return ctx, err
|
||||
}
|
||||
|
||||
@ -342,19 +364,18 @@ func (m userMods) RandomizeAllColumns(f *faker.Faker) UserMod {
|
||||
UserMods.RandomUsername(f),
|
||||
UserMods.RandomPassword(f),
|
||||
UserMods.RandomProfilePictureID(f),
|
||||
UserMods.RandomChallenge(f),
|
||||
}
|
||||
}
|
||||
|
||||
// Set the model columns to this value
|
||||
func (m userMods) ID(val int32) UserMod {
|
||||
func (m userMods) ID(val int64) UserMod {
|
||||
return UserModFunc(func(o *UserTemplate) {
|
||||
o.ID = func() int32 { return val }
|
||||
o.ID = func() int64 { return val }
|
||||
})
|
||||
}
|
||||
|
||||
// Set the Column from the function
|
||||
func (m userMods) IDFunc(f func() int32) UserMod {
|
||||
func (m userMods) IDFunc(f func() int64) UserMod {
|
||||
return UserModFunc(func(o *UserTemplate) {
|
||||
o.ID = f
|
||||
})
|
||||
@ -371,21 +392,21 @@ func (m userMods) UnsetID() UserMod {
|
||||
// if faker is nil, a default faker is used
|
||||
func (m userMods) RandomID(f *faker.Faker) UserMod {
|
||||
return UserModFunc(func(o *UserTemplate) {
|
||||
o.ID = func() int32 {
|
||||
return random_int32(f)
|
||||
o.ID = func() int64 {
|
||||
return random_int64(f)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// Set the model columns to this value
|
||||
func (m userMods) Username(val null.Val[string]) UserMod {
|
||||
func (m userMods) Username(val string) UserMod {
|
||||
return UserModFunc(func(o *UserTemplate) {
|
||||
o.Username = func() null.Val[string] { return val }
|
||||
o.Username = func() string { return val }
|
||||
})
|
||||
}
|
||||
|
||||
// Set the Column from the function
|
||||
func (m userMods) UsernameFunc(f func() null.Val[string]) UserMod {
|
||||
func (m userMods) UsernameFunc(f func() string) UserMod {
|
||||
return UserModFunc(func(o *UserTemplate) {
|
||||
o.Username = f
|
||||
})
|
||||
@ -402,29 +423,21 @@ func (m userMods) UnsetUsername() UserMod {
|
||||
// if faker is nil, a default faker is used
|
||||
func (m userMods) RandomUsername(f *faker.Faker) UserMod {
|
||||
return UserModFunc(func(o *UserTemplate) {
|
||||
o.Username = func() null.Val[string] {
|
||||
if f == nil {
|
||||
f = &defaultFaker
|
||||
}
|
||||
|
||||
if f.Bool() {
|
||||
return null.FromPtr[string](nil)
|
||||
}
|
||||
|
||||
return null.From(random_string(f))
|
||||
o.Username = func() string {
|
||||
return random_string(f)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// Set the model columns to this value
|
||||
func (m userMods) Password(val null.Val[string]) UserMod {
|
||||
func (m userMods) Password(val string) UserMod {
|
||||
return UserModFunc(func(o *UserTemplate) {
|
||||
o.Password = func() null.Val[string] { return val }
|
||||
o.Password = func() string { return val }
|
||||
})
|
||||
}
|
||||
|
||||
// Set the Column from the function
|
||||
func (m userMods) PasswordFunc(f func() null.Val[string]) UserMod {
|
||||
func (m userMods) PasswordFunc(f func() string) UserMod {
|
||||
return UserModFunc(func(o *UserTemplate) {
|
||||
o.Password = f
|
||||
})
|
||||
@ -441,29 +454,21 @@ func (m userMods) UnsetPassword() UserMod {
|
||||
// if faker is nil, a default faker is used
|
||||
func (m userMods) RandomPassword(f *faker.Faker) UserMod {
|
||||
return UserModFunc(func(o *UserTemplate) {
|
||||
o.Password = func() null.Val[string] {
|
||||
if f == nil {
|
||||
f = &defaultFaker
|
||||
}
|
||||
|
||||
if f.Bool() {
|
||||
return null.FromPtr[string](nil)
|
||||
}
|
||||
|
||||
return null.From(random_string(f))
|
||||
o.Password = func() string {
|
||||
return random_string(f)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// Set the model columns to this value
|
||||
func (m userMods) ProfilePictureID(val null.Val[int32]) UserMod {
|
||||
func (m userMods) ProfilePictureID(val null.Val[int64]) UserMod {
|
||||
return UserModFunc(func(o *UserTemplate) {
|
||||
o.ProfilePictureID = func() null.Val[int32] { return val }
|
||||
o.ProfilePictureID = func() null.Val[int64] { return val }
|
||||
})
|
||||
}
|
||||
|
||||
// Set the Column from the function
|
||||
func (m userMods) ProfilePictureIDFunc(f func() null.Val[int32]) UserMod {
|
||||
func (m userMods) ProfilePictureIDFunc(f func() null.Val[int64]) UserMod {
|
||||
return UserModFunc(func(o *UserTemplate) {
|
||||
o.ProfilePictureID = f
|
||||
})
|
||||
@ -480,59 +485,42 @@ func (m userMods) UnsetProfilePictureID() UserMod {
|
||||
// if faker is nil, a default faker is used
|
||||
func (m userMods) RandomProfilePictureID(f *faker.Faker) UserMod {
|
||||
return UserModFunc(func(o *UserTemplate) {
|
||||
o.ProfilePictureID = func() null.Val[int32] {
|
||||
o.ProfilePictureID = func() null.Val[int64] {
|
||||
if f == nil {
|
||||
f = &defaultFaker
|
||||
}
|
||||
|
||||
if f.Bool() {
|
||||
return null.FromPtr[int32](nil)
|
||||
return null.FromPtr[int64](nil)
|
||||
}
|
||||
|
||||
return null.From(random_int32(f))
|
||||
return null.From(random_int64(f))
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// Set the model columns to this value
|
||||
func (m userMods) Challenge(val null.Val[string]) UserMod {
|
||||
func (m userMods) WithProfilePictureFile(rel *FileTemplate) UserMod {
|
||||
return UserModFunc(func(o *UserTemplate) {
|
||||
o.Challenge = func() null.Val[string] { return val }
|
||||
})
|
||||
}
|
||||
|
||||
// Set the Column from the function
|
||||
func (m userMods) ChallengeFunc(f func() null.Val[string]) UserMod {
|
||||
return UserModFunc(func(o *UserTemplate) {
|
||||
o.Challenge = f
|
||||
})
|
||||
}
|
||||
|
||||
// Clear any values for the column
|
||||
func (m userMods) UnsetChallenge() UserMod {
|
||||
return UserModFunc(func(o *UserTemplate) {
|
||||
o.Challenge = nil
|
||||
})
|
||||
}
|
||||
|
||||
// Generates a random value for the column using the given faker
|
||||
// if faker is nil, a default faker is used
|
||||
func (m userMods) RandomChallenge(f *faker.Faker) UserMod {
|
||||
return UserModFunc(func(o *UserTemplate) {
|
||||
o.Challenge = func() null.Val[string] {
|
||||
if f == nil {
|
||||
f = &defaultFaker
|
||||
}
|
||||
|
||||
if f.Bool() {
|
||||
return null.FromPtr[string](nil)
|
||||
}
|
||||
|
||||
return null.From(random_string(f))
|
||||
o.r.ProfilePictureFile = &userRProfilePictureFileR{
|
||||
o: rel,
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func (m userMods) WithNewProfilePictureFile(mods ...FileMod) UserMod {
|
||||
return UserModFunc(func(o *UserTemplate) {
|
||||
related := o.f.NewFile(mods...)
|
||||
|
||||
m.WithProfilePictureFile(related).Apply(o)
|
||||
})
|
||||
}
|
||||
|
||||
func (m userMods) WithoutProfilePictureFile() UserMod {
|
||||
return UserModFunc(func(o *UserTemplate) {
|
||||
o.r.ProfilePictureFile = nil
|
||||
})
|
||||
}
|
||||
|
||||
func (m userMods) WithFiles(number int, related *FileTemplate) UserMod {
|
||||
return UserModFunc(func(o *UserTemplate) {
|
||||
o.r.Files = []*userRFilesR{{
|
Reference in New Issue
Block a user