diff --git a/README.md b/README.md index 3480706..550a998 100644 --- a/README.md +++ b/README.md @@ -192,7 +192,7 @@ and generate factory based on them. Both struct and pointer of struct are OK. Post function will run after struct created, with input value as param. ## MetaFactory API -MetaFactory API can be categorized into 7 types of method: +MetaFactory API can be categorized into 8 types of method: - Each field in `To` struct has 4 types: - **Default**: `Set{Field}Default` - **Sequence**: `Set{Field}Sequence` @@ -205,10 +205,8 @@ MetaFactory API can be categorized into 7 types of method: - Each name in `[]Traits` has 1 type: - **Trait**: `Set{TraitName}Trait` -- Each `MetaFactory` has 1 type: +- Each `MetaFactory` has 2 type: - **BeforeCreate**: `SetBeforeCreateFunc` - -- Each `MetaFactory` has 1 type: - **AfterCreate**: `SetAfterCreateFunc` The evaluation order of these methods are: @@ -260,6 +258,15 @@ entUserMetaFactory.SetEmailLazy( }, ) ``` +You can get original ent mutation using `i.EntCreator()`: +```go +entUserMetaFactory.SetEmailLazy( + func(ctx context.Context, i *factory.EntUserMutator) (string, error) { + i.EntCreator().SetName("apple") + return "apple@carrier.com", nil + }, +) +``` #### Factory If a field's value has related factory, use `relatedFactory.Create` method as param here. You can also set the function manually. @@ -275,22 +282,18 @@ Make sure related factory's ent client is set. By using factory wrapper or set i #### BeforeCreate For struct factory, before create function is called after all lazy functions done. For ent factory, before create function is called right before to ent's `Save` method. ```go -groupMetaFactory.SetBeforeCreateFunc(func(ctx context.Context, creator *ent.GroupCreate) error { - // client *ent.Client - user, err := client.User.Create(). - SetAge(30). - SetName("John"). - Save(ctx) - if err != nil { - return err - } - creator.AddUsers(user) +groupMetaFactory.SetBeforeCreateFunc(func(ctx context.Context, i *model.User) error { return nil }) ``` +**> ent** -**Attention:** -Do not call other factories from this function to prevent infinite loops. +```go +entUserMetaFactory.SetBeforeCreateFunc(func(ctx context.Context, i *factory.EntUserMutator) error { + return nil +}) + +``` #### AfterCreate For struct factory, after create function is called after all lazy functions done. For ent factory, after create function is called next to ent's `Save` method. diff --git a/examples/ent_recipe/carrier/factory.go b/examples/ent_recipe/carrier/factory.go index 95bfcb0..cdf9a46 100644 --- a/examples/ent_recipe/carrier/factory.go +++ b/examples/ent_recipe/carrier/factory.go @@ -7,13 +7,16 @@ import ( "github.com/Yiling-J/carrier/examples/ent_recipe/ent" ) +// Factory is struct factory wrapper type Factory struct { } +// NewFactory return a new struct factory wrapper func NewFactory() *Factory { return &Factory{} } +// EntFactory is ent factory wrapper type EntFactory struct { recipeFactory *factory.EntRecipeFactory @@ -30,76 +33,108 @@ type EntFactory struct { client *ent.Client } +// NewEntFactory return a new ent factory wrapper func NewEntFactory(client *ent.Client) *EntFactory { return &EntFactory{client: client} } +// Client return wrappper's ent client func (f *EntFactory) Client() *ent.Client { return f.client } +// EntRecipeMetaFactory return a new meta factory with given ent client func EntRecipeMetaFactory() *factory.EntRecipeMetaFactory { return &factory.EntRecipeMetaFactory{} } + +// SetRecipeFactory set a factory in wrapper func (f *EntFactory) SetRecipeFactory(c *factory.EntRecipeFactory) *EntFactory { f.recipeFactory = c.Client(f.client) return f } + +// RecipeFactory return the EntRecipeFactory in wrapper func (f *EntFactory) RecipeFactory() *factory.EntRecipeFactory { return f.recipeFactory } +// EntUserMetaFactory return a new meta factory with given ent client func EntUserMetaFactory() *factory.EntUserMetaFactory { return &factory.EntUserMetaFactory{} } + +// SetUserFactory set a factory in wrapper func (f *EntFactory) SetUserFactory(c *factory.EntUserFactory) *EntFactory { f.userFactory = c.Client(f.client) return f } + +// UserFactory return the EntUserFactory in wrapper func (f *EntFactory) UserFactory() *factory.EntUserFactory { return f.userFactory } +// EntIngredientMetaFactory return a new meta factory with given ent client func EntIngredientMetaFactory() *factory.EntIngredientMetaFactory { return &factory.EntIngredientMetaFactory{} } + +// SetIngredientFactory set a factory in wrapper func (f *EntFactory) SetIngredientFactory(c *factory.EntIngredientFactory) *EntFactory { f.ingredientFactory = c.Client(f.client) return f } + +// IngredientFactory return the EntIngredientFactory in wrapper func (f *EntFactory) IngredientFactory() *factory.EntIngredientFactory { return f.ingredientFactory } +// EntCategoryMetaFactory return a new meta factory with given ent client func EntCategoryMetaFactory() *factory.EntCategoryMetaFactory { return &factory.EntCategoryMetaFactory{} } + +// SetCategoryFactory set a factory in wrapper func (f *EntFactory) SetCategoryFactory(c *factory.EntCategoryFactory) *EntFactory { f.categoryFactory = c.Client(f.client) return f } + +// CategoryFactory return the EntCategoryFactory in wrapper func (f *EntFactory) CategoryFactory() *factory.EntCategoryFactory { return f.categoryFactory } +// EntRecipeIngredientMetaFactory return a new meta factory with given ent client func EntRecipeIngredientMetaFactory() *factory.EntRecipeIngredientMetaFactory { return &factory.EntRecipeIngredientMetaFactory{} } + +// SetRecipeIngredientFactory set a factory in wrapper func (f *EntFactory) SetRecipeIngredientFactory(c *factory.EntRecipeIngredientFactory) *EntFactory { f.recipeIngredientFactory = c.Client(f.client) return f } + +// RecipeIngredientFactory return the EntRecipeIngredientFactory in wrapper func (f *EntFactory) RecipeIngredientFactory() *factory.EntRecipeIngredientFactory { return f.recipeIngredientFactory } +// EntStepMetaFactory return a new meta factory with given ent client func EntStepMetaFactory() *factory.EntStepMetaFactory { return &factory.EntStepMetaFactory{} } + +// SetStepFactory set a factory in wrapper func (f *EntFactory) SetStepFactory(c *factory.EntStepFactory) *EntFactory { f.stepFactory = c.Client(f.client) return f } + +// StepFactory return the EntStepFactory in wrapper func (f *EntFactory) StepFactory() *factory.EntStepFactory { return f.stepFactory } diff --git a/examples/ent_recipe/carrier/factory/ent_category.go b/examples/ent_recipe/carrier/factory/ent_category.go index 349e2c2..8723eaa 100644 --- a/examples/ent_recipe/carrier/factory/ent_category.go +++ b/examples/ent_recipe/carrier/factory/ent_category.go @@ -9,13 +9,20 @@ import ( type EntCategoryMutator struct { Name string + + _creator *ent.CategoryCreate +} + +func (m *EntCategoryMutator) EntCreator() *ent.CategoryCreate { + return m._creator } type entCategoryMutation struct { nameType int - nameFunc func(ctx context.Context, i *EntCategoryMutator, c int, creator *ent.CategoryCreate) error + nameFunc func(ctx context.Context, i *EntCategoryMutator, c int) error - afterCreateFunc func(ctx context.Context, i *ent.Category) error + beforeCreateFunc func(ctx context.Context, i *EntCategoryMutator) error + afterCreateFunc func(ctx context.Context, i *ent.Category) error } type EntCategoryMetaFactory struct { mutation entCategoryMutation @@ -28,6 +35,11 @@ type entCategoryTrait struct { func EntCategoryTrait() *entCategoryTrait { return &entCategoryTrait{} } +func (*entCategoryMutation) beforeCreateMutateFunc(fn func(ctx context.Context, i *EntCategoryMutator) error) func(m *entCategoryMutation) { + return func(m *entCategoryMutation) { + m.beforeCreateFunc = fn + } +} func (*entCategoryMutation) afterCreateMutateFunc(fn func(ctx context.Context, i *ent.Category) error) func(m *entCategoryMutation) { return func(m *entCategoryMutation) { m.afterCreateFunc = fn @@ -37,7 +49,7 @@ func (*entCategoryMutation) afterCreateMutateFunc(fn func(ctx context.Context, i func (*entCategoryMutation) nameSequenceMutateFunc(fn func(ctx context.Context, i int) (string, error)) func(m *entCategoryMutation) { return func(m *entCategoryMutation) { m.nameType = TypeSequence - m.nameFunc = func(ctx context.Context, i *EntCategoryMutator, c int, creator *ent.CategoryCreate) error { + m.nameFunc = func(ctx context.Context, i *EntCategoryMutator, c int) error { if fn == nil { return nil } @@ -46,7 +58,7 @@ func (*entCategoryMutation) nameSequenceMutateFunc(fn func(ctx context.Context, return err } - creator.SetName(value) + i.EntCreator().SetName(value) i.Name = value return nil @@ -56,7 +68,7 @@ func (*entCategoryMutation) nameSequenceMutateFunc(fn func(ctx context.Context, func (*entCategoryMutation) nameLazyMutateFunc(fn func(ctx context.Context, i *EntCategoryMutator) (string, error)) func(m *entCategoryMutation) { return func(m *entCategoryMutation) { m.nameType = TypeLazy - m.nameFunc = func(ctx context.Context, i *EntCategoryMutator, c int, creator *ent.CategoryCreate) error { + m.nameFunc = func(ctx context.Context, i *EntCategoryMutator, c int) error { if fn == nil { return nil } @@ -65,7 +77,7 @@ func (*entCategoryMutation) nameLazyMutateFunc(fn func(ctx context.Context, i *E return err } - creator.SetName(value) + i.EntCreator().SetName(value) i.Name = value return nil @@ -75,9 +87,9 @@ func (*entCategoryMutation) nameLazyMutateFunc(fn func(ctx context.Context, i *E func (*entCategoryMutation) nameDefaultMutateFunc(v string) func(m *entCategoryMutation) { return func(m *entCategoryMutation) { m.nameType = TypeDefault - m.nameFunc = func(ctx context.Context, i *EntCategoryMutator, c int, creator *ent.CategoryCreate) error { + m.nameFunc = func(ctx context.Context, i *EntCategoryMutator, c int) error { - creator.SetName(v) + i.EntCreator().SetName(v) i.Name = v return nil @@ -87,7 +99,7 @@ func (*entCategoryMutation) nameDefaultMutateFunc(v string) func(m *entCategoryM func (*entCategoryMutation) nameFactoryMutateFunc(fn func(ctx context.Context) (string, error)) func(m *entCategoryMutation) { return func(m *entCategoryMutation) { m.nameType = TypeFactory - m.nameFunc = func(ctx context.Context, i *EntCategoryMutator, c int, creator *ent.CategoryCreate) error { + m.nameFunc = func(ctx context.Context, i *EntCategoryMutator, c int) error { if fn == nil { return nil } @@ -96,7 +108,7 @@ func (*entCategoryMutation) nameFactoryMutateFunc(fn func(ctx context.Context) ( return err } - creator.SetName(value) + i.EntCreator().SetName(value) i.Name = value @@ -105,48 +117,79 @@ func (*entCategoryMutation) nameFactoryMutateFunc(fn func(ctx context.Context) ( } } +// SetNameSequence register a function which accept a sequence counter and set return value to Name field func (f *EntCategoryMetaFactory) SetNameSequence(fn func(ctx context.Context, i int) (string, error)) *EntCategoryMetaFactory { f.mutation.nameSequenceMutateFunc(fn)(&f.mutation) return f } + +// SetNameLazy register a function which accept the build struct and set return value to Name field func (f *EntCategoryMetaFactory) SetNameLazy(fn func(ctx context.Context, i *EntCategoryMutator) (string, error)) *EntCategoryMetaFactory { f.mutation.nameLazyMutateFunc(fn)(&f.mutation) return f } + +// SetNameDefault assign a default value to Name field func (f *EntCategoryMetaFactory) SetNameDefault(v string) *EntCategoryMetaFactory { f.mutation.nameDefaultMutateFunc(v)(&f.mutation) return f } + +// SetNameFactory register a factory function and assign return value to Name, you can also use related factory's Create/CreateV as input function here func (f *EntCategoryMetaFactory) SetNameFactory(fn func(ctx context.Context) (string, error)) *EntCategoryMetaFactory { f.mutation.nameFactoryMutateFunc(fn)(&f.mutation) return f } + +// SetNameSequence register a function which accept a sequence counter and set return value to Name field func (t *entCategoryTrait) SetNameSequence(fn func(ctx context.Context, i int) (string, error)) *entCategoryTrait { t.updates = append(t.updates, t.mutation.nameSequenceMutateFunc(fn)) return t } + +// SetNameLazy register a function which accept the build struct and set return value to Name field func (t *entCategoryTrait) SetNameLazy(fn func(ctx context.Context, i *EntCategoryMutator) (string, error)) *entCategoryTrait { t.updates = append(t.updates, t.mutation.nameLazyMutateFunc(fn)) return t } + +// SetNameDefault assign a default value to Name field func (t *entCategoryTrait) SetNameDefault(v string) *entCategoryTrait { t.updates = append(t.updates, t.mutation.nameDefaultMutateFunc(v)) return t } + +// SetNameFactory register a factory function and assign return value to Name, you can also use related factory's Create/CreateV as input function here func (t *entCategoryTrait) SetNameFactory(fn func(ctx context.Context) (string, error)) *entCategoryTrait { t.updates = append(t.updates, t.mutation.nameFactoryMutateFunc(fn)) return t } +// SetAfterCreateFunc register a function to be called after struct create func (f *EntCategoryMetaFactory) SetAfterCreateFunc(fn func(ctx context.Context, i *ent.Category) error) *EntCategoryMetaFactory { f.mutation.afterCreateFunc = fn return f } + +// SetBeforeCreateFunc register a function to be called before struct create +func (f *EntCategoryMetaFactory) SetBeforeCreateFunc(fn func(ctx context.Context, i *EntCategoryMutator) error) *EntCategoryMetaFactory { + f.mutation.beforeCreateFunc = fn + return f +} + +// SetAfterCreateFunc register a function to be called after struct create func (t *entCategoryTrait) SetAfterCreateFunc(fn func(ctx context.Context, i *ent.Category) error) *entCategoryTrait { t.updates = append(t.updates, t.mutation.afterCreateMutateFunc(fn)) return t } +// SetBeforeCreateFunc register a function to be called before struct create +func (t *entCategoryTrait) SetBeforeCreateFunc(fn func(ctx context.Context, i *EntCategoryMutator) error) *entCategoryTrait { + t.updates = append(t.updates, t.mutation.beforeCreateMutateFunc(fn)) + return t +} + +// Build create a EntCategoryFactory from EntCategoryMetaFactory func (f *EntCategoryMetaFactory) Build() *EntCategoryFactory { return &EntCategoryFactory{meta: *f, counter: &Counter{}} } @@ -158,6 +201,7 @@ type EntCategoryFactory struct { client *ent.Client } +// SetName set the Name field func (f *EntCategoryFactory) SetName(i string) *EntCategoryBuilder { builder := &EntCategoryBuilder{mutation: f.meta.mutation, counter: f.counter, factory: f} builder.SetName(i) @@ -167,6 +211,7 @@ func (f *EntCategoryFactory) SetName(i string) *EntCategoryBuilder { return builder } +// Create return a new *ent.Category func (f *EntCategoryFactory) Create(ctx context.Context) (*ent.Category, error) { builder := &EntCategoryBuilder{mutation: f.meta.mutation, counter: f.counter, factory: f} @@ -174,6 +219,8 @@ func (f *EntCategoryFactory) Create(ctx context.Context) (*ent.Category, error) return builder.Create(ctx) } + +// CreateV return a new ent.Category func (f *EntCategoryFactory) CreateV(ctx context.Context) (ent.Category, error) { builder := &EntCategoryBuilder{mutation: f.meta.mutation, counter: f.counter, factory: f} @@ -181,6 +228,8 @@ func (f *EntCategoryFactory) CreateV(ctx context.Context) (ent.Category, error) return builder.CreateV(ctx) } + +// CreateBatch return a []*ent.Category slice func (f *EntCategoryFactory) CreateBatch(ctx context.Context, n int) ([]*ent.Category, error) { builder := &EntCategoryBuilder{mutation: f.meta.mutation, counter: f.counter, factory: f} @@ -188,6 +237,8 @@ func (f *EntCategoryFactory) CreateBatch(ctx context.Context, n int) ([]*ent.Cat return builder.CreateBatch(ctx, n) } + +// CreateBatchV return a []ent.Category slice func (f *EntCategoryFactory) CreateBatchV(ctx context.Context, n int) ([]ent.Category, error) { builder := &EntCategoryBuilder{mutation: f.meta.mutation, counter: f.counter, factory: f} @@ -196,6 +247,7 @@ func (f *EntCategoryFactory) CreateBatchV(ctx context.Context, n int) ([]ent.Cat return builder.CreateBatchV(ctx, n) } +// Client set ent client to EntCategoryFactory func (f *EntCategoryFactory) Client(c *ent.Client) *EntCategoryFactory { f.client = c return f @@ -217,12 +269,14 @@ func (b *EntCategoryBuilder) Client(c *ent.Client) *EntCategoryBuilder { return b } +// SetName set the Name field func (b *EntCategoryBuilder) SetName(i string) *EntCategoryBuilder { b.nameOverride = i b.nameOverriden = true return b } +// CreateV return a new ent.Category func (b *EntCategoryBuilder) CreateV(ctx context.Context) (ent.Category, error) { var d ent.Category p, err := b.Create(ctx) @@ -232,11 +286,12 @@ func (b *EntCategoryBuilder) CreateV(ctx context.Context) (ent.Category, error) return d, err } +// Create return a new *ent.Category func (b *EntCategoryBuilder) Create(ctx context.Context) (*ent.Category, error) { - var preSlice = []func(ctx context.Context, i *EntCategoryMutator, c int, creator *ent.CategoryCreate) error{} - var lazySlice = []func(ctx context.Context, i *EntCategoryMutator, c int, creator *ent.CategoryCreate) error{} - var postSlice = []func(ctx context.Context, i *ent.Category, c int, creator *ent.CategoryCreate) error{} + var preSlice = []func(ctx context.Context, i *EntCategoryMutator, c int) error{} + var lazySlice = []func(ctx context.Context, i *EntCategoryMutator, c int) error{} + var postSlice = []func(ctx context.Context, i *ent.Category, c int) error{} index := b.counter.Get() _ = index @@ -245,10 +300,10 @@ func (b *EntCategoryBuilder) Create(ctx context.Context) (*ent.Category, error) entBuilder := client.Category.Create() if b.nameOverriden { - preSlice = append(preSlice, func(ctx context.Context, i *EntCategoryMutator, c int, creator *ent.CategoryCreate) error { + preSlice = append(preSlice, func(ctx context.Context, i *EntCategoryMutator, c int) error { value := b.nameOverride - creator.SetName(value) + i.EntCreator().SetName(value) i.Name = value return nil @@ -267,9 +322,12 @@ func (b *EntCategoryBuilder) Create(ctx context.Context) (*ent.Category, error) } v := &EntCategoryMutator{} + + v._creator = entBuilder + for _, f := range preSlice { - err := f(ctx, v, index, entBuilder) + err := f(ctx, v, index) if err != nil { return nil, err @@ -277,12 +335,17 @@ func (b *EntCategoryBuilder) Create(ctx context.Context) (*ent.Category, error) } for _, f := range lazySlice { - err := f(ctx, v, index, entBuilder) + err := f(ctx, v, index) if err != nil { return nil, err } } + if b.mutation.beforeCreateFunc != nil { + if err := b.mutation.beforeCreateFunc(ctx, v); err != nil { + return nil, err + } + } new, err := entBuilder.Save(ctx) if err != nil { @@ -296,9 +359,7 @@ func (b *EntCategoryBuilder) Create(ctx context.Context) (*ent.Category, error) } } for _, f := range postSlice { - - err := f(ctx, new, index, entBuilder) - + err := f(ctx, new, index) if err != nil { return nil, err } diff --git a/examples/ent_recipe/carrier/factory/ent_ingredient.go b/examples/ent_recipe/carrier/factory/ent_ingredient.go index ba5dded..708cf01 100644 --- a/examples/ent_recipe/carrier/factory/ent_ingredient.go +++ b/examples/ent_recipe/carrier/factory/ent_ingredient.go @@ -9,13 +9,20 @@ import ( type EntIngredientMutator struct { Name string + + _creator *ent.IngredientCreate +} + +func (m *EntIngredientMutator) EntCreator() *ent.IngredientCreate { + return m._creator } type entIngredientMutation struct { nameType int - nameFunc func(ctx context.Context, i *EntIngredientMutator, c int, creator *ent.IngredientCreate) error + nameFunc func(ctx context.Context, i *EntIngredientMutator, c int) error - afterCreateFunc func(ctx context.Context, i *ent.Ingredient) error + beforeCreateFunc func(ctx context.Context, i *EntIngredientMutator) error + afterCreateFunc func(ctx context.Context, i *ent.Ingredient) error } type EntIngredientMetaFactory struct { mutation entIngredientMutation @@ -28,6 +35,11 @@ type entIngredientTrait struct { func EntIngredientTrait() *entIngredientTrait { return &entIngredientTrait{} } +func (*entIngredientMutation) beforeCreateMutateFunc(fn func(ctx context.Context, i *EntIngredientMutator) error) func(m *entIngredientMutation) { + return func(m *entIngredientMutation) { + m.beforeCreateFunc = fn + } +} func (*entIngredientMutation) afterCreateMutateFunc(fn func(ctx context.Context, i *ent.Ingredient) error) func(m *entIngredientMutation) { return func(m *entIngredientMutation) { m.afterCreateFunc = fn @@ -37,7 +49,7 @@ func (*entIngredientMutation) afterCreateMutateFunc(fn func(ctx context.Context, func (*entIngredientMutation) nameSequenceMutateFunc(fn func(ctx context.Context, i int) (string, error)) func(m *entIngredientMutation) { return func(m *entIngredientMutation) { m.nameType = TypeSequence - m.nameFunc = func(ctx context.Context, i *EntIngredientMutator, c int, creator *ent.IngredientCreate) error { + m.nameFunc = func(ctx context.Context, i *EntIngredientMutator, c int) error { if fn == nil { return nil } @@ -46,7 +58,7 @@ func (*entIngredientMutation) nameSequenceMutateFunc(fn func(ctx context.Context return err } - creator.SetName(value) + i.EntCreator().SetName(value) i.Name = value return nil @@ -56,7 +68,7 @@ func (*entIngredientMutation) nameSequenceMutateFunc(fn func(ctx context.Context func (*entIngredientMutation) nameLazyMutateFunc(fn func(ctx context.Context, i *EntIngredientMutator) (string, error)) func(m *entIngredientMutation) { return func(m *entIngredientMutation) { m.nameType = TypeLazy - m.nameFunc = func(ctx context.Context, i *EntIngredientMutator, c int, creator *ent.IngredientCreate) error { + m.nameFunc = func(ctx context.Context, i *EntIngredientMutator, c int) error { if fn == nil { return nil } @@ -65,7 +77,7 @@ func (*entIngredientMutation) nameLazyMutateFunc(fn func(ctx context.Context, i return err } - creator.SetName(value) + i.EntCreator().SetName(value) i.Name = value return nil @@ -75,9 +87,9 @@ func (*entIngredientMutation) nameLazyMutateFunc(fn func(ctx context.Context, i func (*entIngredientMutation) nameDefaultMutateFunc(v string) func(m *entIngredientMutation) { return func(m *entIngredientMutation) { m.nameType = TypeDefault - m.nameFunc = func(ctx context.Context, i *EntIngredientMutator, c int, creator *ent.IngredientCreate) error { + m.nameFunc = func(ctx context.Context, i *EntIngredientMutator, c int) error { - creator.SetName(v) + i.EntCreator().SetName(v) i.Name = v return nil @@ -87,7 +99,7 @@ func (*entIngredientMutation) nameDefaultMutateFunc(v string) func(m *entIngredi func (*entIngredientMutation) nameFactoryMutateFunc(fn func(ctx context.Context) (string, error)) func(m *entIngredientMutation) { return func(m *entIngredientMutation) { m.nameType = TypeFactory - m.nameFunc = func(ctx context.Context, i *EntIngredientMutator, c int, creator *ent.IngredientCreate) error { + m.nameFunc = func(ctx context.Context, i *EntIngredientMutator, c int) error { if fn == nil { return nil } @@ -96,7 +108,7 @@ func (*entIngredientMutation) nameFactoryMutateFunc(fn func(ctx context.Context) return err } - creator.SetName(value) + i.EntCreator().SetName(value) i.Name = value @@ -105,48 +117,79 @@ func (*entIngredientMutation) nameFactoryMutateFunc(fn func(ctx context.Context) } } +// SetNameSequence register a function which accept a sequence counter and set return value to Name field func (f *EntIngredientMetaFactory) SetNameSequence(fn func(ctx context.Context, i int) (string, error)) *EntIngredientMetaFactory { f.mutation.nameSequenceMutateFunc(fn)(&f.mutation) return f } + +// SetNameLazy register a function which accept the build struct and set return value to Name field func (f *EntIngredientMetaFactory) SetNameLazy(fn func(ctx context.Context, i *EntIngredientMutator) (string, error)) *EntIngredientMetaFactory { f.mutation.nameLazyMutateFunc(fn)(&f.mutation) return f } + +// SetNameDefault assign a default value to Name field func (f *EntIngredientMetaFactory) SetNameDefault(v string) *EntIngredientMetaFactory { f.mutation.nameDefaultMutateFunc(v)(&f.mutation) return f } + +// SetNameFactory register a factory function and assign return value to Name, you can also use related factory's Create/CreateV as input function here func (f *EntIngredientMetaFactory) SetNameFactory(fn func(ctx context.Context) (string, error)) *EntIngredientMetaFactory { f.mutation.nameFactoryMutateFunc(fn)(&f.mutation) return f } + +// SetNameSequence register a function which accept a sequence counter and set return value to Name field func (t *entIngredientTrait) SetNameSequence(fn func(ctx context.Context, i int) (string, error)) *entIngredientTrait { t.updates = append(t.updates, t.mutation.nameSequenceMutateFunc(fn)) return t } + +// SetNameLazy register a function which accept the build struct and set return value to Name field func (t *entIngredientTrait) SetNameLazy(fn func(ctx context.Context, i *EntIngredientMutator) (string, error)) *entIngredientTrait { t.updates = append(t.updates, t.mutation.nameLazyMutateFunc(fn)) return t } + +// SetNameDefault assign a default value to Name field func (t *entIngredientTrait) SetNameDefault(v string) *entIngredientTrait { t.updates = append(t.updates, t.mutation.nameDefaultMutateFunc(v)) return t } + +// SetNameFactory register a factory function and assign return value to Name, you can also use related factory's Create/CreateV as input function here func (t *entIngredientTrait) SetNameFactory(fn func(ctx context.Context) (string, error)) *entIngredientTrait { t.updates = append(t.updates, t.mutation.nameFactoryMutateFunc(fn)) return t } +// SetAfterCreateFunc register a function to be called after struct create func (f *EntIngredientMetaFactory) SetAfterCreateFunc(fn func(ctx context.Context, i *ent.Ingredient) error) *EntIngredientMetaFactory { f.mutation.afterCreateFunc = fn return f } + +// SetBeforeCreateFunc register a function to be called before struct create +func (f *EntIngredientMetaFactory) SetBeforeCreateFunc(fn func(ctx context.Context, i *EntIngredientMutator) error) *EntIngredientMetaFactory { + f.mutation.beforeCreateFunc = fn + return f +} + +// SetAfterCreateFunc register a function to be called after struct create func (t *entIngredientTrait) SetAfterCreateFunc(fn func(ctx context.Context, i *ent.Ingredient) error) *entIngredientTrait { t.updates = append(t.updates, t.mutation.afterCreateMutateFunc(fn)) return t } +// SetBeforeCreateFunc register a function to be called before struct create +func (t *entIngredientTrait) SetBeforeCreateFunc(fn func(ctx context.Context, i *EntIngredientMutator) error) *entIngredientTrait { + t.updates = append(t.updates, t.mutation.beforeCreateMutateFunc(fn)) + return t +} + +// Build create a EntIngredientFactory from EntIngredientMetaFactory func (f *EntIngredientMetaFactory) Build() *EntIngredientFactory { return &EntIngredientFactory{meta: *f, counter: &Counter{}} } @@ -158,6 +201,7 @@ type EntIngredientFactory struct { client *ent.Client } +// SetName set the Name field func (f *EntIngredientFactory) SetName(i string) *EntIngredientBuilder { builder := &EntIngredientBuilder{mutation: f.meta.mutation, counter: f.counter, factory: f} builder.SetName(i) @@ -167,6 +211,7 @@ func (f *EntIngredientFactory) SetName(i string) *EntIngredientBuilder { return builder } +// Create return a new *ent.Ingredient func (f *EntIngredientFactory) Create(ctx context.Context) (*ent.Ingredient, error) { builder := &EntIngredientBuilder{mutation: f.meta.mutation, counter: f.counter, factory: f} @@ -174,6 +219,8 @@ func (f *EntIngredientFactory) Create(ctx context.Context) (*ent.Ingredient, err return builder.Create(ctx) } + +// CreateV return a new ent.Ingredient func (f *EntIngredientFactory) CreateV(ctx context.Context) (ent.Ingredient, error) { builder := &EntIngredientBuilder{mutation: f.meta.mutation, counter: f.counter, factory: f} @@ -181,6 +228,8 @@ func (f *EntIngredientFactory) CreateV(ctx context.Context) (ent.Ingredient, err return builder.CreateV(ctx) } + +// CreateBatch return a []*ent.Ingredient slice func (f *EntIngredientFactory) CreateBatch(ctx context.Context, n int) ([]*ent.Ingredient, error) { builder := &EntIngredientBuilder{mutation: f.meta.mutation, counter: f.counter, factory: f} @@ -188,6 +237,8 @@ func (f *EntIngredientFactory) CreateBatch(ctx context.Context, n int) ([]*ent.I return builder.CreateBatch(ctx, n) } + +// CreateBatchV return a []ent.Ingredient slice func (f *EntIngredientFactory) CreateBatchV(ctx context.Context, n int) ([]ent.Ingredient, error) { builder := &EntIngredientBuilder{mutation: f.meta.mutation, counter: f.counter, factory: f} @@ -196,6 +247,7 @@ func (f *EntIngredientFactory) CreateBatchV(ctx context.Context, n int) ([]ent.I return builder.CreateBatchV(ctx, n) } +// Client set ent client to EntIngredientFactory func (f *EntIngredientFactory) Client(c *ent.Client) *EntIngredientFactory { f.client = c return f @@ -217,12 +269,14 @@ func (b *EntIngredientBuilder) Client(c *ent.Client) *EntIngredientBuilder { return b } +// SetName set the Name field func (b *EntIngredientBuilder) SetName(i string) *EntIngredientBuilder { b.nameOverride = i b.nameOverriden = true return b } +// CreateV return a new ent.Ingredient func (b *EntIngredientBuilder) CreateV(ctx context.Context) (ent.Ingredient, error) { var d ent.Ingredient p, err := b.Create(ctx) @@ -232,11 +286,12 @@ func (b *EntIngredientBuilder) CreateV(ctx context.Context) (ent.Ingredient, err return d, err } +// Create return a new *ent.Ingredient func (b *EntIngredientBuilder) Create(ctx context.Context) (*ent.Ingredient, error) { - var preSlice = []func(ctx context.Context, i *EntIngredientMutator, c int, creator *ent.IngredientCreate) error{} - var lazySlice = []func(ctx context.Context, i *EntIngredientMutator, c int, creator *ent.IngredientCreate) error{} - var postSlice = []func(ctx context.Context, i *ent.Ingredient, c int, creator *ent.IngredientCreate) error{} + var preSlice = []func(ctx context.Context, i *EntIngredientMutator, c int) error{} + var lazySlice = []func(ctx context.Context, i *EntIngredientMutator, c int) error{} + var postSlice = []func(ctx context.Context, i *ent.Ingredient, c int) error{} index := b.counter.Get() _ = index @@ -245,10 +300,10 @@ func (b *EntIngredientBuilder) Create(ctx context.Context) (*ent.Ingredient, err entBuilder := client.Ingredient.Create() if b.nameOverriden { - preSlice = append(preSlice, func(ctx context.Context, i *EntIngredientMutator, c int, creator *ent.IngredientCreate) error { + preSlice = append(preSlice, func(ctx context.Context, i *EntIngredientMutator, c int) error { value := b.nameOverride - creator.SetName(value) + i.EntCreator().SetName(value) i.Name = value return nil @@ -267,9 +322,12 @@ func (b *EntIngredientBuilder) Create(ctx context.Context) (*ent.Ingredient, err } v := &EntIngredientMutator{} + + v._creator = entBuilder + for _, f := range preSlice { - err := f(ctx, v, index, entBuilder) + err := f(ctx, v, index) if err != nil { return nil, err @@ -277,12 +335,17 @@ func (b *EntIngredientBuilder) Create(ctx context.Context) (*ent.Ingredient, err } for _, f := range lazySlice { - err := f(ctx, v, index, entBuilder) + err := f(ctx, v, index) if err != nil { return nil, err } } + if b.mutation.beforeCreateFunc != nil { + if err := b.mutation.beforeCreateFunc(ctx, v); err != nil { + return nil, err + } + } new, err := entBuilder.Save(ctx) if err != nil { @@ -296,9 +359,7 @@ func (b *EntIngredientBuilder) Create(ctx context.Context) (*ent.Ingredient, err } } for _, f := range postSlice { - - err := f(ctx, new, index, entBuilder) - + err := f(ctx, new, index) if err != nil { return nil, err } diff --git a/examples/ent_recipe/carrier/factory/ent_recipe.go b/examples/ent_recipe/carrier/factory/ent_recipe.go index 4361865..9a7b985 100644 --- a/examples/ent_recipe/carrier/factory/ent_recipe.go +++ b/examples/ent_recipe/carrier/factory/ent_recipe.go @@ -15,26 +15,33 @@ type EntRecipeMutator struct { Name string Servings int + + _creator *ent.RecipeCreate +} + +func (m *EntRecipeMutator) EntCreator() *ent.RecipeCreate { + return m._creator } type entRecipeMutation struct { authorType int - authorFunc func(ctx context.Context, i *EntRecipeMutator, c int, creator *ent.RecipeCreate) error + authorFunc func(ctx context.Context, i *EntRecipeMutator, c int) error authorIDType int - authorIDFunc func(ctx context.Context, i *EntRecipeMutator, c int, creator *ent.RecipeCreate) error + authorIDFunc func(ctx context.Context, i *EntRecipeMutator, c int) error nameType int - nameFunc func(ctx context.Context, i *EntRecipeMutator, c int, creator *ent.RecipeCreate) error + nameFunc func(ctx context.Context, i *EntRecipeMutator, c int) error servingsType int - servingsFunc func(ctx context.Context, i *EntRecipeMutator, c int, creator *ent.RecipeCreate) error + servingsFunc func(ctx context.Context, i *EntRecipeMutator, c int) error _postStepsCountFunc func(ctx context.Context, set bool, obj *ent.Recipe, i int) error _postIngredientsCountFunc func(ctx context.Context, set bool, obj *ent.Recipe, i int) error - afterCreateFunc func(ctx context.Context, i *ent.Recipe) error + beforeCreateFunc func(ctx context.Context, i *EntRecipeMutator) error + afterCreateFunc func(ctx context.Context, i *ent.Recipe) error } type EntRecipeMetaFactory struct { mutation entRecipeMutation @@ -51,6 +58,11 @@ type entRecipeTrait struct { func EntRecipeTrait() *entRecipeTrait { return &entRecipeTrait{} } +func (*entRecipeMutation) beforeCreateMutateFunc(fn func(ctx context.Context, i *EntRecipeMutator) error) func(m *entRecipeMutation) { + return func(m *entRecipeMutation) { + m.beforeCreateFunc = fn + } +} func (*entRecipeMutation) afterCreateMutateFunc(fn func(ctx context.Context, i *ent.Recipe) error) func(m *entRecipeMutation) { return func(m *entRecipeMutation) { m.afterCreateFunc = fn @@ -60,7 +72,7 @@ func (*entRecipeMutation) afterCreateMutateFunc(fn func(ctx context.Context, i * func (*entRecipeMutation) authorSequenceMutateFunc(fn func(ctx context.Context, i int) (*ent.User, error)) func(m *entRecipeMutation) { return func(m *entRecipeMutation) { m.authorType = TypeSequence - m.authorFunc = func(ctx context.Context, i *EntRecipeMutator, c int, creator *ent.RecipeCreate) error { + m.authorFunc = func(ctx context.Context, i *EntRecipeMutator, c int) error { if fn == nil { return nil } @@ -69,7 +81,7 @@ func (*entRecipeMutation) authorSequenceMutateFunc(fn func(ctx context.Context, return err } - creator.SetAuthor(value) + i.EntCreator().SetAuthor(value) i.Author = value return nil @@ -79,7 +91,7 @@ func (*entRecipeMutation) authorSequenceMutateFunc(fn func(ctx context.Context, func (*entRecipeMutation) authorLazyMutateFunc(fn func(ctx context.Context, i *EntRecipeMutator) (*ent.User, error)) func(m *entRecipeMutation) { return func(m *entRecipeMutation) { m.authorType = TypeLazy - m.authorFunc = func(ctx context.Context, i *EntRecipeMutator, c int, creator *ent.RecipeCreate) error { + m.authorFunc = func(ctx context.Context, i *EntRecipeMutator, c int) error { if fn == nil { return nil } @@ -88,7 +100,7 @@ func (*entRecipeMutation) authorLazyMutateFunc(fn func(ctx context.Context, i *E return err } - creator.SetAuthor(value) + i.EntCreator().SetAuthor(value) i.Author = value return nil @@ -98,9 +110,9 @@ func (*entRecipeMutation) authorLazyMutateFunc(fn func(ctx context.Context, i *E func (*entRecipeMutation) authorDefaultMutateFunc(v *ent.User) func(m *entRecipeMutation) { return func(m *entRecipeMutation) { m.authorType = TypeDefault - m.authorFunc = func(ctx context.Context, i *EntRecipeMutator, c int, creator *ent.RecipeCreate) error { + m.authorFunc = func(ctx context.Context, i *EntRecipeMutator, c int) error { - creator.SetAuthor(v) + i.EntCreator().SetAuthor(v) i.Author = v return nil @@ -110,7 +122,7 @@ func (*entRecipeMutation) authorDefaultMutateFunc(v *ent.User) func(m *entRecipe func (*entRecipeMutation) authorFactoryMutateFunc(fn func(ctx context.Context) (*ent.User, error)) func(m *entRecipeMutation) { return func(m *entRecipeMutation) { m.authorType = TypeFactory - m.authorFunc = func(ctx context.Context, i *EntRecipeMutator, c int, creator *ent.RecipeCreate) error { + m.authorFunc = func(ctx context.Context, i *EntRecipeMutator, c int) error { if fn == nil { return nil } @@ -119,7 +131,7 @@ func (*entRecipeMutation) authorFactoryMutateFunc(fn func(ctx context.Context) ( return err } - creator.SetAuthor(value) + i.EntCreator().SetAuthor(value) i.Author = value @@ -128,34 +140,49 @@ func (*entRecipeMutation) authorFactoryMutateFunc(fn func(ctx context.Context) ( } } +// SetAuthorSequence register a function which accept a sequence counter and set return value to Author field func (f *EntRecipeMetaFactory) SetAuthorSequence(fn func(ctx context.Context, i int) (*ent.User, error)) *EntRecipeMetaFactory { f.mutation.authorSequenceMutateFunc(fn)(&f.mutation) return f } + +// SetAuthorLazy register a function which accept the build struct and set return value to Author field func (f *EntRecipeMetaFactory) SetAuthorLazy(fn func(ctx context.Context, i *EntRecipeMutator) (*ent.User, error)) *EntRecipeMetaFactory { f.mutation.authorLazyMutateFunc(fn)(&f.mutation) return f } + +// SetAuthorDefault assign a default value to Author field func (f *EntRecipeMetaFactory) SetAuthorDefault(v *ent.User) *EntRecipeMetaFactory { f.mutation.authorDefaultMutateFunc(v)(&f.mutation) return f } + +// SetAuthorFactory register a factory function and assign return value to Author, you can also use related factory's Create/CreateV as input function here func (f *EntRecipeMetaFactory) SetAuthorFactory(fn func(ctx context.Context) (*ent.User, error)) *EntRecipeMetaFactory { f.mutation.authorFactoryMutateFunc(fn)(&f.mutation) return f } + +// SetAuthorSequence register a function which accept a sequence counter and set return value to Author field func (t *entRecipeTrait) SetAuthorSequence(fn func(ctx context.Context, i int) (*ent.User, error)) *entRecipeTrait { t.updates = append(t.updates, t.mutation.authorSequenceMutateFunc(fn)) return t } + +// SetAuthorLazy register a function which accept the build struct and set return value to Author field func (t *entRecipeTrait) SetAuthorLazy(fn func(ctx context.Context, i *EntRecipeMutator) (*ent.User, error)) *entRecipeTrait { t.updates = append(t.updates, t.mutation.authorLazyMutateFunc(fn)) return t } + +// SetAuthorDefault assign a default value to Author field func (t *entRecipeTrait) SetAuthorDefault(v *ent.User) *entRecipeTrait { t.updates = append(t.updates, t.mutation.authorDefaultMutateFunc(v)) return t } + +// SetAuthorFactory register a factory function and assign return value to Author, you can also use related factory's Create/CreateV as input function here func (t *entRecipeTrait) SetAuthorFactory(fn func(ctx context.Context) (*ent.User, error)) *entRecipeTrait { t.updates = append(t.updates, t.mutation.authorFactoryMutateFunc(fn)) return t @@ -164,7 +191,7 @@ func (t *entRecipeTrait) SetAuthorFactory(fn func(ctx context.Context) (*ent.Use func (*entRecipeMutation) authorIDSequenceMutateFunc(fn func(ctx context.Context, i int) (int, error)) func(m *entRecipeMutation) { return func(m *entRecipeMutation) { m.authorIDType = TypeSequence - m.authorIDFunc = func(ctx context.Context, i *EntRecipeMutator, c int, creator *ent.RecipeCreate) error { + m.authorIDFunc = func(ctx context.Context, i *EntRecipeMutator, c int) error { if fn == nil { return nil } @@ -173,7 +200,7 @@ func (*entRecipeMutation) authorIDSequenceMutateFunc(fn func(ctx context.Context return err } - creator.SetAuthorID(value) + i.EntCreator().SetAuthorID(value) i.AuthorID = value return nil @@ -183,7 +210,7 @@ func (*entRecipeMutation) authorIDSequenceMutateFunc(fn func(ctx context.Context func (*entRecipeMutation) authorIDLazyMutateFunc(fn func(ctx context.Context, i *EntRecipeMutator) (int, error)) func(m *entRecipeMutation) { return func(m *entRecipeMutation) { m.authorIDType = TypeLazy - m.authorIDFunc = func(ctx context.Context, i *EntRecipeMutator, c int, creator *ent.RecipeCreate) error { + m.authorIDFunc = func(ctx context.Context, i *EntRecipeMutator, c int) error { if fn == nil { return nil } @@ -192,7 +219,7 @@ func (*entRecipeMutation) authorIDLazyMutateFunc(fn func(ctx context.Context, i return err } - creator.SetAuthorID(value) + i.EntCreator().SetAuthorID(value) i.AuthorID = value return nil @@ -202,9 +229,9 @@ func (*entRecipeMutation) authorIDLazyMutateFunc(fn func(ctx context.Context, i func (*entRecipeMutation) authorIDDefaultMutateFunc(v int) func(m *entRecipeMutation) { return func(m *entRecipeMutation) { m.authorIDType = TypeDefault - m.authorIDFunc = func(ctx context.Context, i *EntRecipeMutator, c int, creator *ent.RecipeCreate) error { + m.authorIDFunc = func(ctx context.Context, i *EntRecipeMutator, c int) error { - creator.SetAuthorID(v) + i.EntCreator().SetAuthorID(v) i.AuthorID = v return nil @@ -214,7 +241,7 @@ func (*entRecipeMutation) authorIDDefaultMutateFunc(v int) func(m *entRecipeMuta func (*entRecipeMutation) authorIDFactoryMutateFunc(fn func(ctx context.Context) (int, error)) func(m *entRecipeMutation) { return func(m *entRecipeMutation) { m.authorIDType = TypeFactory - m.authorIDFunc = func(ctx context.Context, i *EntRecipeMutator, c int, creator *ent.RecipeCreate) error { + m.authorIDFunc = func(ctx context.Context, i *EntRecipeMutator, c int) error { if fn == nil { return nil } @@ -223,7 +250,7 @@ func (*entRecipeMutation) authorIDFactoryMutateFunc(fn func(ctx context.Context) return err } - creator.SetAuthorID(value) + i.EntCreator().SetAuthorID(value) i.AuthorID = value @@ -232,34 +259,49 @@ func (*entRecipeMutation) authorIDFactoryMutateFunc(fn func(ctx context.Context) } } +// SetAuthorIDSequence register a function which accept a sequence counter and set return value to AuthorID field func (f *EntRecipeMetaFactory) SetAuthorIDSequence(fn func(ctx context.Context, i int) (int, error)) *EntRecipeMetaFactory { f.mutation.authorIDSequenceMutateFunc(fn)(&f.mutation) return f } + +// SetAuthorIDLazy register a function which accept the build struct and set return value to AuthorID field func (f *EntRecipeMetaFactory) SetAuthorIDLazy(fn func(ctx context.Context, i *EntRecipeMutator) (int, error)) *EntRecipeMetaFactory { f.mutation.authorIDLazyMutateFunc(fn)(&f.mutation) return f } + +// SetAuthorIDDefault assign a default value to AuthorID field func (f *EntRecipeMetaFactory) SetAuthorIDDefault(v int) *EntRecipeMetaFactory { f.mutation.authorIDDefaultMutateFunc(v)(&f.mutation) return f } + +// SetAuthorIDFactory register a factory function and assign return value to AuthorID, you can also use related factory's Create/CreateV as input function here func (f *EntRecipeMetaFactory) SetAuthorIDFactory(fn func(ctx context.Context) (int, error)) *EntRecipeMetaFactory { f.mutation.authorIDFactoryMutateFunc(fn)(&f.mutation) return f } + +// SetAuthorIDSequence register a function which accept a sequence counter and set return value to AuthorID field func (t *entRecipeTrait) SetAuthorIDSequence(fn func(ctx context.Context, i int) (int, error)) *entRecipeTrait { t.updates = append(t.updates, t.mutation.authorIDSequenceMutateFunc(fn)) return t } + +// SetAuthorIDLazy register a function which accept the build struct and set return value to AuthorID field func (t *entRecipeTrait) SetAuthorIDLazy(fn func(ctx context.Context, i *EntRecipeMutator) (int, error)) *entRecipeTrait { t.updates = append(t.updates, t.mutation.authorIDLazyMutateFunc(fn)) return t } + +// SetAuthorIDDefault assign a default value to AuthorID field func (t *entRecipeTrait) SetAuthorIDDefault(v int) *entRecipeTrait { t.updates = append(t.updates, t.mutation.authorIDDefaultMutateFunc(v)) return t } + +// SetAuthorIDFactory register a factory function and assign return value to AuthorID, you can also use related factory's Create/CreateV as input function here func (t *entRecipeTrait) SetAuthorIDFactory(fn func(ctx context.Context) (int, error)) *entRecipeTrait { t.updates = append(t.updates, t.mutation.authorIDFactoryMutateFunc(fn)) return t @@ -268,7 +310,7 @@ func (t *entRecipeTrait) SetAuthorIDFactory(fn func(ctx context.Context) (int, e func (*entRecipeMutation) nameSequenceMutateFunc(fn func(ctx context.Context, i int) (string, error)) func(m *entRecipeMutation) { return func(m *entRecipeMutation) { m.nameType = TypeSequence - m.nameFunc = func(ctx context.Context, i *EntRecipeMutator, c int, creator *ent.RecipeCreate) error { + m.nameFunc = func(ctx context.Context, i *EntRecipeMutator, c int) error { if fn == nil { return nil } @@ -277,7 +319,7 @@ func (*entRecipeMutation) nameSequenceMutateFunc(fn func(ctx context.Context, i return err } - creator.SetName(value) + i.EntCreator().SetName(value) i.Name = value return nil @@ -287,7 +329,7 @@ func (*entRecipeMutation) nameSequenceMutateFunc(fn func(ctx context.Context, i func (*entRecipeMutation) nameLazyMutateFunc(fn func(ctx context.Context, i *EntRecipeMutator) (string, error)) func(m *entRecipeMutation) { return func(m *entRecipeMutation) { m.nameType = TypeLazy - m.nameFunc = func(ctx context.Context, i *EntRecipeMutator, c int, creator *ent.RecipeCreate) error { + m.nameFunc = func(ctx context.Context, i *EntRecipeMutator, c int) error { if fn == nil { return nil } @@ -296,7 +338,7 @@ func (*entRecipeMutation) nameLazyMutateFunc(fn func(ctx context.Context, i *Ent return err } - creator.SetName(value) + i.EntCreator().SetName(value) i.Name = value return nil @@ -306,9 +348,9 @@ func (*entRecipeMutation) nameLazyMutateFunc(fn func(ctx context.Context, i *Ent func (*entRecipeMutation) nameDefaultMutateFunc(v string) func(m *entRecipeMutation) { return func(m *entRecipeMutation) { m.nameType = TypeDefault - m.nameFunc = func(ctx context.Context, i *EntRecipeMutator, c int, creator *ent.RecipeCreate) error { + m.nameFunc = func(ctx context.Context, i *EntRecipeMutator, c int) error { - creator.SetName(v) + i.EntCreator().SetName(v) i.Name = v return nil @@ -318,7 +360,7 @@ func (*entRecipeMutation) nameDefaultMutateFunc(v string) func(m *entRecipeMutat func (*entRecipeMutation) nameFactoryMutateFunc(fn func(ctx context.Context) (string, error)) func(m *entRecipeMutation) { return func(m *entRecipeMutation) { m.nameType = TypeFactory - m.nameFunc = func(ctx context.Context, i *EntRecipeMutator, c int, creator *ent.RecipeCreate) error { + m.nameFunc = func(ctx context.Context, i *EntRecipeMutator, c int) error { if fn == nil { return nil } @@ -327,7 +369,7 @@ func (*entRecipeMutation) nameFactoryMutateFunc(fn func(ctx context.Context) (st return err } - creator.SetName(value) + i.EntCreator().SetName(value) i.Name = value @@ -336,34 +378,49 @@ func (*entRecipeMutation) nameFactoryMutateFunc(fn func(ctx context.Context) (st } } +// SetNameSequence register a function which accept a sequence counter and set return value to Name field func (f *EntRecipeMetaFactory) SetNameSequence(fn func(ctx context.Context, i int) (string, error)) *EntRecipeMetaFactory { f.mutation.nameSequenceMutateFunc(fn)(&f.mutation) return f } + +// SetNameLazy register a function which accept the build struct and set return value to Name field func (f *EntRecipeMetaFactory) SetNameLazy(fn func(ctx context.Context, i *EntRecipeMutator) (string, error)) *EntRecipeMetaFactory { f.mutation.nameLazyMutateFunc(fn)(&f.mutation) return f } + +// SetNameDefault assign a default value to Name field func (f *EntRecipeMetaFactory) SetNameDefault(v string) *EntRecipeMetaFactory { f.mutation.nameDefaultMutateFunc(v)(&f.mutation) return f } + +// SetNameFactory register a factory function and assign return value to Name, you can also use related factory's Create/CreateV as input function here func (f *EntRecipeMetaFactory) SetNameFactory(fn func(ctx context.Context) (string, error)) *EntRecipeMetaFactory { f.mutation.nameFactoryMutateFunc(fn)(&f.mutation) return f } + +// SetNameSequence register a function which accept a sequence counter and set return value to Name field func (t *entRecipeTrait) SetNameSequence(fn func(ctx context.Context, i int) (string, error)) *entRecipeTrait { t.updates = append(t.updates, t.mutation.nameSequenceMutateFunc(fn)) return t } + +// SetNameLazy register a function which accept the build struct and set return value to Name field func (t *entRecipeTrait) SetNameLazy(fn func(ctx context.Context, i *EntRecipeMutator) (string, error)) *entRecipeTrait { t.updates = append(t.updates, t.mutation.nameLazyMutateFunc(fn)) return t } + +// SetNameDefault assign a default value to Name field func (t *entRecipeTrait) SetNameDefault(v string) *entRecipeTrait { t.updates = append(t.updates, t.mutation.nameDefaultMutateFunc(v)) return t } + +// SetNameFactory register a factory function and assign return value to Name, you can also use related factory's Create/CreateV as input function here func (t *entRecipeTrait) SetNameFactory(fn func(ctx context.Context) (string, error)) *entRecipeTrait { t.updates = append(t.updates, t.mutation.nameFactoryMutateFunc(fn)) return t @@ -372,7 +429,7 @@ func (t *entRecipeTrait) SetNameFactory(fn func(ctx context.Context) (string, er func (*entRecipeMutation) servingsSequenceMutateFunc(fn func(ctx context.Context, i int) (int, error)) func(m *entRecipeMutation) { return func(m *entRecipeMutation) { m.servingsType = TypeSequence - m.servingsFunc = func(ctx context.Context, i *EntRecipeMutator, c int, creator *ent.RecipeCreate) error { + m.servingsFunc = func(ctx context.Context, i *EntRecipeMutator, c int) error { if fn == nil { return nil } @@ -381,7 +438,7 @@ func (*entRecipeMutation) servingsSequenceMutateFunc(fn func(ctx context.Context return err } - creator.SetServings(value) + i.EntCreator().SetServings(value) i.Servings = value return nil @@ -391,7 +448,7 @@ func (*entRecipeMutation) servingsSequenceMutateFunc(fn func(ctx context.Context func (*entRecipeMutation) servingsLazyMutateFunc(fn func(ctx context.Context, i *EntRecipeMutator) (int, error)) func(m *entRecipeMutation) { return func(m *entRecipeMutation) { m.servingsType = TypeLazy - m.servingsFunc = func(ctx context.Context, i *EntRecipeMutator, c int, creator *ent.RecipeCreate) error { + m.servingsFunc = func(ctx context.Context, i *EntRecipeMutator, c int) error { if fn == nil { return nil } @@ -400,7 +457,7 @@ func (*entRecipeMutation) servingsLazyMutateFunc(fn func(ctx context.Context, i return err } - creator.SetServings(value) + i.EntCreator().SetServings(value) i.Servings = value return nil @@ -410,9 +467,9 @@ func (*entRecipeMutation) servingsLazyMutateFunc(fn func(ctx context.Context, i func (*entRecipeMutation) servingsDefaultMutateFunc(v int) func(m *entRecipeMutation) { return func(m *entRecipeMutation) { m.servingsType = TypeDefault - m.servingsFunc = func(ctx context.Context, i *EntRecipeMutator, c int, creator *ent.RecipeCreate) error { + m.servingsFunc = func(ctx context.Context, i *EntRecipeMutator, c int) error { - creator.SetServings(v) + i.EntCreator().SetServings(v) i.Servings = v return nil @@ -422,7 +479,7 @@ func (*entRecipeMutation) servingsDefaultMutateFunc(v int) func(m *entRecipeMuta func (*entRecipeMutation) servingsFactoryMutateFunc(fn func(ctx context.Context) (int, error)) func(m *entRecipeMutation) { return func(m *entRecipeMutation) { m.servingsType = TypeFactory - m.servingsFunc = func(ctx context.Context, i *EntRecipeMutator, c int, creator *ent.RecipeCreate) error { + m.servingsFunc = func(ctx context.Context, i *EntRecipeMutator, c int) error { if fn == nil { return nil } @@ -431,7 +488,7 @@ func (*entRecipeMutation) servingsFactoryMutateFunc(fn func(ctx context.Context) return err } - creator.SetServings(value) + i.EntCreator().SetServings(value) i.Servings = value @@ -440,34 +497,49 @@ func (*entRecipeMutation) servingsFactoryMutateFunc(fn func(ctx context.Context) } } +// SetServingsSequence register a function which accept a sequence counter and set return value to Servings field func (f *EntRecipeMetaFactory) SetServingsSequence(fn func(ctx context.Context, i int) (int, error)) *EntRecipeMetaFactory { f.mutation.servingsSequenceMutateFunc(fn)(&f.mutation) return f } + +// SetServingsLazy register a function which accept the build struct and set return value to Servings field func (f *EntRecipeMetaFactory) SetServingsLazy(fn func(ctx context.Context, i *EntRecipeMutator) (int, error)) *EntRecipeMetaFactory { f.mutation.servingsLazyMutateFunc(fn)(&f.mutation) return f } + +// SetServingsDefault assign a default value to Servings field func (f *EntRecipeMetaFactory) SetServingsDefault(v int) *EntRecipeMetaFactory { f.mutation.servingsDefaultMutateFunc(v)(&f.mutation) return f } + +// SetServingsFactory register a factory function and assign return value to Servings, you can also use related factory's Create/CreateV as input function here func (f *EntRecipeMetaFactory) SetServingsFactory(fn func(ctx context.Context) (int, error)) *EntRecipeMetaFactory { f.mutation.servingsFactoryMutateFunc(fn)(&f.mutation) return f } + +// SetServingsSequence register a function which accept a sequence counter and set return value to Servings field func (t *entRecipeTrait) SetServingsSequence(fn func(ctx context.Context, i int) (int, error)) *entRecipeTrait { t.updates = append(t.updates, t.mutation.servingsSequenceMutateFunc(fn)) return t } + +// SetServingsLazy register a function which accept the build struct and set return value to Servings field func (t *entRecipeTrait) SetServingsLazy(fn func(ctx context.Context, i *EntRecipeMutator) (int, error)) *entRecipeTrait { t.updates = append(t.updates, t.mutation.servingsLazyMutateFunc(fn)) return t } + +// SetServingsDefault assign a default value to Servings field func (t *entRecipeTrait) SetServingsDefault(v int) *entRecipeTrait { t.updates = append(t.updates, t.mutation.servingsDefaultMutateFunc(v)) return t } + +// SetServingsFactory register a factory function and assign return value to Servings, you can also use related factory's Create/CreateV as input function here func (t *entRecipeTrait) SetServingsFactory(fn func(ctx context.Context) (int, error)) *entRecipeTrait { t.updates = append(t.updates, t.mutation.servingsFactoryMutateFunc(fn)) return t @@ -478,6 +550,8 @@ func (*entRecipeMutation) stepsCountPostMutateFunc(fn func(ctx context.Context, m._postStepsCountFunc = fn } } + +// SetStepsCountPostFunc register a post function which will be called in factory SetStepsCountPost method func (f *EntRecipeMetaFactory) SetStepsCountPostFunc(fn func(ctx context.Context, set bool, obj *ent.Recipe, i int) error) *EntRecipeMetaFactory { f.mutation.stepsCountPostMutateFunc(fn)(&f.mutation) return f @@ -492,6 +566,8 @@ func (*entRecipeMutation) ingredientsCountPostMutateFunc(fn func(ctx context.Con m._postIngredientsCountFunc = fn } } + +// SetIngredientsCountPostFunc register a post function which will be called in factory SetIngredientsCountPost method func (f *EntRecipeMetaFactory) SetIngredientsCountPostFunc(fn func(ctx context.Context, set bool, obj *ent.Recipe, i int) error) *EntRecipeMetaFactory { f.mutation.ingredientsCountPostMutateFunc(fn)(&f.mutation) return f @@ -501,25 +577,43 @@ func (t *entRecipeTrait) SetIngredientsCountPostFunc(fn func(ctx context.Context return t } +// SetVeganTrait accept a entRecipeTrait, will override builder using Trait's methods if enable func (f *EntRecipeMetaFactory) SetVeganTrait(t *entRecipeTrait) *EntRecipeMetaFactory { f.veganTrait = t return f } +// SetKetoTrait accept a entRecipeTrait, will override builder using Trait's methods if enable func (f *EntRecipeMetaFactory) SetKetoTrait(t *entRecipeTrait) *EntRecipeMetaFactory { f.ketoTrait = t return f } +// SetAfterCreateFunc register a function to be called after struct create func (f *EntRecipeMetaFactory) SetAfterCreateFunc(fn func(ctx context.Context, i *ent.Recipe) error) *EntRecipeMetaFactory { f.mutation.afterCreateFunc = fn return f } + +// SetBeforeCreateFunc register a function to be called before struct create +func (f *EntRecipeMetaFactory) SetBeforeCreateFunc(fn func(ctx context.Context, i *EntRecipeMutator) error) *EntRecipeMetaFactory { + f.mutation.beforeCreateFunc = fn + return f +} + +// SetAfterCreateFunc register a function to be called after struct create func (t *entRecipeTrait) SetAfterCreateFunc(fn func(ctx context.Context, i *ent.Recipe) error) *entRecipeTrait { t.updates = append(t.updates, t.mutation.afterCreateMutateFunc(fn)) return t } +// SetBeforeCreateFunc register a function to be called before struct create +func (t *entRecipeTrait) SetBeforeCreateFunc(fn func(ctx context.Context, i *EntRecipeMutator) error) *entRecipeTrait { + t.updates = append(t.updates, t.mutation.beforeCreateMutateFunc(fn)) + return t +} + +// Build create a EntRecipeFactory from EntRecipeMetaFactory func (f *EntRecipeMetaFactory) Build() *EntRecipeFactory { return &EntRecipeFactory{meta: *f, counter: &Counter{}} } @@ -531,6 +625,7 @@ type EntRecipeFactory struct { client *ent.Client } +// SetAuthor set the Author field func (f *EntRecipeFactory) SetAuthor(i *ent.User) *EntRecipeBuilder { builder := &EntRecipeBuilder{mutation: f.meta.mutation, counter: f.counter, factory: f} builder.SetAuthor(i) @@ -540,6 +635,7 @@ func (f *EntRecipeFactory) SetAuthor(i *ent.User) *EntRecipeBuilder { return builder } +// SetAuthorID set the AuthorID field func (f *EntRecipeFactory) SetAuthorID(i int) *EntRecipeBuilder { builder := &EntRecipeBuilder{mutation: f.meta.mutation, counter: f.counter, factory: f} builder.SetAuthorID(i) @@ -549,6 +645,7 @@ func (f *EntRecipeFactory) SetAuthorID(i int) *EntRecipeBuilder { return builder } +// SetName set the Name field func (f *EntRecipeFactory) SetName(i string) *EntRecipeBuilder { builder := &EntRecipeBuilder{mutation: f.meta.mutation, counter: f.counter, factory: f} builder.SetName(i) @@ -558,6 +655,7 @@ func (f *EntRecipeFactory) SetName(i string) *EntRecipeBuilder { return builder } +// SetServings set the Servings field func (f *EntRecipeFactory) SetServings(i int) *EntRecipeBuilder { builder := &EntRecipeBuilder{mutation: f.meta.mutation, counter: f.counter, factory: f} builder.SetServings(i) @@ -567,6 +665,7 @@ func (f *EntRecipeFactory) SetServings(i int) *EntRecipeBuilder { return builder } +// SetStepsCountPost call the post function with int input func (f *EntRecipeFactory) SetStepsCountPost(i int) *EntRecipeBuilder { builder := &EntRecipeBuilder{mutation: f.meta.mutation, counter: f.counter, factory: f} builder.SetStepsCountPost(i) @@ -576,6 +675,7 @@ func (f *EntRecipeFactory) SetStepsCountPost(i int) *EntRecipeBuilder { return builder } +// SetIngredientsCountPost call the post function with int input func (f *EntRecipeFactory) SetIngredientsCountPost(i int) *EntRecipeBuilder { builder := &EntRecipeBuilder{mutation: f.meta.mutation, counter: f.counter, factory: f} builder.SetIngredientsCountPost(i) @@ -585,6 +685,7 @@ func (f *EntRecipeFactory) SetIngredientsCountPost(i int) *EntRecipeBuilder { return builder } +// WithVeganTrait() enable the Vegan trait func (f *EntRecipeFactory) WithVeganTrait() *EntRecipeBuilder { builder := &EntRecipeBuilder{mutation: f.meta.mutation, counter: f.counter} builder.factory = f @@ -600,6 +701,7 @@ func (f *EntRecipeFactory) WithVeganTrait() *EntRecipeBuilder { return builder } +// WithKetoTrait() enable the Keto trait func (f *EntRecipeFactory) WithKetoTrait() *EntRecipeBuilder { builder := &EntRecipeBuilder{mutation: f.meta.mutation, counter: f.counter} builder.factory = f @@ -615,6 +717,7 @@ func (f *EntRecipeFactory) WithKetoTrait() *EntRecipeBuilder { return builder } +// Create return a new *ent.Recipe func (f *EntRecipeFactory) Create(ctx context.Context) (*ent.Recipe, error) { builder := &EntRecipeBuilder{mutation: f.meta.mutation, counter: f.counter, factory: f} @@ -622,6 +725,8 @@ func (f *EntRecipeFactory) Create(ctx context.Context) (*ent.Recipe, error) { return builder.Create(ctx) } + +// CreateV return a new ent.Recipe func (f *EntRecipeFactory) CreateV(ctx context.Context) (ent.Recipe, error) { builder := &EntRecipeBuilder{mutation: f.meta.mutation, counter: f.counter, factory: f} @@ -629,6 +734,8 @@ func (f *EntRecipeFactory) CreateV(ctx context.Context) (ent.Recipe, error) { return builder.CreateV(ctx) } + +// CreateBatch return a []*ent.Recipe slice func (f *EntRecipeFactory) CreateBatch(ctx context.Context, n int) ([]*ent.Recipe, error) { builder := &EntRecipeBuilder{mutation: f.meta.mutation, counter: f.counter, factory: f} @@ -636,6 +743,8 @@ func (f *EntRecipeFactory) CreateBatch(ctx context.Context, n int) ([]*ent.Recip return builder.CreateBatch(ctx, n) } + +// CreateBatchV return a []ent.Recipe slice func (f *EntRecipeFactory) CreateBatchV(ctx context.Context, n int) ([]ent.Recipe, error) { builder := &EntRecipeBuilder{mutation: f.meta.mutation, counter: f.counter, factory: f} @@ -644,6 +753,7 @@ func (f *EntRecipeFactory) CreateBatchV(ctx context.Context, n int) ([]ent.Recip return builder.CreateBatchV(ctx, n) } +// Client set ent client to EntRecipeFactory func (f *EntRecipeFactory) Client(c *ent.Client) *EntRecipeFactory { f.client = c return f @@ -680,42 +790,49 @@ func (b *EntRecipeBuilder) Client(c *ent.Client) *EntRecipeBuilder { return b } +// SetAuthor set the Author field func (b *EntRecipeBuilder) SetAuthor(i *ent.User) *EntRecipeBuilder { b.authorOverride = i b.authorOverriden = true return b } +// SetAuthorID set the AuthorID field func (b *EntRecipeBuilder) SetAuthorID(i int) *EntRecipeBuilder { b.authorIDOverride = i b.authorIDOverriden = true return b } +// SetName set the Name field func (b *EntRecipeBuilder) SetName(i string) *EntRecipeBuilder { b.nameOverride = i b.nameOverriden = true return b } +// SetServings set the Servings field func (b *EntRecipeBuilder) SetServings(i int) *EntRecipeBuilder { b.servingsOverride = i b.servingsOverriden = true return b } +// SetStepsCountPost call the post function with int input func (b *EntRecipeBuilder) SetStepsCountPost(i int) *EntRecipeBuilder { b._postStepsCount = i b._postStepsCountSet = true return b } +// SetIngredientsCountPost call the post function with int input func (b *EntRecipeBuilder) SetIngredientsCountPost(i int) *EntRecipeBuilder { b._postIngredientsCount = i b._postIngredientsCountSet = true return b } +// WithVeganTrait() enable the Vegan trait func (b *EntRecipeBuilder) WithVeganTrait() *EntRecipeBuilder { if b.factory.meta.veganTrait == nil { return b @@ -726,6 +843,7 @@ func (b *EntRecipeBuilder) WithVeganTrait() *EntRecipeBuilder { return b } +// WithKetoTrait() enable the Keto trait func (b *EntRecipeBuilder) WithKetoTrait() *EntRecipeBuilder { if b.factory.meta.ketoTrait == nil { return b @@ -736,6 +854,7 @@ func (b *EntRecipeBuilder) WithKetoTrait() *EntRecipeBuilder { return b } +// CreateV return a new ent.Recipe func (b *EntRecipeBuilder) CreateV(ctx context.Context) (ent.Recipe, error) { var d ent.Recipe p, err := b.Create(ctx) @@ -745,11 +864,12 @@ func (b *EntRecipeBuilder) CreateV(ctx context.Context) (ent.Recipe, error) { return d, err } +// Create return a new *ent.Recipe func (b *EntRecipeBuilder) Create(ctx context.Context) (*ent.Recipe, error) { - var preSlice = []func(ctx context.Context, i *EntRecipeMutator, c int, creator *ent.RecipeCreate) error{} - var lazySlice = []func(ctx context.Context, i *EntRecipeMutator, c int, creator *ent.RecipeCreate) error{} - var postSlice = []func(ctx context.Context, i *ent.Recipe, c int, creator *ent.RecipeCreate) error{} + var preSlice = []func(ctx context.Context, i *EntRecipeMutator, c int) error{} + var lazySlice = []func(ctx context.Context, i *EntRecipeMutator, c int) error{} + var postSlice = []func(ctx context.Context, i *ent.Recipe, c int) error{} index := b.counter.Get() _ = index @@ -758,10 +878,10 @@ func (b *EntRecipeBuilder) Create(ctx context.Context) (*ent.Recipe, error) { entBuilder := client.Recipe.Create() if b.authorOverriden { - preSlice = append(preSlice, func(ctx context.Context, i *EntRecipeMutator, c int, creator *ent.RecipeCreate) error { + preSlice = append(preSlice, func(ctx context.Context, i *EntRecipeMutator, c int) error { value := b.authorOverride - creator.SetAuthor(value) + i.EntCreator().SetAuthor(value) i.Author = value return nil @@ -780,10 +900,10 @@ func (b *EntRecipeBuilder) Create(ctx context.Context) (*ent.Recipe, error) { } if b.authorIDOverriden { - preSlice = append(preSlice, func(ctx context.Context, i *EntRecipeMutator, c int, creator *ent.RecipeCreate) error { + preSlice = append(preSlice, func(ctx context.Context, i *EntRecipeMutator, c int) error { value := b.authorIDOverride - creator.SetAuthorID(value) + i.EntCreator().SetAuthorID(value) i.AuthorID = value return nil @@ -802,10 +922,10 @@ func (b *EntRecipeBuilder) Create(ctx context.Context) (*ent.Recipe, error) { } if b.nameOverriden { - preSlice = append(preSlice, func(ctx context.Context, i *EntRecipeMutator, c int, creator *ent.RecipeCreate) error { + preSlice = append(preSlice, func(ctx context.Context, i *EntRecipeMutator, c int) error { value := b.nameOverride - creator.SetName(value) + i.EntCreator().SetName(value) i.Name = value return nil @@ -824,10 +944,10 @@ func (b *EntRecipeBuilder) Create(ctx context.Context) (*ent.Recipe, error) { } if b.servingsOverriden { - preSlice = append(preSlice, func(ctx context.Context, i *EntRecipeMutator, c int, creator *ent.RecipeCreate) error { + preSlice = append(preSlice, func(ctx context.Context, i *EntRecipeMutator, c int) error { value := b.servingsOverride - creator.SetServings(value) + i.EntCreator().SetServings(value) i.Servings = value return nil @@ -846,23 +966,26 @@ func (b *EntRecipeBuilder) Create(ctx context.Context) (*ent.Recipe, error) { } if b.mutation._postStepsCountFunc != nil { - postSlice = append(postSlice, func(ctx context.Context, i *ent.Recipe, c int, creator *ent.RecipeCreate) error { + postSlice = append(postSlice, func(ctx context.Context, i *ent.Recipe, c int) error { err := b.mutation._postStepsCountFunc(ctx, b._postStepsCountSet, i, b._postStepsCount) return err }) } if b.mutation._postIngredientsCountFunc != nil { - postSlice = append(postSlice, func(ctx context.Context, i *ent.Recipe, c int, creator *ent.RecipeCreate) error { + postSlice = append(postSlice, func(ctx context.Context, i *ent.Recipe, c int) error { err := b.mutation._postIngredientsCountFunc(ctx, b._postIngredientsCountSet, i, b._postIngredientsCount) return err }) } v := &EntRecipeMutator{} + + v._creator = entBuilder + for _, f := range preSlice { - err := f(ctx, v, index, entBuilder) + err := f(ctx, v, index) if err != nil { return nil, err @@ -870,12 +993,17 @@ func (b *EntRecipeBuilder) Create(ctx context.Context) (*ent.Recipe, error) { } for _, f := range lazySlice { - err := f(ctx, v, index, entBuilder) + err := f(ctx, v, index) if err != nil { return nil, err } } + if b.mutation.beforeCreateFunc != nil { + if err := b.mutation.beforeCreateFunc(ctx, v); err != nil { + return nil, err + } + } new, err := entBuilder.Save(ctx) if err != nil { @@ -889,9 +1017,7 @@ func (b *EntRecipeBuilder) Create(ctx context.Context) (*ent.Recipe, error) { } } for _, f := range postSlice { - - err := f(ctx, new, index, entBuilder) - + err := f(ctx, new, index) if err != nil { return nil, err } diff --git a/examples/ent_recipe/carrier/factory/ent_recipeingredient.go b/examples/ent_recipe/carrier/factory/ent_recipeingredient.go index 19c2a5b..33b8441 100644 --- a/examples/ent_recipe/carrier/factory/ent_recipeingredient.go +++ b/examples/ent_recipe/carrier/factory/ent_recipeingredient.go @@ -15,22 +15,29 @@ type EntRecipeIngredientMutator struct { Quantity float32 Unit string + + _creator *ent.RecipeIngredientCreate +} + +func (m *EntRecipeIngredientMutator) EntCreator() *ent.RecipeIngredientCreate { + return m._creator } type entRecipeIngredientMutation struct { ingredientType int - ingredientFunc func(ctx context.Context, i *EntRecipeIngredientMutator, c int, creator *ent.RecipeIngredientCreate) error + ingredientFunc func(ctx context.Context, i *EntRecipeIngredientMutator, c int) error ingredientIDType int - ingredientIDFunc func(ctx context.Context, i *EntRecipeIngredientMutator, c int, creator *ent.RecipeIngredientCreate) error + ingredientIDFunc func(ctx context.Context, i *EntRecipeIngredientMutator, c int) error quantityType int - quantityFunc func(ctx context.Context, i *EntRecipeIngredientMutator, c int, creator *ent.RecipeIngredientCreate) error + quantityFunc func(ctx context.Context, i *EntRecipeIngredientMutator, c int) error unitType int - unitFunc func(ctx context.Context, i *EntRecipeIngredientMutator, c int, creator *ent.RecipeIngredientCreate) error + unitFunc func(ctx context.Context, i *EntRecipeIngredientMutator, c int) error - afterCreateFunc func(ctx context.Context, i *ent.RecipeIngredient) error + beforeCreateFunc func(ctx context.Context, i *EntRecipeIngredientMutator) error + afterCreateFunc func(ctx context.Context, i *ent.RecipeIngredient) error } type EntRecipeIngredientMetaFactory struct { mutation entRecipeIngredientMutation @@ -43,6 +50,11 @@ type entRecipeIngredientTrait struct { func EntRecipeIngredientTrait() *entRecipeIngredientTrait { return &entRecipeIngredientTrait{} } +func (*entRecipeIngredientMutation) beforeCreateMutateFunc(fn func(ctx context.Context, i *EntRecipeIngredientMutator) error) func(m *entRecipeIngredientMutation) { + return func(m *entRecipeIngredientMutation) { + m.beforeCreateFunc = fn + } +} func (*entRecipeIngredientMutation) afterCreateMutateFunc(fn func(ctx context.Context, i *ent.RecipeIngredient) error) func(m *entRecipeIngredientMutation) { return func(m *entRecipeIngredientMutation) { m.afterCreateFunc = fn @@ -52,7 +64,7 @@ func (*entRecipeIngredientMutation) afterCreateMutateFunc(fn func(ctx context.Co func (*entRecipeIngredientMutation) ingredientSequenceMutateFunc(fn func(ctx context.Context, i int) (*ent.Ingredient, error)) func(m *entRecipeIngredientMutation) { return func(m *entRecipeIngredientMutation) { m.ingredientType = TypeSequence - m.ingredientFunc = func(ctx context.Context, i *EntRecipeIngredientMutator, c int, creator *ent.RecipeIngredientCreate) error { + m.ingredientFunc = func(ctx context.Context, i *EntRecipeIngredientMutator, c int) error { if fn == nil { return nil } @@ -61,7 +73,7 @@ func (*entRecipeIngredientMutation) ingredientSequenceMutateFunc(fn func(ctx con return err } - creator.SetIngredient(value) + i.EntCreator().SetIngredient(value) i.Ingredient = value return nil @@ -71,7 +83,7 @@ func (*entRecipeIngredientMutation) ingredientSequenceMutateFunc(fn func(ctx con func (*entRecipeIngredientMutation) ingredientLazyMutateFunc(fn func(ctx context.Context, i *EntRecipeIngredientMutator) (*ent.Ingredient, error)) func(m *entRecipeIngredientMutation) { return func(m *entRecipeIngredientMutation) { m.ingredientType = TypeLazy - m.ingredientFunc = func(ctx context.Context, i *EntRecipeIngredientMutator, c int, creator *ent.RecipeIngredientCreate) error { + m.ingredientFunc = func(ctx context.Context, i *EntRecipeIngredientMutator, c int) error { if fn == nil { return nil } @@ -80,7 +92,7 @@ func (*entRecipeIngredientMutation) ingredientLazyMutateFunc(fn func(ctx context return err } - creator.SetIngredient(value) + i.EntCreator().SetIngredient(value) i.Ingredient = value return nil @@ -90,9 +102,9 @@ func (*entRecipeIngredientMutation) ingredientLazyMutateFunc(fn func(ctx context func (*entRecipeIngredientMutation) ingredientDefaultMutateFunc(v *ent.Ingredient) func(m *entRecipeIngredientMutation) { return func(m *entRecipeIngredientMutation) { m.ingredientType = TypeDefault - m.ingredientFunc = func(ctx context.Context, i *EntRecipeIngredientMutator, c int, creator *ent.RecipeIngredientCreate) error { + m.ingredientFunc = func(ctx context.Context, i *EntRecipeIngredientMutator, c int) error { - creator.SetIngredient(v) + i.EntCreator().SetIngredient(v) i.Ingredient = v return nil @@ -102,7 +114,7 @@ func (*entRecipeIngredientMutation) ingredientDefaultMutateFunc(v *ent.Ingredien func (*entRecipeIngredientMutation) ingredientFactoryMutateFunc(fn func(ctx context.Context) (*ent.Ingredient, error)) func(m *entRecipeIngredientMutation) { return func(m *entRecipeIngredientMutation) { m.ingredientType = TypeFactory - m.ingredientFunc = func(ctx context.Context, i *EntRecipeIngredientMutator, c int, creator *ent.RecipeIngredientCreate) error { + m.ingredientFunc = func(ctx context.Context, i *EntRecipeIngredientMutator, c int) error { if fn == nil { return nil } @@ -111,7 +123,7 @@ func (*entRecipeIngredientMutation) ingredientFactoryMutateFunc(fn func(ctx cont return err } - creator.SetIngredient(value) + i.EntCreator().SetIngredient(value) i.Ingredient = value @@ -120,34 +132,49 @@ func (*entRecipeIngredientMutation) ingredientFactoryMutateFunc(fn func(ctx cont } } +// SetIngredientSequence register a function which accept a sequence counter and set return value to Ingredient field func (f *EntRecipeIngredientMetaFactory) SetIngredientSequence(fn func(ctx context.Context, i int) (*ent.Ingredient, error)) *EntRecipeIngredientMetaFactory { f.mutation.ingredientSequenceMutateFunc(fn)(&f.mutation) return f } + +// SetIngredientLazy register a function which accept the build struct and set return value to Ingredient field func (f *EntRecipeIngredientMetaFactory) SetIngredientLazy(fn func(ctx context.Context, i *EntRecipeIngredientMutator) (*ent.Ingredient, error)) *EntRecipeIngredientMetaFactory { f.mutation.ingredientLazyMutateFunc(fn)(&f.mutation) return f } + +// SetIngredientDefault assign a default value to Ingredient field func (f *EntRecipeIngredientMetaFactory) SetIngredientDefault(v *ent.Ingredient) *EntRecipeIngredientMetaFactory { f.mutation.ingredientDefaultMutateFunc(v)(&f.mutation) return f } + +// SetIngredientFactory register a factory function and assign return value to Ingredient, you can also use related factory's Create/CreateV as input function here func (f *EntRecipeIngredientMetaFactory) SetIngredientFactory(fn func(ctx context.Context) (*ent.Ingredient, error)) *EntRecipeIngredientMetaFactory { f.mutation.ingredientFactoryMutateFunc(fn)(&f.mutation) return f } + +// SetIngredientSequence register a function which accept a sequence counter and set return value to Ingredient field func (t *entRecipeIngredientTrait) SetIngredientSequence(fn func(ctx context.Context, i int) (*ent.Ingredient, error)) *entRecipeIngredientTrait { t.updates = append(t.updates, t.mutation.ingredientSequenceMutateFunc(fn)) return t } + +// SetIngredientLazy register a function which accept the build struct and set return value to Ingredient field func (t *entRecipeIngredientTrait) SetIngredientLazy(fn func(ctx context.Context, i *EntRecipeIngredientMutator) (*ent.Ingredient, error)) *entRecipeIngredientTrait { t.updates = append(t.updates, t.mutation.ingredientLazyMutateFunc(fn)) return t } + +// SetIngredientDefault assign a default value to Ingredient field func (t *entRecipeIngredientTrait) SetIngredientDefault(v *ent.Ingredient) *entRecipeIngredientTrait { t.updates = append(t.updates, t.mutation.ingredientDefaultMutateFunc(v)) return t } + +// SetIngredientFactory register a factory function and assign return value to Ingredient, you can also use related factory's Create/CreateV as input function here func (t *entRecipeIngredientTrait) SetIngredientFactory(fn func(ctx context.Context) (*ent.Ingredient, error)) *entRecipeIngredientTrait { t.updates = append(t.updates, t.mutation.ingredientFactoryMutateFunc(fn)) return t @@ -156,7 +183,7 @@ func (t *entRecipeIngredientTrait) SetIngredientFactory(fn func(ctx context.Cont func (*entRecipeIngredientMutation) ingredientIDSequenceMutateFunc(fn func(ctx context.Context, i int) (int, error)) func(m *entRecipeIngredientMutation) { return func(m *entRecipeIngredientMutation) { m.ingredientIDType = TypeSequence - m.ingredientIDFunc = func(ctx context.Context, i *EntRecipeIngredientMutator, c int, creator *ent.RecipeIngredientCreate) error { + m.ingredientIDFunc = func(ctx context.Context, i *EntRecipeIngredientMutator, c int) error { if fn == nil { return nil } @@ -165,7 +192,7 @@ func (*entRecipeIngredientMutation) ingredientIDSequenceMutateFunc(fn func(ctx c return err } - creator.SetIngredientID(value) + i.EntCreator().SetIngredientID(value) i.IngredientID = value return nil @@ -175,7 +202,7 @@ func (*entRecipeIngredientMutation) ingredientIDSequenceMutateFunc(fn func(ctx c func (*entRecipeIngredientMutation) ingredientIDLazyMutateFunc(fn func(ctx context.Context, i *EntRecipeIngredientMutator) (int, error)) func(m *entRecipeIngredientMutation) { return func(m *entRecipeIngredientMutation) { m.ingredientIDType = TypeLazy - m.ingredientIDFunc = func(ctx context.Context, i *EntRecipeIngredientMutator, c int, creator *ent.RecipeIngredientCreate) error { + m.ingredientIDFunc = func(ctx context.Context, i *EntRecipeIngredientMutator, c int) error { if fn == nil { return nil } @@ -184,7 +211,7 @@ func (*entRecipeIngredientMutation) ingredientIDLazyMutateFunc(fn func(ctx conte return err } - creator.SetIngredientID(value) + i.EntCreator().SetIngredientID(value) i.IngredientID = value return nil @@ -194,9 +221,9 @@ func (*entRecipeIngredientMutation) ingredientIDLazyMutateFunc(fn func(ctx conte func (*entRecipeIngredientMutation) ingredientIDDefaultMutateFunc(v int) func(m *entRecipeIngredientMutation) { return func(m *entRecipeIngredientMutation) { m.ingredientIDType = TypeDefault - m.ingredientIDFunc = func(ctx context.Context, i *EntRecipeIngredientMutator, c int, creator *ent.RecipeIngredientCreate) error { + m.ingredientIDFunc = func(ctx context.Context, i *EntRecipeIngredientMutator, c int) error { - creator.SetIngredientID(v) + i.EntCreator().SetIngredientID(v) i.IngredientID = v return nil @@ -206,7 +233,7 @@ func (*entRecipeIngredientMutation) ingredientIDDefaultMutateFunc(v int) func(m func (*entRecipeIngredientMutation) ingredientIDFactoryMutateFunc(fn func(ctx context.Context) (int, error)) func(m *entRecipeIngredientMutation) { return func(m *entRecipeIngredientMutation) { m.ingredientIDType = TypeFactory - m.ingredientIDFunc = func(ctx context.Context, i *EntRecipeIngredientMutator, c int, creator *ent.RecipeIngredientCreate) error { + m.ingredientIDFunc = func(ctx context.Context, i *EntRecipeIngredientMutator, c int) error { if fn == nil { return nil } @@ -215,7 +242,7 @@ func (*entRecipeIngredientMutation) ingredientIDFactoryMutateFunc(fn func(ctx co return err } - creator.SetIngredientID(value) + i.EntCreator().SetIngredientID(value) i.IngredientID = value @@ -224,34 +251,49 @@ func (*entRecipeIngredientMutation) ingredientIDFactoryMutateFunc(fn func(ctx co } } +// SetIngredientIDSequence register a function which accept a sequence counter and set return value to IngredientID field func (f *EntRecipeIngredientMetaFactory) SetIngredientIDSequence(fn func(ctx context.Context, i int) (int, error)) *EntRecipeIngredientMetaFactory { f.mutation.ingredientIDSequenceMutateFunc(fn)(&f.mutation) return f } + +// SetIngredientIDLazy register a function which accept the build struct and set return value to IngredientID field func (f *EntRecipeIngredientMetaFactory) SetIngredientIDLazy(fn func(ctx context.Context, i *EntRecipeIngredientMutator) (int, error)) *EntRecipeIngredientMetaFactory { f.mutation.ingredientIDLazyMutateFunc(fn)(&f.mutation) return f } + +// SetIngredientIDDefault assign a default value to IngredientID field func (f *EntRecipeIngredientMetaFactory) SetIngredientIDDefault(v int) *EntRecipeIngredientMetaFactory { f.mutation.ingredientIDDefaultMutateFunc(v)(&f.mutation) return f } + +// SetIngredientIDFactory register a factory function and assign return value to IngredientID, you can also use related factory's Create/CreateV as input function here func (f *EntRecipeIngredientMetaFactory) SetIngredientIDFactory(fn func(ctx context.Context) (int, error)) *EntRecipeIngredientMetaFactory { f.mutation.ingredientIDFactoryMutateFunc(fn)(&f.mutation) return f } + +// SetIngredientIDSequence register a function which accept a sequence counter and set return value to IngredientID field func (t *entRecipeIngredientTrait) SetIngredientIDSequence(fn func(ctx context.Context, i int) (int, error)) *entRecipeIngredientTrait { t.updates = append(t.updates, t.mutation.ingredientIDSequenceMutateFunc(fn)) return t } + +// SetIngredientIDLazy register a function which accept the build struct and set return value to IngredientID field func (t *entRecipeIngredientTrait) SetIngredientIDLazy(fn func(ctx context.Context, i *EntRecipeIngredientMutator) (int, error)) *entRecipeIngredientTrait { t.updates = append(t.updates, t.mutation.ingredientIDLazyMutateFunc(fn)) return t } + +// SetIngredientIDDefault assign a default value to IngredientID field func (t *entRecipeIngredientTrait) SetIngredientIDDefault(v int) *entRecipeIngredientTrait { t.updates = append(t.updates, t.mutation.ingredientIDDefaultMutateFunc(v)) return t } + +// SetIngredientIDFactory register a factory function and assign return value to IngredientID, you can also use related factory's Create/CreateV as input function here func (t *entRecipeIngredientTrait) SetIngredientIDFactory(fn func(ctx context.Context) (int, error)) *entRecipeIngredientTrait { t.updates = append(t.updates, t.mutation.ingredientIDFactoryMutateFunc(fn)) return t @@ -260,7 +302,7 @@ func (t *entRecipeIngredientTrait) SetIngredientIDFactory(fn func(ctx context.Co func (*entRecipeIngredientMutation) quantitySequenceMutateFunc(fn func(ctx context.Context, i int) (float32, error)) func(m *entRecipeIngredientMutation) { return func(m *entRecipeIngredientMutation) { m.quantityType = TypeSequence - m.quantityFunc = func(ctx context.Context, i *EntRecipeIngredientMutator, c int, creator *ent.RecipeIngredientCreate) error { + m.quantityFunc = func(ctx context.Context, i *EntRecipeIngredientMutator, c int) error { if fn == nil { return nil } @@ -269,7 +311,7 @@ func (*entRecipeIngredientMutation) quantitySequenceMutateFunc(fn func(ctx conte return err } - creator.SetQuantity(value) + i.EntCreator().SetQuantity(value) i.Quantity = value return nil @@ -279,7 +321,7 @@ func (*entRecipeIngredientMutation) quantitySequenceMutateFunc(fn func(ctx conte func (*entRecipeIngredientMutation) quantityLazyMutateFunc(fn func(ctx context.Context, i *EntRecipeIngredientMutator) (float32, error)) func(m *entRecipeIngredientMutation) { return func(m *entRecipeIngredientMutation) { m.quantityType = TypeLazy - m.quantityFunc = func(ctx context.Context, i *EntRecipeIngredientMutator, c int, creator *ent.RecipeIngredientCreate) error { + m.quantityFunc = func(ctx context.Context, i *EntRecipeIngredientMutator, c int) error { if fn == nil { return nil } @@ -288,7 +330,7 @@ func (*entRecipeIngredientMutation) quantityLazyMutateFunc(fn func(ctx context.C return err } - creator.SetQuantity(value) + i.EntCreator().SetQuantity(value) i.Quantity = value return nil @@ -298,9 +340,9 @@ func (*entRecipeIngredientMutation) quantityLazyMutateFunc(fn func(ctx context.C func (*entRecipeIngredientMutation) quantityDefaultMutateFunc(v float32) func(m *entRecipeIngredientMutation) { return func(m *entRecipeIngredientMutation) { m.quantityType = TypeDefault - m.quantityFunc = func(ctx context.Context, i *EntRecipeIngredientMutator, c int, creator *ent.RecipeIngredientCreate) error { + m.quantityFunc = func(ctx context.Context, i *EntRecipeIngredientMutator, c int) error { - creator.SetQuantity(v) + i.EntCreator().SetQuantity(v) i.Quantity = v return nil @@ -310,7 +352,7 @@ func (*entRecipeIngredientMutation) quantityDefaultMutateFunc(v float32) func(m func (*entRecipeIngredientMutation) quantityFactoryMutateFunc(fn func(ctx context.Context) (float32, error)) func(m *entRecipeIngredientMutation) { return func(m *entRecipeIngredientMutation) { m.quantityType = TypeFactory - m.quantityFunc = func(ctx context.Context, i *EntRecipeIngredientMutator, c int, creator *ent.RecipeIngredientCreate) error { + m.quantityFunc = func(ctx context.Context, i *EntRecipeIngredientMutator, c int) error { if fn == nil { return nil } @@ -319,7 +361,7 @@ func (*entRecipeIngredientMutation) quantityFactoryMutateFunc(fn func(ctx contex return err } - creator.SetQuantity(value) + i.EntCreator().SetQuantity(value) i.Quantity = value @@ -328,34 +370,49 @@ func (*entRecipeIngredientMutation) quantityFactoryMutateFunc(fn func(ctx contex } } +// SetQuantitySequence register a function which accept a sequence counter and set return value to Quantity field func (f *EntRecipeIngredientMetaFactory) SetQuantitySequence(fn func(ctx context.Context, i int) (float32, error)) *EntRecipeIngredientMetaFactory { f.mutation.quantitySequenceMutateFunc(fn)(&f.mutation) return f } + +// SetQuantityLazy register a function which accept the build struct and set return value to Quantity field func (f *EntRecipeIngredientMetaFactory) SetQuantityLazy(fn func(ctx context.Context, i *EntRecipeIngredientMutator) (float32, error)) *EntRecipeIngredientMetaFactory { f.mutation.quantityLazyMutateFunc(fn)(&f.mutation) return f } + +// SetQuantityDefault assign a default value to Quantity field func (f *EntRecipeIngredientMetaFactory) SetQuantityDefault(v float32) *EntRecipeIngredientMetaFactory { f.mutation.quantityDefaultMutateFunc(v)(&f.mutation) return f } + +// SetQuantityFactory register a factory function and assign return value to Quantity, you can also use related factory's Create/CreateV as input function here func (f *EntRecipeIngredientMetaFactory) SetQuantityFactory(fn func(ctx context.Context) (float32, error)) *EntRecipeIngredientMetaFactory { f.mutation.quantityFactoryMutateFunc(fn)(&f.mutation) return f } + +// SetQuantitySequence register a function which accept a sequence counter and set return value to Quantity field func (t *entRecipeIngredientTrait) SetQuantitySequence(fn func(ctx context.Context, i int) (float32, error)) *entRecipeIngredientTrait { t.updates = append(t.updates, t.mutation.quantitySequenceMutateFunc(fn)) return t } + +// SetQuantityLazy register a function which accept the build struct and set return value to Quantity field func (t *entRecipeIngredientTrait) SetQuantityLazy(fn func(ctx context.Context, i *EntRecipeIngredientMutator) (float32, error)) *entRecipeIngredientTrait { t.updates = append(t.updates, t.mutation.quantityLazyMutateFunc(fn)) return t } + +// SetQuantityDefault assign a default value to Quantity field func (t *entRecipeIngredientTrait) SetQuantityDefault(v float32) *entRecipeIngredientTrait { t.updates = append(t.updates, t.mutation.quantityDefaultMutateFunc(v)) return t } + +// SetQuantityFactory register a factory function and assign return value to Quantity, you can also use related factory's Create/CreateV as input function here func (t *entRecipeIngredientTrait) SetQuantityFactory(fn func(ctx context.Context) (float32, error)) *entRecipeIngredientTrait { t.updates = append(t.updates, t.mutation.quantityFactoryMutateFunc(fn)) return t @@ -364,7 +421,7 @@ func (t *entRecipeIngredientTrait) SetQuantityFactory(fn func(ctx context.Contex func (*entRecipeIngredientMutation) unitSequenceMutateFunc(fn func(ctx context.Context, i int) (string, error)) func(m *entRecipeIngredientMutation) { return func(m *entRecipeIngredientMutation) { m.unitType = TypeSequence - m.unitFunc = func(ctx context.Context, i *EntRecipeIngredientMutator, c int, creator *ent.RecipeIngredientCreate) error { + m.unitFunc = func(ctx context.Context, i *EntRecipeIngredientMutator, c int) error { if fn == nil { return nil } @@ -373,7 +430,7 @@ func (*entRecipeIngredientMutation) unitSequenceMutateFunc(fn func(ctx context.C return err } - creator.SetUnit(value) + i.EntCreator().SetUnit(value) i.Unit = value return nil @@ -383,7 +440,7 @@ func (*entRecipeIngredientMutation) unitSequenceMutateFunc(fn func(ctx context.C func (*entRecipeIngredientMutation) unitLazyMutateFunc(fn func(ctx context.Context, i *EntRecipeIngredientMutator) (string, error)) func(m *entRecipeIngredientMutation) { return func(m *entRecipeIngredientMutation) { m.unitType = TypeLazy - m.unitFunc = func(ctx context.Context, i *EntRecipeIngredientMutator, c int, creator *ent.RecipeIngredientCreate) error { + m.unitFunc = func(ctx context.Context, i *EntRecipeIngredientMutator, c int) error { if fn == nil { return nil } @@ -392,7 +449,7 @@ func (*entRecipeIngredientMutation) unitLazyMutateFunc(fn func(ctx context.Conte return err } - creator.SetUnit(value) + i.EntCreator().SetUnit(value) i.Unit = value return nil @@ -402,9 +459,9 @@ func (*entRecipeIngredientMutation) unitLazyMutateFunc(fn func(ctx context.Conte func (*entRecipeIngredientMutation) unitDefaultMutateFunc(v string) func(m *entRecipeIngredientMutation) { return func(m *entRecipeIngredientMutation) { m.unitType = TypeDefault - m.unitFunc = func(ctx context.Context, i *EntRecipeIngredientMutator, c int, creator *ent.RecipeIngredientCreate) error { + m.unitFunc = func(ctx context.Context, i *EntRecipeIngredientMutator, c int) error { - creator.SetUnit(v) + i.EntCreator().SetUnit(v) i.Unit = v return nil @@ -414,7 +471,7 @@ func (*entRecipeIngredientMutation) unitDefaultMutateFunc(v string) func(m *entR func (*entRecipeIngredientMutation) unitFactoryMutateFunc(fn func(ctx context.Context) (string, error)) func(m *entRecipeIngredientMutation) { return func(m *entRecipeIngredientMutation) { m.unitType = TypeFactory - m.unitFunc = func(ctx context.Context, i *EntRecipeIngredientMutator, c int, creator *ent.RecipeIngredientCreate) error { + m.unitFunc = func(ctx context.Context, i *EntRecipeIngredientMutator, c int) error { if fn == nil { return nil } @@ -423,7 +480,7 @@ func (*entRecipeIngredientMutation) unitFactoryMutateFunc(fn func(ctx context.Co return err } - creator.SetUnit(value) + i.EntCreator().SetUnit(value) i.Unit = value @@ -432,48 +489,79 @@ func (*entRecipeIngredientMutation) unitFactoryMutateFunc(fn func(ctx context.Co } } +// SetUnitSequence register a function which accept a sequence counter and set return value to Unit field func (f *EntRecipeIngredientMetaFactory) SetUnitSequence(fn func(ctx context.Context, i int) (string, error)) *EntRecipeIngredientMetaFactory { f.mutation.unitSequenceMutateFunc(fn)(&f.mutation) return f } + +// SetUnitLazy register a function which accept the build struct and set return value to Unit field func (f *EntRecipeIngredientMetaFactory) SetUnitLazy(fn func(ctx context.Context, i *EntRecipeIngredientMutator) (string, error)) *EntRecipeIngredientMetaFactory { f.mutation.unitLazyMutateFunc(fn)(&f.mutation) return f } + +// SetUnitDefault assign a default value to Unit field func (f *EntRecipeIngredientMetaFactory) SetUnitDefault(v string) *EntRecipeIngredientMetaFactory { f.mutation.unitDefaultMutateFunc(v)(&f.mutation) return f } + +// SetUnitFactory register a factory function and assign return value to Unit, you can also use related factory's Create/CreateV as input function here func (f *EntRecipeIngredientMetaFactory) SetUnitFactory(fn func(ctx context.Context) (string, error)) *EntRecipeIngredientMetaFactory { f.mutation.unitFactoryMutateFunc(fn)(&f.mutation) return f } + +// SetUnitSequence register a function which accept a sequence counter and set return value to Unit field func (t *entRecipeIngredientTrait) SetUnitSequence(fn func(ctx context.Context, i int) (string, error)) *entRecipeIngredientTrait { t.updates = append(t.updates, t.mutation.unitSequenceMutateFunc(fn)) return t } + +// SetUnitLazy register a function which accept the build struct and set return value to Unit field func (t *entRecipeIngredientTrait) SetUnitLazy(fn func(ctx context.Context, i *EntRecipeIngredientMutator) (string, error)) *entRecipeIngredientTrait { t.updates = append(t.updates, t.mutation.unitLazyMutateFunc(fn)) return t } + +// SetUnitDefault assign a default value to Unit field func (t *entRecipeIngredientTrait) SetUnitDefault(v string) *entRecipeIngredientTrait { t.updates = append(t.updates, t.mutation.unitDefaultMutateFunc(v)) return t } + +// SetUnitFactory register a factory function and assign return value to Unit, you can also use related factory's Create/CreateV as input function here func (t *entRecipeIngredientTrait) SetUnitFactory(fn func(ctx context.Context) (string, error)) *entRecipeIngredientTrait { t.updates = append(t.updates, t.mutation.unitFactoryMutateFunc(fn)) return t } +// SetAfterCreateFunc register a function to be called after struct create func (f *EntRecipeIngredientMetaFactory) SetAfterCreateFunc(fn func(ctx context.Context, i *ent.RecipeIngredient) error) *EntRecipeIngredientMetaFactory { f.mutation.afterCreateFunc = fn return f } + +// SetBeforeCreateFunc register a function to be called before struct create +func (f *EntRecipeIngredientMetaFactory) SetBeforeCreateFunc(fn func(ctx context.Context, i *EntRecipeIngredientMutator) error) *EntRecipeIngredientMetaFactory { + f.mutation.beforeCreateFunc = fn + return f +} + +// SetAfterCreateFunc register a function to be called after struct create func (t *entRecipeIngredientTrait) SetAfterCreateFunc(fn func(ctx context.Context, i *ent.RecipeIngredient) error) *entRecipeIngredientTrait { t.updates = append(t.updates, t.mutation.afterCreateMutateFunc(fn)) return t } +// SetBeforeCreateFunc register a function to be called before struct create +func (t *entRecipeIngredientTrait) SetBeforeCreateFunc(fn func(ctx context.Context, i *EntRecipeIngredientMutator) error) *entRecipeIngredientTrait { + t.updates = append(t.updates, t.mutation.beforeCreateMutateFunc(fn)) + return t +} + +// Build create a EntRecipeIngredientFactory from EntRecipeIngredientMetaFactory func (f *EntRecipeIngredientMetaFactory) Build() *EntRecipeIngredientFactory { return &EntRecipeIngredientFactory{meta: *f, counter: &Counter{}} } @@ -485,6 +573,7 @@ type EntRecipeIngredientFactory struct { client *ent.Client } +// SetIngredient set the Ingredient field func (f *EntRecipeIngredientFactory) SetIngredient(i *ent.Ingredient) *EntRecipeIngredientBuilder { builder := &EntRecipeIngredientBuilder{mutation: f.meta.mutation, counter: f.counter, factory: f} builder.SetIngredient(i) @@ -494,6 +583,7 @@ func (f *EntRecipeIngredientFactory) SetIngredient(i *ent.Ingredient) *EntRecipe return builder } +// SetIngredientID set the IngredientID field func (f *EntRecipeIngredientFactory) SetIngredientID(i int) *EntRecipeIngredientBuilder { builder := &EntRecipeIngredientBuilder{mutation: f.meta.mutation, counter: f.counter, factory: f} builder.SetIngredientID(i) @@ -503,6 +593,7 @@ func (f *EntRecipeIngredientFactory) SetIngredientID(i int) *EntRecipeIngredient return builder } +// SetQuantity set the Quantity field func (f *EntRecipeIngredientFactory) SetQuantity(i float32) *EntRecipeIngredientBuilder { builder := &EntRecipeIngredientBuilder{mutation: f.meta.mutation, counter: f.counter, factory: f} builder.SetQuantity(i) @@ -512,6 +603,7 @@ func (f *EntRecipeIngredientFactory) SetQuantity(i float32) *EntRecipeIngredient return builder } +// SetUnit set the Unit field func (f *EntRecipeIngredientFactory) SetUnit(i string) *EntRecipeIngredientBuilder { builder := &EntRecipeIngredientBuilder{mutation: f.meta.mutation, counter: f.counter, factory: f} builder.SetUnit(i) @@ -521,6 +613,7 @@ func (f *EntRecipeIngredientFactory) SetUnit(i string) *EntRecipeIngredientBuild return builder } +// Create return a new *ent.RecipeIngredient func (f *EntRecipeIngredientFactory) Create(ctx context.Context) (*ent.RecipeIngredient, error) { builder := &EntRecipeIngredientBuilder{mutation: f.meta.mutation, counter: f.counter, factory: f} @@ -528,6 +621,8 @@ func (f *EntRecipeIngredientFactory) Create(ctx context.Context) (*ent.RecipeIng return builder.Create(ctx) } + +// CreateV return a new ent.RecipeIngredient func (f *EntRecipeIngredientFactory) CreateV(ctx context.Context) (ent.RecipeIngredient, error) { builder := &EntRecipeIngredientBuilder{mutation: f.meta.mutation, counter: f.counter, factory: f} @@ -535,6 +630,8 @@ func (f *EntRecipeIngredientFactory) CreateV(ctx context.Context) (ent.RecipeIng return builder.CreateV(ctx) } + +// CreateBatch return a []*ent.RecipeIngredient slice func (f *EntRecipeIngredientFactory) CreateBatch(ctx context.Context, n int) ([]*ent.RecipeIngredient, error) { builder := &EntRecipeIngredientBuilder{mutation: f.meta.mutation, counter: f.counter, factory: f} @@ -542,6 +639,8 @@ func (f *EntRecipeIngredientFactory) CreateBatch(ctx context.Context, n int) ([] return builder.CreateBatch(ctx, n) } + +// CreateBatchV return a []ent.RecipeIngredient slice func (f *EntRecipeIngredientFactory) CreateBatchV(ctx context.Context, n int) ([]ent.RecipeIngredient, error) { builder := &EntRecipeIngredientBuilder{mutation: f.meta.mutation, counter: f.counter, factory: f} @@ -550,6 +649,7 @@ func (f *EntRecipeIngredientFactory) CreateBatchV(ctx context.Context, n int) ([ return builder.CreateBatchV(ctx, n) } +// Client set ent client to EntRecipeIngredientFactory func (f *EntRecipeIngredientFactory) Client(c *ent.Client) *EntRecipeIngredientFactory { f.client = c return f @@ -580,30 +680,35 @@ func (b *EntRecipeIngredientBuilder) Client(c *ent.Client) *EntRecipeIngredientB return b } +// SetIngredient set the Ingredient field func (b *EntRecipeIngredientBuilder) SetIngredient(i *ent.Ingredient) *EntRecipeIngredientBuilder { b.ingredientOverride = i b.ingredientOverriden = true return b } +// SetIngredientID set the IngredientID field func (b *EntRecipeIngredientBuilder) SetIngredientID(i int) *EntRecipeIngredientBuilder { b.ingredientIDOverride = i b.ingredientIDOverriden = true return b } +// SetQuantity set the Quantity field func (b *EntRecipeIngredientBuilder) SetQuantity(i float32) *EntRecipeIngredientBuilder { b.quantityOverride = i b.quantityOverriden = true return b } +// SetUnit set the Unit field func (b *EntRecipeIngredientBuilder) SetUnit(i string) *EntRecipeIngredientBuilder { b.unitOverride = i b.unitOverriden = true return b } +// CreateV return a new ent.RecipeIngredient func (b *EntRecipeIngredientBuilder) CreateV(ctx context.Context) (ent.RecipeIngredient, error) { var d ent.RecipeIngredient p, err := b.Create(ctx) @@ -613,11 +718,12 @@ func (b *EntRecipeIngredientBuilder) CreateV(ctx context.Context) (ent.RecipeIng return d, err } +// Create return a new *ent.RecipeIngredient func (b *EntRecipeIngredientBuilder) Create(ctx context.Context) (*ent.RecipeIngredient, error) { - var preSlice = []func(ctx context.Context, i *EntRecipeIngredientMutator, c int, creator *ent.RecipeIngredientCreate) error{} - var lazySlice = []func(ctx context.Context, i *EntRecipeIngredientMutator, c int, creator *ent.RecipeIngredientCreate) error{} - var postSlice = []func(ctx context.Context, i *ent.RecipeIngredient, c int, creator *ent.RecipeIngredientCreate) error{} + var preSlice = []func(ctx context.Context, i *EntRecipeIngredientMutator, c int) error{} + var lazySlice = []func(ctx context.Context, i *EntRecipeIngredientMutator, c int) error{} + var postSlice = []func(ctx context.Context, i *ent.RecipeIngredient, c int) error{} index := b.counter.Get() _ = index @@ -626,10 +732,10 @@ func (b *EntRecipeIngredientBuilder) Create(ctx context.Context) (*ent.RecipeIng entBuilder := client.RecipeIngredient.Create() if b.ingredientOverriden { - preSlice = append(preSlice, func(ctx context.Context, i *EntRecipeIngredientMutator, c int, creator *ent.RecipeIngredientCreate) error { + preSlice = append(preSlice, func(ctx context.Context, i *EntRecipeIngredientMutator, c int) error { value := b.ingredientOverride - creator.SetIngredient(value) + i.EntCreator().SetIngredient(value) i.Ingredient = value return nil @@ -648,10 +754,10 @@ func (b *EntRecipeIngredientBuilder) Create(ctx context.Context) (*ent.RecipeIng } if b.ingredientIDOverriden { - preSlice = append(preSlice, func(ctx context.Context, i *EntRecipeIngredientMutator, c int, creator *ent.RecipeIngredientCreate) error { + preSlice = append(preSlice, func(ctx context.Context, i *EntRecipeIngredientMutator, c int) error { value := b.ingredientIDOverride - creator.SetIngredientID(value) + i.EntCreator().SetIngredientID(value) i.IngredientID = value return nil @@ -670,10 +776,10 @@ func (b *EntRecipeIngredientBuilder) Create(ctx context.Context) (*ent.RecipeIng } if b.quantityOverriden { - preSlice = append(preSlice, func(ctx context.Context, i *EntRecipeIngredientMutator, c int, creator *ent.RecipeIngredientCreate) error { + preSlice = append(preSlice, func(ctx context.Context, i *EntRecipeIngredientMutator, c int) error { value := b.quantityOverride - creator.SetQuantity(value) + i.EntCreator().SetQuantity(value) i.Quantity = value return nil @@ -692,10 +798,10 @@ func (b *EntRecipeIngredientBuilder) Create(ctx context.Context) (*ent.RecipeIng } if b.unitOverriden { - preSlice = append(preSlice, func(ctx context.Context, i *EntRecipeIngredientMutator, c int, creator *ent.RecipeIngredientCreate) error { + preSlice = append(preSlice, func(ctx context.Context, i *EntRecipeIngredientMutator, c int) error { value := b.unitOverride - creator.SetUnit(value) + i.EntCreator().SetUnit(value) i.Unit = value return nil @@ -714,9 +820,12 @@ func (b *EntRecipeIngredientBuilder) Create(ctx context.Context) (*ent.RecipeIng } v := &EntRecipeIngredientMutator{} + + v._creator = entBuilder + for _, f := range preSlice { - err := f(ctx, v, index, entBuilder) + err := f(ctx, v, index) if err != nil { return nil, err @@ -724,12 +833,17 @@ func (b *EntRecipeIngredientBuilder) Create(ctx context.Context) (*ent.RecipeIng } for _, f := range lazySlice { - err := f(ctx, v, index, entBuilder) + err := f(ctx, v, index) if err != nil { return nil, err } } + if b.mutation.beforeCreateFunc != nil { + if err := b.mutation.beforeCreateFunc(ctx, v); err != nil { + return nil, err + } + } new, err := entBuilder.Save(ctx) if err != nil { @@ -743,9 +857,7 @@ func (b *EntRecipeIngredientBuilder) Create(ctx context.Context) (*ent.RecipeIng } } for _, f := range postSlice { - - err := f(ctx, new, index, entBuilder) - + err := f(ctx, new, index) if err != nil { return nil, err } diff --git a/examples/ent_recipe/carrier/factory/ent_step.go b/examples/ent_recipe/carrier/factory/ent_step.go index 2bad05b..88ce133 100644 --- a/examples/ent_recipe/carrier/factory/ent_step.go +++ b/examples/ent_recipe/carrier/factory/ent_step.go @@ -13,19 +13,26 @@ type EntStepMutator struct { RecipeID int Text string + + _creator *ent.StepCreate +} + +func (m *EntStepMutator) EntCreator() *ent.StepCreate { + return m._creator } type entStepMutation struct { recipeType int - recipeFunc func(ctx context.Context, i *EntStepMutator, c int, creator *ent.StepCreate) error + recipeFunc func(ctx context.Context, i *EntStepMutator, c int) error recipeIDType int - recipeIDFunc func(ctx context.Context, i *EntStepMutator, c int, creator *ent.StepCreate) error + recipeIDFunc func(ctx context.Context, i *EntStepMutator, c int) error textType int - textFunc func(ctx context.Context, i *EntStepMutator, c int, creator *ent.StepCreate) error + textFunc func(ctx context.Context, i *EntStepMutator, c int) error - afterCreateFunc func(ctx context.Context, i *ent.Step) error + beforeCreateFunc func(ctx context.Context, i *EntStepMutator) error + afterCreateFunc func(ctx context.Context, i *ent.Step) error } type EntStepMetaFactory struct { mutation entStepMutation @@ -38,6 +45,11 @@ type entStepTrait struct { func EntStepTrait() *entStepTrait { return &entStepTrait{} } +func (*entStepMutation) beforeCreateMutateFunc(fn func(ctx context.Context, i *EntStepMutator) error) func(m *entStepMutation) { + return func(m *entStepMutation) { + m.beforeCreateFunc = fn + } +} func (*entStepMutation) afterCreateMutateFunc(fn func(ctx context.Context, i *ent.Step) error) func(m *entStepMutation) { return func(m *entStepMutation) { m.afterCreateFunc = fn @@ -47,7 +59,7 @@ func (*entStepMutation) afterCreateMutateFunc(fn func(ctx context.Context, i *en func (*entStepMutation) recipeSequenceMutateFunc(fn func(ctx context.Context, i int) (*ent.Recipe, error)) func(m *entStepMutation) { return func(m *entStepMutation) { m.recipeType = TypeSequence - m.recipeFunc = func(ctx context.Context, i *EntStepMutator, c int, creator *ent.StepCreate) error { + m.recipeFunc = func(ctx context.Context, i *EntStepMutator, c int) error { if fn == nil { return nil } @@ -56,7 +68,7 @@ func (*entStepMutation) recipeSequenceMutateFunc(fn func(ctx context.Context, i return err } - creator.SetRecipe(value) + i.EntCreator().SetRecipe(value) i.Recipe = value return nil @@ -66,7 +78,7 @@ func (*entStepMutation) recipeSequenceMutateFunc(fn func(ctx context.Context, i func (*entStepMutation) recipeLazyMutateFunc(fn func(ctx context.Context, i *EntStepMutator) (*ent.Recipe, error)) func(m *entStepMutation) { return func(m *entStepMutation) { m.recipeType = TypeLazy - m.recipeFunc = func(ctx context.Context, i *EntStepMutator, c int, creator *ent.StepCreate) error { + m.recipeFunc = func(ctx context.Context, i *EntStepMutator, c int) error { if fn == nil { return nil } @@ -75,7 +87,7 @@ func (*entStepMutation) recipeLazyMutateFunc(fn func(ctx context.Context, i *Ent return err } - creator.SetRecipe(value) + i.EntCreator().SetRecipe(value) i.Recipe = value return nil @@ -85,9 +97,9 @@ func (*entStepMutation) recipeLazyMutateFunc(fn func(ctx context.Context, i *Ent func (*entStepMutation) recipeDefaultMutateFunc(v *ent.Recipe) func(m *entStepMutation) { return func(m *entStepMutation) { m.recipeType = TypeDefault - m.recipeFunc = func(ctx context.Context, i *EntStepMutator, c int, creator *ent.StepCreate) error { + m.recipeFunc = func(ctx context.Context, i *EntStepMutator, c int) error { - creator.SetRecipe(v) + i.EntCreator().SetRecipe(v) i.Recipe = v return nil @@ -97,7 +109,7 @@ func (*entStepMutation) recipeDefaultMutateFunc(v *ent.Recipe) func(m *entStepMu func (*entStepMutation) recipeFactoryMutateFunc(fn func(ctx context.Context) (*ent.Recipe, error)) func(m *entStepMutation) { return func(m *entStepMutation) { m.recipeType = TypeFactory - m.recipeFunc = func(ctx context.Context, i *EntStepMutator, c int, creator *ent.StepCreate) error { + m.recipeFunc = func(ctx context.Context, i *EntStepMutator, c int) error { if fn == nil { return nil } @@ -106,7 +118,7 @@ func (*entStepMutation) recipeFactoryMutateFunc(fn func(ctx context.Context) (*e return err } - creator.SetRecipe(value) + i.EntCreator().SetRecipe(value) i.Recipe = value @@ -115,34 +127,49 @@ func (*entStepMutation) recipeFactoryMutateFunc(fn func(ctx context.Context) (*e } } +// SetRecipeSequence register a function which accept a sequence counter and set return value to Recipe field func (f *EntStepMetaFactory) SetRecipeSequence(fn func(ctx context.Context, i int) (*ent.Recipe, error)) *EntStepMetaFactory { f.mutation.recipeSequenceMutateFunc(fn)(&f.mutation) return f } + +// SetRecipeLazy register a function which accept the build struct and set return value to Recipe field func (f *EntStepMetaFactory) SetRecipeLazy(fn func(ctx context.Context, i *EntStepMutator) (*ent.Recipe, error)) *EntStepMetaFactory { f.mutation.recipeLazyMutateFunc(fn)(&f.mutation) return f } + +// SetRecipeDefault assign a default value to Recipe field func (f *EntStepMetaFactory) SetRecipeDefault(v *ent.Recipe) *EntStepMetaFactory { f.mutation.recipeDefaultMutateFunc(v)(&f.mutation) return f } + +// SetRecipeFactory register a factory function and assign return value to Recipe, you can also use related factory's Create/CreateV as input function here func (f *EntStepMetaFactory) SetRecipeFactory(fn func(ctx context.Context) (*ent.Recipe, error)) *EntStepMetaFactory { f.mutation.recipeFactoryMutateFunc(fn)(&f.mutation) return f } + +// SetRecipeSequence register a function which accept a sequence counter and set return value to Recipe field func (t *entStepTrait) SetRecipeSequence(fn func(ctx context.Context, i int) (*ent.Recipe, error)) *entStepTrait { t.updates = append(t.updates, t.mutation.recipeSequenceMutateFunc(fn)) return t } + +// SetRecipeLazy register a function which accept the build struct and set return value to Recipe field func (t *entStepTrait) SetRecipeLazy(fn func(ctx context.Context, i *EntStepMutator) (*ent.Recipe, error)) *entStepTrait { t.updates = append(t.updates, t.mutation.recipeLazyMutateFunc(fn)) return t } + +// SetRecipeDefault assign a default value to Recipe field func (t *entStepTrait) SetRecipeDefault(v *ent.Recipe) *entStepTrait { t.updates = append(t.updates, t.mutation.recipeDefaultMutateFunc(v)) return t } + +// SetRecipeFactory register a factory function and assign return value to Recipe, you can also use related factory's Create/CreateV as input function here func (t *entStepTrait) SetRecipeFactory(fn func(ctx context.Context) (*ent.Recipe, error)) *entStepTrait { t.updates = append(t.updates, t.mutation.recipeFactoryMutateFunc(fn)) return t @@ -151,7 +178,7 @@ func (t *entStepTrait) SetRecipeFactory(fn func(ctx context.Context) (*ent.Recip func (*entStepMutation) recipeIDSequenceMutateFunc(fn func(ctx context.Context, i int) (int, error)) func(m *entStepMutation) { return func(m *entStepMutation) { m.recipeIDType = TypeSequence - m.recipeIDFunc = func(ctx context.Context, i *EntStepMutator, c int, creator *ent.StepCreate) error { + m.recipeIDFunc = func(ctx context.Context, i *EntStepMutator, c int) error { if fn == nil { return nil } @@ -160,7 +187,7 @@ func (*entStepMutation) recipeIDSequenceMutateFunc(fn func(ctx context.Context, return err } - creator.SetRecipeID(value) + i.EntCreator().SetRecipeID(value) i.RecipeID = value return nil @@ -170,7 +197,7 @@ func (*entStepMutation) recipeIDSequenceMutateFunc(fn func(ctx context.Context, func (*entStepMutation) recipeIDLazyMutateFunc(fn func(ctx context.Context, i *EntStepMutator) (int, error)) func(m *entStepMutation) { return func(m *entStepMutation) { m.recipeIDType = TypeLazy - m.recipeIDFunc = func(ctx context.Context, i *EntStepMutator, c int, creator *ent.StepCreate) error { + m.recipeIDFunc = func(ctx context.Context, i *EntStepMutator, c int) error { if fn == nil { return nil } @@ -179,7 +206,7 @@ func (*entStepMutation) recipeIDLazyMutateFunc(fn func(ctx context.Context, i *E return err } - creator.SetRecipeID(value) + i.EntCreator().SetRecipeID(value) i.RecipeID = value return nil @@ -189,9 +216,9 @@ func (*entStepMutation) recipeIDLazyMutateFunc(fn func(ctx context.Context, i *E func (*entStepMutation) recipeIDDefaultMutateFunc(v int) func(m *entStepMutation) { return func(m *entStepMutation) { m.recipeIDType = TypeDefault - m.recipeIDFunc = func(ctx context.Context, i *EntStepMutator, c int, creator *ent.StepCreate) error { + m.recipeIDFunc = func(ctx context.Context, i *EntStepMutator, c int) error { - creator.SetRecipeID(v) + i.EntCreator().SetRecipeID(v) i.RecipeID = v return nil @@ -201,7 +228,7 @@ func (*entStepMutation) recipeIDDefaultMutateFunc(v int) func(m *entStepMutation func (*entStepMutation) recipeIDFactoryMutateFunc(fn func(ctx context.Context) (int, error)) func(m *entStepMutation) { return func(m *entStepMutation) { m.recipeIDType = TypeFactory - m.recipeIDFunc = func(ctx context.Context, i *EntStepMutator, c int, creator *ent.StepCreate) error { + m.recipeIDFunc = func(ctx context.Context, i *EntStepMutator, c int) error { if fn == nil { return nil } @@ -210,7 +237,7 @@ func (*entStepMutation) recipeIDFactoryMutateFunc(fn func(ctx context.Context) ( return err } - creator.SetRecipeID(value) + i.EntCreator().SetRecipeID(value) i.RecipeID = value @@ -219,34 +246,49 @@ func (*entStepMutation) recipeIDFactoryMutateFunc(fn func(ctx context.Context) ( } } +// SetRecipeIDSequence register a function which accept a sequence counter and set return value to RecipeID field func (f *EntStepMetaFactory) SetRecipeIDSequence(fn func(ctx context.Context, i int) (int, error)) *EntStepMetaFactory { f.mutation.recipeIDSequenceMutateFunc(fn)(&f.mutation) return f } + +// SetRecipeIDLazy register a function which accept the build struct and set return value to RecipeID field func (f *EntStepMetaFactory) SetRecipeIDLazy(fn func(ctx context.Context, i *EntStepMutator) (int, error)) *EntStepMetaFactory { f.mutation.recipeIDLazyMutateFunc(fn)(&f.mutation) return f } + +// SetRecipeIDDefault assign a default value to RecipeID field func (f *EntStepMetaFactory) SetRecipeIDDefault(v int) *EntStepMetaFactory { f.mutation.recipeIDDefaultMutateFunc(v)(&f.mutation) return f } + +// SetRecipeIDFactory register a factory function and assign return value to RecipeID, you can also use related factory's Create/CreateV as input function here func (f *EntStepMetaFactory) SetRecipeIDFactory(fn func(ctx context.Context) (int, error)) *EntStepMetaFactory { f.mutation.recipeIDFactoryMutateFunc(fn)(&f.mutation) return f } + +// SetRecipeIDSequence register a function which accept a sequence counter and set return value to RecipeID field func (t *entStepTrait) SetRecipeIDSequence(fn func(ctx context.Context, i int) (int, error)) *entStepTrait { t.updates = append(t.updates, t.mutation.recipeIDSequenceMutateFunc(fn)) return t } + +// SetRecipeIDLazy register a function which accept the build struct and set return value to RecipeID field func (t *entStepTrait) SetRecipeIDLazy(fn func(ctx context.Context, i *EntStepMutator) (int, error)) *entStepTrait { t.updates = append(t.updates, t.mutation.recipeIDLazyMutateFunc(fn)) return t } + +// SetRecipeIDDefault assign a default value to RecipeID field func (t *entStepTrait) SetRecipeIDDefault(v int) *entStepTrait { t.updates = append(t.updates, t.mutation.recipeIDDefaultMutateFunc(v)) return t } + +// SetRecipeIDFactory register a factory function and assign return value to RecipeID, you can also use related factory's Create/CreateV as input function here func (t *entStepTrait) SetRecipeIDFactory(fn func(ctx context.Context) (int, error)) *entStepTrait { t.updates = append(t.updates, t.mutation.recipeIDFactoryMutateFunc(fn)) return t @@ -255,7 +297,7 @@ func (t *entStepTrait) SetRecipeIDFactory(fn func(ctx context.Context) (int, err func (*entStepMutation) textSequenceMutateFunc(fn func(ctx context.Context, i int) (string, error)) func(m *entStepMutation) { return func(m *entStepMutation) { m.textType = TypeSequence - m.textFunc = func(ctx context.Context, i *EntStepMutator, c int, creator *ent.StepCreate) error { + m.textFunc = func(ctx context.Context, i *EntStepMutator, c int) error { if fn == nil { return nil } @@ -264,7 +306,7 @@ func (*entStepMutation) textSequenceMutateFunc(fn func(ctx context.Context, i in return err } - creator.SetText(value) + i.EntCreator().SetText(value) i.Text = value return nil @@ -274,7 +316,7 @@ func (*entStepMutation) textSequenceMutateFunc(fn func(ctx context.Context, i in func (*entStepMutation) textLazyMutateFunc(fn func(ctx context.Context, i *EntStepMutator) (string, error)) func(m *entStepMutation) { return func(m *entStepMutation) { m.textType = TypeLazy - m.textFunc = func(ctx context.Context, i *EntStepMutator, c int, creator *ent.StepCreate) error { + m.textFunc = func(ctx context.Context, i *EntStepMutator, c int) error { if fn == nil { return nil } @@ -283,7 +325,7 @@ func (*entStepMutation) textLazyMutateFunc(fn func(ctx context.Context, i *EntSt return err } - creator.SetText(value) + i.EntCreator().SetText(value) i.Text = value return nil @@ -293,9 +335,9 @@ func (*entStepMutation) textLazyMutateFunc(fn func(ctx context.Context, i *EntSt func (*entStepMutation) textDefaultMutateFunc(v string) func(m *entStepMutation) { return func(m *entStepMutation) { m.textType = TypeDefault - m.textFunc = func(ctx context.Context, i *EntStepMutator, c int, creator *ent.StepCreate) error { + m.textFunc = func(ctx context.Context, i *EntStepMutator, c int) error { - creator.SetText(v) + i.EntCreator().SetText(v) i.Text = v return nil @@ -305,7 +347,7 @@ func (*entStepMutation) textDefaultMutateFunc(v string) func(m *entStepMutation) func (*entStepMutation) textFactoryMutateFunc(fn func(ctx context.Context) (string, error)) func(m *entStepMutation) { return func(m *entStepMutation) { m.textType = TypeFactory - m.textFunc = func(ctx context.Context, i *EntStepMutator, c int, creator *ent.StepCreate) error { + m.textFunc = func(ctx context.Context, i *EntStepMutator, c int) error { if fn == nil { return nil } @@ -314,7 +356,7 @@ func (*entStepMutation) textFactoryMutateFunc(fn func(ctx context.Context) (stri return err } - creator.SetText(value) + i.EntCreator().SetText(value) i.Text = value @@ -323,48 +365,79 @@ func (*entStepMutation) textFactoryMutateFunc(fn func(ctx context.Context) (stri } } +// SetTextSequence register a function which accept a sequence counter and set return value to Text field func (f *EntStepMetaFactory) SetTextSequence(fn func(ctx context.Context, i int) (string, error)) *EntStepMetaFactory { f.mutation.textSequenceMutateFunc(fn)(&f.mutation) return f } + +// SetTextLazy register a function which accept the build struct and set return value to Text field func (f *EntStepMetaFactory) SetTextLazy(fn func(ctx context.Context, i *EntStepMutator) (string, error)) *EntStepMetaFactory { f.mutation.textLazyMutateFunc(fn)(&f.mutation) return f } + +// SetTextDefault assign a default value to Text field func (f *EntStepMetaFactory) SetTextDefault(v string) *EntStepMetaFactory { f.mutation.textDefaultMutateFunc(v)(&f.mutation) return f } + +// SetTextFactory register a factory function and assign return value to Text, you can also use related factory's Create/CreateV as input function here func (f *EntStepMetaFactory) SetTextFactory(fn func(ctx context.Context) (string, error)) *EntStepMetaFactory { f.mutation.textFactoryMutateFunc(fn)(&f.mutation) return f } + +// SetTextSequence register a function which accept a sequence counter and set return value to Text field func (t *entStepTrait) SetTextSequence(fn func(ctx context.Context, i int) (string, error)) *entStepTrait { t.updates = append(t.updates, t.mutation.textSequenceMutateFunc(fn)) return t } + +// SetTextLazy register a function which accept the build struct and set return value to Text field func (t *entStepTrait) SetTextLazy(fn func(ctx context.Context, i *EntStepMutator) (string, error)) *entStepTrait { t.updates = append(t.updates, t.mutation.textLazyMutateFunc(fn)) return t } + +// SetTextDefault assign a default value to Text field func (t *entStepTrait) SetTextDefault(v string) *entStepTrait { t.updates = append(t.updates, t.mutation.textDefaultMutateFunc(v)) return t } + +// SetTextFactory register a factory function and assign return value to Text, you can also use related factory's Create/CreateV as input function here func (t *entStepTrait) SetTextFactory(fn func(ctx context.Context) (string, error)) *entStepTrait { t.updates = append(t.updates, t.mutation.textFactoryMutateFunc(fn)) return t } +// SetAfterCreateFunc register a function to be called after struct create func (f *EntStepMetaFactory) SetAfterCreateFunc(fn func(ctx context.Context, i *ent.Step) error) *EntStepMetaFactory { f.mutation.afterCreateFunc = fn return f } + +// SetBeforeCreateFunc register a function to be called before struct create +func (f *EntStepMetaFactory) SetBeforeCreateFunc(fn func(ctx context.Context, i *EntStepMutator) error) *EntStepMetaFactory { + f.mutation.beforeCreateFunc = fn + return f +} + +// SetAfterCreateFunc register a function to be called after struct create func (t *entStepTrait) SetAfterCreateFunc(fn func(ctx context.Context, i *ent.Step) error) *entStepTrait { t.updates = append(t.updates, t.mutation.afterCreateMutateFunc(fn)) return t } +// SetBeforeCreateFunc register a function to be called before struct create +func (t *entStepTrait) SetBeforeCreateFunc(fn func(ctx context.Context, i *EntStepMutator) error) *entStepTrait { + t.updates = append(t.updates, t.mutation.beforeCreateMutateFunc(fn)) + return t +} + +// Build create a EntStepFactory from EntStepMetaFactory func (f *EntStepMetaFactory) Build() *EntStepFactory { return &EntStepFactory{meta: *f, counter: &Counter{}} } @@ -376,6 +449,7 @@ type EntStepFactory struct { client *ent.Client } +// SetRecipe set the Recipe field func (f *EntStepFactory) SetRecipe(i *ent.Recipe) *EntStepBuilder { builder := &EntStepBuilder{mutation: f.meta.mutation, counter: f.counter, factory: f} builder.SetRecipe(i) @@ -385,6 +459,7 @@ func (f *EntStepFactory) SetRecipe(i *ent.Recipe) *EntStepBuilder { return builder } +// SetRecipeID set the RecipeID field func (f *EntStepFactory) SetRecipeID(i int) *EntStepBuilder { builder := &EntStepBuilder{mutation: f.meta.mutation, counter: f.counter, factory: f} builder.SetRecipeID(i) @@ -394,6 +469,7 @@ func (f *EntStepFactory) SetRecipeID(i int) *EntStepBuilder { return builder } +// SetText set the Text field func (f *EntStepFactory) SetText(i string) *EntStepBuilder { builder := &EntStepBuilder{mutation: f.meta.mutation, counter: f.counter, factory: f} builder.SetText(i) @@ -403,6 +479,7 @@ func (f *EntStepFactory) SetText(i string) *EntStepBuilder { return builder } +// Create return a new *ent.Step func (f *EntStepFactory) Create(ctx context.Context) (*ent.Step, error) { builder := &EntStepBuilder{mutation: f.meta.mutation, counter: f.counter, factory: f} @@ -410,6 +487,8 @@ func (f *EntStepFactory) Create(ctx context.Context) (*ent.Step, error) { return builder.Create(ctx) } + +// CreateV return a new ent.Step func (f *EntStepFactory) CreateV(ctx context.Context) (ent.Step, error) { builder := &EntStepBuilder{mutation: f.meta.mutation, counter: f.counter, factory: f} @@ -417,6 +496,8 @@ func (f *EntStepFactory) CreateV(ctx context.Context) (ent.Step, error) { return builder.CreateV(ctx) } + +// CreateBatch return a []*ent.Step slice func (f *EntStepFactory) CreateBatch(ctx context.Context, n int) ([]*ent.Step, error) { builder := &EntStepBuilder{mutation: f.meta.mutation, counter: f.counter, factory: f} @@ -424,6 +505,8 @@ func (f *EntStepFactory) CreateBatch(ctx context.Context, n int) ([]*ent.Step, e return builder.CreateBatch(ctx, n) } + +// CreateBatchV return a []ent.Step slice func (f *EntStepFactory) CreateBatchV(ctx context.Context, n int) ([]ent.Step, error) { builder := &EntStepBuilder{mutation: f.meta.mutation, counter: f.counter, factory: f} @@ -432,6 +515,7 @@ func (f *EntStepFactory) CreateBatchV(ctx context.Context, n int) ([]ent.Step, e return builder.CreateBatchV(ctx, n) } +// Client set ent client to EntStepFactory func (f *EntStepFactory) Client(c *ent.Client) *EntStepFactory { f.client = c return f @@ -459,24 +543,28 @@ func (b *EntStepBuilder) Client(c *ent.Client) *EntStepBuilder { return b } +// SetRecipe set the Recipe field func (b *EntStepBuilder) SetRecipe(i *ent.Recipe) *EntStepBuilder { b.recipeOverride = i b.recipeOverriden = true return b } +// SetRecipeID set the RecipeID field func (b *EntStepBuilder) SetRecipeID(i int) *EntStepBuilder { b.recipeIDOverride = i b.recipeIDOverriden = true return b } +// SetText set the Text field func (b *EntStepBuilder) SetText(i string) *EntStepBuilder { b.textOverride = i b.textOverriden = true return b } +// CreateV return a new ent.Step func (b *EntStepBuilder) CreateV(ctx context.Context) (ent.Step, error) { var d ent.Step p, err := b.Create(ctx) @@ -486,11 +574,12 @@ func (b *EntStepBuilder) CreateV(ctx context.Context) (ent.Step, error) { return d, err } +// Create return a new *ent.Step func (b *EntStepBuilder) Create(ctx context.Context) (*ent.Step, error) { - var preSlice = []func(ctx context.Context, i *EntStepMutator, c int, creator *ent.StepCreate) error{} - var lazySlice = []func(ctx context.Context, i *EntStepMutator, c int, creator *ent.StepCreate) error{} - var postSlice = []func(ctx context.Context, i *ent.Step, c int, creator *ent.StepCreate) error{} + var preSlice = []func(ctx context.Context, i *EntStepMutator, c int) error{} + var lazySlice = []func(ctx context.Context, i *EntStepMutator, c int) error{} + var postSlice = []func(ctx context.Context, i *ent.Step, c int) error{} index := b.counter.Get() _ = index @@ -499,10 +588,10 @@ func (b *EntStepBuilder) Create(ctx context.Context) (*ent.Step, error) { entBuilder := client.Step.Create() if b.recipeOverriden { - preSlice = append(preSlice, func(ctx context.Context, i *EntStepMutator, c int, creator *ent.StepCreate) error { + preSlice = append(preSlice, func(ctx context.Context, i *EntStepMutator, c int) error { value := b.recipeOverride - creator.SetRecipe(value) + i.EntCreator().SetRecipe(value) i.Recipe = value return nil @@ -521,10 +610,10 @@ func (b *EntStepBuilder) Create(ctx context.Context) (*ent.Step, error) { } if b.recipeIDOverriden { - preSlice = append(preSlice, func(ctx context.Context, i *EntStepMutator, c int, creator *ent.StepCreate) error { + preSlice = append(preSlice, func(ctx context.Context, i *EntStepMutator, c int) error { value := b.recipeIDOverride - creator.SetRecipeID(value) + i.EntCreator().SetRecipeID(value) i.RecipeID = value return nil @@ -543,10 +632,10 @@ func (b *EntStepBuilder) Create(ctx context.Context) (*ent.Step, error) { } if b.textOverriden { - preSlice = append(preSlice, func(ctx context.Context, i *EntStepMutator, c int, creator *ent.StepCreate) error { + preSlice = append(preSlice, func(ctx context.Context, i *EntStepMutator, c int) error { value := b.textOverride - creator.SetText(value) + i.EntCreator().SetText(value) i.Text = value return nil @@ -565,9 +654,12 @@ func (b *EntStepBuilder) Create(ctx context.Context) (*ent.Step, error) { } v := &EntStepMutator{} + + v._creator = entBuilder + for _, f := range preSlice { - err := f(ctx, v, index, entBuilder) + err := f(ctx, v, index) if err != nil { return nil, err @@ -575,12 +667,17 @@ func (b *EntStepBuilder) Create(ctx context.Context) (*ent.Step, error) { } for _, f := range lazySlice { - err := f(ctx, v, index, entBuilder) + err := f(ctx, v, index) if err != nil { return nil, err } } + if b.mutation.beforeCreateFunc != nil { + if err := b.mutation.beforeCreateFunc(ctx, v); err != nil { + return nil, err + } + } new, err := entBuilder.Save(ctx) if err != nil { @@ -594,9 +691,7 @@ func (b *EntStepBuilder) Create(ctx context.Context) (*ent.Step, error) { } } for _, f := range postSlice { - - err := f(ctx, new, index, entBuilder) - + err := f(ctx, new, index) if err != nil { return nil, err } diff --git a/examples/ent_recipe/carrier/factory/ent_user.go b/examples/ent_recipe/carrier/factory/ent_user.go index d78c280..523033a 100644 --- a/examples/ent_recipe/carrier/factory/ent_user.go +++ b/examples/ent_recipe/carrier/factory/ent_user.go @@ -9,15 +9,22 @@ import ( type EntUserMutator struct { Name string + + _creator *ent.UserCreate +} + +func (m *EntUserMutator) EntCreator() *ent.UserCreate { + return m._creator } type entUserMutation struct { nameType int - nameFunc func(ctx context.Context, i *EntUserMutator, c int, creator *ent.UserCreate) error + nameFunc func(ctx context.Context, i *EntUserMutator, c int) error _postRecipesFunc func(ctx context.Context, set bool, obj *ent.User, i int) error - afterCreateFunc func(ctx context.Context, i *ent.User) error + beforeCreateFunc func(ctx context.Context, i *EntUserMutator) error + afterCreateFunc func(ctx context.Context, i *ent.User) error } type EntUserMetaFactory struct { mutation entUserMutation @@ -30,6 +37,11 @@ type entUserTrait struct { func EntUserTrait() *entUserTrait { return &entUserTrait{} } +func (*entUserMutation) beforeCreateMutateFunc(fn func(ctx context.Context, i *EntUserMutator) error) func(m *entUserMutation) { + return func(m *entUserMutation) { + m.beforeCreateFunc = fn + } +} func (*entUserMutation) afterCreateMutateFunc(fn func(ctx context.Context, i *ent.User) error) func(m *entUserMutation) { return func(m *entUserMutation) { m.afterCreateFunc = fn @@ -39,7 +51,7 @@ func (*entUserMutation) afterCreateMutateFunc(fn func(ctx context.Context, i *en func (*entUserMutation) nameSequenceMutateFunc(fn func(ctx context.Context, i int) (string, error)) func(m *entUserMutation) { return func(m *entUserMutation) { m.nameType = TypeSequence - m.nameFunc = func(ctx context.Context, i *EntUserMutator, c int, creator *ent.UserCreate) error { + m.nameFunc = func(ctx context.Context, i *EntUserMutator, c int) error { if fn == nil { return nil } @@ -48,7 +60,7 @@ func (*entUserMutation) nameSequenceMutateFunc(fn func(ctx context.Context, i in return err } - creator.SetName(value) + i.EntCreator().SetName(value) i.Name = value return nil @@ -58,7 +70,7 @@ func (*entUserMutation) nameSequenceMutateFunc(fn func(ctx context.Context, i in func (*entUserMutation) nameLazyMutateFunc(fn func(ctx context.Context, i *EntUserMutator) (string, error)) func(m *entUserMutation) { return func(m *entUserMutation) { m.nameType = TypeLazy - m.nameFunc = func(ctx context.Context, i *EntUserMutator, c int, creator *ent.UserCreate) error { + m.nameFunc = func(ctx context.Context, i *EntUserMutator, c int) error { if fn == nil { return nil } @@ -67,7 +79,7 @@ func (*entUserMutation) nameLazyMutateFunc(fn func(ctx context.Context, i *EntUs return err } - creator.SetName(value) + i.EntCreator().SetName(value) i.Name = value return nil @@ -77,9 +89,9 @@ func (*entUserMutation) nameLazyMutateFunc(fn func(ctx context.Context, i *EntUs func (*entUserMutation) nameDefaultMutateFunc(v string) func(m *entUserMutation) { return func(m *entUserMutation) { m.nameType = TypeDefault - m.nameFunc = func(ctx context.Context, i *EntUserMutator, c int, creator *ent.UserCreate) error { + m.nameFunc = func(ctx context.Context, i *EntUserMutator, c int) error { - creator.SetName(v) + i.EntCreator().SetName(v) i.Name = v return nil @@ -89,7 +101,7 @@ func (*entUserMutation) nameDefaultMutateFunc(v string) func(m *entUserMutation) func (*entUserMutation) nameFactoryMutateFunc(fn func(ctx context.Context) (string, error)) func(m *entUserMutation) { return func(m *entUserMutation) { m.nameType = TypeFactory - m.nameFunc = func(ctx context.Context, i *EntUserMutator, c int, creator *ent.UserCreate) error { + m.nameFunc = func(ctx context.Context, i *EntUserMutator, c int) error { if fn == nil { return nil } @@ -98,7 +110,7 @@ func (*entUserMutation) nameFactoryMutateFunc(fn func(ctx context.Context) (stri return err } - creator.SetName(value) + i.EntCreator().SetName(value) i.Name = value @@ -107,34 +119,49 @@ func (*entUserMutation) nameFactoryMutateFunc(fn func(ctx context.Context) (stri } } +// SetNameSequence register a function which accept a sequence counter and set return value to Name field func (f *EntUserMetaFactory) SetNameSequence(fn func(ctx context.Context, i int) (string, error)) *EntUserMetaFactory { f.mutation.nameSequenceMutateFunc(fn)(&f.mutation) return f } + +// SetNameLazy register a function which accept the build struct and set return value to Name field func (f *EntUserMetaFactory) SetNameLazy(fn func(ctx context.Context, i *EntUserMutator) (string, error)) *EntUserMetaFactory { f.mutation.nameLazyMutateFunc(fn)(&f.mutation) return f } + +// SetNameDefault assign a default value to Name field func (f *EntUserMetaFactory) SetNameDefault(v string) *EntUserMetaFactory { f.mutation.nameDefaultMutateFunc(v)(&f.mutation) return f } + +// SetNameFactory register a factory function and assign return value to Name, you can also use related factory's Create/CreateV as input function here func (f *EntUserMetaFactory) SetNameFactory(fn func(ctx context.Context) (string, error)) *EntUserMetaFactory { f.mutation.nameFactoryMutateFunc(fn)(&f.mutation) return f } + +// SetNameSequence register a function which accept a sequence counter and set return value to Name field func (t *entUserTrait) SetNameSequence(fn func(ctx context.Context, i int) (string, error)) *entUserTrait { t.updates = append(t.updates, t.mutation.nameSequenceMutateFunc(fn)) return t } + +// SetNameLazy register a function which accept the build struct and set return value to Name field func (t *entUserTrait) SetNameLazy(fn func(ctx context.Context, i *EntUserMutator) (string, error)) *entUserTrait { t.updates = append(t.updates, t.mutation.nameLazyMutateFunc(fn)) return t } + +// SetNameDefault assign a default value to Name field func (t *entUserTrait) SetNameDefault(v string) *entUserTrait { t.updates = append(t.updates, t.mutation.nameDefaultMutateFunc(v)) return t } + +// SetNameFactory register a factory function and assign return value to Name, you can also use related factory's Create/CreateV as input function here func (t *entUserTrait) SetNameFactory(fn func(ctx context.Context) (string, error)) *entUserTrait { t.updates = append(t.updates, t.mutation.nameFactoryMutateFunc(fn)) return t @@ -145,6 +172,8 @@ func (*entUserMutation) recipesPostMutateFunc(fn func(ctx context.Context, set b m._postRecipesFunc = fn } } + +// SetRecipesPostFunc register a post function which will be called in factory SetRecipesPost method func (f *EntUserMetaFactory) SetRecipesPostFunc(fn func(ctx context.Context, set bool, obj *ent.User, i int) error) *EntUserMetaFactory { f.mutation.recipesPostMutateFunc(fn)(&f.mutation) return f @@ -154,15 +183,31 @@ func (t *entUserTrait) SetRecipesPostFunc(fn func(ctx context.Context, set bool, return t } +// SetAfterCreateFunc register a function to be called after struct create func (f *EntUserMetaFactory) SetAfterCreateFunc(fn func(ctx context.Context, i *ent.User) error) *EntUserMetaFactory { f.mutation.afterCreateFunc = fn return f } + +// SetBeforeCreateFunc register a function to be called before struct create +func (f *EntUserMetaFactory) SetBeforeCreateFunc(fn func(ctx context.Context, i *EntUserMutator) error) *EntUserMetaFactory { + f.mutation.beforeCreateFunc = fn + return f +} + +// SetAfterCreateFunc register a function to be called after struct create func (t *entUserTrait) SetAfterCreateFunc(fn func(ctx context.Context, i *ent.User) error) *entUserTrait { t.updates = append(t.updates, t.mutation.afterCreateMutateFunc(fn)) return t } +// SetBeforeCreateFunc register a function to be called before struct create +func (t *entUserTrait) SetBeforeCreateFunc(fn func(ctx context.Context, i *EntUserMutator) error) *entUserTrait { + t.updates = append(t.updates, t.mutation.beforeCreateMutateFunc(fn)) + return t +} + +// Build create a EntUserFactory from EntUserMetaFactory func (f *EntUserMetaFactory) Build() *EntUserFactory { return &EntUserFactory{meta: *f, counter: &Counter{}} } @@ -174,6 +219,7 @@ type EntUserFactory struct { client *ent.Client } +// SetName set the Name field func (f *EntUserFactory) SetName(i string) *EntUserBuilder { builder := &EntUserBuilder{mutation: f.meta.mutation, counter: f.counter, factory: f} builder.SetName(i) @@ -183,6 +229,7 @@ func (f *EntUserFactory) SetName(i string) *EntUserBuilder { return builder } +// SetRecipesPost call the post function with int input func (f *EntUserFactory) SetRecipesPost(i int) *EntUserBuilder { builder := &EntUserBuilder{mutation: f.meta.mutation, counter: f.counter, factory: f} builder.SetRecipesPost(i) @@ -192,6 +239,7 @@ func (f *EntUserFactory) SetRecipesPost(i int) *EntUserBuilder { return builder } +// Create return a new *ent.User func (f *EntUserFactory) Create(ctx context.Context) (*ent.User, error) { builder := &EntUserBuilder{mutation: f.meta.mutation, counter: f.counter, factory: f} @@ -199,6 +247,8 @@ func (f *EntUserFactory) Create(ctx context.Context) (*ent.User, error) { return builder.Create(ctx) } + +// CreateV return a new ent.User func (f *EntUserFactory) CreateV(ctx context.Context) (ent.User, error) { builder := &EntUserBuilder{mutation: f.meta.mutation, counter: f.counter, factory: f} @@ -206,6 +256,8 @@ func (f *EntUserFactory) CreateV(ctx context.Context) (ent.User, error) { return builder.CreateV(ctx) } + +// CreateBatch return a []*ent.User slice func (f *EntUserFactory) CreateBatch(ctx context.Context, n int) ([]*ent.User, error) { builder := &EntUserBuilder{mutation: f.meta.mutation, counter: f.counter, factory: f} @@ -213,6 +265,8 @@ func (f *EntUserFactory) CreateBatch(ctx context.Context, n int) ([]*ent.User, e return builder.CreateBatch(ctx, n) } + +// CreateBatchV return a []ent.User slice func (f *EntUserFactory) CreateBatchV(ctx context.Context, n int) ([]ent.User, error) { builder := &EntUserBuilder{mutation: f.meta.mutation, counter: f.counter, factory: f} @@ -221,6 +275,7 @@ func (f *EntUserFactory) CreateBatchV(ctx context.Context, n int) ([]ent.User, e return builder.CreateBatchV(ctx, n) } +// Client set ent client to EntUserFactory func (f *EntUserFactory) Client(c *ent.Client) *EntUserFactory { f.client = c return f @@ -245,18 +300,21 @@ func (b *EntUserBuilder) Client(c *ent.Client) *EntUserBuilder { return b } +// SetName set the Name field func (b *EntUserBuilder) SetName(i string) *EntUserBuilder { b.nameOverride = i b.nameOverriden = true return b } +// SetRecipesPost call the post function with int input func (b *EntUserBuilder) SetRecipesPost(i int) *EntUserBuilder { b._postRecipes = i b._postRecipesSet = true return b } +// CreateV return a new ent.User func (b *EntUserBuilder) CreateV(ctx context.Context) (ent.User, error) { var d ent.User p, err := b.Create(ctx) @@ -266,11 +324,12 @@ func (b *EntUserBuilder) CreateV(ctx context.Context) (ent.User, error) { return d, err } +// Create return a new *ent.User func (b *EntUserBuilder) Create(ctx context.Context) (*ent.User, error) { - var preSlice = []func(ctx context.Context, i *EntUserMutator, c int, creator *ent.UserCreate) error{} - var lazySlice = []func(ctx context.Context, i *EntUserMutator, c int, creator *ent.UserCreate) error{} - var postSlice = []func(ctx context.Context, i *ent.User, c int, creator *ent.UserCreate) error{} + var preSlice = []func(ctx context.Context, i *EntUserMutator, c int) error{} + var lazySlice = []func(ctx context.Context, i *EntUserMutator, c int) error{} + var postSlice = []func(ctx context.Context, i *ent.User, c int) error{} index := b.counter.Get() _ = index @@ -279,10 +338,10 @@ func (b *EntUserBuilder) Create(ctx context.Context) (*ent.User, error) { entBuilder := client.User.Create() if b.nameOverriden { - preSlice = append(preSlice, func(ctx context.Context, i *EntUserMutator, c int, creator *ent.UserCreate) error { + preSlice = append(preSlice, func(ctx context.Context, i *EntUserMutator, c int) error { value := b.nameOverride - creator.SetName(value) + i.EntCreator().SetName(value) i.Name = value return nil @@ -301,16 +360,19 @@ func (b *EntUserBuilder) Create(ctx context.Context) (*ent.User, error) { } if b.mutation._postRecipesFunc != nil { - postSlice = append(postSlice, func(ctx context.Context, i *ent.User, c int, creator *ent.UserCreate) error { + postSlice = append(postSlice, func(ctx context.Context, i *ent.User, c int) error { err := b.mutation._postRecipesFunc(ctx, b._postRecipesSet, i, b._postRecipes) return err }) } v := &EntUserMutator{} + + v._creator = entBuilder + for _, f := range preSlice { - err := f(ctx, v, index, entBuilder) + err := f(ctx, v, index) if err != nil { return nil, err @@ -318,12 +380,17 @@ func (b *EntUserBuilder) Create(ctx context.Context) (*ent.User, error) { } for _, f := range lazySlice { - err := f(ctx, v, index, entBuilder) + err := f(ctx, v, index) if err != nil { return nil, err } } + if b.mutation.beforeCreateFunc != nil { + if err := b.mutation.beforeCreateFunc(ctx, v); err != nil { + return nil, err + } + } new, err := entBuilder.Save(ctx) if err != nil { @@ -337,9 +404,7 @@ func (b *EntUserBuilder) Create(ctx context.Context) (*ent.User, error) { } } for _, f := range postSlice { - - err := f(ctx, new, index, entBuilder) - + err := f(ctx, new, index) if err != nil { return nil, err } diff --git a/examples/recipe/carrier/factory.go b/examples/recipe/carrier/factory.go index 0fb6c4c..c7d0079 100644 --- a/examples/recipe/carrier/factory.go +++ b/examples/recipe/carrier/factory.go @@ -5,6 +5,7 @@ import ( "github.com/Yiling-J/carrier/examples/recipe/carrier/factory" ) +// Factory is struct factory wrapper type Factory struct { recipeFactory *factory.RecipeFactory @@ -19,78 +20,103 @@ type Factory struct { userFactory *factory.UserFactory } +// NewFactory return a new struct factory wrapper func NewFactory() *Factory { return &Factory{} } +// RecipeMetaFactory() return a new RecipeMetaFactory func RecipeMetaFactory() *factory.RecipeMetaFactory { return &factory.RecipeMetaFactory{} } + +// SetRecipeFactory set a factory in wrapper func (f *Factory) SetRecipeFactory(c *factory.RecipeFactory) *Factory { f.recipeFactory = c return f } +// RecipeFactory return the RecipeFactory in wrapper func (f *Factory) RecipeFactory() *factory.RecipeFactory { return f.recipeFactory } +// RecipeStepMetaFactory() return a new RecipeStepMetaFactory func RecipeStepMetaFactory() *factory.RecipeStepMetaFactory { return &factory.RecipeStepMetaFactory{} } + +// SetRecipeStepFactory set a factory in wrapper func (f *Factory) SetRecipeStepFactory(c *factory.RecipeStepFactory) *Factory { f.recipeStepFactory = c return f } +// RecipeStepFactory return the RecipeStepFactory in wrapper func (f *Factory) RecipeStepFactory() *factory.RecipeStepFactory { return f.recipeStepFactory } +// IngredientMetaFactory() return a new IngredientMetaFactory func IngredientMetaFactory() *factory.IngredientMetaFactory { return &factory.IngredientMetaFactory{} } + +// SetIngredientFactory set a factory in wrapper func (f *Factory) SetIngredientFactory(c *factory.IngredientFactory) *Factory { f.ingredientFactory = c return f } +// IngredientFactory return the IngredientFactory in wrapper func (f *Factory) IngredientFactory() *factory.IngredientFactory { return f.ingredientFactory } +// RecipeIngredientMetaFactory() return a new RecipeIngredientMetaFactory func RecipeIngredientMetaFactory() *factory.RecipeIngredientMetaFactory { return &factory.RecipeIngredientMetaFactory{} } + +// SetRecipeIngredientFactory set a factory in wrapper func (f *Factory) SetRecipeIngredientFactory(c *factory.RecipeIngredientFactory) *Factory { f.recipeIngredientFactory = c return f } +// RecipeIngredientFactory return the RecipeIngredientFactory in wrapper func (f *Factory) RecipeIngredientFactory() *factory.RecipeIngredientFactory { return f.recipeIngredientFactory } +// CategoryMetaFactory() return a new CategoryMetaFactory func CategoryMetaFactory() *factory.CategoryMetaFactory { return &factory.CategoryMetaFactory{} } + +// SetCategoryFactory set a factory in wrapper func (f *Factory) SetCategoryFactory(c *factory.CategoryFactory) *Factory { f.categoryFactory = c return f } +// CategoryFactory return the CategoryFactory in wrapper func (f *Factory) CategoryFactory() *factory.CategoryFactory { return f.categoryFactory } +// UserMetaFactory() return a new UserMetaFactory func UserMetaFactory() *factory.UserMetaFactory { return &factory.UserMetaFactory{} } + +// SetUserFactory set a factory in wrapper func (f *Factory) SetUserFactory(c *factory.UserFactory) *Factory { f.userFactory = c return f } +// UserFactory return the UserFactory in wrapper func (f *Factory) UserFactory() *factory.UserFactory { return f.userFactory } diff --git a/examples/recipe/carrier/factory/category.go b/examples/recipe/carrier/factory/category.go index 6912cfe..0201a7a 100644 --- a/examples/recipe/carrier/factory/category.go +++ b/examples/recipe/carrier/factory/category.go @@ -11,7 +11,8 @@ type categoryMutation struct { nameType int nameFunc func(ctx context.Context, i *model.Category, c int) error - afterCreateFunc func(ctx context.Context, i *model.Category) error + beforeCreateFunc func(ctx context.Context, i *model.Category) error + afterCreateFunc func(ctx context.Context, i *model.Category) error } type CategoryMetaFactory struct { mutation categoryMutation @@ -24,6 +25,11 @@ type categoryTrait struct { func CategoryTrait() *categoryTrait { return &categoryTrait{} } +func (*categoryMutation) beforeCreateMutateFunc(fn func(ctx context.Context, i *model.Category) error) func(m *categoryMutation) { + return func(m *categoryMutation) { + m.beforeCreateFunc = fn + } +} func (*categoryMutation) afterCreateMutateFunc(fn func(ctx context.Context, i *model.Category) error) func(m *categoryMutation) { return func(m *categoryMutation) { m.afterCreateFunc = fn @@ -93,48 +99,79 @@ func (*categoryMutation) nameFactoryMutateFunc(fn func(ctx context.Context) (str } } +// SetNameSequence register a function which accept a sequence counter and set return value to Name field func (f *CategoryMetaFactory) SetNameSequence(fn func(ctx context.Context, i int) (string, error)) *CategoryMetaFactory { f.mutation.nameSequenceMutateFunc(fn)(&f.mutation) return f } + +// SetNameLazy register a function which accept the build struct and set return value to Name field func (f *CategoryMetaFactory) SetNameLazy(fn func(ctx context.Context, i *model.Category) (string, error)) *CategoryMetaFactory { f.mutation.nameLazyMutateFunc(fn)(&f.mutation) return f } + +// SetNameDefault assign a default value to Name field func (f *CategoryMetaFactory) SetNameDefault(v string) *CategoryMetaFactory { f.mutation.nameDefaultMutateFunc(v)(&f.mutation) return f } + +// SetNameFactory register a factory function and assign return value to Name, you can also use related factory's Create/CreateV as input function here func (f *CategoryMetaFactory) SetNameFactory(fn func(ctx context.Context) (string, error)) *CategoryMetaFactory { f.mutation.nameFactoryMutateFunc(fn)(&f.mutation) return f } + +// SetNameSequence register a function which accept a sequence counter and set return value to Name field func (t *categoryTrait) SetNameSequence(fn func(ctx context.Context, i int) (string, error)) *categoryTrait { t.updates = append(t.updates, t.mutation.nameSequenceMutateFunc(fn)) return t } + +// SetNameLazy register a function which accept the build struct and set return value to Name field func (t *categoryTrait) SetNameLazy(fn func(ctx context.Context, i *model.Category) (string, error)) *categoryTrait { t.updates = append(t.updates, t.mutation.nameLazyMutateFunc(fn)) return t } + +// SetNameDefault assign a default value to Name field func (t *categoryTrait) SetNameDefault(v string) *categoryTrait { t.updates = append(t.updates, t.mutation.nameDefaultMutateFunc(v)) return t } + +// SetNameFactory register a factory function and assign return value to Name, you can also use related factory's Create/CreateV as input function here func (t *categoryTrait) SetNameFactory(fn func(ctx context.Context) (string, error)) *categoryTrait { t.updates = append(t.updates, t.mutation.nameFactoryMutateFunc(fn)) return t } +// SetAfterCreateFunc register a function to be called after struct create func (f *CategoryMetaFactory) SetAfterCreateFunc(fn func(ctx context.Context, i *model.Category) error) *CategoryMetaFactory { f.mutation.afterCreateFunc = fn return f } + +// SetBeforeCreateFunc register a function to be called before struct create +func (f *CategoryMetaFactory) SetBeforeCreateFunc(fn func(ctx context.Context, i *model.Category) error) *CategoryMetaFactory { + f.mutation.beforeCreateFunc = fn + return f +} + +// SetAfterCreateFunc register a function to be called after struct create func (t *categoryTrait) SetAfterCreateFunc(fn func(ctx context.Context, i *model.Category) error) *categoryTrait { t.updates = append(t.updates, t.mutation.afterCreateMutateFunc(fn)) return t } +// SetBeforeCreateFunc register a function to be called before struct create +func (t *categoryTrait) SetBeforeCreateFunc(fn func(ctx context.Context, i *model.Category) error) *categoryTrait { + t.updates = append(t.updates, t.mutation.beforeCreateMutateFunc(fn)) + return t +} + +// Build create a CategoryFactory from CategoryMetaFactory func (f *CategoryMetaFactory) Build() *CategoryFactory { return &CategoryFactory{meta: *f, counter: &Counter{}} } @@ -144,6 +181,7 @@ type CategoryFactory struct { counter *Counter } +// SetName set the Name field func (f *CategoryFactory) SetName(i string) *CategoryBuilder { builder := &CategoryBuilder{mutation: f.meta.mutation, counter: f.counter, factory: f} builder.SetName(i) @@ -151,21 +189,28 @@ func (f *CategoryFactory) SetName(i string) *CategoryBuilder { return builder } +// Create return a new *model.Category func (f *CategoryFactory) Create(ctx context.Context) (*model.Category, error) { builder := &CategoryBuilder{mutation: f.meta.mutation, counter: f.counter, factory: f} return builder.Create(ctx) } + +// CreateV return a new model.Category func (f *CategoryFactory) CreateV(ctx context.Context) (model.Category, error) { builder := &CategoryBuilder{mutation: f.meta.mutation, counter: f.counter, factory: f} return builder.CreateV(ctx) } + +// CreateBatch return a []*model.Category slice func (f *CategoryFactory) CreateBatch(ctx context.Context, n int) ([]*model.Category, error) { builder := &CategoryBuilder{mutation: f.meta.mutation, counter: f.counter, factory: f} return builder.CreateBatch(ctx, n) } + +// CreateBatchV return a []model.Category slice func (f *CategoryFactory) CreateBatchV(ctx context.Context, n int) ([]model.Category, error) { builder := &CategoryBuilder{mutation: f.meta.mutation, counter: f.counter, factory: f} @@ -181,12 +226,14 @@ type CategoryBuilder struct { nameOverriden bool } +// SetName set the Name field func (b *CategoryBuilder) SetName(i string) *CategoryBuilder { b.nameOverride = i b.nameOverriden = true return b } +// CreateV return a new model.Category func (b *CategoryBuilder) CreateV(ctx context.Context) (model.Category, error) { var d model.Category p, err := b.Create(ctx) @@ -196,6 +243,7 @@ func (b *CategoryBuilder) CreateV(ctx context.Context) (model.Category, error) { return d, err } +// Create return a new *model.Category func (b *CategoryBuilder) Create(ctx context.Context) (*model.Category, error) { var preSlice = []func(ctx context.Context, i *model.Category, c int) error{} @@ -226,6 +274,7 @@ func (b *CategoryBuilder) Create(ctx context.Context) (*model.Category, error) { } v := &model.Category{} + for _, f := range preSlice { err := f(ctx, v, index) @@ -242,6 +291,11 @@ func (b *CategoryBuilder) Create(ctx context.Context) (*model.Category, error) { return nil, err } } + if b.mutation.beforeCreateFunc != nil { + if err := b.mutation.beforeCreateFunc(ctx, v); err != nil { + return nil, err + } + } new := v @@ -252,9 +306,7 @@ func (b *CategoryBuilder) Create(ctx context.Context) (*model.Category, error) { } } for _, f := range postSlice { - err := f(ctx, new, index) - if err != nil { return nil, err } diff --git a/examples/recipe/carrier/factory/ingredient.go b/examples/recipe/carrier/factory/ingredient.go index 6974fcd..8404049 100644 --- a/examples/recipe/carrier/factory/ingredient.go +++ b/examples/recipe/carrier/factory/ingredient.go @@ -14,7 +14,8 @@ type ingredientMutation struct { tagsType int tagsFunc func(ctx context.Context, i *model.Ingredient, c int) error - afterCreateFunc func(ctx context.Context, i *model.Ingredient) error + beforeCreateFunc func(ctx context.Context, i *model.Ingredient) error + afterCreateFunc func(ctx context.Context, i *model.Ingredient) error } type IngredientMetaFactory struct { mutation ingredientMutation @@ -27,6 +28,11 @@ type ingredientTrait struct { func IngredientTrait() *ingredientTrait { return &ingredientTrait{} } +func (*ingredientMutation) beforeCreateMutateFunc(fn func(ctx context.Context, i *model.Ingredient) error) func(m *ingredientMutation) { + return func(m *ingredientMutation) { + m.beforeCreateFunc = fn + } +} func (*ingredientMutation) afterCreateMutateFunc(fn func(ctx context.Context, i *model.Ingredient) error) func(m *ingredientMutation) { return func(m *ingredientMutation) { m.afterCreateFunc = fn @@ -96,34 +102,49 @@ func (*ingredientMutation) nameFactoryMutateFunc(fn func(ctx context.Context) (s } } +// SetNameSequence register a function which accept a sequence counter and set return value to Name field func (f *IngredientMetaFactory) SetNameSequence(fn func(ctx context.Context, i int) (string, error)) *IngredientMetaFactory { f.mutation.nameSequenceMutateFunc(fn)(&f.mutation) return f } + +// SetNameLazy register a function which accept the build struct and set return value to Name field func (f *IngredientMetaFactory) SetNameLazy(fn func(ctx context.Context, i *model.Ingredient) (string, error)) *IngredientMetaFactory { f.mutation.nameLazyMutateFunc(fn)(&f.mutation) return f } + +// SetNameDefault assign a default value to Name field func (f *IngredientMetaFactory) SetNameDefault(v string) *IngredientMetaFactory { f.mutation.nameDefaultMutateFunc(v)(&f.mutation) return f } + +// SetNameFactory register a factory function and assign return value to Name, you can also use related factory's Create/CreateV as input function here func (f *IngredientMetaFactory) SetNameFactory(fn func(ctx context.Context) (string, error)) *IngredientMetaFactory { f.mutation.nameFactoryMutateFunc(fn)(&f.mutation) return f } + +// SetNameSequence register a function which accept a sequence counter and set return value to Name field func (t *ingredientTrait) SetNameSequence(fn func(ctx context.Context, i int) (string, error)) *ingredientTrait { t.updates = append(t.updates, t.mutation.nameSequenceMutateFunc(fn)) return t } + +// SetNameLazy register a function which accept the build struct and set return value to Name field func (t *ingredientTrait) SetNameLazy(fn func(ctx context.Context, i *model.Ingredient) (string, error)) *ingredientTrait { t.updates = append(t.updates, t.mutation.nameLazyMutateFunc(fn)) return t } + +// SetNameDefault assign a default value to Name field func (t *ingredientTrait) SetNameDefault(v string) *ingredientTrait { t.updates = append(t.updates, t.mutation.nameDefaultMutateFunc(v)) return t } + +// SetNameFactory register a factory function and assign return value to Name, you can also use related factory's Create/CreateV as input function here func (t *ingredientTrait) SetNameFactory(fn func(ctx context.Context) (string, error)) *ingredientTrait { t.updates = append(t.updates, t.mutation.nameFactoryMutateFunc(fn)) return t @@ -192,48 +213,79 @@ func (*ingredientMutation) tagsFactoryMutateFunc(fn func(ctx context.Context) ([ } } +// SetTagsSequence register a function which accept a sequence counter and set return value to Tags field func (f *IngredientMetaFactory) SetTagsSequence(fn func(ctx context.Context, i int) ([]*model.Category, error)) *IngredientMetaFactory { f.mutation.tagsSequenceMutateFunc(fn)(&f.mutation) return f } + +// SetTagsLazy register a function which accept the build struct and set return value to Tags field func (f *IngredientMetaFactory) SetTagsLazy(fn func(ctx context.Context, i *model.Ingredient) ([]*model.Category, error)) *IngredientMetaFactory { f.mutation.tagsLazyMutateFunc(fn)(&f.mutation) return f } + +// SetTagsDefault assign a default value to Tags field func (f *IngredientMetaFactory) SetTagsDefault(v []*model.Category) *IngredientMetaFactory { f.mutation.tagsDefaultMutateFunc(v)(&f.mutation) return f } + +// SetTagsFactory register a factory function and assign return value to Tags, you can also use related factory's Create/CreateV as input function here func (f *IngredientMetaFactory) SetTagsFactory(fn func(ctx context.Context) ([]*model.Category, error)) *IngredientMetaFactory { f.mutation.tagsFactoryMutateFunc(fn)(&f.mutation) return f } + +// SetTagsSequence register a function which accept a sequence counter and set return value to Tags field func (t *ingredientTrait) SetTagsSequence(fn func(ctx context.Context, i int) ([]*model.Category, error)) *ingredientTrait { t.updates = append(t.updates, t.mutation.tagsSequenceMutateFunc(fn)) return t } + +// SetTagsLazy register a function which accept the build struct and set return value to Tags field func (t *ingredientTrait) SetTagsLazy(fn func(ctx context.Context, i *model.Ingredient) ([]*model.Category, error)) *ingredientTrait { t.updates = append(t.updates, t.mutation.tagsLazyMutateFunc(fn)) return t } + +// SetTagsDefault assign a default value to Tags field func (t *ingredientTrait) SetTagsDefault(v []*model.Category) *ingredientTrait { t.updates = append(t.updates, t.mutation.tagsDefaultMutateFunc(v)) return t } + +// SetTagsFactory register a factory function and assign return value to Tags, you can also use related factory's Create/CreateV as input function here func (t *ingredientTrait) SetTagsFactory(fn func(ctx context.Context) ([]*model.Category, error)) *ingredientTrait { t.updates = append(t.updates, t.mutation.tagsFactoryMutateFunc(fn)) return t } +// SetAfterCreateFunc register a function to be called after struct create func (f *IngredientMetaFactory) SetAfterCreateFunc(fn func(ctx context.Context, i *model.Ingredient) error) *IngredientMetaFactory { f.mutation.afterCreateFunc = fn return f } + +// SetBeforeCreateFunc register a function to be called before struct create +func (f *IngredientMetaFactory) SetBeforeCreateFunc(fn func(ctx context.Context, i *model.Ingredient) error) *IngredientMetaFactory { + f.mutation.beforeCreateFunc = fn + return f +} + +// SetAfterCreateFunc register a function to be called after struct create func (t *ingredientTrait) SetAfterCreateFunc(fn func(ctx context.Context, i *model.Ingredient) error) *ingredientTrait { t.updates = append(t.updates, t.mutation.afterCreateMutateFunc(fn)) return t } +// SetBeforeCreateFunc register a function to be called before struct create +func (t *ingredientTrait) SetBeforeCreateFunc(fn func(ctx context.Context, i *model.Ingredient) error) *ingredientTrait { + t.updates = append(t.updates, t.mutation.beforeCreateMutateFunc(fn)) + return t +} + +// Build create a IngredientFactory from IngredientMetaFactory func (f *IngredientMetaFactory) Build() *IngredientFactory { return &IngredientFactory{meta: *f, counter: &Counter{}} } @@ -243,6 +295,7 @@ type IngredientFactory struct { counter *Counter } +// SetName set the Name field func (f *IngredientFactory) SetName(i string) *IngredientBuilder { builder := &IngredientBuilder{mutation: f.meta.mutation, counter: f.counter, factory: f} builder.SetName(i) @@ -250,6 +303,7 @@ func (f *IngredientFactory) SetName(i string) *IngredientBuilder { return builder } +// SetTags set the Tags field func (f *IngredientFactory) SetTags(i []*model.Category) *IngredientBuilder { builder := &IngredientBuilder{mutation: f.meta.mutation, counter: f.counter, factory: f} builder.SetTags(i) @@ -257,21 +311,28 @@ func (f *IngredientFactory) SetTags(i []*model.Category) *IngredientBuilder { return builder } +// Create return a new *model.Ingredient func (f *IngredientFactory) Create(ctx context.Context) (*model.Ingredient, error) { builder := &IngredientBuilder{mutation: f.meta.mutation, counter: f.counter, factory: f} return builder.Create(ctx) } + +// CreateV return a new model.Ingredient func (f *IngredientFactory) CreateV(ctx context.Context) (model.Ingredient, error) { builder := &IngredientBuilder{mutation: f.meta.mutation, counter: f.counter, factory: f} return builder.CreateV(ctx) } + +// CreateBatch return a []*model.Ingredient slice func (f *IngredientFactory) CreateBatch(ctx context.Context, n int) ([]*model.Ingredient, error) { builder := &IngredientBuilder{mutation: f.meta.mutation, counter: f.counter, factory: f} return builder.CreateBatch(ctx, n) } + +// CreateBatchV return a []model.Ingredient slice func (f *IngredientFactory) CreateBatchV(ctx context.Context, n int) ([]model.Ingredient, error) { builder := &IngredientBuilder{mutation: f.meta.mutation, counter: f.counter, factory: f} @@ -290,18 +351,21 @@ type IngredientBuilder struct { tagsOverriden bool } +// SetName set the Name field func (b *IngredientBuilder) SetName(i string) *IngredientBuilder { b.nameOverride = i b.nameOverriden = true return b } +// SetTags set the Tags field func (b *IngredientBuilder) SetTags(i []*model.Category) *IngredientBuilder { b.tagsOverride = i b.tagsOverriden = true return b } +// CreateV return a new model.Ingredient func (b *IngredientBuilder) CreateV(ctx context.Context) (model.Ingredient, error) { var d model.Ingredient p, err := b.Create(ctx) @@ -311,6 +375,7 @@ func (b *IngredientBuilder) CreateV(ctx context.Context) (model.Ingredient, erro return d, err } +// Create return a new *model.Ingredient func (b *IngredientBuilder) Create(ctx context.Context) (*model.Ingredient, error) { var preSlice = []func(ctx context.Context, i *model.Ingredient, c int) error{} @@ -361,6 +426,7 @@ func (b *IngredientBuilder) Create(ctx context.Context) (*model.Ingredient, erro } v := &model.Ingredient{} + for _, f := range preSlice { err := f(ctx, v, index) @@ -377,6 +443,11 @@ func (b *IngredientBuilder) Create(ctx context.Context) (*model.Ingredient, erro return nil, err } } + if b.mutation.beforeCreateFunc != nil { + if err := b.mutation.beforeCreateFunc(ctx, v); err != nil { + return nil, err + } + } new := v @@ -387,9 +458,7 @@ func (b *IngredientBuilder) Create(ctx context.Context) (*model.Ingredient, erro } } for _, f := range postSlice { - err := f(ctx, new, index) - if err != nil { return nil, err } diff --git a/examples/recipe/carrier/factory/recipe.go b/examples/recipe/carrier/factory/recipe.go index 10fea0b..1190c8a 100644 --- a/examples/recipe/carrier/factory/recipe.go +++ b/examples/recipe/carrier/factory/recipe.go @@ -33,7 +33,8 @@ type recipeMutation struct { _postIngredientsCountFunc func(ctx context.Context, set bool, obj *model.Recipe, i int) error - afterCreateFunc func(ctx context.Context, i *model.Recipe) error + beforeCreateFunc func(ctx context.Context, i *model.Recipe) error + afterCreateFunc func(ctx context.Context, i *model.Recipe) error } type RecipeMetaFactory struct { mutation recipeMutation @@ -50,6 +51,11 @@ type recipeTrait struct { func RecipeTrait() *recipeTrait { return &recipeTrait{} } +func (*recipeMutation) beforeCreateMutateFunc(fn func(ctx context.Context, i *model.Recipe) error) func(m *recipeMutation) { + return func(m *recipeMutation) { + m.beforeCreateFunc = fn + } +} func (*recipeMutation) afterCreateMutateFunc(fn func(ctx context.Context, i *model.Recipe) error) func(m *recipeMutation) { return func(m *recipeMutation) { m.afterCreateFunc = fn @@ -119,34 +125,49 @@ func (*recipeMutation) nameFactoryMutateFunc(fn func(ctx context.Context) (strin } } +// SetNameSequence register a function which accept a sequence counter and set return value to Name field func (f *RecipeMetaFactory) SetNameSequence(fn func(ctx context.Context, i int) (string, error)) *RecipeMetaFactory { f.mutation.nameSequenceMutateFunc(fn)(&f.mutation) return f } + +// SetNameLazy register a function which accept the build struct and set return value to Name field func (f *RecipeMetaFactory) SetNameLazy(fn func(ctx context.Context, i *model.Recipe) (string, error)) *RecipeMetaFactory { f.mutation.nameLazyMutateFunc(fn)(&f.mutation) return f } + +// SetNameDefault assign a default value to Name field func (f *RecipeMetaFactory) SetNameDefault(v string) *RecipeMetaFactory { f.mutation.nameDefaultMutateFunc(v)(&f.mutation) return f } + +// SetNameFactory register a factory function and assign return value to Name, you can also use related factory's Create/CreateV as input function here func (f *RecipeMetaFactory) SetNameFactory(fn func(ctx context.Context) (string, error)) *RecipeMetaFactory { f.mutation.nameFactoryMutateFunc(fn)(&f.mutation) return f } + +// SetNameSequence register a function which accept a sequence counter and set return value to Name field func (t *recipeTrait) SetNameSequence(fn func(ctx context.Context, i int) (string, error)) *recipeTrait { t.updates = append(t.updates, t.mutation.nameSequenceMutateFunc(fn)) return t } + +// SetNameLazy register a function which accept the build struct and set return value to Name field func (t *recipeTrait) SetNameLazy(fn func(ctx context.Context, i *model.Recipe) (string, error)) *recipeTrait { t.updates = append(t.updates, t.mutation.nameLazyMutateFunc(fn)) return t } + +// SetNameDefault assign a default value to Name field func (t *recipeTrait) SetNameDefault(v string) *recipeTrait { t.updates = append(t.updates, t.mutation.nameDefaultMutateFunc(v)) return t } + +// SetNameFactory register a factory function and assign return value to Name, you can also use related factory's Create/CreateV as input function here func (t *recipeTrait) SetNameFactory(fn func(ctx context.Context) (string, error)) *recipeTrait { t.updates = append(t.updates, t.mutation.nameFactoryMutateFunc(fn)) return t @@ -215,34 +236,49 @@ func (*recipeMutation) authorNameFactoryMutateFunc(fn func(ctx context.Context) } } +// SetAuthorNameSequence register a function which accept a sequence counter and set return value to AuthorName field func (f *RecipeMetaFactory) SetAuthorNameSequence(fn func(ctx context.Context, i int) (string, error)) *RecipeMetaFactory { f.mutation.authorNameSequenceMutateFunc(fn)(&f.mutation) return f } + +// SetAuthorNameLazy register a function which accept the build struct and set return value to AuthorName field func (f *RecipeMetaFactory) SetAuthorNameLazy(fn func(ctx context.Context, i *model.Recipe) (string, error)) *RecipeMetaFactory { f.mutation.authorNameLazyMutateFunc(fn)(&f.mutation) return f } + +// SetAuthorNameDefault assign a default value to AuthorName field func (f *RecipeMetaFactory) SetAuthorNameDefault(v string) *RecipeMetaFactory { f.mutation.authorNameDefaultMutateFunc(v)(&f.mutation) return f } + +// SetAuthorNameFactory register a factory function and assign return value to AuthorName, you can also use related factory's Create/CreateV as input function here func (f *RecipeMetaFactory) SetAuthorNameFactory(fn func(ctx context.Context) (string, error)) *RecipeMetaFactory { f.mutation.authorNameFactoryMutateFunc(fn)(&f.mutation) return f } + +// SetAuthorNameSequence register a function which accept a sequence counter and set return value to AuthorName field func (t *recipeTrait) SetAuthorNameSequence(fn func(ctx context.Context, i int) (string, error)) *recipeTrait { t.updates = append(t.updates, t.mutation.authorNameSequenceMutateFunc(fn)) return t } + +// SetAuthorNameLazy register a function which accept the build struct and set return value to AuthorName field func (t *recipeTrait) SetAuthorNameLazy(fn func(ctx context.Context, i *model.Recipe) (string, error)) *recipeTrait { t.updates = append(t.updates, t.mutation.authorNameLazyMutateFunc(fn)) return t } + +// SetAuthorNameDefault assign a default value to AuthorName field func (t *recipeTrait) SetAuthorNameDefault(v string) *recipeTrait { t.updates = append(t.updates, t.mutation.authorNameDefaultMutateFunc(v)) return t } + +// SetAuthorNameFactory register a factory function and assign return value to AuthorName, you can also use related factory's Create/CreateV as input function here func (t *recipeTrait) SetAuthorNameFactory(fn func(ctx context.Context) (string, error)) *recipeTrait { t.updates = append(t.updates, t.mutation.authorNameFactoryMutateFunc(fn)) return t @@ -311,34 +347,49 @@ func (*recipeMutation) servingsFactoryMutateFunc(fn func(ctx context.Context) (i } } +// SetServingsSequence register a function which accept a sequence counter and set return value to Servings field func (f *RecipeMetaFactory) SetServingsSequence(fn func(ctx context.Context, i int) (int, error)) *RecipeMetaFactory { f.mutation.servingsSequenceMutateFunc(fn)(&f.mutation) return f } + +// SetServingsLazy register a function which accept the build struct and set return value to Servings field func (f *RecipeMetaFactory) SetServingsLazy(fn func(ctx context.Context, i *model.Recipe) (int, error)) *RecipeMetaFactory { f.mutation.servingsLazyMutateFunc(fn)(&f.mutation) return f } + +// SetServingsDefault assign a default value to Servings field func (f *RecipeMetaFactory) SetServingsDefault(v int) *RecipeMetaFactory { f.mutation.servingsDefaultMutateFunc(v)(&f.mutation) return f } + +// SetServingsFactory register a factory function and assign return value to Servings, you can also use related factory's Create/CreateV as input function here func (f *RecipeMetaFactory) SetServingsFactory(fn func(ctx context.Context) (int, error)) *RecipeMetaFactory { f.mutation.servingsFactoryMutateFunc(fn)(&f.mutation) return f } + +// SetServingsSequence register a function which accept a sequence counter and set return value to Servings field func (t *recipeTrait) SetServingsSequence(fn func(ctx context.Context, i int) (int, error)) *recipeTrait { t.updates = append(t.updates, t.mutation.servingsSequenceMutateFunc(fn)) return t } + +// SetServingsLazy register a function which accept the build struct and set return value to Servings field func (t *recipeTrait) SetServingsLazy(fn func(ctx context.Context, i *model.Recipe) (int, error)) *recipeTrait { t.updates = append(t.updates, t.mutation.servingsLazyMutateFunc(fn)) return t } + +// SetServingsDefault assign a default value to Servings field func (t *recipeTrait) SetServingsDefault(v int) *recipeTrait { t.updates = append(t.updates, t.mutation.servingsDefaultMutateFunc(v)) return t } + +// SetServingsFactory register a factory function and assign return value to Servings, you can also use related factory's Create/CreateV as input function here func (t *recipeTrait) SetServingsFactory(fn func(ctx context.Context) (int, error)) *recipeTrait { t.updates = append(t.updates, t.mutation.servingsFactoryMutateFunc(fn)) return t @@ -407,34 +458,49 @@ func (*recipeMutation) stepsFactoryMutateFunc(fn func(ctx context.Context) ([]*m } } +// SetStepsSequence register a function which accept a sequence counter and set return value to Steps field func (f *RecipeMetaFactory) SetStepsSequence(fn func(ctx context.Context, i int) ([]*model.RecipeStep, error)) *RecipeMetaFactory { f.mutation.stepsSequenceMutateFunc(fn)(&f.mutation) return f } + +// SetStepsLazy register a function which accept the build struct and set return value to Steps field func (f *RecipeMetaFactory) SetStepsLazy(fn func(ctx context.Context, i *model.Recipe) ([]*model.RecipeStep, error)) *RecipeMetaFactory { f.mutation.stepsLazyMutateFunc(fn)(&f.mutation) return f } + +// SetStepsDefault assign a default value to Steps field func (f *RecipeMetaFactory) SetStepsDefault(v []*model.RecipeStep) *RecipeMetaFactory { f.mutation.stepsDefaultMutateFunc(v)(&f.mutation) return f } + +// SetStepsFactory register a factory function and assign return value to Steps, you can also use related factory's Create/CreateV as input function here func (f *RecipeMetaFactory) SetStepsFactory(fn func(ctx context.Context) ([]*model.RecipeStep, error)) *RecipeMetaFactory { f.mutation.stepsFactoryMutateFunc(fn)(&f.mutation) return f } + +// SetStepsSequence register a function which accept a sequence counter and set return value to Steps field func (t *recipeTrait) SetStepsSequence(fn func(ctx context.Context, i int) ([]*model.RecipeStep, error)) *recipeTrait { t.updates = append(t.updates, t.mutation.stepsSequenceMutateFunc(fn)) return t } + +// SetStepsLazy register a function which accept the build struct and set return value to Steps field func (t *recipeTrait) SetStepsLazy(fn func(ctx context.Context, i *model.Recipe) ([]*model.RecipeStep, error)) *recipeTrait { t.updates = append(t.updates, t.mutation.stepsLazyMutateFunc(fn)) return t } + +// SetStepsDefault assign a default value to Steps field func (t *recipeTrait) SetStepsDefault(v []*model.RecipeStep) *recipeTrait { t.updates = append(t.updates, t.mutation.stepsDefaultMutateFunc(v)) return t } + +// SetStepsFactory register a factory function and assign return value to Steps, you can also use related factory's Create/CreateV as input function here func (t *recipeTrait) SetStepsFactory(fn func(ctx context.Context) ([]*model.RecipeStep, error)) *recipeTrait { t.updates = append(t.updates, t.mutation.stepsFactoryMutateFunc(fn)) return t @@ -503,34 +569,49 @@ func (*recipeMutation) ingredientsFactoryMutateFunc(fn func(ctx context.Context) } } +// SetIngredientsSequence register a function which accept a sequence counter and set return value to Ingredients field func (f *RecipeMetaFactory) SetIngredientsSequence(fn func(ctx context.Context, i int) ([]*model.RecipeIngredient, error)) *RecipeMetaFactory { f.mutation.ingredientsSequenceMutateFunc(fn)(&f.mutation) return f } + +// SetIngredientsLazy register a function which accept the build struct and set return value to Ingredients field func (f *RecipeMetaFactory) SetIngredientsLazy(fn func(ctx context.Context, i *model.Recipe) ([]*model.RecipeIngredient, error)) *RecipeMetaFactory { f.mutation.ingredientsLazyMutateFunc(fn)(&f.mutation) return f } + +// SetIngredientsDefault assign a default value to Ingredients field func (f *RecipeMetaFactory) SetIngredientsDefault(v []*model.RecipeIngredient) *RecipeMetaFactory { f.mutation.ingredientsDefaultMutateFunc(v)(&f.mutation) return f } + +// SetIngredientsFactory register a factory function and assign return value to Ingredients, you can also use related factory's Create/CreateV as input function here func (f *RecipeMetaFactory) SetIngredientsFactory(fn func(ctx context.Context) ([]*model.RecipeIngredient, error)) *RecipeMetaFactory { f.mutation.ingredientsFactoryMutateFunc(fn)(&f.mutation) return f } + +// SetIngredientsSequence register a function which accept a sequence counter and set return value to Ingredients field func (t *recipeTrait) SetIngredientsSequence(fn func(ctx context.Context, i int) ([]*model.RecipeIngredient, error)) *recipeTrait { t.updates = append(t.updates, t.mutation.ingredientsSequenceMutateFunc(fn)) return t } + +// SetIngredientsLazy register a function which accept the build struct and set return value to Ingredients field func (t *recipeTrait) SetIngredientsLazy(fn func(ctx context.Context, i *model.Recipe) ([]*model.RecipeIngredient, error)) *recipeTrait { t.updates = append(t.updates, t.mutation.ingredientsLazyMutateFunc(fn)) return t } + +// SetIngredientsDefault assign a default value to Ingredients field func (t *recipeTrait) SetIngredientsDefault(v []*model.RecipeIngredient) *recipeTrait { t.updates = append(t.updates, t.mutation.ingredientsDefaultMutateFunc(v)) return t } + +// SetIngredientsFactory register a factory function and assign return value to Ingredients, you can also use related factory's Create/CreateV as input function here func (t *recipeTrait) SetIngredientsFactory(fn func(ctx context.Context) ([]*model.RecipeIngredient, error)) *recipeTrait { t.updates = append(t.updates, t.mutation.ingredientsFactoryMutateFunc(fn)) return t @@ -599,34 +680,49 @@ func (*recipeMutation) tagsFactoryMutateFunc(fn func(ctx context.Context) ([]*mo } } +// SetTagsSequence register a function which accept a sequence counter and set return value to Tags field func (f *RecipeMetaFactory) SetTagsSequence(fn func(ctx context.Context, i int) ([]*model.Category, error)) *RecipeMetaFactory { f.mutation.tagsSequenceMutateFunc(fn)(&f.mutation) return f } + +// SetTagsLazy register a function which accept the build struct and set return value to Tags field func (f *RecipeMetaFactory) SetTagsLazy(fn func(ctx context.Context, i *model.Recipe) ([]*model.Category, error)) *RecipeMetaFactory { f.mutation.tagsLazyMutateFunc(fn)(&f.mutation) return f } + +// SetTagsDefault assign a default value to Tags field func (f *RecipeMetaFactory) SetTagsDefault(v []*model.Category) *RecipeMetaFactory { f.mutation.tagsDefaultMutateFunc(v)(&f.mutation) return f } + +// SetTagsFactory register a factory function and assign return value to Tags, you can also use related factory's Create/CreateV as input function here func (f *RecipeMetaFactory) SetTagsFactory(fn func(ctx context.Context) ([]*model.Category, error)) *RecipeMetaFactory { f.mutation.tagsFactoryMutateFunc(fn)(&f.mutation) return f } + +// SetTagsSequence register a function which accept a sequence counter and set return value to Tags field func (t *recipeTrait) SetTagsSequence(fn func(ctx context.Context, i int) ([]*model.Category, error)) *recipeTrait { t.updates = append(t.updates, t.mutation.tagsSequenceMutateFunc(fn)) return t } + +// SetTagsLazy register a function which accept the build struct and set return value to Tags field func (t *recipeTrait) SetTagsLazy(fn func(ctx context.Context, i *model.Recipe) ([]*model.Category, error)) *recipeTrait { t.updates = append(t.updates, t.mutation.tagsLazyMutateFunc(fn)) return t } + +// SetTagsDefault assign a default value to Tags field func (t *recipeTrait) SetTagsDefault(v []*model.Category) *recipeTrait { t.updates = append(t.updates, t.mutation.tagsDefaultMutateFunc(v)) return t } + +// SetTagsFactory register a factory function and assign return value to Tags, you can also use related factory's Create/CreateV as input function here func (t *recipeTrait) SetTagsFactory(fn func(ctx context.Context) ([]*model.Category, error)) *recipeTrait { t.updates = append(t.updates, t.mutation.tagsFactoryMutateFunc(fn)) return t @@ -695,34 +791,49 @@ func (*recipeMutation) relatedFactoryMutateFunc(fn func(ctx context.Context) ([] } } +// SetRelatedSequence register a function which accept a sequence counter and set return value to Related field func (f *RecipeMetaFactory) SetRelatedSequence(fn func(ctx context.Context, i int) ([]*model.Recipe, error)) *RecipeMetaFactory { f.mutation.relatedSequenceMutateFunc(fn)(&f.mutation) return f } + +// SetRelatedLazy register a function which accept the build struct and set return value to Related field func (f *RecipeMetaFactory) SetRelatedLazy(fn func(ctx context.Context, i *model.Recipe) ([]*model.Recipe, error)) *RecipeMetaFactory { f.mutation.relatedLazyMutateFunc(fn)(&f.mutation) return f } + +// SetRelatedDefault assign a default value to Related field func (f *RecipeMetaFactory) SetRelatedDefault(v []*model.Recipe) *RecipeMetaFactory { f.mutation.relatedDefaultMutateFunc(v)(&f.mutation) return f } + +// SetRelatedFactory register a factory function and assign return value to Related, you can also use related factory's Create/CreateV as input function here func (f *RecipeMetaFactory) SetRelatedFactory(fn func(ctx context.Context) ([]*model.Recipe, error)) *RecipeMetaFactory { f.mutation.relatedFactoryMutateFunc(fn)(&f.mutation) return f } + +// SetRelatedSequence register a function which accept a sequence counter and set return value to Related field func (t *recipeTrait) SetRelatedSequence(fn func(ctx context.Context, i int) ([]*model.Recipe, error)) *recipeTrait { t.updates = append(t.updates, t.mutation.relatedSequenceMutateFunc(fn)) return t } + +// SetRelatedLazy register a function which accept the build struct and set return value to Related field func (t *recipeTrait) SetRelatedLazy(fn func(ctx context.Context, i *model.Recipe) ([]*model.Recipe, error)) *recipeTrait { t.updates = append(t.updates, t.mutation.relatedLazyMutateFunc(fn)) return t } + +// SetRelatedDefault assign a default value to Related field func (t *recipeTrait) SetRelatedDefault(v []*model.Recipe) *recipeTrait { t.updates = append(t.updates, t.mutation.relatedDefaultMutateFunc(v)) return t } + +// SetRelatedFactory register a factory function and assign return value to Related, you can also use related factory's Create/CreateV as input function here func (t *recipeTrait) SetRelatedFactory(fn func(ctx context.Context) ([]*model.Recipe, error)) *recipeTrait { t.updates = append(t.updates, t.mutation.relatedFactoryMutateFunc(fn)) return t @@ -733,6 +844,8 @@ func (*recipeMutation) stepsCountPostMutateFunc(fn func(ctx context.Context, set m._postStepsCountFunc = fn } } + +// SetStepsCountPostFunc register a post function which will be called in factory SetStepsCountPost method func (f *RecipeMetaFactory) SetStepsCountPostFunc(fn func(ctx context.Context, set bool, obj *model.Recipe, i int) error) *RecipeMetaFactory { f.mutation.stepsCountPostMutateFunc(fn)(&f.mutation) return f @@ -747,6 +860,8 @@ func (*recipeMutation) ingredientsCountPostMutateFunc(fn func(ctx context.Contex m._postIngredientsCountFunc = fn } } + +// SetIngredientsCountPostFunc register a post function which will be called in factory SetIngredientsCountPost method func (f *RecipeMetaFactory) SetIngredientsCountPostFunc(fn func(ctx context.Context, set bool, obj *model.Recipe, i int) error) *RecipeMetaFactory { f.mutation.ingredientsCountPostMutateFunc(fn)(&f.mutation) return f @@ -756,25 +871,43 @@ func (t *recipeTrait) SetIngredientsCountPostFunc(fn func(ctx context.Context, s return t } +// SetVeganTrait accept a recipeTrait, will override builder using Trait's methods if enable func (f *RecipeMetaFactory) SetVeganTrait(t *recipeTrait) *RecipeMetaFactory { f.veganTrait = t return f } +// SetKetoTrait accept a recipeTrait, will override builder using Trait's methods if enable func (f *RecipeMetaFactory) SetKetoTrait(t *recipeTrait) *RecipeMetaFactory { f.ketoTrait = t return f } +// SetAfterCreateFunc register a function to be called after struct create func (f *RecipeMetaFactory) SetAfterCreateFunc(fn func(ctx context.Context, i *model.Recipe) error) *RecipeMetaFactory { f.mutation.afterCreateFunc = fn return f } + +// SetBeforeCreateFunc register a function to be called before struct create +func (f *RecipeMetaFactory) SetBeforeCreateFunc(fn func(ctx context.Context, i *model.Recipe) error) *RecipeMetaFactory { + f.mutation.beforeCreateFunc = fn + return f +} + +// SetAfterCreateFunc register a function to be called after struct create func (t *recipeTrait) SetAfterCreateFunc(fn func(ctx context.Context, i *model.Recipe) error) *recipeTrait { t.updates = append(t.updates, t.mutation.afterCreateMutateFunc(fn)) return t } +// SetBeforeCreateFunc register a function to be called before struct create +func (t *recipeTrait) SetBeforeCreateFunc(fn func(ctx context.Context, i *model.Recipe) error) *recipeTrait { + t.updates = append(t.updates, t.mutation.beforeCreateMutateFunc(fn)) + return t +} + +// Build create a RecipeFactory from RecipeMetaFactory func (f *RecipeMetaFactory) Build() *RecipeFactory { return &RecipeFactory{meta: *f, counter: &Counter{}} } @@ -784,6 +917,7 @@ type RecipeFactory struct { counter *Counter } +// SetName set the Name field func (f *RecipeFactory) SetName(i string) *RecipeBuilder { builder := &RecipeBuilder{mutation: f.meta.mutation, counter: f.counter, factory: f} builder.SetName(i) @@ -791,6 +925,7 @@ func (f *RecipeFactory) SetName(i string) *RecipeBuilder { return builder } +// SetAuthorName set the AuthorName field func (f *RecipeFactory) SetAuthorName(i string) *RecipeBuilder { builder := &RecipeBuilder{mutation: f.meta.mutation, counter: f.counter, factory: f} builder.SetAuthorName(i) @@ -798,6 +933,7 @@ func (f *RecipeFactory) SetAuthorName(i string) *RecipeBuilder { return builder } +// SetServings set the Servings field func (f *RecipeFactory) SetServings(i int) *RecipeBuilder { builder := &RecipeBuilder{mutation: f.meta.mutation, counter: f.counter, factory: f} builder.SetServings(i) @@ -805,6 +941,7 @@ func (f *RecipeFactory) SetServings(i int) *RecipeBuilder { return builder } +// SetSteps set the Steps field func (f *RecipeFactory) SetSteps(i []*model.RecipeStep) *RecipeBuilder { builder := &RecipeBuilder{mutation: f.meta.mutation, counter: f.counter, factory: f} builder.SetSteps(i) @@ -812,6 +949,7 @@ func (f *RecipeFactory) SetSteps(i []*model.RecipeStep) *RecipeBuilder { return builder } +// SetIngredients set the Ingredients field func (f *RecipeFactory) SetIngredients(i []*model.RecipeIngredient) *RecipeBuilder { builder := &RecipeBuilder{mutation: f.meta.mutation, counter: f.counter, factory: f} builder.SetIngredients(i) @@ -819,6 +957,7 @@ func (f *RecipeFactory) SetIngredients(i []*model.RecipeIngredient) *RecipeBuild return builder } +// SetTags set the Tags field func (f *RecipeFactory) SetTags(i []*model.Category) *RecipeBuilder { builder := &RecipeBuilder{mutation: f.meta.mutation, counter: f.counter, factory: f} builder.SetTags(i) @@ -826,6 +965,7 @@ func (f *RecipeFactory) SetTags(i []*model.Category) *RecipeBuilder { return builder } +// SetRelated set the Related field func (f *RecipeFactory) SetRelated(i []*model.Recipe) *RecipeBuilder { builder := &RecipeBuilder{mutation: f.meta.mutation, counter: f.counter, factory: f} builder.SetRelated(i) @@ -833,6 +973,7 @@ func (f *RecipeFactory) SetRelated(i []*model.Recipe) *RecipeBuilder { return builder } +// SetStepsCountPost call the post function with int input func (f *RecipeFactory) SetStepsCountPost(i int) *RecipeBuilder { builder := &RecipeBuilder{mutation: f.meta.mutation, counter: f.counter, factory: f} builder.SetStepsCountPost(i) @@ -840,6 +981,7 @@ func (f *RecipeFactory) SetStepsCountPost(i int) *RecipeBuilder { return builder } +// SetIngredientsCountPost call the post function with int input func (f *RecipeFactory) SetIngredientsCountPost(i int) *RecipeBuilder { builder := &RecipeBuilder{mutation: f.meta.mutation, counter: f.counter, factory: f} builder.SetIngredientsCountPost(i) @@ -847,6 +989,7 @@ func (f *RecipeFactory) SetIngredientsCountPost(i int) *RecipeBuilder { return builder } +// WithVeganTrait() enable the Vegan trait func (f *RecipeFactory) WithVeganTrait() *RecipeBuilder { builder := &RecipeBuilder{mutation: f.meta.mutation, counter: f.counter} builder.factory = f @@ -860,6 +1003,7 @@ func (f *RecipeFactory) WithVeganTrait() *RecipeBuilder { return builder } +// WithKetoTrait() enable the Keto trait func (f *RecipeFactory) WithKetoTrait() *RecipeBuilder { builder := &RecipeBuilder{mutation: f.meta.mutation, counter: f.counter} builder.factory = f @@ -873,21 +1017,28 @@ func (f *RecipeFactory) WithKetoTrait() *RecipeBuilder { return builder } +// Create return a new *model.Recipe func (f *RecipeFactory) Create(ctx context.Context) (*model.Recipe, error) { builder := &RecipeBuilder{mutation: f.meta.mutation, counter: f.counter, factory: f} return builder.Create(ctx) } + +// CreateV return a new model.Recipe func (f *RecipeFactory) CreateV(ctx context.Context) (model.Recipe, error) { builder := &RecipeBuilder{mutation: f.meta.mutation, counter: f.counter, factory: f} return builder.CreateV(ctx) } + +// CreateBatch return a []*model.Recipe slice func (f *RecipeFactory) CreateBatch(ctx context.Context, n int) ([]*model.Recipe, error) { builder := &RecipeBuilder{mutation: f.meta.mutation, counter: f.counter, factory: f} return builder.CreateBatch(ctx, n) } + +// CreateBatchV return a []model.Recipe slice func (f *RecipeFactory) CreateBatchV(ctx context.Context, n int) ([]model.Recipe, error) { builder := &RecipeBuilder{mutation: f.meta.mutation, counter: f.counter, factory: f} @@ -927,60 +1078,70 @@ type RecipeBuilder struct { _postIngredientsCountSet bool } +// SetName set the Name field func (b *RecipeBuilder) SetName(i string) *RecipeBuilder { b.nameOverride = i b.nameOverriden = true return b } +// SetAuthorName set the AuthorName field func (b *RecipeBuilder) SetAuthorName(i string) *RecipeBuilder { b.authorNameOverride = i b.authorNameOverriden = true return b } +// SetServings set the Servings field func (b *RecipeBuilder) SetServings(i int) *RecipeBuilder { b.servingsOverride = i b.servingsOverriden = true return b } +// SetSteps set the Steps field func (b *RecipeBuilder) SetSteps(i []*model.RecipeStep) *RecipeBuilder { b.stepsOverride = i b.stepsOverriden = true return b } +// SetIngredients set the Ingredients field func (b *RecipeBuilder) SetIngredients(i []*model.RecipeIngredient) *RecipeBuilder { b.ingredientsOverride = i b.ingredientsOverriden = true return b } +// SetTags set the Tags field func (b *RecipeBuilder) SetTags(i []*model.Category) *RecipeBuilder { b.tagsOverride = i b.tagsOverriden = true return b } +// SetRelated set the Related field func (b *RecipeBuilder) SetRelated(i []*model.Recipe) *RecipeBuilder { b.relatedOverride = i b.relatedOverriden = true return b } +// SetStepsCountPost call the post function with int input func (b *RecipeBuilder) SetStepsCountPost(i int) *RecipeBuilder { b._postStepsCount = i b._postStepsCountSet = true return b } +// SetIngredientsCountPost call the post function with int input func (b *RecipeBuilder) SetIngredientsCountPost(i int) *RecipeBuilder { b._postIngredientsCount = i b._postIngredientsCountSet = true return b } +// WithVeganTrait() enable the Vegan trait func (b *RecipeBuilder) WithVeganTrait() *RecipeBuilder { if b.factory.meta.veganTrait == nil { return b @@ -991,6 +1152,7 @@ func (b *RecipeBuilder) WithVeganTrait() *RecipeBuilder { return b } +// WithKetoTrait() enable the Keto trait func (b *RecipeBuilder) WithKetoTrait() *RecipeBuilder { if b.factory.meta.ketoTrait == nil { return b @@ -1001,6 +1163,7 @@ func (b *RecipeBuilder) WithKetoTrait() *RecipeBuilder { return b } +// CreateV return a new model.Recipe func (b *RecipeBuilder) CreateV(ctx context.Context) (model.Recipe, error) { var d model.Recipe p, err := b.Create(ctx) @@ -1010,6 +1173,7 @@ func (b *RecipeBuilder) CreateV(ctx context.Context) (model.Recipe, error) { return d, err } +// Create return a new *model.Recipe func (b *RecipeBuilder) Create(ctx context.Context) (*model.Recipe, error) { var preSlice = []func(ctx context.Context, i *model.Recipe, c int) error{} @@ -1174,6 +1338,7 @@ func (b *RecipeBuilder) Create(ctx context.Context) (*model.Recipe, error) { } v := &model.Recipe{} + for _, f := range preSlice { err := f(ctx, v, index) @@ -1190,6 +1355,11 @@ func (b *RecipeBuilder) Create(ctx context.Context) (*model.Recipe, error) { return nil, err } } + if b.mutation.beforeCreateFunc != nil { + if err := b.mutation.beforeCreateFunc(ctx, v); err != nil { + return nil, err + } + } new := v @@ -1200,9 +1370,7 @@ func (b *RecipeBuilder) Create(ctx context.Context) (*model.Recipe, error) { } } for _, f := range postSlice { - err := f(ctx, new, index) - if err != nil { return nil, err } diff --git a/examples/recipe/carrier/factory/recipeingredient.go b/examples/recipe/carrier/factory/recipeingredient.go index 139fb23..e796b00 100644 --- a/examples/recipe/carrier/factory/recipeingredient.go +++ b/examples/recipe/carrier/factory/recipeingredient.go @@ -17,7 +17,8 @@ type recipeIngredientMutation struct { unitType int unitFunc func(ctx context.Context, i *model.RecipeIngredient, c int) error - afterCreateFunc func(ctx context.Context, i *model.RecipeIngredient) error + beforeCreateFunc func(ctx context.Context, i *model.RecipeIngredient) error + afterCreateFunc func(ctx context.Context, i *model.RecipeIngredient) error } type RecipeIngredientMetaFactory struct { mutation recipeIngredientMutation @@ -30,6 +31,11 @@ type recipeIngredientTrait struct { func RecipeIngredientTrait() *recipeIngredientTrait { return &recipeIngredientTrait{} } +func (*recipeIngredientMutation) beforeCreateMutateFunc(fn func(ctx context.Context, i *model.RecipeIngredient) error) func(m *recipeIngredientMutation) { + return func(m *recipeIngredientMutation) { + m.beforeCreateFunc = fn + } +} func (*recipeIngredientMutation) afterCreateMutateFunc(fn func(ctx context.Context, i *model.RecipeIngredient) error) func(m *recipeIngredientMutation) { return func(m *recipeIngredientMutation) { m.afterCreateFunc = fn @@ -99,34 +105,49 @@ func (*recipeIngredientMutation) ingredientFactoryMutateFunc(fn func(ctx context } } +// SetIngredientSequence register a function which accept a sequence counter and set return value to Ingredient field func (f *RecipeIngredientMetaFactory) SetIngredientSequence(fn func(ctx context.Context, i int) (*model.Ingredient, error)) *RecipeIngredientMetaFactory { f.mutation.ingredientSequenceMutateFunc(fn)(&f.mutation) return f } + +// SetIngredientLazy register a function which accept the build struct and set return value to Ingredient field func (f *RecipeIngredientMetaFactory) SetIngredientLazy(fn func(ctx context.Context, i *model.RecipeIngredient) (*model.Ingredient, error)) *RecipeIngredientMetaFactory { f.mutation.ingredientLazyMutateFunc(fn)(&f.mutation) return f } + +// SetIngredientDefault assign a default value to Ingredient field func (f *RecipeIngredientMetaFactory) SetIngredientDefault(v *model.Ingredient) *RecipeIngredientMetaFactory { f.mutation.ingredientDefaultMutateFunc(v)(&f.mutation) return f } + +// SetIngredientFactory register a factory function and assign return value to Ingredient, you can also use related factory's Create/CreateV as input function here func (f *RecipeIngredientMetaFactory) SetIngredientFactory(fn func(ctx context.Context) (*model.Ingredient, error)) *RecipeIngredientMetaFactory { f.mutation.ingredientFactoryMutateFunc(fn)(&f.mutation) return f } + +// SetIngredientSequence register a function which accept a sequence counter and set return value to Ingredient field func (t *recipeIngredientTrait) SetIngredientSequence(fn func(ctx context.Context, i int) (*model.Ingredient, error)) *recipeIngredientTrait { t.updates = append(t.updates, t.mutation.ingredientSequenceMutateFunc(fn)) return t } + +// SetIngredientLazy register a function which accept the build struct and set return value to Ingredient field func (t *recipeIngredientTrait) SetIngredientLazy(fn func(ctx context.Context, i *model.RecipeIngredient) (*model.Ingredient, error)) *recipeIngredientTrait { t.updates = append(t.updates, t.mutation.ingredientLazyMutateFunc(fn)) return t } + +// SetIngredientDefault assign a default value to Ingredient field func (t *recipeIngredientTrait) SetIngredientDefault(v *model.Ingredient) *recipeIngredientTrait { t.updates = append(t.updates, t.mutation.ingredientDefaultMutateFunc(v)) return t } + +// SetIngredientFactory register a factory function and assign return value to Ingredient, you can also use related factory's Create/CreateV as input function here func (t *recipeIngredientTrait) SetIngredientFactory(fn func(ctx context.Context) (*model.Ingredient, error)) *recipeIngredientTrait { t.updates = append(t.updates, t.mutation.ingredientFactoryMutateFunc(fn)) return t @@ -195,34 +216,49 @@ func (*recipeIngredientMutation) quantityFactoryMutateFunc(fn func(ctx context.C } } +// SetQuantitySequence register a function which accept a sequence counter and set return value to Quantity field func (f *RecipeIngredientMetaFactory) SetQuantitySequence(fn func(ctx context.Context, i int) (float32, error)) *RecipeIngredientMetaFactory { f.mutation.quantitySequenceMutateFunc(fn)(&f.mutation) return f } + +// SetQuantityLazy register a function which accept the build struct and set return value to Quantity field func (f *RecipeIngredientMetaFactory) SetQuantityLazy(fn func(ctx context.Context, i *model.RecipeIngredient) (float32, error)) *RecipeIngredientMetaFactory { f.mutation.quantityLazyMutateFunc(fn)(&f.mutation) return f } + +// SetQuantityDefault assign a default value to Quantity field func (f *RecipeIngredientMetaFactory) SetQuantityDefault(v float32) *RecipeIngredientMetaFactory { f.mutation.quantityDefaultMutateFunc(v)(&f.mutation) return f } + +// SetQuantityFactory register a factory function and assign return value to Quantity, you can also use related factory's Create/CreateV as input function here func (f *RecipeIngredientMetaFactory) SetQuantityFactory(fn func(ctx context.Context) (float32, error)) *RecipeIngredientMetaFactory { f.mutation.quantityFactoryMutateFunc(fn)(&f.mutation) return f } + +// SetQuantitySequence register a function which accept a sequence counter and set return value to Quantity field func (t *recipeIngredientTrait) SetQuantitySequence(fn func(ctx context.Context, i int) (float32, error)) *recipeIngredientTrait { t.updates = append(t.updates, t.mutation.quantitySequenceMutateFunc(fn)) return t } + +// SetQuantityLazy register a function which accept the build struct and set return value to Quantity field func (t *recipeIngredientTrait) SetQuantityLazy(fn func(ctx context.Context, i *model.RecipeIngredient) (float32, error)) *recipeIngredientTrait { t.updates = append(t.updates, t.mutation.quantityLazyMutateFunc(fn)) return t } + +// SetQuantityDefault assign a default value to Quantity field func (t *recipeIngredientTrait) SetQuantityDefault(v float32) *recipeIngredientTrait { t.updates = append(t.updates, t.mutation.quantityDefaultMutateFunc(v)) return t } + +// SetQuantityFactory register a factory function and assign return value to Quantity, you can also use related factory's Create/CreateV as input function here func (t *recipeIngredientTrait) SetQuantityFactory(fn func(ctx context.Context) (float32, error)) *recipeIngredientTrait { t.updates = append(t.updates, t.mutation.quantityFactoryMutateFunc(fn)) return t @@ -291,48 +327,79 @@ func (*recipeIngredientMutation) unitFactoryMutateFunc(fn func(ctx context.Conte } } +// SetUnitSequence register a function which accept a sequence counter and set return value to Unit field func (f *RecipeIngredientMetaFactory) SetUnitSequence(fn func(ctx context.Context, i int) (string, error)) *RecipeIngredientMetaFactory { f.mutation.unitSequenceMutateFunc(fn)(&f.mutation) return f } + +// SetUnitLazy register a function which accept the build struct and set return value to Unit field func (f *RecipeIngredientMetaFactory) SetUnitLazy(fn func(ctx context.Context, i *model.RecipeIngredient) (string, error)) *RecipeIngredientMetaFactory { f.mutation.unitLazyMutateFunc(fn)(&f.mutation) return f } + +// SetUnitDefault assign a default value to Unit field func (f *RecipeIngredientMetaFactory) SetUnitDefault(v string) *RecipeIngredientMetaFactory { f.mutation.unitDefaultMutateFunc(v)(&f.mutation) return f } + +// SetUnitFactory register a factory function and assign return value to Unit, you can also use related factory's Create/CreateV as input function here func (f *RecipeIngredientMetaFactory) SetUnitFactory(fn func(ctx context.Context) (string, error)) *RecipeIngredientMetaFactory { f.mutation.unitFactoryMutateFunc(fn)(&f.mutation) return f } + +// SetUnitSequence register a function which accept a sequence counter and set return value to Unit field func (t *recipeIngredientTrait) SetUnitSequence(fn func(ctx context.Context, i int) (string, error)) *recipeIngredientTrait { t.updates = append(t.updates, t.mutation.unitSequenceMutateFunc(fn)) return t } + +// SetUnitLazy register a function which accept the build struct and set return value to Unit field func (t *recipeIngredientTrait) SetUnitLazy(fn func(ctx context.Context, i *model.RecipeIngredient) (string, error)) *recipeIngredientTrait { t.updates = append(t.updates, t.mutation.unitLazyMutateFunc(fn)) return t } + +// SetUnitDefault assign a default value to Unit field func (t *recipeIngredientTrait) SetUnitDefault(v string) *recipeIngredientTrait { t.updates = append(t.updates, t.mutation.unitDefaultMutateFunc(v)) return t } + +// SetUnitFactory register a factory function and assign return value to Unit, you can also use related factory's Create/CreateV as input function here func (t *recipeIngredientTrait) SetUnitFactory(fn func(ctx context.Context) (string, error)) *recipeIngredientTrait { t.updates = append(t.updates, t.mutation.unitFactoryMutateFunc(fn)) return t } +// SetAfterCreateFunc register a function to be called after struct create func (f *RecipeIngredientMetaFactory) SetAfterCreateFunc(fn func(ctx context.Context, i *model.RecipeIngredient) error) *RecipeIngredientMetaFactory { f.mutation.afterCreateFunc = fn return f } + +// SetBeforeCreateFunc register a function to be called before struct create +func (f *RecipeIngredientMetaFactory) SetBeforeCreateFunc(fn func(ctx context.Context, i *model.RecipeIngredient) error) *RecipeIngredientMetaFactory { + f.mutation.beforeCreateFunc = fn + return f +} + +// SetAfterCreateFunc register a function to be called after struct create func (t *recipeIngredientTrait) SetAfterCreateFunc(fn func(ctx context.Context, i *model.RecipeIngredient) error) *recipeIngredientTrait { t.updates = append(t.updates, t.mutation.afterCreateMutateFunc(fn)) return t } +// SetBeforeCreateFunc register a function to be called before struct create +func (t *recipeIngredientTrait) SetBeforeCreateFunc(fn func(ctx context.Context, i *model.RecipeIngredient) error) *recipeIngredientTrait { + t.updates = append(t.updates, t.mutation.beforeCreateMutateFunc(fn)) + return t +} + +// Build create a RecipeIngredientFactory from RecipeIngredientMetaFactory func (f *RecipeIngredientMetaFactory) Build() *RecipeIngredientFactory { return &RecipeIngredientFactory{meta: *f, counter: &Counter{}} } @@ -342,6 +409,7 @@ type RecipeIngredientFactory struct { counter *Counter } +// SetIngredient set the Ingredient field func (f *RecipeIngredientFactory) SetIngredient(i *model.Ingredient) *RecipeIngredientBuilder { builder := &RecipeIngredientBuilder{mutation: f.meta.mutation, counter: f.counter, factory: f} builder.SetIngredient(i) @@ -349,6 +417,7 @@ func (f *RecipeIngredientFactory) SetIngredient(i *model.Ingredient) *RecipeIngr return builder } +// SetQuantity set the Quantity field func (f *RecipeIngredientFactory) SetQuantity(i float32) *RecipeIngredientBuilder { builder := &RecipeIngredientBuilder{mutation: f.meta.mutation, counter: f.counter, factory: f} builder.SetQuantity(i) @@ -356,6 +425,7 @@ func (f *RecipeIngredientFactory) SetQuantity(i float32) *RecipeIngredientBuilde return builder } +// SetUnit set the Unit field func (f *RecipeIngredientFactory) SetUnit(i string) *RecipeIngredientBuilder { builder := &RecipeIngredientBuilder{mutation: f.meta.mutation, counter: f.counter, factory: f} builder.SetUnit(i) @@ -363,21 +433,28 @@ func (f *RecipeIngredientFactory) SetUnit(i string) *RecipeIngredientBuilder { return builder } +// Create return a new *model.RecipeIngredient func (f *RecipeIngredientFactory) Create(ctx context.Context) (*model.RecipeIngredient, error) { builder := &RecipeIngredientBuilder{mutation: f.meta.mutation, counter: f.counter, factory: f} return builder.Create(ctx) } + +// CreateV return a new model.RecipeIngredient func (f *RecipeIngredientFactory) CreateV(ctx context.Context) (model.RecipeIngredient, error) { builder := &RecipeIngredientBuilder{mutation: f.meta.mutation, counter: f.counter, factory: f} return builder.CreateV(ctx) } + +// CreateBatch return a []*model.RecipeIngredient slice func (f *RecipeIngredientFactory) CreateBatch(ctx context.Context, n int) ([]*model.RecipeIngredient, error) { builder := &RecipeIngredientBuilder{mutation: f.meta.mutation, counter: f.counter, factory: f} return builder.CreateBatch(ctx, n) } + +// CreateBatchV return a []model.RecipeIngredient slice func (f *RecipeIngredientFactory) CreateBatchV(ctx context.Context, n int) ([]model.RecipeIngredient, error) { builder := &RecipeIngredientBuilder{mutation: f.meta.mutation, counter: f.counter, factory: f} @@ -399,24 +476,28 @@ type RecipeIngredientBuilder struct { unitOverriden bool } +// SetIngredient set the Ingredient field func (b *RecipeIngredientBuilder) SetIngredient(i *model.Ingredient) *RecipeIngredientBuilder { b.ingredientOverride = i b.ingredientOverriden = true return b } +// SetQuantity set the Quantity field func (b *RecipeIngredientBuilder) SetQuantity(i float32) *RecipeIngredientBuilder { b.quantityOverride = i b.quantityOverriden = true return b } +// SetUnit set the Unit field func (b *RecipeIngredientBuilder) SetUnit(i string) *RecipeIngredientBuilder { b.unitOverride = i b.unitOverriden = true return b } +// CreateV return a new model.RecipeIngredient func (b *RecipeIngredientBuilder) CreateV(ctx context.Context) (model.RecipeIngredient, error) { var d model.RecipeIngredient p, err := b.Create(ctx) @@ -426,6 +507,7 @@ func (b *RecipeIngredientBuilder) CreateV(ctx context.Context) (model.RecipeIngr return d, err } +// Create return a new *model.RecipeIngredient func (b *RecipeIngredientBuilder) Create(ctx context.Context) (*model.RecipeIngredient, error) { var preSlice = []func(ctx context.Context, i *model.RecipeIngredient, c int) error{} @@ -496,6 +578,7 @@ func (b *RecipeIngredientBuilder) Create(ctx context.Context) (*model.RecipeIngr } v := &model.RecipeIngredient{} + for _, f := range preSlice { err := f(ctx, v, index) @@ -512,6 +595,11 @@ func (b *RecipeIngredientBuilder) Create(ctx context.Context) (*model.RecipeIngr return nil, err } } + if b.mutation.beforeCreateFunc != nil { + if err := b.mutation.beforeCreateFunc(ctx, v); err != nil { + return nil, err + } + } new := v @@ -522,9 +610,7 @@ func (b *RecipeIngredientBuilder) Create(ctx context.Context) (*model.RecipeIngr } } for _, f := range postSlice { - err := f(ctx, new, index) - if err != nil { return nil, err } diff --git a/examples/recipe/carrier/factory/recipestep.go b/examples/recipe/carrier/factory/recipestep.go index 92fc99a..10397a1 100644 --- a/examples/recipe/carrier/factory/recipestep.go +++ b/examples/recipe/carrier/factory/recipestep.go @@ -11,7 +11,8 @@ type recipeStepMutation struct { textType int textFunc func(ctx context.Context, i *model.RecipeStep, c int) error - afterCreateFunc func(ctx context.Context, i *model.RecipeStep) error + beforeCreateFunc func(ctx context.Context, i *model.RecipeStep) error + afterCreateFunc func(ctx context.Context, i *model.RecipeStep) error } type RecipeStepMetaFactory struct { mutation recipeStepMutation @@ -24,6 +25,11 @@ type recipeStepTrait struct { func RecipeStepTrait() *recipeStepTrait { return &recipeStepTrait{} } +func (*recipeStepMutation) beforeCreateMutateFunc(fn func(ctx context.Context, i *model.RecipeStep) error) func(m *recipeStepMutation) { + return func(m *recipeStepMutation) { + m.beforeCreateFunc = fn + } +} func (*recipeStepMutation) afterCreateMutateFunc(fn func(ctx context.Context, i *model.RecipeStep) error) func(m *recipeStepMutation) { return func(m *recipeStepMutation) { m.afterCreateFunc = fn @@ -93,48 +99,79 @@ func (*recipeStepMutation) textFactoryMutateFunc(fn func(ctx context.Context) (s } } +// SetTextSequence register a function which accept a sequence counter and set return value to Text field func (f *RecipeStepMetaFactory) SetTextSequence(fn func(ctx context.Context, i int) (string, error)) *RecipeStepMetaFactory { f.mutation.textSequenceMutateFunc(fn)(&f.mutation) return f } + +// SetTextLazy register a function which accept the build struct and set return value to Text field func (f *RecipeStepMetaFactory) SetTextLazy(fn func(ctx context.Context, i *model.RecipeStep) (string, error)) *RecipeStepMetaFactory { f.mutation.textLazyMutateFunc(fn)(&f.mutation) return f } + +// SetTextDefault assign a default value to Text field func (f *RecipeStepMetaFactory) SetTextDefault(v string) *RecipeStepMetaFactory { f.mutation.textDefaultMutateFunc(v)(&f.mutation) return f } + +// SetTextFactory register a factory function and assign return value to Text, you can also use related factory's Create/CreateV as input function here func (f *RecipeStepMetaFactory) SetTextFactory(fn func(ctx context.Context) (string, error)) *RecipeStepMetaFactory { f.mutation.textFactoryMutateFunc(fn)(&f.mutation) return f } + +// SetTextSequence register a function which accept a sequence counter and set return value to Text field func (t *recipeStepTrait) SetTextSequence(fn func(ctx context.Context, i int) (string, error)) *recipeStepTrait { t.updates = append(t.updates, t.mutation.textSequenceMutateFunc(fn)) return t } + +// SetTextLazy register a function which accept the build struct and set return value to Text field func (t *recipeStepTrait) SetTextLazy(fn func(ctx context.Context, i *model.RecipeStep) (string, error)) *recipeStepTrait { t.updates = append(t.updates, t.mutation.textLazyMutateFunc(fn)) return t } + +// SetTextDefault assign a default value to Text field func (t *recipeStepTrait) SetTextDefault(v string) *recipeStepTrait { t.updates = append(t.updates, t.mutation.textDefaultMutateFunc(v)) return t } + +// SetTextFactory register a factory function and assign return value to Text, you can also use related factory's Create/CreateV as input function here func (t *recipeStepTrait) SetTextFactory(fn func(ctx context.Context) (string, error)) *recipeStepTrait { t.updates = append(t.updates, t.mutation.textFactoryMutateFunc(fn)) return t } +// SetAfterCreateFunc register a function to be called after struct create func (f *RecipeStepMetaFactory) SetAfterCreateFunc(fn func(ctx context.Context, i *model.RecipeStep) error) *RecipeStepMetaFactory { f.mutation.afterCreateFunc = fn return f } + +// SetBeforeCreateFunc register a function to be called before struct create +func (f *RecipeStepMetaFactory) SetBeforeCreateFunc(fn func(ctx context.Context, i *model.RecipeStep) error) *RecipeStepMetaFactory { + f.mutation.beforeCreateFunc = fn + return f +} + +// SetAfterCreateFunc register a function to be called after struct create func (t *recipeStepTrait) SetAfterCreateFunc(fn func(ctx context.Context, i *model.RecipeStep) error) *recipeStepTrait { t.updates = append(t.updates, t.mutation.afterCreateMutateFunc(fn)) return t } +// SetBeforeCreateFunc register a function to be called before struct create +func (t *recipeStepTrait) SetBeforeCreateFunc(fn func(ctx context.Context, i *model.RecipeStep) error) *recipeStepTrait { + t.updates = append(t.updates, t.mutation.beforeCreateMutateFunc(fn)) + return t +} + +// Build create a RecipeStepFactory from RecipeStepMetaFactory func (f *RecipeStepMetaFactory) Build() *RecipeStepFactory { return &RecipeStepFactory{meta: *f, counter: &Counter{}} } @@ -144,6 +181,7 @@ type RecipeStepFactory struct { counter *Counter } +// SetText set the Text field func (f *RecipeStepFactory) SetText(i string) *RecipeStepBuilder { builder := &RecipeStepBuilder{mutation: f.meta.mutation, counter: f.counter, factory: f} builder.SetText(i) @@ -151,21 +189,28 @@ func (f *RecipeStepFactory) SetText(i string) *RecipeStepBuilder { return builder } +// Create return a new *model.RecipeStep func (f *RecipeStepFactory) Create(ctx context.Context) (*model.RecipeStep, error) { builder := &RecipeStepBuilder{mutation: f.meta.mutation, counter: f.counter, factory: f} return builder.Create(ctx) } + +// CreateV return a new model.RecipeStep func (f *RecipeStepFactory) CreateV(ctx context.Context) (model.RecipeStep, error) { builder := &RecipeStepBuilder{mutation: f.meta.mutation, counter: f.counter, factory: f} return builder.CreateV(ctx) } + +// CreateBatch return a []*model.RecipeStep slice func (f *RecipeStepFactory) CreateBatch(ctx context.Context, n int) ([]*model.RecipeStep, error) { builder := &RecipeStepBuilder{mutation: f.meta.mutation, counter: f.counter, factory: f} return builder.CreateBatch(ctx, n) } + +// CreateBatchV return a []model.RecipeStep slice func (f *RecipeStepFactory) CreateBatchV(ctx context.Context, n int) ([]model.RecipeStep, error) { builder := &RecipeStepBuilder{mutation: f.meta.mutation, counter: f.counter, factory: f} @@ -181,12 +226,14 @@ type RecipeStepBuilder struct { textOverriden bool } +// SetText set the Text field func (b *RecipeStepBuilder) SetText(i string) *RecipeStepBuilder { b.textOverride = i b.textOverriden = true return b } +// CreateV return a new model.RecipeStep func (b *RecipeStepBuilder) CreateV(ctx context.Context) (model.RecipeStep, error) { var d model.RecipeStep p, err := b.Create(ctx) @@ -196,6 +243,7 @@ func (b *RecipeStepBuilder) CreateV(ctx context.Context) (model.RecipeStep, erro return d, err } +// Create return a new *model.RecipeStep func (b *RecipeStepBuilder) Create(ctx context.Context) (*model.RecipeStep, error) { var preSlice = []func(ctx context.Context, i *model.RecipeStep, c int) error{} @@ -226,6 +274,7 @@ func (b *RecipeStepBuilder) Create(ctx context.Context) (*model.RecipeStep, erro } v := &model.RecipeStep{} + for _, f := range preSlice { err := f(ctx, v, index) @@ -242,6 +291,11 @@ func (b *RecipeStepBuilder) Create(ctx context.Context) (*model.RecipeStep, erro return nil, err } } + if b.mutation.beforeCreateFunc != nil { + if err := b.mutation.beforeCreateFunc(ctx, v); err != nil { + return nil, err + } + } new := v @@ -252,9 +306,7 @@ func (b *RecipeStepBuilder) Create(ctx context.Context) (*model.RecipeStep, erro } } for _, f := range postSlice { - err := f(ctx, new, index) - if err != nil { return nil, err } diff --git a/examples/recipe/carrier/factory/user.go b/examples/recipe/carrier/factory/user.go index 9a28af0..44304ca 100644 --- a/examples/recipe/carrier/factory/user.go +++ b/examples/recipe/carrier/factory/user.go @@ -19,7 +19,8 @@ type userMutation struct { _postRecipesFunc func(ctx context.Context, set bool, obj *model.User, i int) error - afterCreateFunc func(ctx context.Context, i *model.User) error + beforeCreateFunc func(ctx context.Context, i *model.User) error + afterCreateFunc func(ctx context.Context, i *model.User) error } type UserMetaFactory struct { mutation userMutation @@ -32,6 +33,11 @@ type userTrait struct { func UserTrait() *userTrait { return &userTrait{} } +func (*userMutation) beforeCreateMutateFunc(fn func(ctx context.Context, i *model.User) error) func(m *userMutation) { + return func(m *userMutation) { + m.beforeCreateFunc = fn + } +} func (*userMutation) afterCreateMutateFunc(fn func(ctx context.Context, i *model.User) error) func(m *userMutation) { return func(m *userMutation) { m.afterCreateFunc = fn @@ -101,34 +107,49 @@ func (*userMutation) nameFactoryMutateFunc(fn func(ctx context.Context) (string, } } +// SetNameSequence register a function which accept a sequence counter and set return value to Name field func (f *UserMetaFactory) SetNameSequence(fn func(ctx context.Context, i int) (string, error)) *UserMetaFactory { f.mutation.nameSequenceMutateFunc(fn)(&f.mutation) return f } + +// SetNameLazy register a function which accept the build struct and set return value to Name field func (f *UserMetaFactory) SetNameLazy(fn func(ctx context.Context, i *model.User) (string, error)) *UserMetaFactory { f.mutation.nameLazyMutateFunc(fn)(&f.mutation) return f } + +// SetNameDefault assign a default value to Name field func (f *UserMetaFactory) SetNameDefault(v string) *UserMetaFactory { f.mutation.nameDefaultMutateFunc(v)(&f.mutation) return f } + +// SetNameFactory register a factory function and assign return value to Name, you can also use related factory's Create/CreateV as input function here func (f *UserMetaFactory) SetNameFactory(fn func(ctx context.Context) (string, error)) *UserMetaFactory { f.mutation.nameFactoryMutateFunc(fn)(&f.mutation) return f } + +// SetNameSequence register a function which accept a sequence counter and set return value to Name field func (t *userTrait) SetNameSequence(fn func(ctx context.Context, i int) (string, error)) *userTrait { t.updates = append(t.updates, t.mutation.nameSequenceMutateFunc(fn)) return t } + +// SetNameLazy register a function which accept the build struct and set return value to Name field func (t *userTrait) SetNameLazy(fn func(ctx context.Context, i *model.User) (string, error)) *userTrait { t.updates = append(t.updates, t.mutation.nameLazyMutateFunc(fn)) return t } + +// SetNameDefault assign a default value to Name field func (t *userTrait) SetNameDefault(v string) *userTrait { t.updates = append(t.updates, t.mutation.nameDefaultMutateFunc(v)) return t } + +// SetNameFactory register a factory function and assign return value to Name, you can also use related factory's Create/CreateV as input function here func (t *userTrait) SetNameFactory(fn func(ctx context.Context) (string, error)) *userTrait { t.updates = append(t.updates, t.mutation.nameFactoryMutateFunc(fn)) return t @@ -197,34 +218,49 @@ func (*userMutation) tagsFactoryMutateFunc(fn func(ctx context.Context) ([]*mode } } +// SetTagsSequence register a function which accept a sequence counter and set return value to Tags field func (f *UserMetaFactory) SetTagsSequence(fn func(ctx context.Context, i int) ([]*model.Category, error)) *UserMetaFactory { f.mutation.tagsSequenceMutateFunc(fn)(&f.mutation) return f } + +// SetTagsLazy register a function which accept the build struct and set return value to Tags field func (f *UserMetaFactory) SetTagsLazy(fn func(ctx context.Context, i *model.User) ([]*model.Category, error)) *UserMetaFactory { f.mutation.tagsLazyMutateFunc(fn)(&f.mutation) return f } + +// SetTagsDefault assign a default value to Tags field func (f *UserMetaFactory) SetTagsDefault(v []*model.Category) *UserMetaFactory { f.mutation.tagsDefaultMutateFunc(v)(&f.mutation) return f } + +// SetTagsFactory register a factory function and assign return value to Tags, you can also use related factory's Create/CreateV as input function here func (f *UserMetaFactory) SetTagsFactory(fn func(ctx context.Context) ([]*model.Category, error)) *UserMetaFactory { f.mutation.tagsFactoryMutateFunc(fn)(&f.mutation) return f } + +// SetTagsSequence register a function which accept a sequence counter and set return value to Tags field func (t *userTrait) SetTagsSequence(fn func(ctx context.Context, i int) ([]*model.Category, error)) *userTrait { t.updates = append(t.updates, t.mutation.tagsSequenceMutateFunc(fn)) return t } + +// SetTagsLazy register a function which accept the build struct and set return value to Tags field func (t *userTrait) SetTagsLazy(fn func(ctx context.Context, i *model.User) ([]*model.Category, error)) *userTrait { t.updates = append(t.updates, t.mutation.tagsLazyMutateFunc(fn)) return t } + +// SetTagsDefault assign a default value to Tags field func (t *userTrait) SetTagsDefault(v []*model.Category) *userTrait { t.updates = append(t.updates, t.mutation.tagsDefaultMutateFunc(v)) return t } + +// SetTagsFactory register a factory function and assign return value to Tags, you can also use related factory's Create/CreateV as input function here func (t *userTrait) SetTagsFactory(fn func(ctx context.Context) ([]*model.Category, error)) *userTrait { t.updates = append(t.updates, t.mutation.tagsFactoryMutateFunc(fn)) return t @@ -293,34 +329,49 @@ func (*userMutation) recipesFactoryMutateFunc(fn func(ctx context.Context) ([]*m } } +// SetRecipesSequence register a function which accept a sequence counter and set return value to Recipes field func (f *UserMetaFactory) SetRecipesSequence(fn func(ctx context.Context, i int) ([]*model.Recipe, error)) *UserMetaFactory { f.mutation.recipesSequenceMutateFunc(fn)(&f.mutation) return f } + +// SetRecipesLazy register a function which accept the build struct and set return value to Recipes field func (f *UserMetaFactory) SetRecipesLazy(fn func(ctx context.Context, i *model.User) ([]*model.Recipe, error)) *UserMetaFactory { f.mutation.recipesLazyMutateFunc(fn)(&f.mutation) return f } + +// SetRecipesDefault assign a default value to Recipes field func (f *UserMetaFactory) SetRecipesDefault(v []*model.Recipe) *UserMetaFactory { f.mutation.recipesDefaultMutateFunc(v)(&f.mutation) return f } + +// SetRecipesFactory register a factory function and assign return value to Recipes, you can also use related factory's Create/CreateV as input function here func (f *UserMetaFactory) SetRecipesFactory(fn func(ctx context.Context) ([]*model.Recipe, error)) *UserMetaFactory { f.mutation.recipesFactoryMutateFunc(fn)(&f.mutation) return f } + +// SetRecipesSequence register a function which accept a sequence counter and set return value to Recipes field func (t *userTrait) SetRecipesSequence(fn func(ctx context.Context, i int) ([]*model.Recipe, error)) *userTrait { t.updates = append(t.updates, t.mutation.recipesSequenceMutateFunc(fn)) return t } + +// SetRecipesLazy register a function which accept the build struct and set return value to Recipes field func (t *userTrait) SetRecipesLazy(fn func(ctx context.Context, i *model.User) ([]*model.Recipe, error)) *userTrait { t.updates = append(t.updates, t.mutation.recipesLazyMutateFunc(fn)) return t } + +// SetRecipesDefault assign a default value to Recipes field func (t *userTrait) SetRecipesDefault(v []*model.Recipe) *userTrait { t.updates = append(t.updates, t.mutation.recipesDefaultMutateFunc(v)) return t } + +// SetRecipesFactory register a factory function and assign return value to Recipes, you can also use related factory's Create/CreateV as input function here func (t *userTrait) SetRecipesFactory(fn func(ctx context.Context) ([]*model.Recipe, error)) *userTrait { t.updates = append(t.updates, t.mutation.recipesFactoryMutateFunc(fn)) return t @@ -331,6 +382,8 @@ func (*userMutation) recipesPostMutateFunc(fn func(ctx context.Context, set bool m._postRecipesFunc = fn } } + +// SetRecipesPostFunc register a post function which will be called in factory SetRecipesPost method func (f *UserMetaFactory) SetRecipesPostFunc(fn func(ctx context.Context, set bool, obj *model.User, i int) error) *UserMetaFactory { f.mutation.recipesPostMutateFunc(fn)(&f.mutation) return f @@ -340,15 +393,31 @@ func (t *userTrait) SetRecipesPostFunc(fn func(ctx context.Context, set bool, ob return t } +// SetAfterCreateFunc register a function to be called after struct create func (f *UserMetaFactory) SetAfterCreateFunc(fn func(ctx context.Context, i *model.User) error) *UserMetaFactory { f.mutation.afterCreateFunc = fn return f } + +// SetBeforeCreateFunc register a function to be called before struct create +func (f *UserMetaFactory) SetBeforeCreateFunc(fn func(ctx context.Context, i *model.User) error) *UserMetaFactory { + f.mutation.beforeCreateFunc = fn + return f +} + +// SetAfterCreateFunc register a function to be called after struct create func (t *userTrait) SetAfterCreateFunc(fn func(ctx context.Context, i *model.User) error) *userTrait { t.updates = append(t.updates, t.mutation.afterCreateMutateFunc(fn)) return t } +// SetBeforeCreateFunc register a function to be called before struct create +func (t *userTrait) SetBeforeCreateFunc(fn func(ctx context.Context, i *model.User) error) *userTrait { + t.updates = append(t.updates, t.mutation.beforeCreateMutateFunc(fn)) + return t +} + +// Build create a UserFactory from UserMetaFactory func (f *UserMetaFactory) Build() *UserFactory { return &UserFactory{meta: *f, counter: &Counter{}} } @@ -358,6 +427,7 @@ type UserFactory struct { counter *Counter } +// SetName set the Name field func (f *UserFactory) SetName(i string) *UserBuilder { builder := &UserBuilder{mutation: f.meta.mutation, counter: f.counter, factory: f} builder.SetName(i) @@ -365,6 +435,7 @@ func (f *UserFactory) SetName(i string) *UserBuilder { return builder } +// SetTags set the Tags field func (f *UserFactory) SetTags(i []*model.Category) *UserBuilder { builder := &UserBuilder{mutation: f.meta.mutation, counter: f.counter, factory: f} builder.SetTags(i) @@ -372,6 +443,7 @@ func (f *UserFactory) SetTags(i []*model.Category) *UserBuilder { return builder } +// SetRecipes set the Recipes field func (f *UserFactory) SetRecipes(i []*model.Recipe) *UserBuilder { builder := &UserBuilder{mutation: f.meta.mutation, counter: f.counter, factory: f} builder.SetRecipes(i) @@ -379,6 +451,7 @@ func (f *UserFactory) SetRecipes(i []*model.Recipe) *UserBuilder { return builder } +// SetRecipesPost call the post function with int input func (f *UserFactory) SetRecipesPost(i int) *UserBuilder { builder := &UserBuilder{mutation: f.meta.mutation, counter: f.counter, factory: f} builder.SetRecipesPost(i) @@ -386,21 +459,28 @@ func (f *UserFactory) SetRecipesPost(i int) *UserBuilder { return builder } +// Create return a new *model.User func (f *UserFactory) Create(ctx context.Context) (*model.User, error) { builder := &UserBuilder{mutation: f.meta.mutation, counter: f.counter, factory: f} return builder.Create(ctx) } + +// CreateV return a new model.User func (f *UserFactory) CreateV(ctx context.Context) (model.User, error) { builder := &UserBuilder{mutation: f.meta.mutation, counter: f.counter, factory: f} return builder.CreateV(ctx) } + +// CreateBatch return a []*model.User slice func (f *UserFactory) CreateBatch(ctx context.Context, n int) ([]*model.User, error) { builder := &UserBuilder{mutation: f.meta.mutation, counter: f.counter, factory: f} return builder.CreateBatch(ctx, n) } + +// CreateBatchV return a []model.User slice func (f *UserFactory) CreateBatchV(ctx context.Context, n int) ([]model.User, error) { builder := &UserBuilder{mutation: f.meta.mutation, counter: f.counter, factory: f} @@ -425,30 +505,35 @@ type UserBuilder struct { _postRecipesSet bool } +// SetName set the Name field func (b *UserBuilder) SetName(i string) *UserBuilder { b.nameOverride = i b.nameOverriden = true return b } +// SetTags set the Tags field func (b *UserBuilder) SetTags(i []*model.Category) *UserBuilder { b.tagsOverride = i b.tagsOverriden = true return b } +// SetRecipes set the Recipes field func (b *UserBuilder) SetRecipes(i []*model.Recipe) *UserBuilder { b.recipesOverride = i b.recipesOverriden = true return b } +// SetRecipesPost call the post function with int input func (b *UserBuilder) SetRecipesPost(i int) *UserBuilder { b._postRecipes = i b._postRecipesSet = true return b } +// CreateV return a new model.User func (b *UserBuilder) CreateV(ctx context.Context) (model.User, error) { var d model.User p, err := b.Create(ctx) @@ -458,6 +543,7 @@ func (b *UserBuilder) CreateV(ctx context.Context) (model.User, error) { return d, err } +// Create return a new *model.User func (b *UserBuilder) Create(ctx context.Context) (*model.User, error) { var preSlice = []func(ctx context.Context, i *model.User, c int) error{} @@ -535,6 +621,7 @@ func (b *UserBuilder) Create(ctx context.Context) (*model.User, error) { } v := &model.User{} + for _, f := range preSlice { err := f(ctx, v, index) @@ -551,6 +638,11 @@ func (b *UserBuilder) Create(ctx context.Context) (*model.User, error) { return nil, err } } + if b.mutation.beforeCreateFunc != nil { + if err := b.mutation.beforeCreateFunc(ctx, v); err != nil { + return nil, err + } + } new := v @@ -561,9 +653,7 @@ func (b *UserBuilder) Create(ctx context.Context) (*model.User, error) { } } for _, f := range postSlice { - err := f(ctx, new, index) - if err != nil { return nil, err } diff --git a/integration/carrier/factory/bar.go b/integration/carrier/factory/bar.go index a499230..4fa3f83 100644 --- a/integration/carrier/factory/bar.go +++ b/integration/carrier/factory/bar.go @@ -11,7 +11,7 @@ type barMutation struct { nameType int nameFunc func(ctx context.Context, i *model.Foo, c int) error - beforeCreateFunc func(ctx context.Context) error + beforeCreateFunc func(ctx context.Context, i *model.Foo) error afterCreateFunc func(ctx context.Context, i *model.Foo) error } type BarMetaFactory struct { @@ -25,6 +25,11 @@ type barTrait struct { func BarTrait() *barTrait { return &barTrait{} } +func (*barMutation) beforeCreateMutateFunc(fn func(ctx context.Context, i *model.Foo) error) func(m *barMutation) { + return func(m *barMutation) { + m.beforeCreateFunc = fn + } +} func (*barMutation) afterCreateMutateFunc(fn func(ctx context.Context, i *model.Foo) error) func(m *barMutation) { return func(m *barMutation) { m.afterCreateFunc = fn @@ -148,8 +153,8 @@ func (f *BarMetaFactory) SetAfterCreateFunc(fn func(ctx context.Context, i *mode return f } -// SetBeforeCreateFunc register a function to be called after struct create -func (f *BarMetaFactory) SetBeforeCreateFunc(fn func(ctx context.Context) error) *BarMetaFactory { +// SetBeforeCreateFunc register a function to be called before struct create +func (f *BarMetaFactory) SetBeforeCreateFunc(fn func(ctx context.Context, i *model.Foo) error) *BarMetaFactory { f.mutation.beforeCreateFunc = fn return f } @@ -160,6 +165,12 @@ func (t *barTrait) SetAfterCreateFunc(fn func(ctx context.Context, i *model.Foo) return t } +// SetBeforeCreateFunc register a function to be called before struct create +func (t *barTrait) SetBeforeCreateFunc(fn func(ctx context.Context, i *model.Foo) error) *barTrait { + t.updates = append(t.updates, t.mutation.beforeCreateMutateFunc(fn)) + return t +} + // Build create a BarFactory from BarMetaFactory func (f *BarMetaFactory) Build() *BarFactory { return &BarFactory{meta: *f, counter: &Counter{}} @@ -263,6 +274,7 @@ func (b *BarBuilder) Create(ctx context.Context) (*model.Foo, error) { } v := &model.Foo{} + for _, f := range preSlice { err := f(ctx, v, index) @@ -279,12 +291,12 @@ func (b *BarBuilder) Create(ctx context.Context) (*model.Foo, error) { return nil, err } } - if b.mutation.beforeCreateFunc != nil { - if err := b.mutation.beforeCreateFunc(ctx); err != nil { + if err := b.mutation.beforeCreateFunc(ctx, v); err != nil { return nil, err } } + new := v if b.mutation.afterCreateFunc != nil { @@ -294,9 +306,7 @@ func (b *BarBuilder) Create(ctx context.Context) (*model.Foo, error) { } } for _, f := range postSlice { - err := f(ctx, new, index) - if err != nil { return nil, err } diff --git a/integration/carrier/factory/ent_car.go b/integration/carrier/factory/ent_car.go index aa2069a..090b4e3 100644 --- a/integration/carrier/factory/ent_car.go +++ b/integration/carrier/factory/ent_car.go @@ -17,22 +17,28 @@ type EntCarMutator struct { OwnerID int RegisteredAt time.Time + + _creator *ent.CarCreate +} + +func (m *EntCarMutator) EntCreator() *ent.CarCreate { + return m._creator } type entCarMutation struct { modelType int - modelFunc func(ctx context.Context, i *EntCarMutator, c int, creator *ent.CarCreate) error + modelFunc func(ctx context.Context, i *EntCarMutator, c int) error ownerType int - ownerFunc func(ctx context.Context, i *EntCarMutator, c int, creator *ent.CarCreate) error + ownerFunc func(ctx context.Context, i *EntCarMutator, c int) error ownerIDType int - ownerIDFunc func(ctx context.Context, i *EntCarMutator, c int, creator *ent.CarCreate) error + ownerIDFunc func(ctx context.Context, i *EntCarMutator, c int) error registeredAtType int - registeredAtFunc func(ctx context.Context, i *EntCarMutator, c int, creator *ent.CarCreate) error + registeredAtFunc func(ctx context.Context, i *EntCarMutator, c int) error - beforeCreateFunc func(ctx context.Context, creator *ent.CarCreate) error + beforeCreateFunc func(ctx context.Context, i *EntCarMutator) error afterCreateFunc func(ctx context.Context, i *ent.Car) error } type EntCarMetaFactory struct { @@ -46,6 +52,11 @@ type entCarTrait struct { func EntCarTrait() *entCarTrait { return &entCarTrait{} } +func (*entCarMutation) beforeCreateMutateFunc(fn func(ctx context.Context, i *EntCarMutator) error) func(m *entCarMutation) { + return func(m *entCarMutation) { + m.beforeCreateFunc = fn + } +} func (*entCarMutation) afterCreateMutateFunc(fn func(ctx context.Context, i *ent.Car) error) func(m *entCarMutation) { return func(m *entCarMutation) { m.afterCreateFunc = fn @@ -55,7 +66,7 @@ func (*entCarMutation) afterCreateMutateFunc(fn func(ctx context.Context, i *ent func (*entCarMutation) modelSequenceMutateFunc(fn func(ctx context.Context, i int) (string, error)) func(m *entCarMutation) { return func(m *entCarMutation) { m.modelType = TypeSequence - m.modelFunc = func(ctx context.Context, i *EntCarMutator, c int, creator *ent.CarCreate) error { + m.modelFunc = func(ctx context.Context, i *EntCarMutator, c int) error { if fn == nil { return nil } @@ -64,7 +75,7 @@ func (*entCarMutation) modelSequenceMutateFunc(fn func(ctx context.Context, i in return err } - creator.SetModel(value) + i.EntCreator().SetModel(value) i.Model = value return nil @@ -74,7 +85,7 @@ func (*entCarMutation) modelSequenceMutateFunc(fn func(ctx context.Context, i in func (*entCarMutation) modelLazyMutateFunc(fn func(ctx context.Context, i *EntCarMutator) (string, error)) func(m *entCarMutation) { return func(m *entCarMutation) { m.modelType = TypeLazy - m.modelFunc = func(ctx context.Context, i *EntCarMutator, c int, creator *ent.CarCreate) error { + m.modelFunc = func(ctx context.Context, i *EntCarMutator, c int) error { if fn == nil { return nil } @@ -83,7 +94,7 @@ func (*entCarMutation) modelLazyMutateFunc(fn func(ctx context.Context, i *EntCa return err } - creator.SetModel(value) + i.EntCreator().SetModel(value) i.Model = value return nil @@ -93,9 +104,9 @@ func (*entCarMutation) modelLazyMutateFunc(fn func(ctx context.Context, i *EntCa func (*entCarMutation) modelDefaultMutateFunc(v string) func(m *entCarMutation) { return func(m *entCarMutation) { m.modelType = TypeDefault - m.modelFunc = func(ctx context.Context, i *EntCarMutator, c int, creator *ent.CarCreate) error { + m.modelFunc = func(ctx context.Context, i *EntCarMutator, c int) error { - creator.SetModel(v) + i.EntCreator().SetModel(v) i.Model = v return nil @@ -105,7 +116,7 @@ func (*entCarMutation) modelDefaultMutateFunc(v string) func(m *entCarMutation) func (*entCarMutation) modelFactoryMutateFunc(fn func(ctx context.Context) (string, error)) func(m *entCarMutation) { return func(m *entCarMutation) { m.modelType = TypeFactory - m.modelFunc = func(ctx context.Context, i *EntCarMutator, c int, creator *ent.CarCreate) error { + m.modelFunc = func(ctx context.Context, i *EntCarMutator, c int) error { if fn == nil { return nil } @@ -114,7 +125,7 @@ func (*entCarMutation) modelFactoryMutateFunc(fn func(ctx context.Context) (stri return err } - creator.SetModel(value) + i.EntCreator().SetModel(value) i.Model = value @@ -174,7 +185,7 @@ func (t *entCarTrait) SetModelFactory(fn func(ctx context.Context) (string, erro func (*entCarMutation) ownerSequenceMutateFunc(fn func(ctx context.Context, i int) (*ent.User, error)) func(m *entCarMutation) { return func(m *entCarMutation) { m.ownerType = TypeSequence - m.ownerFunc = func(ctx context.Context, i *EntCarMutator, c int, creator *ent.CarCreate) error { + m.ownerFunc = func(ctx context.Context, i *EntCarMutator, c int) error { if fn == nil { return nil } @@ -183,7 +194,7 @@ func (*entCarMutation) ownerSequenceMutateFunc(fn func(ctx context.Context, i in return err } - creator.SetOwner(value) + i.EntCreator().SetOwner(value) i.Owner = value return nil @@ -193,7 +204,7 @@ func (*entCarMutation) ownerSequenceMutateFunc(fn func(ctx context.Context, i in func (*entCarMutation) ownerLazyMutateFunc(fn func(ctx context.Context, i *EntCarMutator) (*ent.User, error)) func(m *entCarMutation) { return func(m *entCarMutation) { m.ownerType = TypeLazy - m.ownerFunc = func(ctx context.Context, i *EntCarMutator, c int, creator *ent.CarCreate) error { + m.ownerFunc = func(ctx context.Context, i *EntCarMutator, c int) error { if fn == nil { return nil } @@ -202,7 +213,7 @@ func (*entCarMutation) ownerLazyMutateFunc(fn func(ctx context.Context, i *EntCa return err } - creator.SetOwner(value) + i.EntCreator().SetOwner(value) i.Owner = value return nil @@ -212,9 +223,9 @@ func (*entCarMutation) ownerLazyMutateFunc(fn func(ctx context.Context, i *EntCa func (*entCarMutation) ownerDefaultMutateFunc(v *ent.User) func(m *entCarMutation) { return func(m *entCarMutation) { m.ownerType = TypeDefault - m.ownerFunc = func(ctx context.Context, i *EntCarMutator, c int, creator *ent.CarCreate) error { + m.ownerFunc = func(ctx context.Context, i *EntCarMutator, c int) error { - creator.SetOwner(v) + i.EntCreator().SetOwner(v) i.Owner = v return nil @@ -224,7 +235,7 @@ func (*entCarMutation) ownerDefaultMutateFunc(v *ent.User) func(m *entCarMutatio func (*entCarMutation) ownerFactoryMutateFunc(fn func(ctx context.Context) (*ent.User, error)) func(m *entCarMutation) { return func(m *entCarMutation) { m.ownerType = TypeFactory - m.ownerFunc = func(ctx context.Context, i *EntCarMutator, c int, creator *ent.CarCreate) error { + m.ownerFunc = func(ctx context.Context, i *EntCarMutator, c int) error { if fn == nil { return nil } @@ -233,7 +244,7 @@ func (*entCarMutation) ownerFactoryMutateFunc(fn func(ctx context.Context) (*ent return err } - creator.SetOwner(value) + i.EntCreator().SetOwner(value) i.Owner = value @@ -293,7 +304,7 @@ func (t *entCarTrait) SetOwnerFactory(fn func(ctx context.Context) (*ent.User, e func (*entCarMutation) ownerIDSequenceMutateFunc(fn func(ctx context.Context, i int) (int, error)) func(m *entCarMutation) { return func(m *entCarMutation) { m.ownerIDType = TypeSequence - m.ownerIDFunc = func(ctx context.Context, i *EntCarMutator, c int, creator *ent.CarCreate) error { + m.ownerIDFunc = func(ctx context.Context, i *EntCarMutator, c int) error { if fn == nil { return nil } @@ -302,7 +313,7 @@ func (*entCarMutation) ownerIDSequenceMutateFunc(fn func(ctx context.Context, i return err } - creator.SetOwnerID(value) + i.EntCreator().SetOwnerID(value) i.OwnerID = value return nil @@ -312,7 +323,7 @@ func (*entCarMutation) ownerIDSequenceMutateFunc(fn func(ctx context.Context, i func (*entCarMutation) ownerIDLazyMutateFunc(fn func(ctx context.Context, i *EntCarMutator) (int, error)) func(m *entCarMutation) { return func(m *entCarMutation) { m.ownerIDType = TypeLazy - m.ownerIDFunc = func(ctx context.Context, i *EntCarMutator, c int, creator *ent.CarCreate) error { + m.ownerIDFunc = func(ctx context.Context, i *EntCarMutator, c int) error { if fn == nil { return nil } @@ -321,7 +332,7 @@ func (*entCarMutation) ownerIDLazyMutateFunc(fn func(ctx context.Context, i *Ent return err } - creator.SetOwnerID(value) + i.EntCreator().SetOwnerID(value) i.OwnerID = value return nil @@ -331,9 +342,9 @@ func (*entCarMutation) ownerIDLazyMutateFunc(fn func(ctx context.Context, i *Ent func (*entCarMutation) ownerIDDefaultMutateFunc(v int) func(m *entCarMutation) { return func(m *entCarMutation) { m.ownerIDType = TypeDefault - m.ownerIDFunc = func(ctx context.Context, i *EntCarMutator, c int, creator *ent.CarCreate) error { + m.ownerIDFunc = func(ctx context.Context, i *EntCarMutator, c int) error { - creator.SetOwnerID(v) + i.EntCreator().SetOwnerID(v) i.OwnerID = v return nil @@ -343,7 +354,7 @@ func (*entCarMutation) ownerIDDefaultMutateFunc(v int) func(m *entCarMutation) { func (*entCarMutation) ownerIDFactoryMutateFunc(fn func(ctx context.Context) (int, error)) func(m *entCarMutation) { return func(m *entCarMutation) { m.ownerIDType = TypeFactory - m.ownerIDFunc = func(ctx context.Context, i *EntCarMutator, c int, creator *ent.CarCreate) error { + m.ownerIDFunc = func(ctx context.Context, i *EntCarMutator, c int) error { if fn == nil { return nil } @@ -352,7 +363,7 @@ func (*entCarMutation) ownerIDFactoryMutateFunc(fn func(ctx context.Context) (in return err } - creator.SetOwnerID(value) + i.EntCreator().SetOwnerID(value) i.OwnerID = value @@ -412,7 +423,7 @@ func (t *entCarTrait) SetOwnerIDFactory(fn func(ctx context.Context) (int, error func (*entCarMutation) registeredAtSequenceMutateFunc(fn func(ctx context.Context, i int) (time.Time, error)) func(m *entCarMutation) { return func(m *entCarMutation) { m.registeredAtType = TypeSequence - m.registeredAtFunc = func(ctx context.Context, i *EntCarMutator, c int, creator *ent.CarCreate) error { + m.registeredAtFunc = func(ctx context.Context, i *EntCarMutator, c int) error { if fn == nil { return nil } @@ -421,7 +432,7 @@ func (*entCarMutation) registeredAtSequenceMutateFunc(fn func(ctx context.Contex return err } - creator.SetRegisteredAt(value) + i.EntCreator().SetRegisteredAt(value) i.RegisteredAt = value return nil @@ -431,7 +442,7 @@ func (*entCarMutation) registeredAtSequenceMutateFunc(fn func(ctx context.Contex func (*entCarMutation) registeredAtLazyMutateFunc(fn func(ctx context.Context, i *EntCarMutator) (time.Time, error)) func(m *entCarMutation) { return func(m *entCarMutation) { m.registeredAtType = TypeLazy - m.registeredAtFunc = func(ctx context.Context, i *EntCarMutator, c int, creator *ent.CarCreate) error { + m.registeredAtFunc = func(ctx context.Context, i *EntCarMutator, c int) error { if fn == nil { return nil } @@ -440,7 +451,7 @@ func (*entCarMutation) registeredAtLazyMutateFunc(fn func(ctx context.Context, i return err } - creator.SetRegisteredAt(value) + i.EntCreator().SetRegisteredAt(value) i.RegisteredAt = value return nil @@ -450,9 +461,9 @@ func (*entCarMutation) registeredAtLazyMutateFunc(fn func(ctx context.Context, i func (*entCarMutation) registeredAtDefaultMutateFunc(v time.Time) func(m *entCarMutation) { return func(m *entCarMutation) { m.registeredAtType = TypeDefault - m.registeredAtFunc = func(ctx context.Context, i *EntCarMutator, c int, creator *ent.CarCreate) error { + m.registeredAtFunc = func(ctx context.Context, i *EntCarMutator, c int) error { - creator.SetRegisteredAt(v) + i.EntCreator().SetRegisteredAt(v) i.RegisteredAt = v return nil @@ -462,7 +473,7 @@ func (*entCarMutation) registeredAtDefaultMutateFunc(v time.Time) func(m *entCar func (*entCarMutation) registeredAtFactoryMutateFunc(fn func(ctx context.Context) (time.Time, error)) func(m *entCarMutation) { return func(m *entCarMutation) { m.registeredAtType = TypeFactory - m.registeredAtFunc = func(ctx context.Context, i *EntCarMutator, c int, creator *ent.CarCreate) error { + m.registeredAtFunc = func(ctx context.Context, i *EntCarMutator, c int) error { if fn == nil { return nil } @@ -471,7 +482,7 @@ func (*entCarMutation) registeredAtFactoryMutateFunc(fn func(ctx context.Context return err } - creator.SetRegisteredAt(value) + i.EntCreator().SetRegisteredAt(value) i.RegisteredAt = value @@ -534,8 +545,8 @@ func (f *EntCarMetaFactory) SetAfterCreateFunc(fn func(ctx context.Context, i *e return f } -// SetBeforeCreateFunc register a function to be called after struct create -func (f *EntCarMetaFactory) SetBeforeCreateFunc(fn func(ctx context.Context, creator *ent.CarCreate) error) *EntCarMetaFactory { +// SetBeforeCreateFunc register a function to be called before struct create +func (f *EntCarMetaFactory) SetBeforeCreateFunc(fn func(ctx context.Context, i *EntCarMutator) error) *EntCarMetaFactory { f.mutation.beforeCreateFunc = fn return f } @@ -546,6 +557,12 @@ func (t *entCarTrait) SetAfterCreateFunc(fn func(ctx context.Context, i *ent.Car return t } +// SetBeforeCreateFunc register a function to be called before struct create +func (t *entCarTrait) SetBeforeCreateFunc(fn func(ctx context.Context, i *EntCarMutator) error) *entCarTrait { + t.updates = append(t.updates, t.mutation.beforeCreateMutateFunc(fn)) + return t +} + // Build create a EntCarFactory from EntCarMetaFactory func (f *EntCarMetaFactory) Build() *EntCarFactory { return &EntCarFactory{meta: *f, counter: &Counter{}} @@ -706,9 +723,9 @@ func (b *EntCarBuilder) CreateV(ctx context.Context) (ent.Car, error) { // Create return a new *ent.Car func (b *EntCarBuilder) Create(ctx context.Context) (*ent.Car, error) { - var preSlice = []func(ctx context.Context, i *EntCarMutator, c int, creator *ent.CarCreate) error{} - var lazySlice = []func(ctx context.Context, i *EntCarMutator, c int, creator *ent.CarCreate) error{} - var postSlice = []func(ctx context.Context, i *ent.Car, c int, creator *ent.CarCreate) error{} + var preSlice = []func(ctx context.Context, i *EntCarMutator, c int) error{} + var lazySlice = []func(ctx context.Context, i *EntCarMutator, c int) error{} + var postSlice = []func(ctx context.Context, i *ent.Car, c int) error{} index := b.counter.Get() _ = index @@ -717,10 +734,10 @@ func (b *EntCarBuilder) Create(ctx context.Context) (*ent.Car, error) { entBuilder := client.Car.Create() if b.modelOverriden { - preSlice = append(preSlice, func(ctx context.Context, i *EntCarMutator, c int, creator *ent.CarCreate) error { + preSlice = append(preSlice, func(ctx context.Context, i *EntCarMutator, c int) error { value := b.modelOverride - creator.SetModel(value) + i.EntCreator().SetModel(value) i.Model = value return nil @@ -739,10 +756,10 @@ func (b *EntCarBuilder) Create(ctx context.Context) (*ent.Car, error) { } if b.ownerOverriden { - preSlice = append(preSlice, func(ctx context.Context, i *EntCarMutator, c int, creator *ent.CarCreate) error { + preSlice = append(preSlice, func(ctx context.Context, i *EntCarMutator, c int) error { value := b.ownerOverride - creator.SetOwner(value) + i.EntCreator().SetOwner(value) i.Owner = value return nil @@ -761,10 +778,10 @@ func (b *EntCarBuilder) Create(ctx context.Context) (*ent.Car, error) { } if b.ownerIDOverriden { - preSlice = append(preSlice, func(ctx context.Context, i *EntCarMutator, c int, creator *ent.CarCreate) error { + preSlice = append(preSlice, func(ctx context.Context, i *EntCarMutator, c int) error { value := b.ownerIDOverride - creator.SetOwnerID(value) + i.EntCreator().SetOwnerID(value) i.OwnerID = value return nil @@ -783,10 +800,10 @@ func (b *EntCarBuilder) Create(ctx context.Context) (*ent.Car, error) { } if b.registeredAtOverriden { - preSlice = append(preSlice, func(ctx context.Context, i *EntCarMutator, c int, creator *ent.CarCreate) error { + preSlice = append(preSlice, func(ctx context.Context, i *EntCarMutator, c int) error { value := b.registeredAtOverride - creator.SetRegisteredAt(value) + i.EntCreator().SetRegisteredAt(value) i.RegisteredAt = value return nil @@ -805,9 +822,12 @@ func (b *EntCarBuilder) Create(ctx context.Context) (*ent.Car, error) { } v := &EntCarMutator{} + + v._creator = entBuilder + for _, f := range preSlice { - err := f(ctx, v, index, entBuilder) + err := f(ctx, v, index) if err != nil { return nil, err @@ -815,18 +835,18 @@ func (b *EntCarBuilder) Create(ctx context.Context) (*ent.Car, error) { } for _, f := range lazySlice { - err := f(ctx, v, index, entBuilder) + err := f(ctx, v, index) if err != nil { return nil, err } } - if b.mutation.beforeCreateFunc != nil { - if err := b.mutation.beforeCreateFunc(ctx, entBuilder); err != nil { + if err := b.mutation.beforeCreateFunc(ctx, v); err != nil { return nil, err } } + new, err := entBuilder.Save(ctx) if err != nil { return nil, err @@ -839,9 +859,7 @@ func (b *EntCarBuilder) Create(ctx context.Context) (*ent.Car, error) { } } for _, f := range postSlice { - - err := f(ctx, new, index, entBuilder) - + err := f(ctx, new, index) if err != nil { return nil, err } diff --git a/integration/carrier/factory/ent_group.go b/integration/carrier/factory/ent_group.go index eb2b249..32beecd 100644 --- a/integration/carrier/factory/ent_group.go +++ b/integration/carrier/factory/ent_group.go @@ -9,13 +9,19 @@ import ( type EntGroupMutator struct { Name string + + _creator *ent.GroupCreate +} + +func (m *EntGroupMutator) EntCreator() *ent.GroupCreate { + return m._creator } type entGroupMutation struct { nameType int - nameFunc func(ctx context.Context, i *EntGroupMutator, c int, creator *ent.GroupCreate) error + nameFunc func(ctx context.Context, i *EntGroupMutator, c int) error - beforeCreateFunc func(ctx context.Context, creator *ent.GroupCreate) error + beforeCreateFunc func(ctx context.Context, i *EntGroupMutator) error afterCreateFunc func(ctx context.Context, i *ent.Group) error } type EntGroupMetaFactory struct { @@ -31,6 +37,11 @@ type entGroupTrait struct { func EntGroupTrait() *entGroupTrait { return &entGroupTrait{} } +func (*entGroupMutation) beforeCreateMutateFunc(fn func(ctx context.Context, i *EntGroupMutator) error) func(m *entGroupMutation) { + return func(m *entGroupMutation) { + m.beforeCreateFunc = fn + } +} func (*entGroupMutation) afterCreateMutateFunc(fn func(ctx context.Context, i *ent.Group) error) func(m *entGroupMutation) { return func(m *entGroupMutation) { m.afterCreateFunc = fn @@ -40,7 +51,7 @@ func (*entGroupMutation) afterCreateMutateFunc(fn func(ctx context.Context, i *e func (*entGroupMutation) nameSequenceMutateFunc(fn func(ctx context.Context, i int) (string, error)) func(m *entGroupMutation) { return func(m *entGroupMutation) { m.nameType = TypeSequence - m.nameFunc = func(ctx context.Context, i *EntGroupMutator, c int, creator *ent.GroupCreate) error { + m.nameFunc = func(ctx context.Context, i *EntGroupMutator, c int) error { if fn == nil { return nil } @@ -49,7 +60,7 @@ func (*entGroupMutation) nameSequenceMutateFunc(fn func(ctx context.Context, i i return err } - creator.SetName(value) + i.EntCreator().SetName(value) i.Name = value return nil @@ -59,7 +70,7 @@ func (*entGroupMutation) nameSequenceMutateFunc(fn func(ctx context.Context, i i func (*entGroupMutation) nameLazyMutateFunc(fn func(ctx context.Context, i *EntGroupMutator) (string, error)) func(m *entGroupMutation) { return func(m *entGroupMutation) { m.nameType = TypeLazy - m.nameFunc = func(ctx context.Context, i *EntGroupMutator, c int, creator *ent.GroupCreate) error { + m.nameFunc = func(ctx context.Context, i *EntGroupMutator, c int) error { if fn == nil { return nil } @@ -68,7 +79,7 @@ func (*entGroupMutation) nameLazyMutateFunc(fn func(ctx context.Context, i *EntG return err } - creator.SetName(value) + i.EntCreator().SetName(value) i.Name = value return nil @@ -78,9 +89,9 @@ func (*entGroupMutation) nameLazyMutateFunc(fn func(ctx context.Context, i *EntG func (*entGroupMutation) nameDefaultMutateFunc(v string) func(m *entGroupMutation) { return func(m *entGroupMutation) { m.nameType = TypeDefault - m.nameFunc = func(ctx context.Context, i *EntGroupMutator, c int, creator *ent.GroupCreate) error { + m.nameFunc = func(ctx context.Context, i *EntGroupMutator, c int) error { - creator.SetName(v) + i.EntCreator().SetName(v) i.Name = v return nil @@ -90,7 +101,7 @@ func (*entGroupMutation) nameDefaultMutateFunc(v string) func(m *entGroupMutatio func (*entGroupMutation) nameFactoryMutateFunc(fn func(ctx context.Context) (string, error)) func(m *entGroupMutation) { return func(m *entGroupMutation) { m.nameType = TypeFactory - m.nameFunc = func(ctx context.Context, i *EntGroupMutator, c int, creator *ent.GroupCreate) error { + m.nameFunc = func(ctx context.Context, i *EntGroupMutator, c int) error { if fn == nil { return nil } @@ -99,7 +110,7 @@ func (*entGroupMutation) nameFactoryMutateFunc(fn func(ctx context.Context) (str return err } - creator.SetName(value) + i.EntCreator().SetName(value) i.Name = value @@ -168,8 +179,8 @@ func (f *EntGroupMetaFactory) SetAfterCreateFunc(fn func(ctx context.Context, i return f } -// SetBeforeCreateFunc register a function to be called after struct create -func (f *EntGroupMetaFactory) SetBeforeCreateFunc(fn func(ctx context.Context, creator *ent.GroupCreate) error) *EntGroupMetaFactory { +// SetBeforeCreateFunc register a function to be called before struct create +func (f *EntGroupMetaFactory) SetBeforeCreateFunc(fn func(ctx context.Context, i *EntGroupMutator) error) *EntGroupMetaFactory { f.mutation.beforeCreateFunc = fn return f } @@ -180,6 +191,12 @@ func (t *entGroupTrait) SetAfterCreateFunc(fn func(ctx context.Context, i *ent.G return t } +// SetBeforeCreateFunc register a function to be called before struct create +func (t *entGroupTrait) SetBeforeCreateFunc(fn func(ctx context.Context, i *EntGroupMutator) error) *entGroupTrait { + t.updates = append(t.updates, t.mutation.beforeCreateMutateFunc(fn)) + return t +} + // Build create a EntGroupFactory from EntGroupMetaFactory func (f *EntGroupMetaFactory) Build() *EntGroupFactory { return &EntGroupFactory{meta: *f, counter: &Counter{}} @@ -307,9 +324,9 @@ func (b *EntGroupBuilder) CreateV(ctx context.Context) (ent.Group, error) { // Create return a new *ent.Group func (b *EntGroupBuilder) Create(ctx context.Context) (*ent.Group, error) { - var preSlice = []func(ctx context.Context, i *EntGroupMutator, c int, creator *ent.GroupCreate) error{} - var lazySlice = []func(ctx context.Context, i *EntGroupMutator, c int, creator *ent.GroupCreate) error{} - var postSlice = []func(ctx context.Context, i *ent.Group, c int, creator *ent.GroupCreate) error{} + var preSlice = []func(ctx context.Context, i *EntGroupMutator, c int) error{} + var lazySlice = []func(ctx context.Context, i *EntGroupMutator, c int) error{} + var postSlice = []func(ctx context.Context, i *ent.Group, c int) error{} index := b.counter.Get() _ = index @@ -318,10 +335,10 @@ func (b *EntGroupBuilder) Create(ctx context.Context) (*ent.Group, error) { entBuilder := client.Group.Create() if b.nameOverriden { - preSlice = append(preSlice, func(ctx context.Context, i *EntGroupMutator, c int, creator *ent.GroupCreate) error { + preSlice = append(preSlice, func(ctx context.Context, i *EntGroupMutator, c int) error { value := b.nameOverride - creator.SetName(value) + i.EntCreator().SetName(value) i.Name = value return nil @@ -340,9 +357,12 @@ func (b *EntGroupBuilder) Create(ctx context.Context) (*ent.Group, error) { } v := &EntGroupMutator{} + + v._creator = entBuilder + for _, f := range preSlice { - err := f(ctx, v, index, entBuilder) + err := f(ctx, v, index) if err != nil { return nil, err @@ -350,18 +370,18 @@ func (b *EntGroupBuilder) Create(ctx context.Context) (*ent.Group, error) { } for _, f := range lazySlice { - err := f(ctx, v, index, entBuilder) + err := f(ctx, v, index) if err != nil { return nil, err } } - if b.mutation.beforeCreateFunc != nil { - if err := b.mutation.beforeCreateFunc(ctx, entBuilder); err != nil { + if err := b.mutation.beforeCreateFunc(ctx, v); err != nil { return nil, err } } + new, err := entBuilder.Save(ctx) if err != nil { return nil, err @@ -374,9 +394,7 @@ func (b *EntGroupBuilder) Create(ctx context.Context) (*ent.Group, error) { } } for _, f := range postSlice { - - err := f(ctx, new, index, entBuilder) - + err := f(ctx, new, index) if err != nil { return nil, err } diff --git a/integration/carrier/factory/ent_user.go b/integration/carrier/factory/ent_user.go index 95d715e..4099f4e 100644 --- a/integration/carrier/factory/ent_user.go +++ b/integration/carrier/factory/ent_user.go @@ -13,21 +13,27 @@ type EntUserMutator struct { Email string Name string + + _creator *ent.UserCreate +} + +func (m *EntUserMutator) EntCreator() *ent.UserCreate { + return m._creator } type entUserMutation struct { ageType int - ageFunc func(ctx context.Context, i *EntUserMutator, c int, creator *ent.UserCreate) error + ageFunc func(ctx context.Context, i *EntUserMutator, c int) error emailType int - emailFunc func(ctx context.Context, i *EntUserMutator, c int, creator *ent.UserCreate) error + emailFunc func(ctx context.Context, i *EntUserMutator, c int) error nameType int - nameFunc func(ctx context.Context, i *EntUserMutator, c int, creator *ent.UserCreate) error + nameFunc func(ctx context.Context, i *EntUserMutator, c int) error _postGroupsFunc func(ctx context.Context, set bool, obj *ent.User, i int) error - beforeCreateFunc func(ctx context.Context, creator *ent.UserCreate) error + beforeCreateFunc func(ctx context.Context, i *EntUserMutator) error afterCreateFunc func(ctx context.Context, i *ent.User) error } type EntUserMetaFactory struct { @@ -41,6 +47,11 @@ type entUserTrait struct { func EntUserTrait() *entUserTrait { return &entUserTrait{} } +func (*entUserMutation) beforeCreateMutateFunc(fn func(ctx context.Context, i *EntUserMutator) error) func(m *entUserMutation) { + return func(m *entUserMutation) { + m.beforeCreateFunc = fn + } +} func (*entUserMutation) afterCreateMutateFunc(fn func(ctx context.Context, i *ent.User) error) func(m *entUserMutation) { return func(m *entUserMutation) { m.afterCreateFunc = fn @@ -50,7 +61,7 @@ func (*entUserMutation) afterCreateMutateFunc(fn func(ctx context.Context, i *en func (*entUserMutation) ageSequenceMutateFunc(fn func(ctx context.Context, i int) (int, error)) func(m *entUserMutation) { return func(m *entUserMutation) { m.ageType = TypeSequence - m.ageFunc = func(ctx context.Context, i *EntUserMutator, c int, creator *ent.UserCreate) error { + m.ageFunc = func(ctx context.Context, i *EntUserMutator, c int) error { if fn == nil { return nil } @@ -59,7 +70,7 @@ func (*entUserMutation) ageSequenceMutateFunc(fn func(ctx context.Context, i int return err } - creator.SetAge(value) + i.EntCreator().SetAge(value) i.Age = value return nil @@ -69,7 +80,7 @@ func (*entUserMutation) ageSequenceMutateFunc(fn func(ctx context.Context, i int func (*entUserMutation) ageLazyMutateFunc(fn func(ctx context.Context, i *EntUserMutator) (int, error)) func(m *entUserMutation) { return func(m *entUserMutation) { m.ageType = TypeLazy - m.ageFunc = func(ctx context.Context, i *EntUserMutator, c int, creator *ent.UserCreate) error { + m.ageFunc = func(ctx context.Context, i *EntUserMutator, c int) error { if fn == nil { return nil } @@ -78,7 +89,7 @@ func (*entUserMutation) ageLazyMutateFunc(fn func(ctx context.Context, i *EntUse return err } - creator.SetAge(value) + i.EntCreator().SetAge(value) i.Age = value return nil @@ -88,9 +99,9 @@ func (*entUserMutation) ageLazyMutateFunc(fn func(ctx context.Context, i *EntUse func (*entUserMutation) ageDefaultMutateFunc(v int) func(m *entUserMutation) { return func(m *entUserMutation) { m.ageType = TypeDefault - m.ageFunc = func(ctx context.Context, i *EntUserMutator, c int, creator *ent.UserCreate) error { + m.ageFunc = func(ctx context.Context, i *EntUserMutator, c int) error { - creator.SetAge(v) + i.EntCreator().SetAge(v) i.Age = v return nil @@ -100,7 +111,7 @@ func (*entUserMutation) ageDefaultMutateFunc(v int) func(m *entUserMutation) { func (*entUserMutation) ageFactoryMutateFunc(fn func(ctx context.Context) (int, error)) func(m *entUserMutation) { return func(m *entUserMutation) { m.ageType = TypeFactory - m.ageFunc = func(ctx context.Context, i *EntUserMutator, c int, creator *ent.UserCreate) error { + m.ageFunc = func(ctx context.Context, i *EntUserMutator, c int) error { if fn == nil { return nil } @@ -109,7 +120,7 @@ func (*entUserMutation) ageFactoryMutateFunc(fn func(ctx context.Context) (int, return err } - creator.SetAge(value) + i.EntCreator().SetAge(value) i.Age = value @@ -169,7 +180,7 @@ func (t *entUserTrait) SetAgeFactory(fn func(ctx context.Context) (int, error)) func (*entUserMutation) emailSequenceMutateFunc(fn func(ctx context.Context, i int) (string, error)) func(m *entUserMutation) { return func(m *entUserMutation) { m.emailType = TypeSequence - m.emailFunc = func(ctx context.Context, i *EntUserMutator, c int, creator *ent.UserCreate) error { + m.emailFunc = func(ctx context.Context, i *EntUserMutator, c int) error { if fn == nil { return nil } @@ -178,7 +189,7 @@ func (*entUserMutation) emailSequenceMutateFunc(fn func(ctx context.Context, i i return err } - creator.SetEmail(value) + i.EntCreator().SetEmail(value) i.Email = value return nil @@ -188,7 +199,7 @@ func (*entUserMutation) emailSequenceMutateFunc(fn func(ctx context.Context, i i func (*entUserMutation) emailLazyMutateFunc(fn func(ctx context.Context, i *EntUserMutator) (string, error)) func(m *entUserMutation) { return func(m *entUserMutation) { m.emailType = TypeLazy - m.emailFunc = func(ctx context.Context, i *EntUserMutator, c int, creator *ent.UserCreate) error { + m.emailFunc = func(ctx context.Context, i *EntUserMutator, c int) error { if fn == nil { return nil } @@ -197,7 +208,7 @@ func (*entUserMutation) emailLazyMutateFunc(fn func(ctx context.Context, i *EntU return err } - creator.SetEmail(value) + i.EntCreator().SetEmail(value) i.Email = value return nil @@ -207,9 +218,9 @@ func (*entUserMutation) emailLazyMutateFunc(fn func(ctx context.Context, i *EntU func (*entUserMutation) emailDefaultMutateFunc(v string) func(m *entUserMutation) { return func(m *entUserMutation) { m.emailType = TypeDefault - m.emailFunc = func(ctx context.Context, i *EntUserMutator, c int, creator *ent.UserCreate) error { + m.emailFunc = func(ctx context.Context, i *EntUserMutator, c int) error { - creator.SetEmail(v) + i.EntCreator().SetEmail(v) i.Email = v return nil @@ -219,7 +230,7 @@ func (*entUserMutation) emailDefaultMutateFunc(v string) func(m *entUserMutation func (*entUserMutation) emailFactoryMutateFunc(fn func(ctx context.Context) (string, error)) func(m *entUserMutation) { return func(m *entUserMutation) { m.emailType = TypeFactory - m.emailFunc = func(ctx context.Context, i *EntUserMutator, c int, creator *ent.UserCreate) error { + m.emailFunc = func(ctx context.Context, i *EntUserMutator, c int) error { if fn == nil { return nil } @@ -228,7 +239,7 @@ func (*entUserMutation) emailFactoryMutateFunc(fn func(ctx context.Context) (str return err } - creator.SetEmail(value) + i.EntCreator().SetEmail(value) i.Email = value @@ -288,7 +299,7 @@ func (t *entUserTrait) SetEmailFactory(fn func(ctx context.Context) (string, err func (*entUserMutation) nameSequenceMutateFunc(fn func(ctx context.Context, i int) (string, error)) func(m *entUserMutation) { return func(m *entUserMutation) { m.nameType = TypeSequence - m.nameFunc = func(ctx context.Context, i *EntUserMutator, c int, creator *ent.UserCreate) error { + m.nameFunc = func(ctx context.Context, i *EntUserMutator, c int) error { if fn == nil { return nil } @@ -297,7 +308,7 @@ func (*entUserMutation) nameSequenceMutateFunc(fn func(ctx context.Context, i in return err } - creator.SetName(value) + i.EntCreator().SetName(value) i.Name = value return nil @@ -307,7 +318,7 @@ func (*entUserMutation) nameSequenceMutateFunc(fn func(ctx context.Context, i in func (*entUserMutation) nameLazyMutateFunc(fn func(ctx context.Context, i *EntUserMutator) (string, error)) func(m *entUserMutation) { return func(m *entUserMutation) { m.nameType = TypeLazy - m.nameFunc = func(ctx context.Context, i *EntUserMutator, c int, creator *ent.UserCreate) error { + m.nameFunc = func(ctx context.Context, i *EntUserMutator, c int) error { if fn == nil { return nil } @@ -316,7 +327,7 @@ func (*entUserMutation) nameLazyMutateFunc(fn func(ctx context.Context, i *EntUs return err } - creator.SetName(value) + i.EntCreator().SetName(value) i.Name = value return nil @@ -326,9 +337,9 @@ func (*entUserMutation) nameLazyMutateFunc(fn func(ctx context.Context, i *EntUs func (*entUserMutation) nameDefaultMutateFunc(v string) func(m *entUserMutation) { return func(m *entUserMutation) { m.nameType = TypeDefault - m.nameFunc = func(ctx context.Context, i *EntUserMutator, c int, creator *ent.UserCreate) error { + m.nameFunc = func(ctx context.Context, i *EntUserMutator, c int) error { - creator.SetName(v) + i.EntCreator().SetName(v) i.Name = v return nil @@ -338,7 +349,7 @@ func (*entUserMutation) nameDefaultMutateFunc(v string) func(m *entUserMutation) func (*entUserMutation) nameFactoryMutateFunc(fn func(ctx context.Context) (string, error)) func(m *entUserMutation) { return func(m *entUserMutation) { m.nameType = TypeFactory - m.nameFunc = func(ctx context.Context, i *EntUserMutator, c int, creator *ent.UserCreate) error { + m.nameFunc = func(ctx context.Context, i *EntUserMutator, c int) error { if fn == nil { return nil } @@ -347,7 +358,7 @@ func (*entUserMutation) nameFactoryMutateFunc(fn func(ctx context.Context) (stri return err } - creator.SetName(value) + i.EntCreator().SetName(value) i.Name = value @@ -426,8 +437,8 @@ func (f *EntUserMetaFactory) SetAfterCreateFunc(fn func(ctx context.Context, i * return f } -// SetBeforeCreateFunc register a function to be called after struct create -func (f *EntUserMetaFactory) SetBeforeCreateFunc(fn func(ctx context.Context, creator *ent.UserCreate) error) *EntUserMetaFactory { +// SetBeforeCreateFunc register a function to be called before struct create +func (f *EntUserMetaFactory) SetBeforeCreateFunc(fn func(ctx context.Context, i *EntUserMutator) error) *EntUserMetaFactory { f.mutation.beforeCreateFunc = fn return f } @@ -438,6 +449,12 @@ func (t *entUserTrait) SetAfterCreateFunc(fn func(ctx context.Context, i *ent.Us return t } +// SetBeforeCreateFunc register a function to be called before struct create +func (t *entUserTrait) SetBeforeCreateFunc(fn func(ctx context.Context, i *EntUserMutator) error) *entUserTrait { + t.updates = append(t.updates, t.mutation.beforeCreateMutateFunc(fn)) + return t +} + // Build create a EntUserFactory from EntUserMetaFactory func (f *EntUserMetaFactory) Build() *EntUserFactory { return &EntUserFactory{meta: *f, counter: &Counter{}} @@ -598,9 +615,9 @@ func (b *EntUserBuilder) CreateV(ctx context.Context) (ent.User, error) { // Create return a new *ent.User func (b *EntUserBuilder) Create(ctx context.Context) (*ent.User, error) { - var preSlice = []func(ctx context.Context, i *EntUserMutator, c int, creator *ent.UserCreate) error{} - var lazySlice = []func(ctx context.Context, i *EntUserMutator, c int, creator *ent.UserCreate) error{} - var postSlice = []func(ctx context.Context, i *ent.User, c int, creator *ent.UserCreate) error{} + var preSlice = []func(ctx context.Context, i *EntUserMutator, c int) error{} + var lazySlice = []func(ctx context.Context, i *EntUserMutator, c int) error{} + var postSlice = []func(ctx context.Context, i *ent.User, c int) error{} index := b.counter.Get() _ = index @@ -609,10 +626,10 @@ func (b *EntUserBuilder) Create(ctx context.Context) (*ent.User, error) { entBuilder := client.User.Create() if b.ageOverriden { - preSlice = append(preSlice, func(ctx context.Context, i *EntUserMutator, c int, creator *ent.UserCreate) error { + preSlice = append(preSlice, func(ctx context.Context, i *EntUserMutator, c int) error { value := b.ageOverride - creator.SetAge(value) + i.EntCreator().SetAge(value) i.Age = value return nil @@ -631,10 +648,10 @@ func (b *EntUserBuilder) Create(ctx context.Context) (*ent.User, error) { } if b.emailOverriden { - preSlice = append(preSlice, func(ctx context.Context, i *EntUserMutator, c int, creator *ent.UserCreate) error { + preSlice = append(preSlice, func(ctx context.Context, i *EntUserMutator, c int) error { value := b.emailOverride - creator.SetEmail(value) + i.EntCreator().SetEmail(value) i.Email = value return nil @@ -653,10 +670,10 @@ func (b *EntUserBuilder) Create(ctx context.Context) (*ent.User, error) { } if b.nameOverriden { - preSlice = append(preSlice, func(ctx context.Context, i *EntUserMutator, c int, creator *ent.UserCreate) error { + preSlice = append(preSlice, func(ctx context.Context, i *EntUserMutator, c int) error { value := b.nameOverride - creator.SetName(value) + i.EntCreator().SetName(value) i.Name = value return nil @@ -675,16 +692,19 @@ func (b *EntUserBuilder) Create(ctx context.Context) (*ent.User, error) { } if b.mutation._postGroupsFunc != nil { - postSlice = append(postSlice, func(ctx context.Context, i *ent.User, c int, creator *ent.UserCreate) error { + postSlice = append(postSlice, func(ctx context.Context, i *ent.User, c int) error { err := b.mutation._postGroupsFunc(ctx, b._postGroupsSet, i, b._postGroups) return err }) } v := &EntUserMutator{} + + v._creator = entBuilder + for _, f := range preSlice { - err := f(ctx, v, index, entBuilder) + err := f(ctx, v, index) if err != nil { return nil, err @@ -692,18 +712,18 @@ func (b *EntUserBuilder) Create(ctx context.Context) (*ent.User, error) { } for _, f := range lazySlice { - err := f(ctx, v, index, entBuilder) + err := f(ctx, v, index) if err != nil { return nil, err } } - if b.mutation.beforeCreateFunc != nil { - if err := b.mutation.beforeCreateFunc(ctx, entBuilder); err != nil { + if err := b.mutation.beforeCreateFunc(ctx, v); err != nil { return nil, err } } + new, err := entBuilder.Save(ctx) if err != nil { return nil, err @@ -716,9 +736,7 @@ func (b *EntUserBuilder) Create(ctx context.Context) (*ent.User, error) { } } for _, f := range postSlice { - - err := f(ctx, new, index, entBuilder) - + err := f(ctx, new, index) if err != nil { return nil, err } diff --git a/integration/carrier/factory/food.go b/integration/carrier/factory/food.go index 41df13b..362c170 100644 --- a/integration/carrier/factory/food.go +++ b/integration/carrier/factory/food.go @@ -14,7 +14,7 @@ type foodMutation struct { categoryType int categoryFunc func(ctx context.Context, i *model.Food, c int) error - beforeCreateFunc func(ctx context.Context) error + beforeCreateFunc func(ctx context.Context, i *model.Food) error afterCreateFunc func(ctx context.Context, i *model.Food) error } type FoodMetaFactory struct { @@ -28,6 +28,11 @@ type foodTrait struct { func FoodTrait() *foodTrait { return &foodTrait{} } +func (*foodMutation) beforeCreateMutateFunc(fn func(ctx context.Context, i *model.Food) error) func(m *foodMutation) { + return func(m *foodMutation) { + m.beforeCreateFunc = fn + } +} func (*foodMutation) afterCreateMutateFunc(fn func(ctx context.Context, i *model.Food) error) func(m *foodMutation) { return func(m *foodMutation) { m.afterCreateFunc = fn @@ -262,8 +267,8 @@ func (f *FoodMetaFactory) SetAfterCreateFunc(fn func(ctx context.Context, i *mod return f } -// SetBeforeCreateFunc register a function to be called after struct create -func (f *FoodMetaFactory) SetBeforeCreateFunc(fn func(ctx context.Context) error) *FoodMetaFactory { +// SetBeforeCreateFunc register a function to be called before struct create +func (f *FoodMetaFactory) SetBeforeCreateFunc(fn func(ctx context.Context, i *model.Food) error) *FoodMetaFactory { f.mutation.beforeCreateFunc = fn return f } @@ -274,6 +279,12 @@ func (t *foodTrait) SetAfterCreateFunc(fn func(ctx context.Context, i *model.Foo return t } +// SetBeforeCreateFunc register a function to be called before struct create +func (t *foodTrait) SetBeforeCreateFunc(fn func(ctx context.Context, i *model.Food) error) *foodTrait { + t.updates = append(t.updates, t.mutation.beforeCreateMutateFunc(fn)) + return t +} + // Build create a FoodFactory from FoodMetaFactory func (f *FoodMetaFactory) Build() *FoodFactory { return &FoodFactory{meta: *f, counter: &Counter{}} @@ -415,6 +426,7 @@ func (b *FoodBuilder) Create(ctx context.Context) (*model.Food, error) { } v := &model.Food{} + for _, f := range preSlice { err := f(ctx, v, index) @@ -431,12 +443,12 @@ func (b *FoodBuilder) Create(ctx context.Context) (*model.Food, error) { return nil, err } } - if b.mutation.beforeCreateFunc != nil { - if err := b.mutation.beforeCreateFunc(ctx); err != nil { + if err := b.mutation.beforeCreateFunc(ctx, v); err != nil { return nil, err } } + new := v if b.mutation.afterCreateFunc != nil { @@ -446,9 +458,7 @@ func (b *FoodBuilder) Create(ctx context.Context) (*model.Food, error) { } } for _, f := range postSlice { - err := f(ctx, new, index) - if err != nil { return nil, err } diff --git a/integration/carrier/factory/group.go b/integration/carrier/factory/group.go index 3e00f74..7560586 100644 --- a/integration/carrier/factory/group.go +++ b/integration/carrier/factory/group.go @@ -14,7 +14,7 @@ type groupMutation struct { categoryType int categoryFunc func(ctx context.Context, i *model.Group, c int) error - beforeCreateFunc func(ctx context.Context) error + beforeCreateFunc func(ctx context.Context, i *model.Group) error afterCreateFunc func(ctx context.Context, i *model.Group) error } type GroupMetaFactory struct { @@ -28,6 +28,11 @@ type groupTrait struct { func GroupTrait() *groupTrait { return &groupTrait{} } +func (*groupMutation) beforeCreateMutateFunc(fn func(ctx context.Context, i *model.Group) error) func(m *groupMutation) { + return func(m *groupMutation) { + m.beforeCreateFunc = fn + } +} func (*groupMutation) afterCreateMutateFunc(fn func(ctx context.Context, i *model.Group) error) func(m *groupMutation) { return func(m *groupMutation) { m.afterCreateFunc = fn @@ -262,8 +267,8 @@ func (f *GroupMetaFactory) SetAfterCreateFunc(fn func(ctx context.Context, i *mo return f } -// SetBeforeCreateFunc register a function to be called after struct create -func (f *GroupMetaFactory) SetBeforeCreateFunc(fn func(ctx context.Context) error) *GroupMetaFactory { +// SetBeforeCreateFunc register a function to be called before struct create +func (f *GroupMetaFactory) SetBeforeCreateFunc(fn func(ctx context.Context, i *model.Group) error) *GroupMetaFactory { f.mutation.beforeCreateFunc = fn return f } @@ -274,6 +279,12 @@ func (t *groupTrait) SetAfterCreateFunc(fn func(ctx context.Context, i *model.Gr return t } +// SetBeforeCreateFunc register a function to be called before struct create +func (t *groupTrait) SetBeforeCreateFunc(fn func(ctx context.Context, i *model.Group) error) *groupTrait { + t.updates = append(t.updates, t.mutation.beforeCreateMutateFunc(fn)) + return t +} + // Build create a GroupFactory from GroupMetaFactory func (f *GroupMetaFactory) Build() *GroupFactory { return &GroupFactory{meta: *f, counter: &Counter{}} @@ -415,6 +426,7 @@ func (b *GroupBuilder) Create(ctx context.Context) (*model.Group, error) { } v := &model.Group{} + for _, f := range preSlice { err := f(ctx, v, index) @@ -431,12 +443,12 @@ func (b *GroupBuilder) Create(ctx context.Context) (*model.Group, error) { return nil, err } } - if b.mutation.beforeCreateFunc != nil { - if err := b.mutation.beforeCreateFunc(ctx); err != nil { + if err := b.mutation.beforeCreateFunc(ctx, v); err != nil { return nil, err } } + new := v if b.mutation.afterCreateFunc != nil { @@ -446,9 +458,7 @@ func (b *GroupBuilder) Create(ctx context.Context) (*model.Group, error) { } } for _, f := range postSlice { - err := f(ctx, new, index) - if err != nil { return nil, err } diff --git a/integration/carrier/factory/groupcategory.go b/integration/carrier/factory/groupcategory.go index 9c8645e..03f008c 100644 --- a/integration/carrier/factory/groupcategory.go +++ b/integration/carrier/factory/groupcategory.go @@ -11,7 +11,7 @@ type groupCategoryMutation struct { nameType int nameFunc func(ctx context.Context, i *model.GroupCategory, c int) error - beforeCreateFunc func(ctx context.Context) error + beforeCreateFunc func(ctx context.Context, i *model.GroupCategory) error afterCreateFunc func(ctx context.Context, i *model.GroupCategory) error } type GroupCategoryMetaFactory struct { @@ -25,6 +25,11 @@ type groupCategoryTrait struct { func GroupCategoryTrait() *groupCategoryTrait { return &groupCategoryTrait{} } +func (*groupCategoryMutation) beforeCreateMutateFunc(fn func(ctx context.Context, i *model.GroupCategory) error) func(m *groupCategoryMutation) { + return func(m *groupCategoryMutation) { + m.beforeCreateFunc = fn + } +} func (*groupCategoryMutation) afterCreateMutateFunc(fn func(ctx context.Context, i *model.GroupCategory) error) func(m *groupCategoryMutation) { return func(m *groupCategoryMutation) { m.afterCreateFunc = fn @@ -148,8 +153,8 @@ func (f *GroupCategoryMetaFactory) SetAfterCreateFunc(fn func(ctx context.Contex return f } -// SetBeforeCreateFunc register a function to be called after struct create -func (f *GroupCategoryMetaFactory) SetBeforeCreateFunc(fn func(ctx context.Context) error) *GroupCategoryMetaFactory { +// SetBeforeCreateFunc register a function to be called before struct create +func (f *GroupCategoryMetaFactory) SetBeforeCreateFunc(fn func(ctx context.Context, i *model.GroupCategory) error) *GroupCategoryMetaFactory { f.mutation.beforeCreateFunc = fn return f } @@ -160,6 +165,12 @@ func (t *groupCategoryTrait) SetAfterCreateFunc(fn func(ctx context.Context, i * return t } +// SetBeforeCreateFunc register a function to be called before struct create +func (t *groupCategoryTrait) SetBeforeCreateFunc(fn func(ctx context.Context, i *model.GroupCategory) error) *groupCategoryTrait { + t.updates = append(t.updates, t.mutation.beforeCreateMutateFunc(fn)) + return t +} + // Build create a GroupCategoryFactory from GroupCategoryMetaFactory func (f *GroupCategoryMetaFactory) Build() *GroupCategoryFactory { return &GroupCategoryFactory{meta: *f, counter: &Counter{}} @@ -263,6 +274,7 @@ func (b *GroupCategoryBuilder) Create(ctx context.Context) (*model.GroupCategory } v := &model.GroupCategory{} + for _, f := range preSlice { err := f(ctx, v, index) @@ -279,12 +291,12 @@ func (b *GroupCategoryBuilder) Create(ctx context.Context) (*model.GroupCategory return nil, err } } - if b.mutation.beforeCreateFunc != nil { - if err := b.mutation.beforeCreateFunc(ctx); err != nil { + if err := b.mutation.beforeCreateFunc(ctx, v); err != nil { return nil, err } } + new := v if b.mutation.afterCreateFunc != nil { @@ -294,9 +306,7 @@ func (b *GroupCategoryBuilder) Create(ctx context.Context) (*model.GroupCategory } } for _, f := range postSlice { - err := f(ctx, new, index) - if err != nil { return nil, err } diff --git a/integration/carrier/factory/user.go b/integration/carrier/factory/user.go index 03a418c..a14f84d 100644 --- a/integration/carrier/factory/user.go +++ b/integration/carrier/factory/user.go @@ -22,7 +22,7 @@ type userMutation struct { _postFooFunc func(ctx context.Context, set bool, obj *model.User, i string) error - beforeCreateFunc func(ctx context.Context) error + beforeCreateFunc func(ctx context.Context, i *model.User) error afterCreateFunc func(ctx context.Context, i *model.User) error } type UserMetaFactory struct { @@ -54,6 +54,11 @@ type userTrait struct { func UserTrait() *userTrait { return &userTrait{} } +func (*userMutation) beforeCreateMutateFunc(fn func(ctx context.Context, i *model.User) error) func(m *userMutation) { + return func(m *userMutation) { + m.beforeCreateFunc = fn + } +} func (*userMutation) afterCreateMutateFunc(fn func(ctx context.Context, i *model.User) error) func(m *userMutation) { return func(m *userMutation) { m.afterCreateFunc = fn @@ -580,8 +585,8 @@ func (f *UserMetaFactory) SetAfterCreateFunc(fn func(ctx context.Context, i *mod return f } -// SetBeforeCreateFunc register a function to be called after struct create -func (f *UserMetaFactory) SetBeforeCreateFunc(fn func(ctx context.Context) error) *UserMetaFactory { +// SetBeforeCreateFunc register a function to be called before struct create +func (f *UserMetaFactory) SetBeforeCreateFunc(fn func(ctx context.Context, i *model.User) error) *UserMetaFactory { f.mutation.beforeCreateFunc = fn return f } @@ -592,6 +597,12 @@ func (t *userTrait) SetAfterCreateFunc(fn func(ctx context.Context, i *model.Use return t } +// SetBeforeCreateFunc register a function to be called before struct create +func (t *userTrait) SetBeforeCreateFunc(fn func(ctx context.Context, i *model.User) error) *userTrait { + t.updates = append(t.updates, t.mutation.beforeCreateMutateFunc(fn)) + return t +} + // Build create a UserFactory from UserMetaFactory func (f *UserMetaFactory) Build() *UserFactory { return &UserFactory{meta: *f, counter: &Counter{}} @@ -1059,6 +1070,7 @@ func (b *UserBuilder) Create(ctx context.Context) (*model.User, error) { } v := &model.User{} + for _, f := range preSlice { err := f(ctx, v, index) @@ -1075,12 +1087,12 @@ func (b *UserBuilder) Create(ctx context.Context) (*model.User, error) { return nil, err } } - if b.mutation.beforeCreateFunc != nil { - if err := b.mutation.beforeCreateFunc(ctx); err != nil { + if err := b.mutation.beforeCreateFunc(ctx, v); err != nil { return nil, err } } + new := v if b.mutation.afterCreateFunc != nil { @@ -1090,9 +1102,7 @@ func (b *UserBuilder) Create(ctx context.Context) (*model.User, error) { } } for _, f := range postSlice { - err := f(ctx, new, index) - if err != nil { return nil, err } diff --git a/integration/carrierii/factory/ent_car.go b/integration/carrierii/factory/ent_car.go index b77b5ac..1cb9ce4 100644 --- a/integration/carrierii/factory/ent_car.go +++ b/integration/carrierii/factory/ent_car.go @@ -17,22 +17,29 @@ type EntCarMutator struct { OwnerID int RegisteredAt time.Time + + _creator *ten.CarCreate +} + +func (m *EntCarMutator) EntCreator() *ten.CarCreate { + return m._creator } type entCarMutation struct { modelType int - modelFunc func(ctx context.Context, i *EntCarMutator, c int, creator *ten.CarCreate) error + modelFunc func(ctx context.Context, i *EntCarMutator, c int) error ownerType int - ownerFunc func(ctx context.Context, i *EntCarMutator, c int, creator *ten.CarCreate) error + ownerFunc func(ctx context.Context, i *EntCarMutator, c int) error ownerIDType int - ownerIDFunc func(ctx context.Context, i *EntCarMutator, c int, creator *ten.CarCreate) error + ownerIDFunc func(ctx context.Context, i *EntCarMutator, c int) error registeredAtType int - registeredAtFunc func(ctx context.Context, i *EntCarMutator, c int, creator *ten.CarCreate) error + registeredAtFunc func(ctx context.Context, i *EntCarMutator, c int) error - afterCreateFunc func(ctx context.Context, i *ten.Car) error + beforeCreateFunc func(ctx context.Context, i *EntCarMutator) error + afterCreateFunc func(ctx context.Context, i *ten.Car) error } type EntCarMetaFactory struct { mutation entCarMutation @@ -45,6 +52,11 @@ type entCarTrait struct { func EntCarTrait() *entCarTrait { return &entCarTrait{} } +func (*entCarMutation) beforeCreateMutateFunc(fn func(ctx context.Context, i *EntCarMutator) error) func(m *entCarMutation) { + return func(m *entCarMutation) { + m.beforeCreateFunc = fn + } +} func (*entCarMutation) afterCreateMutateFunc(fn func(ctx context.Context, i *ten.Car) error) func(m *entCarMutation) { return func(m *entCarMutation) { m.afterCreateFunc = fn @@ -54,7 +66,7 @@ func (*entCarMutation) afterCreateMutateFunc(fn func(ctx context.Context, i *ten func (*entCarMutation) modelSequenceMutateFunc(fn func(ctx context.Context, i int) (string, error)) func(m *entCarMutation) { return func(m *entCarMutation) { m.modelType = TypeSequence - m.modelFunc = func(ctx context.Context, i *EntCarMutator, c int, creator *ten.CarCreate) error { + m.modelFunc = func(ctx context.Context, i *EntCarMutator, c int) error { if fn == nil { return nil } @@ -63,7 +75,7 @@ func (*entCarMutation) modelSequenceMutateFunc(fn func(ctx context.Context, i in return err } - creator.SetModel(value) + i.EntCreator().SetModel(value) i.Model = value return nil @@ -73,7 +85,7 @@ func (*entCarMutation) modelSequenceMutateFunc(fn func(ctx context.Context, i in func (*entCarMutation) modelLazyMutateFunc(fn func(ctx context.Context, i *EntCarMutator) (string, error)) func(m *entCarMutation) { return func(m *entCarMutation) { m.modelType = TypeLazy - m.modelFunc = func(ctx context.Context, i *EntCarMutator, c int, creator *ten.CarCreate) error { + m.modelFunc = func(ctx context.Context, i *EntCarMutator, c int) error { if fn == nil { return nil } @@ -82,7 +94,7 @@ func (*entCarMutation) modelLazyMutateFunc(fn func(ctx context.Context, i *EntCa return err } - creator.SetModel(value) + i.EntCreator().SetModel(value) i.Model = value return nil @@ -92,9 +104,9 @@ func (*entCarMutation) modelLazyMutateFunc(fn func(ctx context.Context, i *EntCa func (*entCarMutation) modelDefaultMutateFunc(v string) func(m *entCarMutation) { return func(m *entCarMutation) { m.modelType = TypeDefault - m.modelFunc = func(ctx context.Context, i *EntCarMutator, c int, creator *ten.CarCreate) error { + m.modelFunc = func(ctx context.Context, i *EntCarMutator, c int) error { - creator.SetModel(v) + i.EntCreator().SetModel(v) i.Model = v return nil @@ -104,7 +116,7 @@ func (*entCarMutation) modelDefaultMutateFunc(v string) func(m *entCarMutation) func (*entCarMutation) modelFactoryMutateFunc(fn func(ctx context.Context) (string, error)) func(m *entCarMutation) { return func(m *entCarMutation) { m.modelType = TypeFactory - m.modelFunc = func(ctx context.Context, i *EntCarMutator, c int, creator *ten.CarCreate) error { + m.modelFunc = func(ctx context.Context, i *EntCarMutator, c int) error { if fn == nil { return nil } @@ -113,7 +125,7 @@ func (*entCarMutation) modelFactoryMutateFunc(fn func(ctx context.Context) (stri return err } - creator.SetModel(value) + i.EntCreator().SetModel(value) i.Model = value @@ -173,7 +185,7 @@ func (t *entCarTrait) SetModelFactory(fn func(ctx context.Context) (string, erro func (*entCarMutation) ownerSequenceMutateFunc(fn func(ctx context.Context, i int) (*ten.User, error)) func(m *entCarMutation) { return func(m *entCarMutation) { m.ownerType = TypeSequence - m.ownerFunc = func(ctx context.Context, i *EntCarMutator, c int, creator *ten.CarCreate) error { + m.ownerFunc = func(ctx context.Context, i *EntCarMutator, c int) error { if fn == nil { return nil } @@ -182,7 +194,7 @@ func (*entCarMutation) ownerSequenceMutateFunc(fn func(ctx context.Context, i in return err } - creator.SetOwner(value) + i.EntCreator().SetOwner(value) i.Owner = value return nil @@ -192,7 +204,7 @@ func (*entCarMutation) ownerSequenceMutateFunc(fn func(ctx context.Context, i in func (*entCarMutation) ownerLazyMutateFunc(fn func(ctx context.Context, i *EntCarMutator) (*ten.User, error)) func(m *entCarMutation) { return func(m *entCarMutation) { m.ownerType = TypeLazy - m.ownerFunc = func(ctx context.Context, i *EntCarMutator, c int, creator *ten.CarCreate) error { + m.ownerFunc = func(ctx context.Context, i *EntCarMutator, c int) error { if fn == nil { return nil } @@ -201,7 +213,7 @@ func (*entCarMutation) ownerLazyMutateFunc(fn func(ctx context.Context, i *EntCa return err } - creator.SetOwner(value) + i.EntCreator().SetOwner(value) i.Owner = value return nil @@ -211,9 +223,9 @@ func (*entCarMutation) ownerLazyMutateFunc(fn func(ctx context.Context, i *EntCa func (*entCarMutation) ownerDefaultMutateFunc(v *ten.User) func(m *entCarMutation) { return func(m *entCarMutation) { m.ownerType = TypeDefault - m.ownerFunc = func(ctx context.Context, i *EntCarMutator, c int, creator *ten.CarCreate) error { + m.ownerFunc = func(ctx context.Context, i *EntCarMutator, c int) error { - creator.SetOwner(v) + i.EntCreator().SetOwner(v) i.Owner = v return nil @@ -223,7 +235,7 @@ func (*entCarMutation) ownerDefaultMutateFunc(v *ten.User) func(m *entCarMutatio func (*entCarMutation) ownerFactoryMutateFunc(fn func(ctx context.Context) (*ten.User, error)) func(m *entCarMutation) { return func(m *entCarMutation) { m.ownerType = TypeFactory - m.ownerFunc = func(ctx context.Context, i *EntCarMutator, c int, creator *ten.CarCreate) error { + m.ownerFunc = func(ctx context.Context, i *EntCarMutator, c int) error { if fn == nil { return nil } @@ -232,7 +244,7 @@ func (*entCarMutation) ownerFactoryMutateFunc(fn func(ctx context.Context) (*ten return err } - creator.SetOwner(value) + i.EntCreator().SetOwner(value) i.Owner = value @@ -292,7 +304,7 @@ func (t *entCarTrait) SetOwnerFactory(fn func(ctx context.Context) (*ten.User, e func (*entCarMutation) ownerIDSequenceMutateFunc(fn func(ctx context.Context, i int) (int, error)) func(m *entCarMutation) { return func(m *entCarMutation) { m.ownerIDType = TypeSequence - m.ownerIDFunc = func(ctx context.Context, i *EntCarMutator, c int, creator *ten.CarCreate) error { + m.ownerIDFunc = func(ctx context.Context, i *EntCarMutator, c int) error { if fn == nil { return nil } @@ -301,7 +313,7 @@ func (*entCarMutation) ownerIDSequenceMutateFunc(fn func(ctx context.Context, i return err } - creator.SetOwnerID(value) + i.EntCreator().SetOwnerID(value) i.OwnerID = value return nil @@ -311,7 +323,7 @@ func (*entCarMutation) ownerIDSequenceMutateFunc(fn func(ctx context.Context, i func (*entCarMutation) ownerIDLazyMutateFunc(fn func(ctx context.Context, i *EntCarMutator) (int, error)) func(m *entCarMutation) { return func(m *entCarMutation) { m.ownerIDType = TypeLazy - m.ownerIDFunc = func(ctx context.Context, i *EntCarMutator, c int, creator *ten.CarCreate) error { + m.ownerIDFunc = func(ctx context.Context, i *EntCarMutator, c int) error { if fn == nil { return nil } @@ -320,7 +332,7 @@ func (*entCarMutation) ownerIDLazyMutateFunc(fn func(ctx context.Context, i *Ent return err } - creator.SetOwnerID(value) + i.EntCreator().SetOwnerID(value) i.OwnerID = value return nil @@ -330,9 +342,9 @@ func (*entCarMutation) ownerIDLazyMutateFunc(fn func(ctx context.Context, i *Ent func (*entCarMutation) ownerIDDefaultMutateFunc(v int) func(m *entCarMutation) { return func(m *entCarMutation) { m.ownerIDType = TypeDefault - m.ownerIDFunc = func(ctx context.Context, i *EntCarMutator, c int, creator *ten.CarCreate) error { + m.ownerIDFunc = func(ctx context.Context, i *EntCarMutator, c int) error { - creator.SetOwnerID(v) + i.EntCreator().SetOwnerID(v) i.OwnerID = v return nil @@ -342,7 +354,7 @@ func (*entCarMutation) ownerIDDefaultMutateFunc(v int) func(m *entCarMutation) { func (*entCarMutation) ownerIDFactoryMutateFunc(fn func(ctx context.Context) (int, error)) func(m *entCarMutation) { return func(m *entCarMutation) { m.ownerIDType = TypeFactory - m.ownerIDFunc = func(ctx context.Context, i *EntCarMutator, c int, creator *ten.CarCreate) error { + m.ownerIDFunc = func(ctx context.Context, i *EntCarMutator, c int) error { if fn == nil { return nil } @@ -351,7 +363,7 @@ func (*entCarMutation) ownerIDFactoryMutateFunc(fn func(ctx context.Context) (in return err } - creator.SetOwnerID(value) + i.EntCreator().SetOwnerID(value) i.OwnerID = value @@ -411,7 +423,7 @@ func (t *entCarTrait) SetOwnerIDFactory(fn func(ctx context.Context) (int, error func (*entCarMutation) registeredAtSequenceMutateFunc(fn func(ctx context.Context, i int) (time.Time, error)) func(m *entCarMutation) { return func(m *entCarMutation) { m.registeredAtType = TypeSequence - m.registeredAtFunc = func(ctx context.Context, i *EntCarMutator, c int, creator *ten.CarCreate) error { + m.registeredAtFunc = func(ctx context.Context, i *EntCarMutator, c int) error { if fn == nil { return nil } @@ -420,7 +432,7 @@ func (*entCarMutation) registeredAtSequenceMutateFunc(fn func(ctx context.Contex return err } - creator.SetRegisteredAt(value) + i.EntCreator().SetRegisteredAt(value) i.RegisteredAt = value return nil @@ -430,7 +442,7 @@ func (*entCarMutation) registeredAtSequenceMutateFunc(fn func(ctx context.Contex func (*entCarMutation) registeredAtLazyMutateFunc(fn func(ctx context.Context, i *EntCarMutator) (time.Time, error)) func(m *entCarMutation) { return func(m *entCarMutation) { m.registeredAtType = TypeLazy - m.registeredAtFunc = func(ctx context.Context, i *EntCarMutator, c int, creator *ten.CarCreate) error { + m.registeredAtFunc = func(ctx context.Context, i *EntCarMutator, c int) error { if fn == nil { return nil } @@ -439,7 +451,7 @@ func (*entCarMutation) registeredAtLazyMutateFunc(fn func(ctx context.Context, i return err } - creator.SetRegisteredAt(value) + i.EntCreator().SetRegisteredAt(value) i.RegisteredAt = value return nil @@ -449,9 +461,9 @@ func (*entCarMutation) registeredAtLazyMutateFunc(fn func(ctx context.Context, i func (*entCarMutation) registeredAtDefaultMutateFunc(v time.Time) func(m *entCarMutation) { return func(m *entCarMutation) { m.registeredAtType = TypeDefault - m.registeredAtFunc = func(ctx context.Context, i *EntCarMutator, c int, creator *ten.CarCreate) error { + m.registeredAtFunc = func(ctx context.Context, i *EntCarMutator, c int) error { - creator.SetRegisteredAt(v) + i.EntCreator().SetRegisteredAt(v) i.RegisteredAt = v return nil @@ -461,7 +473,7 @@ func (*entCarMutation) registeredAtDefaultMutateFunc(v time.Time) func(m *entCar func (*entCarMutation) registeredAtFactoryMutateFunc(fn func(ctx context.Context) (time.Time, error)) func(m *entCarMutation) { return func(m *entCarMutation) { m.registeredAtType = TypeFactory - m.registeredAtFunc = func(ctx context.Context, i *EntCarMutator, c int, creator *ten.CarCreate) error { + m.registeredAtFunc = func(ctx context.Context, i *EntCarMutator, c int) error { if fn == nil { return nil } @@ -470,7 +482,7 @@ func (*entCarMutation) registeredAtFactoryMutateFunc(fn func(ctx context.Context return err } - creator.SetRegisteredAt(value) + i.EntCreator().SetRegisteredAt(value) i.RegisteredAt = value @@ -533,12 +545,24 @@ func (f *EntCarMetaFactory) SetAfterCreateFunc(fn func(ctx context.Context, i *t return f } +// SetBeforeCreateFunc register a function to be called before struct create +func (f *EntCarMetaFactory) SetBeforeCreateFunc(fn func(ctx context.Context, i *EntCarMutator) error) *EntCarMetaFactory { + f.mutation.beforeCreateFunc = fn + return f +} + // SetAfterCreateFunc register a function to be called after struct create func (t *entCarTrait) SetAfterCreateFunc(fn func(ctx context.Context, i *ten.Car) error) *entCarTrait { t.updates = append(t.updates, t.mutation.afterCreateMutateFunc(fn)) return t } +// SetBeforeCreateFunc register a function to be called before struct create +func (t *entCarTrait) SetBeforeCreateFunc(fn func(ctx context.Context, i *EntCarMutator) error) *entCarTrait { + t.updates = append(t.updates, t.mutation.beforeCreateMutateFunc(fn)) + return t +} + // Build create a EntCarFactory from EntCarMetaFactory func (f *EntCarMetaFactory) Build() *EntCarFactory { return &EntCarFactory{meta: *f, counter: &Counter{}} @@ -699,9 +723,9 @@ func (b *EntCarBuilder) CreateV(ctx context.Context) (ten.Car, error) { // Create return a new *ten.Car func (b *EntCarBuilder) Create(ctx context.Context) (*ten.Car, error) { - var preSlice = []func(ctx context.Context, i *EntCarMutator, c int, creator *ten.CarCreate) error{} - var lazySlice = []func(ctx context.Context, i *EntCarMutator, c int, creator *ten.CarCreate) error{} - var postSlice = []func(ctx context.Context, i *ten.Car, c int, creator *ten.CarCreate) error{} + var preSlice = []func(ctx context.Context, i *EntCarMutator, c int) error{} + var lazySlice = []func(ctx context.Context, i *EntCarMutator, c int) error{} + var postSlice = []func(ctx context.Context, i *ten.Car, c int) error{} index := b.counter.Get() _ = index @@ -710,10 +734,10 @@ func (b *EntCarBuilder) Create(ctx context.Context) (*ten.Car, error) { entBuilder := client.Car.Create() if b.modelOverriden { - preSlice = append(preSlice, func(ctx context.Context, i *EntCarMutator, c int, creator *ten.CarCreate) error { + preSlice = append(preSlice, func(ctx context.Context, i *EntCarMutator, c int) error { value := b.modelOverride - creator.SetModel(value) + i.EntCreator().SetModel(value) i.Model = value return nil @@ -732,10 +756,10 @@ func (b *EntCarBuilder) Create(ctx context.Context) (*ten.Car, error) { } if b.ownerOverriden { - preSlice = append(preSlice, func(ctx context.Context, i *EntCarMutator, c int, creator *ten.CarCreate) error { + preSlice = append(preSlice, func(ctx context.Context, i *EntCarMutator, c int) error { value := b.ownerOverride - creator.SetOwner(value) + i.EntCreator().SetOwner(value) i.Owner = value return nil @@ -754,10 +778,10 @@ func (b *EntCarBuilder) Create(ctx context.Context) (*ten.Car, error) { } if b.ownerIDOverriden { - preSlice = append(preSlice, func(ctx context.Context, i *EntCarMutator, c int, creator *ten.CarCreate) error { + preSlice = append(preSlice, func(ctx context.Context, i *EntCarMutator, c int) error { value := b.ownerIDOverride - creator.SetOwnerID(value) + i.EntCreator().SetOwnerID(value) i.OwnerID = value return nil @@ -776,10 +800,10 @@ func (b *EntCarBuilder) Create(ctx context.Context) (*ten.Car, error) { } if b.registeredAtOverriden { - preSlice = append(preSlice, func(ctx context.Context, i *EntCarMutator, c int, creator *ten.CarCreate) error { + preSlice = append(preSlice, func(ctx context.Context, i *EntCarMutator, c int) error { value := b.registeredAtOverride - creator.SetRegisteredAt(value) + i.EntCreator().SetRegisteredAt(value) i.RegisteredAt = value return nil @@ -798,9 +822,12 @@ func (b *EntCarBuilder) Create(ctx context.Context) (*ten.Car, error) { } v := &EntCarMutator{} + + v._creator = entBuilder + for _, f := range preSlice { - err := f(ctx, v, index, entBuilder) + err := f(ctx, v, index) if err != nil { return nil, err @@ -808,12 +835,17 @@ func (b *EntCarBuilder) Create(ctx context.Context) (*ten.Car, error) { } for _, f := range lazySlice { - err := f(ctx, v, index, entBuilder) + err := f(ctx, v, index) if err != nil { return nil, err } } + if b.mutation.beforeCreateFunc != nil { + if err := b.mutation.beforeCreateFunc(ctx, v); err != nil { + return nil, err + } + } new, err := entBuilder.Save(ctx) if err != nil { @@ -827,9 +859,7 @@ func (b *EntCarBuilder) Create(ctx context.Context) (*ten.Car, error) { } } for _, f := range postSlice { - - err := f(ctx, new, index, entBuilder) - + err := f(ctx, new, index) if err != nil { return nil, err } diff --git a/integration/carrierii/factory/ent_group.go b/integration/carrierii/factory/ent_group.go index 7e4a49b..ce61a8b 100644 --- a/integration/carrierii/factory/ent_group.go +++ b/integration/carrierii/factory/ent_group.go @@ -9,13 +9,20 @@ import ( type EntGroupMutator struct { Name string + + _creator *ten.GroupCreate +} + +func (m *EntGroupMutator) EntCreator() *ten.GroupCreate { + return m._creator } type entGroupMutation struct { nameType int - nameFunc func(ctx context.Context, i *EntGroupMutator, c int, creator *ten.GroupCreate) error + nameFunc func(ctx context.Context, i *EntGroupMutator, c int) error - afterCreateFunc func(ctx context.Context, i *ten.Group) error + beforeCreateFunc func(ctx context.Context, i *EntGroupMutator) error + afterCreateFunc func(ctx context.Context, i *ten.Group) error } type EntGroupMetaFactory struct { mutation entGroupMutation @@ -30,6 +37,11 @@ type entGroupTrait struct { func EntGroupTrait() *entGroupTrait { return &entGroupTrait{} } +func (*entGroupMutation) beforeCreateMutateFunc(fn func(ctx context.Context, i *EntGroupMutator) error) func(m *entGroupMutation) { + return func(m *entGroupMutation) { + m.beforeCreateFunc = fn + } +} func (*entGroupMutation) afterCreateMutateFunc(fn func(ctx context.Context, i *ten.Group) error) func(m *entGroupMutation) { return func(m *entGroupMutation) { m.afterCreateFunc = fn @@ -39,7 +51,7 @@ func (*entGroupMutation) afterCreateMutateFunc(fn func(ctx context.Context, i *t func (*entGroupMutation) nameSequenceMutateFunc(fn func(ctx context.Context, i int) (string, error)) func(m *entGroupMutation) { return func(m *entGroupMutation) { m.nameType = TypeSequence - m.nameFunc = func(ctx context.Context, i *EntGroupMutator, c int, creator *ten.GroupCreate) error { + m.nameFunc = func(ctx context.Context, i *EntGroupMutator, c int) error { if fn == nil { return nil } @@ -48,7 +60,7 @@ func (*entGroupMutation) nameSequenceMutateFunc(fn func(ctx context.Context, i i return err } - creator.SetName(value) + i.EntCreator().SetName(value) i.Name = value return nil @@ -58,7 +70,7 @@ func (*entGroupMutation) nameSequenceMutateFunc(fn func(ctx context.Context, i i func (*entGroupMutation) nameLazyMutateFunc(fn func(ctx context.Context, i *EntGroupMutator) (string, error)) func(m *entGroupMutation) { return func(m *entGroupMutation) { m.nameType = TypeLazy - m.nameFunc = func(ctx context.Context, i *EntGroupMutator, c int, creator *ten.GroupCreate) error { + m.nameFunc = func(ctx context.Context, i *EntGroupMutator, c int) error { if fn == nil { return nil } @@ -67,7 +79,7 @@ func (*entGroupMutation) nameLazyMutateFunc(fn func(ctx context.Context, i *EntG return err } - creator.SetName(value) + i.EntCreator().SetName(value) i.Name = value return nil @@ -77,9 +89,9 @@ func (*entGroupMutation) nameLazyMutateFunc(fn func(ctx context.Context, i *EntG func (*entGroupMutation) nameDefaultMutateFunc(v string) func(m *entGroupMutation) { return func(m *entGroupMutation) { m.nameType = TypeDefault - m.nameFunc = func(ctx context.Context, i *EntGroupMutator, c int, creator *ten.GroupCreate) error { + m.nameFunc = func(ctx context.Context, i *EntGroupMutator, c int) error { - creator.SetName(v) + i.EntCreator().SetName(v) i.Name = v return nil @@ -89,7 +101,7 @@ func (*entGroupMutation) nameDefaultMutateFunc(v string) func(m *entGroupMutatio func (*entGroupMutation) nameFactoryMutateFunc(fn func(ctx context.Context) (string, error)) func(m *entGroupMutation) { return func(m *entGroupMutation) { m.nameType = TypeFactory - m.nameFunc = func(ctx context.Context, i *EntGroupMutator, c int, creator *ten.GroupCreate) error { + m.nameFunc = func(ctx context.Context, i *EntGroupMutator, c int) error { if fn == nil { return nil } @@ -98,7 +110,7 @@ func (*entGroupMutation) nameFactoryMutateFunc(fn func(ctx context.Context) (str return err } - creator.SetName(value) + i.EntCreator().SetName(value) i.Name = value @@ -167,12 +179,24 @@ func (f *EntGroupMetaFactory) SetAfterCreateFunc(fn func(ctx context.Context, i return f } +// SetBeforeCreateFunc register a function to be called before struct create +func (f *EntGroupMetaFactory) SetBeforeCreateFunc(fn func(ctx context.Context, i *EntGroupMutator) error) *EntGroupMetaFactory { + f.mutation.beforeCreateFunc = fn + return f +} + // SetAfterCreateFunc register a function to be called after struct create func (t *entGroupTrait) SetAfterCreateFunc(fn func(ctx context.Context, i *ten.Group) error) *entGroupTrait { t.updates = append(t.updates, t.mutation.afterCreateMutateFunc(fn)) return t } +// SetBeforeCreateFunc register a function to be called before struct create +func (t *entGroupTrait) SetBeforeCreateFunc(fn func(ctx context.Context, i *EntGroupMutator) error) *entGroupTrait { + t.updates = append(t.updates, t.mutation.beforeCreateMutateFunc(fn)) + return t +} + // Build create a EntGroupFactory from EntGroupMetaFactory func (f *EntGroupMetaFactory) Build() *EntGroupFactory { return &EntGroupFactory{meta: *f, counter: &Counter{}} @@ -300,9 +324,9 @@ func (b *EntGroupBuilder) CreateV(ctx context.Context) (ten.Group, error) { // Create return a new *ten.Group func (b *EntGroupBuilder) Create(ctx context.Context) (*ten.Group, error) { - var preSlice = []func(ctx context.Context, i *EntGroupMutator, c int, creator *ten.GroupCreate) error{} - var lazySlice = []func(ctx context.Context, i *EntGroupMutator, c int, creator *ten.GroupCreate) error{} - var postSlice = []func(ctx context.Context, i *ten.Group, c int, creator *ten.GroupCreate) error{} + var preSlice = []func(ctx context.Context, i *EntGroupMutator, c int) error{} + var lazySlice = []func(ctx context.Context, i *EntGroupMutator, c int) error{} + var postSlice = []func(ctx context.Context, i *ten.Group, c int) error{} index := b.counter.Get() _ = index @@ -311,10 +335,10 @@ func (b *EntGroupBuilder) Create(ctx context.Context) (*ten.Group, error) { entBuilder := client.Group.Create() if b.nameOverriden { - preSlice = append(preSlice, func(ctx context.Context, i *EntGroupMutator, c int, creator *ten.GroupCreate) error { + preSlice = append(preSlice, func(ctx context.Context, i *EntGroupMutator, c int) error { value := b.nameOverride - creator.SetName(value) + i.EntCreator().SetName(value) i.Name = value return nil @@ -333,9 +357,12 @@ func (b *EntGroupBuilder) Create(ctx context.Context) (*ten.Group, error) { } v := &EntGroupMutator{} + + v._creator = entBuilder + for _, f := range preSlice { - err := f(ctx, v, index, entBuilder) + err := f(ctx, v, index) if err != nil { return nil, err @@ -343,12 +370,17 @@ func (b *EntGroupBuilder) Create(ctx context.Context) (*ten.Group, error) { } for _, f := range lazySlice { - err := f(ctx, v, index, entBuilder) + err := f(ctx, v, index) if err != nil { return nil, err } } + if b.mutation.beforeCreateFunc != nil { + if err := b.mutation.beforeCreateFunc(ctx, v); err != nil { + return nil, err + } + } new, err := entBuilder.Save(ctx) if err != nil { @@ -362,9 +394,7 @@ func (b *EntGroupBuilder) Create(ctx context.Context) (*ten.Group, error) { } } for _, f := range postSlice { - - err := f(ctx, new, index, entBuilder) - + err := f(ctx, new, index) if err != nil { return nil, err } diff --git a/integration/carrierii/factory/ent_user.go b/integration/carrierii/factory/ent_user.go index 494b9ef..c03097c 100644 --- a/integration/carrierii/factory/ent_user.go +++ b/integration/carrierii/factory/ent_user.go @@ -13,21 +13,28 @@ type EntUserMutator struct { Email string Name string + + _creator *ten.UserCreate +} + +func (m *EntUserMutator) EntCreator() *ten.UserCreate { + return m._creator } type entUserMutation struct { ageType int - ageFunc func(ctx context.Context, i *EntUserMutator, c int, creator *ten.UserCreate) error + ageFunc func(ctx context.Context, i *EntUserMutator, c int) error emailType int - emailFunc func(ctx context.Context, i *EntUserMutator, c int, creator *ten.UserCreate) error + emailFunc func(ctx context.Context, i *EntUserMutator, c int) error nameType int - nameFunc func(ctx context.Context, i *EntUserMutator, c int, creator *ten.UserCreate) error + nameFunc func(ctx context.Context, i *EntUserMutator, c int) error _postGroupsFunc func(ctx context.Context, set bool, obj *ten.User, i int) error - afterCreateFunc func(ctx context.Context, i *ten.User) error + beforeCreateFunc func(ctx context.Context, i *EntUserMutator) error + afterCreateFunc func(ctx context.Context, i *ten.User) error } type EntUserMetaFactory struct { mutation entUserMutation @@ -40,6 +47,11 @@ type entUserTrait struct { func EntUserTrait() *entUserTrait { return &entUserTrait{} } +func (*entUserMutation) beforeCreateMutateFunc(fn func(ctx context.Context, i *EntUserMutator) error) func(m *entUserMutation) { + return func(m *entUserMutation) { + m.beforeCreateFunc = fn + } +} func (*entUserMutation) afterCreateMutateFunc(fn func(ctx context.Context, i *ten.User) error) func(m *entUserMutation) { return func(m *entUserMutation) { m.afterCreateFunc = fn @@ -49,7 +61,7 @@ func (*entUserMutation) afterCreateMutateFunc(fn func(ctx context.Context, i *te func (*entUserMutation) ageSequenceMutateFunc(fn func(ctx context.Context, i int) (int, error)) func(m *entUserMutation) { return func(m *entUserMutation) { m.ageType = TypeSequence - m.ageFunc = func(ctx context.Context, i *EntUserMutator, c int, creator *ten.UserCreate) error { + m.ageFunc = func(ctx context.Context, i *EntUserMutator, c int) error { if fn == nil { return nil } @@ -58,7 +70,7 @@ func (*entUserMutation) ageSequenceMutateFunc(fn func(ctx context.Context, i int return err } - creator.SetAge(value) + i.EntCreator().SetAge(value) i.Age = value return nil @@ -68,7 +80,7 @@ func (*entUserMutation) ageSequenceMutateFunc(fn func(ctx context.Context, i int func (*entUserMutation) ageLazyMutateFunc(fn func(ctx context.Context, i *EntUserMutator) (int, error)) func(m *entUserMutation) { return func(m *entUserMutation) { m.ageType = TypeLazy - m.ageFunc = func(ctx context.Context, i *EntUserMutator, c int, creator *ten.UserCreate) error { + m.ageFunc = func(ctx context.Context, i *EntUserMutator, c int) error { if fn == nil { return nil } @@ -77,7 +89,7 @@ func (*entUserMutation) ageLazyMutateFunc(fn func(ctx context.Context, i *EntUse return err } - creator.SetAge(value) + i.EntCreator().SetAge(value) i.Age = value return nil @@ -87,9 +99,9 @@ func (*entUserMutation) ageLazyMutateFunc(fn func(ctx context.Context, i *EntUse func (*entUserMutation) ageDefaultMutateFunc(v int) func(m *entUserMutation) { return func(m *entUserMutation) { m.ageType = TypeDefault - m.ageFunc = func(ctx context.Context, i *EntUserMutator, c int, creator *ten.UserCreate) error { + m.ageFunc = func(ctx context.Context, i *EntUserMutator, c int) error { - creator.SetAge(v) + i.EntCreator().SetAge(v) i.Age = v return nil @@ -99,7 +111,7 @@ func (*entUserMutation) ageDefaultMutateFunc(v int) func(m *entUserMutation) { func (*entUserMutation) ageFactoryMutateFunc(fn func(ctx context.Context) (int, error)) func(m *entUserMutation) { return func(m *entUserMutation) { m.ageType = TypeFactory - m.ageFunc = func(ctx context.Context, i *EntUserMutator, c int, creator *ten.UserCreate) error { + m.ageFunc = func(ctx context.Context, i *EntUserMutator, c int) error { if fn == nil { return nil } @@ -108,7 +120,7 @@ func (*entUserMutation) ageFactoryMutateFunc(fn func(ctx context.Context) (int, return err } - creator.SetAge(value) + i.EntCreator().SetAge(value) i.Age = value @@ -168,7 +180,7 @@ func (t *entUserTrait) SetAgeFactory(fn func(ctx context.Context) (int, error)) func (*entUserMutation) emailSequenceMutateFunc(fn func(ctx context.Context, i int) (string, error)) func(m *entUserMutation) { return func(m *entUserMutation) { m.emailType = TypeSequence - m.emailFunc = func(ctx context.Context, i *EntUserMutator, c int, creator *ten.UserCreate) error { + m.emailFunc = func(ctx context.Context, i *EntUserMutator, c int) error { if fn == nil { return nil } @@ -177,7 +189,7 @@ func (*entUserMutation) emailSequenceMutateFunc(fn func(ctx context.Context, i i return err } - creator.SetEmail(value) + i.EntCreator().SetEmail(value) i.Email = value return nil @@ -187,7 +199,7 @@ func (*entUserMutation) emailSequenceMutateFunc(fn func(ctx context.Context, i i func (*entUserMutation) emailLazyMutateFunc(fn func(ctx context.Context, i *EntUserMutator) (string, error)) func(m *entUserMutation) { return func(m *entUserMutation) { m.emailType = TypeLazy - m.emailFunc = func(ctx context.Context, i *EntUserMutator, c int, creator *ten.UserCreate) error { + m.emailFunc = func(ctx context.Context, i *EntUserMutator, c int) error { if fn == nil { return nil } @@ -196,7 +208,7 @@ func (*entUserMutation) emailLazyMutateFunc(fn func(ctx context.Context, i *EntU return err } - creator.SetEmail(value) + i.EntCreator().SetEmail(value) i.Email = value return nil @@ -206,9 +218,9 @@ func (*entUserMutation) emailLazyMutateFunc(fn func(ctx context.Context, i *EntU func (*entUserMutation) emailDefaultMutateFunc(v string) func(m *entUserMutation) { return func(m *entUserMutation) { m.emailType = TypeDefault - m.emailFunc = func(ctx context.Context, i *EntUserMutator, c int, creator *ten.UserCreate) error { + m.emailFunc = func(ctx context.Context, i *EntUserMutator, c int) error { - creator.SetEmail(v) + i.EntCreator().SetEmail(v) i.Email = v return nil @@ -218,7 +230,7 @@ func (*entUserMutation) emailDefaultMutateFunc(v string) func(m *entUserMutation func (*entUserMutation) emailFactoryMutateFunc(fn func(ctx context.Context) (string, error)) func(m *entUserMutation) { return func(m *entUserMutation) { m.emailType = TypeFactory - m.emailFunc = func(ctx context.Context, i *EntUserMutator, c int, creator *ten.UserCreate) error { + m.emailFunc = func(ctx context.Context, i *EntUserMutator, c int) error { if fn == nil { return nil } @@ -227,7 +239,7 @@ func (*entUserMutation) emailFactoryMutateFunc(fn func(ctx context.Context) (str return err } - creator.SetEmail(value) + i.EntCreator().SetEmail(value) i.Email = value @@ -287,7 +299,7 @@ func (t *entUserTrait) SetEmailFactory(fn func(ctx context.Context) (string, err func (*entUserMutation) nameSequenceMutateFunc(fn func(ctx context.Context, i int) (string, error)) func(m *entUserMutation) { return func(m *entUserMutation) { m.nameType = TypeSequence - m.nameFunc = func(ctx context.Context, i *EntUserMutator, c int, creator *ten.UserCreate) error { + m.nameFunc = func(ctx context.Context, i *EntUserMutator, c int) error { if fn == nil { return nil } @@ -296,7 +308,7 @@ func (*entUserMutation) nameSequenceMutateFunc(fn func(ctx context.Context, i in return err } - creator.SetName(value) + i.EntCreator().SetName(value) i.Name = value return nil @@ -306,7 +318,7 @@ func (*entUserMutation) nameSequenceMutateFunc(fn func(ctx context.Context, i in func (*entUserMutation) nameLazyMutateFunc(fn func(ctx context.Context, i *EntUserMutator) (string, error)) func(m *entUserMutation) { return func(m *entUserMutation) { m.nameType = TypeLazy - m.nameFunc = func(ctx context.Context, i *EntUserMutator, c int, creator *ten.UserCreate) error { + m.nameFunc = func(ctx context.Context, i *EntUserMutator, c int) error { if fn == nil { return nil } @@ -315,7 +327,7 @@ func (*entUserMutation) nameLazyMutateFunc(fn func(ctx context.Context, i *EntUs return err } - creator.SetName(value) + i.EntCreator().SetName(value) i.Name = value return nil @@ -325,9 +337,9 @@ func (*entUserMutation) nameLazyMutateFunc(fn func(ctx context.Context, i *EntUs func (*entUserMutation) nameDefaultMutateFunc(v string) func(m *entUserMutation) { return func(m *entUserMutation) { m.nameType = TypeDefault - m.nameFunc = func(ctx context.Context, i *EntUserMutator, c int, creator *ten.UserCreate) error { + m.nameFunc = func(ctx context.Context, i *EntUserMutator, c int) error { - creator.SetName(v) + i.EntCreator().SetName(v) i.Name = v return nil @@ -337,7 +349,7 @@ func (*entUserMutation) nameDefaultMutateFunc(v string) func(m *entUserMutation) func (*entUserMutation) nameFactoryMutateFunc(fn func(ctx context.Context) (string, error)) func(m *entUserMutation) { return func(m *entUserMutation) { m.nameType = TypeFactory - m.nameFunc = func(ctx context.Context, i *EntUserMutator, c int, creator *ten.UserCreate) error { + m.nameFunc = func(ctx context.Context, i *EntUserMutator, c int) error { if fn == nil { return nil } @@ -346,7 +358,7 @@ func (*entUserMutation) nameFactoryMutateFunc(fn func(ctx context.Context) (stri return err } - creator.SetName(value) + i.EntCreator().SetName(value) i.Name = value @@ -425,12 +437,24 @@ func (f *EntUserMetaFactory) SetAfterCreateFunc(fn func(ctx context.Context, i * return f } +// SetBeforeCreateFunc register a function to be called before struct create +func (f *EntUserMetaFactory) SetBeforeCreateFunc(fn func(ctx context.Context, i *EntUserMutator) error) *EntUserMetaFactory { + f.mutation.beforeCreateFunc = fn + return f +} + // SetAfterCreateFunc register a function to be called after struct create func (t *entUserTrait) SetAfterCreateFunc(fn func(ctx context.Context, i *ten.User) error) *entUserTrait { t.updates = append(t.updates, t.mutation.afterCreateMutateFunc(fn)) return t } +// SetBeforeCreateFunc register a function to be called before struct create +func (t *entUserTrait) SetBeforeCreateFunc(fn func(ctx context.Context, i *EntUserMutator) error) *entUserTrait { + t.updates = append(t.updates, t.mutation.beforeCreateMutateFunc(fn)) + return t +} + // Build create a EntUserFactory from EntUserMetaFactory func (f *EntUserMetaFactory) Build() *EntUserFactory { return &EntUserFactory{meta: *f, counter: &Counter{}} @@ -591,9 +615,9 @@ func (b *EntUserBuilder) CreateV(ctx context.Context) (ten.User, error) { // Create return a new *ten.User func (b *EntUserBuilder) Create(ctx context.Context) (*ten.User, error) { - var preSlice = []func(ctx context.Context, i *EntUserMutator, c int, creator *ten.UserCreate) error{} - var lazySlice = []func(ctx context.Context, i *EntUserMutator, c int, creator *ten.UserCreate) error{} - var postSlice = []func(ctx context.Context, i *ten.User, c int, creator *ten.UserCreate) error{} + var preSlice = []func(ctx context.Context, i *EntUserMutator, c int) error{} + var lazySlice = []func(ctx context.Context, i *EntUserMutator, c int) error{} + var postSlice = []func(ctx context.Context, i *ten.User, c int) error{} index := b.counter.Get() _ = index @@ -602,10 +626,10 @@ func (b *EntUserBuilder) Create(ctx context.Context) (*ten.User, error) { entBuilder := client.User.Create() if b.ageOverriden { - preSlice = append(preSlice, func(ctx context.Context, i *EntUserMutator, c int, creator *ten.UserCreate) error { + preSlice = append(preSlice, func(ctx context.Context, i *EntUserMutator, c int) error { value := b.ageOverride - creator.SetAge(value) + i.EntCreator().SetAge(value) i.Age = value return nil @@ -624,10 +648,10 @@ func (b *EntUserBuilder) Create(ctx context.Context) (*ten.User, error) { } if b.emailOverriden { - preSlice = append(preSlice, func(ctx context.Context, i *EntUserMutator, c int, creator *ten.UserCreate) error { + preSlice = append(preSlice, func(ctx context.Context, i *EntUserMutator, c int) error { value := b.emailOverride - creator.SetEmail(value) + i.EntCreator().SetEmail(value) i.Email = value return nil @@ -646,10 +670,10 @@ func (b *EntUserBuilder) Create(ctx context.Context) (*ten.User, error) { } if b.nameOverriden { - preSlice = append(preSlice, func(ctx context.Context, i *EntUserMutator, c int, creator *ten.UserCreate) error { + preSlice = append(preSlice, func(ctx context.Context, i *EntUserMutator, c int) error { value := b.nameOverride - creator.SetName(value) + i.EntCreator().SetName(value) i.Name = value return nil @@ -668,16 +692,19 @@ func (b *EntUserBuilder) Create(ctx context.Context) (*ten.User, error) { } if b.mutation._postGroupsFunc != nil { - postSlice = append(postSlice, func(ctx context.Context, i *ten.User, c int, creator *ten.UserCreate) error { + postSlice = append(postSlice, func(ctx context.Context, i *ten.User, c int) error { err := b.mutation._postGroupsFunc(ctx, b._postGroupsSet, i, b._postGroups) return err }) } v := &EntUserMutator{} + + v._creator = entBuilder + for _, f := range preSlice { - err := f(ctx, v, index, entBuilder) + err := f(ctx, v, index) if err != nil { return nil, err @@ -685,12 +712,17 @@ func (b *EntUserBuilder) Create(ctx context.Context) (*ten.User, error) { } for _, f := range lazySlice { - err := f(ctx, v, index, entBuilder) + err := f(ctx, v, index) if err != nil { return nil, err } } + if b.mutation.beforeCreateFunc != nil { + if err := b.mutation.beforeCreateFunc(ctx, v); err != nil { + return nil, err + } + } new, err := entBuilder.Save(ctx) if err != nil { @@ -704,9 +736,7 @@ func (b *EntUserBuilder) Create(ctx context.Context) (*ten.User, error) { } } for _, f := range postSlice { - - err := f(ctx, new, index, entBuilder) - + err := f(ctx, new, index) if err != nil { return nil, err } diff --git a/integration/integration_test.go b/integration/integration_test.go index 8b022c5..7357811 100644 --- a/integration/integration_test.go +++ b/integration/integration_test.go @@ -62,10 +62,6 @@ func getStructFactory() *carrier.Factory { SetMixnameTrait(factory.UserTrait().SetNameDefault("mix_name")). SetMixemailTrait(factory.UserTrait().SetEmailDefault("mix_email").SetAfterCreateFunc(nil)). SetMixtitleTrait(factory.UserTrait().SetTitleDefault("mix_title")). - SetBeforeCreateFunc(func(ctx context.Context) error { - // do nothing - return nil - }). SetAfterCreateFunc(func(ctx context.Context, i *model.User) error { i.Email = i.Email + ".com" return nil @@ -96,7 +92,7 @@ func getEntFactory() (*carrier.EntFactory, error) { return fmt.Sprintf("group%d", i), nil }, ). - SetBeforeCreateFunc(func(ctx context.Context, creator *ent.GroupCreate) error { + SetBeforeCreateFunc(func(ctx context.Context, c *factory.EntGroupMutator) error { user, err := client.User.Create(). SetAge(int(rand.Uint32())). SetName("group-user"). @@ -104,7 +100,7 @@ func getEntFactory() (*carrier.EntFactory, error) { if err != nil { return err } - creator.AddUsers(user) + c.EntCreator().AddUsers(user) return nil }). SetAfterCreateFunc(func(ctx context.Context, i *ent.Group) error { diff --git a/template/carrier.tmpl b/template/carrier.tmpl index b293933..2c6f377 100644 --- a/template/carrier.tmpl +++ b/template/carrier.tmpl @@ -24,25 +24,21 @@ import ( {{range .Fields}} {{.Name}} {{.ValueType}} {{end}} + _creator *{{$EntPkg}}.{{$RawSchemaName}}Create + } + func (m *{{$SchemaName}}Mutator) EntCreator() *{{$EntPkg}}.{{$RawSchemaName}}Create { + return m._creator } {{end}} type {{$SchemaName | FirstLower}}Mutation struct { {{range .Fields}} - {{$DynamicParams := ""}} - {{if $EntSchema}} - {{$DynamicParams = $RawSchemaName | printf "creator *%s.%sCreate" $EntPkg}} - {{end}} {{.Name | FirstLower}}Type int - {{.Name | FirstLower}}Func func(ctx context.Context, i *{{$SchemaType}}, c int, {{$DynamicParams}}) error + {{.Name | FirstLower}}Func func(ctx context.Context, i *{{$SchemaType}}, c int) error {{end}} {{range .PostFields}} _post{{.Name}}Func func(ctx context.Context, set bool, obj *{{$CreatedType}}, i {{.ValueType}}) error {{end}} - {{$DynamicParams := ""}} - {{if $EntSchema}} - {{$DynamicParams = $RawSchemaName | printf "creator *%s.%sCreate" $EntPkg}} - {{end}} - beforeCreateFunc func(ctx context.Context, {{$DynamicParams}}) error + beforeCreateFunc func(ctx context.Context, i *{{$SchemaType}}) error afterCreateFunc func(ctx context.Context, i *{{$CreatedType}}) error } type {{$SchemaName}}MetaFactory struct { @@ -58,20 +54,21 @@ import ( func {{$SchemaName}}Trait() *{{$SchemaName | FirstLower}}Trait { return &{{$SchemaName | FirstLower}}Trait{} } + func (*{{$SchemaName | FirstLower}}Mutation) beforeCreateMutateFunc(fn func(ctx context.Context, i *{{$SchemaType}}) error) func(m *{{$SchemaName | FirstLower}}Mutation) { + return func(m *{{$SchemaName | FirstLower}}Mutation) { + m.beforeCreateFunc = fn + } + } func (*{{$SchemaName | FirstLower}}Mutation) afterCreateMutateFunc(fn func(ctx context.Context, i *{{$CreatedType}}) error) func(m *{{$SchemaName | FirstLower}}Mutation) { return func(m *{{$SchemaName | FirstLower}}Mutation) { m.afterCreateFunc = fn } } {{range .Fields}} - {{$DynamicParams := ""}} - {{if $EntSchema}} - {{$DynamicParams = $RawSchemaName | printf "creator *%s.%sCreate" $EntPkg}} - {{end}} func (*{{$SchemaName | FirstLower}}Mutation) {{.Name | FirstLower}}SequenceMutateFunc(fn func(ctx context.Context, i int) ({{.ValueType}}, error)) func(m *{{$SchemaName | FirstLower}}Mutation) { return func(m *{{$SchemaName | FirstLower}}Mutation) { m.{{.Name | FirstLower}}Type = TypeSequence - m.{{.Name | FirstLower}}Func = func(ctx context.Context, i *{{$SchemaType}}, c int, {{$DynamicParams}}) error { + m.{{.Name | FirstLower}}Func = func(ctx context.Context, i *{{$SchemaType}}, c int) error { if fn == nil { return nil } @@ -80,7 +77,7 @@ import ( return err } {{if $EntSchema}} - creator.{{.Setter}}(value) + i.EntCreator().{{.Setter}}(value) {{end}} i.{{.Name}} = value return nil @@ -90,7 +87,7 @@ import ( func (*{{$SchemaName | FirstLower}}Mutation) {{.Name | FirstLower}}LazyMutateFunc(fn func(ctx context.Context, i *{{$SchemaType}}) ({{.ValueType}}, error)) func(m *{{$SchemaName | FirstLower}}Mutation) { return func(m *{{$SchemaName | FirstLower}}Mutation) { m.{{.Name | FirstLower}}Type = TypeLazy - m.{{.Name | FirstLower}}Func = func(ctx context.Context, i *{{$SchemaType}}, c int, {{$DynamicParams}}) error { + m.{{.Name | FirstLower}}Func = func(ctx context.Context, i *{{$SchemaType}}, c int) error { if fn == nil { return nil } @@ -99,7 +96,7 @@ import ( return err } {{if $EntSchema}} - creator.{{.Setter}}(value) + i.EntCreator().{{.Setter}}(value) {{end}} i.{{.Name}} = value return nil @@ -109,9 +106,9 @@ import ( func (*{{$SchemaName | FirstLower}}Mutation) {{.Name | FirstLower}}DefaultMutateFunc(v {{.ValueType}}) func(m *{{$SchemaName | FirstLower}}Mutation) { return func(m *{{$SchemaName | FirstLower}}Mutation) { m.{{.Name | FirstLower}}Type = TypeDefault - m.{{.Name | FirstLower}}Func = func(ctx context.Context, i *{{$SchemaType}}, c int, {{$DynamicParams}}) error { + m.{{.Name | FirstLower}}Func = func(ctx context.Context, i *{{$SchemaType}}, c int) error { {{if $EntSchema}} - creator.{{.Setter}}(v) + i.EntCreator().{{.Setter}}(v) {{end}} i.{{.Name}} = v return nil @@ -121,7 +118,7 @@ import ( func (*{{$SchemaName | FirstLower}}Mutation) {{.Name | FirstLower}}FactoryMutateFunc(fn func(ctx context.Context) ({{.ValueType}}, error)) func(m *{{$SchemaName | FirstLower}}Mutation) { return func(m *{{$SchemaName | FirstLower}}Mutation) { m.{{.Name | FirstLower}}Type = TypeFactory - m.{{.Name | FirstLower}}Func = func(ctx context.Context, i *{{$SchemaType}}, c int, {{$DynamicParams}}) error { + m.{{.Name | FirstLower}}Func = func(ctx context.Context, i *{{$SchemaType}}, c int) error { if fn == nil { return nil } @@ -130,7 +127,7 @@ import ( return err } {{if $EntSchema}} - creator.{{.Setter}}(value) + i.EntCreator().{{.Setter}}(value) {{end}} i.{{.Name}} = value @@ -208,7 +205,7 @@ import ( return f } // SetBeforeCreateFunc register a function to be called before struct create - func (f *{{$SchemaName}}MetaFactory) SetBeforeCreateFunc(fn func(ctx context.Context, {{$DynamicParams}}) error) *{{$SchemaName}}MetaFactory { + func (f *{{$SchemaName}}MetaFactory) SetBeforeCreateFunc(fn func(ctx context.Context, i *{{$SchemaType}}) error) *{{$SchemaName}}MetaFactory { f.mutation.beforeCreateFunc = fn return f } @@ -217,6 +214,11 @@ import ( t.updates = append(t.updates, t.mutation.afterCreateMutateFunc(fn)) return t } + // SetBeforeCreateFunc register a function to be called before struct create + func (t *{{$SchemaName | FirstLower}}Trait) SetBeforeCreateFunc(fn func(ctx context.Context, i *{{$SchemaType}}) error) *{{$SchemaName | FirstLower}}Trait { + t.updates = append(t.updates, t.mutation.beforeCreateMutateFunc(fn)) + return t + } // Build create a {{$SchemaName}}Factory from {{$SchemaName}}MetaFactory func (f *{{$SchemaName}}MetaFactory) Build() *{{$SchemaName}}Factory { return &{{$SchemaName}}Factory{meta: *f, counter: &Counter{}} @@ -370,9 +372,9 @@ import ( // Create return a new *{{$CreatedType}} func (b *{{$SchemaName}}Builder) Create(ctx context.Context) (*{{$CreatedType}}, error) { {{if $EntSchema}} - var preSlice = []func(ctx context.Context, i *{{$SchemaType}}, c int, creator *{{$EntPkg}}.{{.Name}}Create) error{} - var lazySlice = []func(ctx context.Context, i *{{$SchemaType}}, c int, creator *{{$EntPkg}}.{{.Name}}Create) error{} - var postSlice = []func(ctx context.Context, i *{{$CreatedType}}, c int, creator *{{$EntPkg}}.{{.Name}}Create) error{} + var preSlice = []func(ctx context.Context, i *{{$SchemaType}}, c int) error{} + var lazySlice = []func(ctx context.Context, i *{{$SchemaType}}, c int) error{} + var postSlice = []func(ctx context.Context, i *{{$CreatedType}}, c int) error{} {{else}} var preSlice = []func(ctx context.Context, i *{{$SchemaType}}, c int) error{} var lazySlice = []func(ctx context.Context, i *{{$SchemaType}}, c int) error{} @@ -387,15 +389,11 @@ import ( {{end}} {{range .Fields}} - {{$DynamicParams := ""}} - {{if $EntSchema}} - {{$DynamicParams = $RawSchemaName | printf "creator *%s.%sCreate" $EntPkg}} - {{end}} if b.{{.Name | FirstLower}}Overriden { - preSlice = append(preSlice, func(ctx context.Context, i *{{$SchemaType}}, c int, {{$DynamicParams}}) error { + preSlice = append(preSlice, func(ctx context.Context, i *{{$SchemaType}}, c int) error { value := b.{{.Name | FirstLower}}Override {{if $EntSchema}} - creator.{{.Setter}}(value) + i.EntCreator().{{.Setter}}(value) {{end}} i.{{.Name}} = value return nil @@ -414,12 +412,8 @@ import ( } {{end}} {{range .PostFields}} - {{$DynamicParams := ""}} - {{if $EntSchema}} - {{$DynamicParams = $RawSchemaName | printf "creator *%s.%sCreate" $EntPkg}} - {{end}} if b.mutation._post{{.Name}}Func != nil { - postSlice = append(postSlice, func(ctx context.Context, i *{{$CreatedType}}, c int, {{$DynamicParams}}) error { + postSlice = append(postSlice, func(ctx context.Context, i *{{$CreatedType}}, c int) error { err := b.mutation._post{{.Name}}Func(ctx, b._post{{.Name}}Set, i, b._post{{.Name}}) return err }) @@ -427,9 +421,12 @@ import ( {{end}} v := &{{$SchemaType}}{} + {{if $EntSchema}} + v._creator = entBuilder + {{end}} for _, f := range preSlice { {{if $EntSchema}} - err := f(ctx, v, index, entBuilder) + err := f(ctx, v, index) {{else}} err := f(ctx, v, index) {{end}} @@ -439,7 +436,7 @@ import ( } for _, f := range lazySlice { {{if $EntSchema}} - err := f(ctx, v, index, entBuilder) + err := f(ctx, v, index) {{else}} err := f(ctx, v, index) {{end}} @@ -447,22 +444,17 @@ import ( return nil, err } } + if b.mutation.beforeCreateFunc != nil { + if err := b.mutation.beforeCreateFunc(ctx, v); err != nil { + return nil, err + } + } {{if $EntSchema}} - if b.mutation.beforeCreateFunc != nil { - if err := b.mutation.beforeCreateFunc(ctx, entBuilder); err != nil { - return nil, err - } - } new, err := entBuilder.Save(ctx) if err != nil { return nil, err } {{else}} - if b.mutation.beforeCreateFunc != nil { - if err := b.mutation.beforeCreateFunc(ctx); err != nil { - return nil, err - } - } new := v {{end}} if b.mutation.afterCreateFunc != nil { @@ -472,11 +464,7 @@ import ( } } for _, f := range postSlice { - {{if $EntSchema}} - err := f(ctx, new, index, entBuilder) - {{else}} - err := f(ctx, new, index) - {{end}} + err := f(ctx, new, index) if err != nil { return nil, err }