nidus-sync/factory/notification.bob.go
Eli Ribble a2e67e3d60
Add oauth token failure model and notification
This will allow me to mark when an oauth token fails and surface it to
the user so that they can re-up on their auth token.
2025-11-11 20:10:56 +00:00

572 lines
16 KiB
Go

// Code generated by BobGen psql v0.41.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"
enums "github.com/Gleipnir-Technology/nidus-sync/enums"
models "github.com/Gleipnir-Technology/nidus-sync/models"
"github.com/aarondl/opt/null"
"github.com/aarondl/opt/omit"
"github.com/aarondl/opt/omitnull"
"github.com/jaswdr/faker/v2"
"github.com/stephenafamo/bob"
)
type NotificationMod interface {
Apply(context.Context, *NotificationTemplate)
}
type NotificationModFunc func(context.Context, *NotificationTemplate)
func (f NotificationModFunc) Apply(ctx context.Context, n *NotificationTemplate) {
f(ctx, n)
}
type NotificationModSlice []NotificationMod
func (mods NotificationModSlice) Apply(ctx context.Context, n *NotificationTemplate) {
for _, f := range mods {
f.Apply(ctx, n)
}
}
// NotificationTemplate is an object representing the database table.
// all columns are optional and should be set by mods
type NotificationTemplate struct {
ID func() int32
UserID func() null.Val[int32]
Message func() null.Val[string]
Link func() null.Val[string]
Type func() null.Val[enums.Notificationtype]
r notificationR
f *Factory
alreadyPersisted bool
}
type notificationR struct {
UserUser *notificationRUserUserR
}
type notificationRUserUserR struct {
o *UserTemplate
}
// Apply mods to the NotificationTemplate
func (o *NotificationTemplate) Apply(ctx context.Context, mods ...NotificationMod) {
for _, mod := range mods {
mod.Apply(ctx, o)
}
}
// setModelRels creates and sets the relationships on *models.Notification
// according to the relationships in the template. Nothing is inserted into the db
func (t NotificationTemplate) setModelRels(o *models.Notification) {
if t.r.UserUser != nil {
rel := t.r.UserUser.o.Build()
rel.R.UserNotifications = append(rel.R.UserNotifications, o)
o.UserID = null.From(rel.ID) // h2
o.R.UserUser = rel
}
}
// BuildSetter returns an *models.NotificationSetter
// this does nothing with the relationship templates
func (o NotificationTemplate) BuildSetter() *models.NotificationSetter {
m := &models.NotificationSetter{}
if o.ID != nil {
val := o.ID()
m.ID = omit.From(val)
}
if o.UserID != nil {
val := o.UserID()
m.UserID = omitnull.FromNull(val)
}
if o.Message != nil {
val := o.Message()
m.Message = omitnull.FromNull(val)
}
if o.Link != nil {
val := o.Link()
m.Link = omitnull.FromNull(val)
}
if o.Type != nil {
val := o.Type()
m.Type = omitnull.FromNull(val)
}
return m
}
// BuildManySetter returns an []*models.NotificationSetter
// this does nothing with the relationship templates
func (o NotificationTemplate) BuildManySetter(number int) []*models.NotificationSetter {
m := make([]*models.NotificationSetter, number)
for i := range m {
m[i] = o.BuildSetter()
}
return m
}
// Build returns an *models.Notification
// Related objects are also created and placed in the .R field
// NOTE: Objects are not inserted into the database. Use NotificationTemplate.Create
func (o NotificationTemplate) Build() *models.Notification {
m := &models.Notification{}
if o.ID != nil {
m.ID = o.ID()
}
if o.UserID != nil {
m.UserID = o.UserID()
}
if o.Message != nil {
m.Message = o.Message()
}
if o.Link != nil {
m.Link = o.Link()
}
if o.Type != nil {
m.Type = o.Type()
}
o.setModelRels(m)
return m
}
// BuildMany returns an models.NotificationSlice
// Related objects are also created and placed in the .R field
// NOTE: Objects are not inserted into the database. Use NotificationTemplate.CreateMany
func (o NotificationTemplate) BuildMany(number int) models.NotificationSlice {
m := make(models.NotificationSlice, number)
for i := range m {
m[i] = o.Build()
}
return m
}
func ensureCreatableNotification(m *models.NotificationSetter) {
}
// insertOptRels creates and inserts any optional the relationships on *models.Notification
// according to the relationships in the template.
// any required relationship should have already exist on the model
func (o *NotificationTemplate) insertOptRels(ctx context.Context, exec bob.Executor, m *models.Notification) error {
var err error
isUserUserDone, _ := notificationRelUserUserCtx.Value(ctx)
if !isUserUserDone && o.r.UserUser != nil {
ctx = notificationRelUserUserCtx.WithValue(ctx, true)
if o.r.UserUser.o.alreadyPersisted {
m.R.UserUser = o.r.UserUser.o.Build()
} else {
var rel0 *models.User
rel0, err = o.r.UserUser.o.Create(ctx, exec)
if err != nil {
return err
}
err = m.AttachUserUser(ctx, exec, rel0)
if err != nil {
return err
}
}
}
return err
}
// Create builds a notification and inserts it into the database
// Relations objects are also inserted and placed in the .R field
func (o *NotificationTemplate) Create(ctx context.Context, exec bob.Executor) (*models.Notification, error) {
var err error
opt := o.BuildSetter()
ensureCreatableNotification(opt)
m, err := models.Notifications.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 notification and inserts it into the database
// Relations objects are also inserted and placed in the .R field
// panics if an error occurs
func (o *NotificationTemplate) MustCreate(ctx context.Context, exec bob.Executor) *models.Notification {
m, err := o.Create(ctx, exec)
if err != nil {
panic(err)
}
return m
}
// CreateOrFail builds a notification 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 *NotificationTemplate) CreateOrFail(ctx context.Context, tb testing.TB, exec bob.Executor) *models.Notification {
tb.Helper()
m, err := o.Create(ctx, exec)
if err != nil {
tb.Fatal(err)
return nil
}
return m
}
// CreateMany builds multiple notifications and inserts them into the database
// Relations objects are also inserted and placed in the .R field
func (o NotificationTemplate) CreateMany(ctx context.Context, exec bob.Executor, number int) (models.NotificationSlice, error) {
var err error
m := make(models.NotificationSlice, number)
for i := range m {
m[i], err = o.Create(ctx, exec)
if err != nil {
return nil, err
}
}
return m, nil
}
// MustCreateMany builds multiple notifications and inserts them into the database
// Relations objects are also inserted and placed in the .R field
// panics if an error occurs
func (o NotificationTemplate) MustCreateMany(ctx context.Context, exec bob.Executor, number int) models.NotificationSlice {
m, err := o.CreateMany(ctx, exec, number)
if err != nil {
panic(err)
}
return m
}
// CreateManyOrFail builds multiple notifications 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 NotificationTemplate) CreateManyOrFail(ctx context.Context, tb testing.TB, exec bob.Executor, number int) models.NotificationSlice {
tb.Helper()
m, err := o.CreateMany(ctx, exec, number)
if err != nil {
tb.Fatal(err)
return nil
}
return m
}
// Notification has methods that act as mods for the NotificationTemplate
var NotificationMods notificationMods
type notificationMods struct{}
func (m notificationMods) RandomizeAllColumns(f *faker.Faker) NotificationMod {
return NotificationModSlice{
NotificationMods.RandomID(f),
NotificationMods.RandomUserID(f),
NotificationMods.RandomMessage(f),
NotificationMods.RandomLink(f),
NotificationMods.RandomType(f),
}
}
// Set the model columns to this value
func (m notificationMods) ID(val int32) NotificationMod {
return NotificationModFunc(func(_ context.Context, o *NotificationTemplate) {
o.ID = func() int32 { return val }
})
}
// Set the Column from the function
func (m notificationMods) IDFunc(f func() int32) NotificationMod {
return NotificationModFunc(func(_ context.Context, o *NotificationTemplate) {
o.ID = f
})
}
// Clear any values for the column
func (m notificationMods) UnsetID() NotificationMod {
return NotificationModFunc(func(_ context.Context, o *NotificationTemplate) {
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 notificationMods) RandomID(f *faker.Faker) NotificationMod {
return NotificationModFunc(func(_ context.Context, o *NotificationTemplate) {
o.ID = func() int32 {
return random_int32(f)
}
})
}
// Set the model columns to this value
func (m notificationMods) UserID(val null.Val[int32]) NotificationMod {
return NotificationModFunc(func(_ context.Context, o *NotificationTemplate) {
o.UserID = func() null.Val[int32] { return val }
})
}
// Set the Column from the function
func (m notificationMods) UserIDFunc(f func() null.Val[int32]) NotificationMod {
return NotificationModFunc(func(_ context.Context, o *NotificationTemplate) {
o.UserID = f
})
}
// Clear any values for the column
func (m notificationMods) UnsetUserID() NotificationMod {
return NotificationModFunc(func(_ context.Context, o *NotificationTemplate) {
o.UserID = nil
})
}
// Generates a random value for the column using the given faker
// if faker is nil, a default faker is used
// The generated value is sometimes null
func (m notificationMods) RandomUserID(f *faker.Faker) NotificationMod {
return NotificationModFunc(func(_ context.Context, o *NotificationTemplate) {
o.UserID = func() null.Val[int32] {
if f == nil {
f = &defaultFaker
}
val := random_int32(f)
return null.From(val)
}
})
}
// Generates a random value for the column using the given faker
// if faker is nil, a default faker is used
// The generated value is never null
func (m notificationMods) RandomUserIDNotNull(f *faker.Faker) NotificationMod {
return NotificationModFunc(func(_ context.Context, o *NotificationTemplate) {
o.UserID = func() null.Val[int32] {
if f == nil {
f = &defaultFaker
}
val := random_int32(f)
return null.From(val)
}
})
}
// Set the model columns to this value
func (m notificationMods) Message(val null.Val[string]) NotificationMod {
return NotificationModFunc(func(_ context.Context, o *NotificationTemplate) {
o.Message = func() null.Val[string] { return val }
})
}
// Set the Column from the function
func (m notificationMods) MessageFunc(f func() null.Val[string]) NotificationMod {
return NotificationModFunc(func(_ context.Context, o *NotificationTemplate) {
o.Message = f
})
}
// Clear any values for the column
func (m notificationMods) UnsetMessage() NotificationMod {
return NotificationModFunc(func(_ context.Context, o *NotificationTemplate) {
o.Message = nil
})
}
// Generates a random value for the column using the given faker
// if faker is nil, a default faker is used
// The generated value is sometimes null
func (m notificationMods) RandomMessage(f *faker.Faker) NotificationMod {
return NotificationModFunc(func(_ context.Context, o *NotificationTemplate) {
o.Message = func() null.Val[string] {
if f == nil {
f = &defaultFaker
}
val := random_string(f)
return null.From(val)
}
})
}
// Generates a random value for the column using the given faker
// if faker is nil, a default faker is used
// The generated value is never null
func (m notificationMods) RandomMessageNotNull(f *faker.Faker) NotificationMod {
return NotificationModFunc(func(_ context.Context, o *NotificationTemplate) {
o.Message = func() null.Val[string] {
if f == nil {
f = &defaultFaker
}
val := random_string(f)
return null.From(val)
}
})
}
// Set the model columns to this value
func (m notificationMods) Link(val null.Val[string]) NotificationMod {
return NotificationModFunc(func(_ context.Context, o *NotificationTemplate) {
o.Link = func() null.Val[string] { return val }
})
}
// Set the Column from the function
func (m notificationMods) LinkFunc(f func() null.Val[string]) NotificationMod {
return NotificationModFunc(func(_ context.Context, o *NotificationTemplate) {
o.Link = f
})
}
// Clear any values for the column
func (m notificationMods) UnsetLink() NotificationMod {
return NotificationModFunc(func(_ context.Context, o *NotificationTemplate) {
o.Link = nil
})
}
// Generates a random value for the column using the given faker
// if faker is nil, a default faker is used
// The generated value is sometimes null
func (m notificationMods) RandomLink(f *faker.Faker) NotificationMod {
return NotificationModFunc(func(_ context.Context, o *NotificationTemplate) {
o.Link = func() null.Val[string] {
if f == nil {
f = &defaultFaker
}
val := random_string(f)
return null.From(val)
}
})
}
// Generates a random value for the column using the given faker
// if faker is nil, a default faker is used
// The generated value is never null
func (m notificationMods) RandomLinkNotNull(f *faker.Faker) NotificationMod {
return NotificationModFunc(func(_ context.Context, o *NotificationTemplate) {
o.Link = func() null.Val[string] {
if f == nil {
f = &defaultFaker
}
val := random_string(f)
return null.From(val)
}
})
}
// Set the model columns to this value
func (m notificationMods) Type(val null.Val[enums.Notificationtype]) NotificationMod {
return NotificationModFunc(func(_ context.Context, o *NotificationTemplate) {
o.Type = func() null.Val[enums.Notificationtype] { return val }
})
}
// Set the Column from the function
func (m notificationMods) TypeFunc(f func() null.Val[enums.Notificationtype]) NotificationMod {
return NotificationModFunc(func(_ context.Context, o *NotificationTemplate) {
o.Type = f
})
}
// Clear any values for the column
func (m notificationMods) UnsetType() NotificationMod {
return NotificationModFunc(func(_ context.Context, o *NotificationTemplate) {
o.Type = nil
})
}
// Generates a random value for the column using the given faker
// if faker is nil, a default faker is used
// The generated value is sometimes null
func (m notificationMods) RandomType(f *faker.Faker) NotificationMod {
return NotificationModFunc(func(_ context.Context, o *NotificationTemplate) {
o.Type = func() null.Val[enums.Notificationtype] {
if f == nil {
f = &defaultFaker
}
val := random_enums_Notificationtype(f)
return null.From(val)
}
})
}
// Generates a random value for the column using the given faker
// if faker is nil, a default faker is used
// The generated value is never null
func (m notificationMods) RandomTypeNotNull(f *faker.Faker) NotificationMod {
return NotificationModFunc(func(_ context.Context, o *NotificationTemplate) {
o.Type = func() null.Val[enums.Notificationtype] {
if f == nil {
f = &defaultFaker
}
val := random_enums_Notificationtype(f)
return null.From(val)
}
})
}
func (m notificationMods) WithParentsCascading() NotificationMod {
return NotificationModFunc(func(ctx context.Context, o *NotificationTemplate) {
if isDone, _ := notificationWithParentsCascadingCtx.Value(ctx); isDone {
return
}
ctx = notificationWithParentsCascadingCtx.WithValue(ctx, true)
{
related := o.f.NewUserWithContext(ctx, UserMods.WithParentsCascading())
m.WithUserUser(related).Apply(ctx, o)
}
})
}
func (m notificationMods) WithUserUser(rel *UserTemplate) NotificationMod {
return NotificationModFunc(func(ctx context.Context, o *NotificationTemplate) {
o.r.UserUser = &notificationRUserUserR{
o: rel,
}
})
}
func (m notificationMods) WithNewUserUser(mods ...UserMod) NotificationMod {
return NotificationModFunc(func(ctx context.Context, o *NotificationTemplate) {
related := o.f.NewUserWithContext(ctx, mods...)
m.WithUserUser(related).Apply(ctx, o)
})
}
func (m notificationMods) WithExistingUserUser(em *models.User) NotificationMod {
return NotificationModFunc(func(ctx context.Context, o *NotificationTemplate) {
o.r.UserUser = &notificationRUserUserR{
o: o.f.FromExistingUser(em),
}
})
}
func (m notificationMods) WithoutUserUser() NotificationMod {
return NotificationModFunc(func(ctx context.Context, o *NotificationTemplate) {
o.r.UserUser = nil
})
}