Documentation ¶
Index ¶
- Variables
- func Build(f *Factory, opts ...factoryOption) *buildTo
- func BuildSlice(f *Factory, count int, opts ...factoryOption) *buildSliceTo
- func Create(f *Factory, opts ...factoryOption) *createTo
- func CreateSlice(f *Factory, count int, opts ...factoryOption) *createSliceTo
- func Delete(f *Factory, instance interface{}) error
- func SetDB(db *sql.DB)
- func WithField(name string, value interface{}) factoryOption
- func WithTraits(traits ...string) factoryOption
- type AssociationFieldValue
- type Callback
- type DynamicFieldValue
- type Factory
- type SequenceFieldValue
Constants ¶
This section is empty.
Variables ¶
var DebugMode = false
DebugMode is a flag controlling whether debug information is outputted to the os.Stdout
Functions ¶
func Build ¶
func Build(f *Factory, opts ...factoryOption) *buildTo
Build creates an instance from a factory model but won't store it into database.
model := &Model{}
err := Build(FactoryModel,
WithTrait("Chinese"), WithField("Name", "new name"), WithField("ID", 123),
).To(model)
func BuildSlice ¶
BuildSlice creates a slice instance from a factory model but won't store them into database.
modelSlice := []*Model{}
err := Build(FactoryModel,
WithTrait("Chinese"), WithField("Name", "new name"),
).To(&modelSlice)
func Create ¶
func Create(f *Factory, opts ...factoryOption) *createTo
Create creates an instance from a factory model and stores it into database.
model := &Model{}
err := Create(FactoryModel,
WithTrait("Chinese"), WithField("Name", "new name"), WithField("ID", 123),
).To(model)
func CreateSlice ¶
CreateSlice creates a slice of instance from a factory model and stores them into database.
modelSlice := []*Model{}
err := CreateSlice(FactoryModel,
WithTrait("Chinese"), WithField("Name", "new name"),
).To(&modelSlice)
func Delete ¶
Delete deletes an instance of a factory model from database. Example: err := Delete(FactoryModel, Model{})
func WithField ¶
func WithField(name string, value interface{}) factoryOption
WithField sets the value of a specific field. This way has the highest priority to set the field value.
func WithTraits ¶
func WithTraits(traits ...string) factoryOption
WithTraits defines which traits the new instance will use. It can take multiple traits. These traits will be execute one by one. So the later one may override the before ones. For example: The trait "trait1" set Field1 as "value1", and at the same time, trait "trait2" set Field1 as "value2". The WithTraits("trait1", "trait2") will finally set Field1 as "value2".
Types ¶
type AssociationFieldValue ¶
type AssociationFieldValue struct { ReferenceField string AssociationReferenceField string OriginalFactory *Factory Factory *Factory }
AssociationFieldValue represents a struct which contains data to generate value of a association field.
type Callback ¶
type Callback func(model interface{}) error
Callback defines the callback function type
type DynamicFieldValue ¶
type DynamicFieldValue func(model interface{}) (interface{}, error)
DynamicFieldValue defines the value generator type of a field. It's return result will be set as the value of the field dynamicly.
type Factory ¶
type Factory struct { ModelType reflect.Type Table string FiledValues map[string]interface{} SequenceFiledValues map[string]*sequenceValue DynamicFieldValues map[string]DynamicFieldValue AssociationFieldValues map[string]*AssociationFieldValue Traits map[string]*Factory AfterBuildCallbacks []Callback BeforeCreateCallbacks []Callback AfterCreateCallbacks []Callback CanHaveAssociations bool CanHaveTraits bool CanHaveCallbacks bool }
Factory represents a factory defined by some model struct
func (*Factory) AddSequenceFiledValue ¶
func (f *Factory) AddSequenceFiledValue(name string, first int64, value SequenceFieldValue)
AddSequenceFiledValue adds sequence field value to factory by field name
type SequenceFieldValue ¶
SequenceFieldValue defines the value generator type of sequence field. It's return result will be set as the value of the sequence field dynamicly.