Documentation
¶
Overview ¶
Package seedr is inspired by Ruby's factory_girl. It's a fixtures replacement that allows to define factories and build/store objects in expressive and flexible way.
Index ¶
- Constants
- type ConfigFunc
- type Factory
- type FactoryConfig
- type Func
- type Generator
- func Auto() Generator
- func ChainInt(init int, f func(prev int) int) Generator
- func CreateRelated(traitName string) Generator
- func CreateRelatedBatch(traitName string, n int) Generator
- func CreateRelatedCustom(traitName string, override Trait) Generator
- func CreateRelatedCustomBatch(traitName string, n int, override Trait) Generator
- func DummyText(limit int) Generator
- func Loop(slice interface{}) Generator
- func PickRandom(slice interface{}) Generator
- func SequenceFunc(f func(int) interface{}, startFrom ...int) Generator
- func SequenceInt(startFrom ...int) Generator
- func SequenceString(fmtStr string, startFrom ...int) Generator
- type MapFieldFunc
- type Relation
- type Relations
- type Seedr
- func (sdr *Seedr) Add(factoryName string, f Factory) *Seedr
- func (sdr *Seedr) Build(traitName string) TraitInstance
- func (sdr *Seedr) BuildBatch(traitName string, n int) *TraitInstances
- func (sdr *Seedr) BuildCustom(traitName string, override Trait) TraitInstance
- func (sdr *Seedr) BuildCustomBatch(traitName string, n int, override Trait) *TraitInstances
- func (sdr *Seedr) Create(traitName string) TraitInstance
- func (sdr *Seedr) CreateBatch(traitName string, n int) *TraitInstances
- func (sdr *Seedr) CreateCustom(traitName string, override Trait) TraitInstance
- func (sdr *Seedr) CreateCustomBatch(traitName string, n int, override Trait) *TraitInstances
- type Trait
- type TraitInstance
- func (ti TraitInstance) CreateRelated(relation, traitName string) TraitInstance
- func (ti TraitInstance) CreateRelatedBatch(relation, traitName string, n int) TraitInstance
- func (ti TraitInstance) CreateRelatedCustom(relation, traitName string, overrides Trait) TraitInstance
- func (ti TraitInstance) CreateRelatedCustomBatch(relation, traitName string, n int, overrides Trait) TraitInstance
- func (ti TraitInstance) Related(relationName string) *TraitInstances
- func (ti TraitInstance) Scan(v interface{}) TraitInstance
- func (ti TraitInstance) ScanRelated(relationName string, v interface{}) TraitInstance
- type TraitInstances
- type Traits
Constants ¶
const ( // Include is a special key for Trait definition // that defines on what traits current trait is based on. // Value must be a string with names of traits (of current factory only) // separated by whitespace. Include = "SEEDR_INCLUDE_TRAITS" )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ConfigFunc ¶
type ConfigFunc func(*Seedr)
ConfigFunc is used to configure Seedr
func SetBuildDriver ¶
func SetBuildDriver(b driver.Driver) ConfigFunc
SetBuildDriver sets driver for `Build*` methods
func SetCreateDriver ¶
func SetCreateDriver(b driver.Driver) ConfigFunc
SetCreateDriver sets driver for `Create*` methods
func SetFieldMapper ¶
func SetFieldMapper(f MapFieldFunc) ConfigFunc
SetFieldMapper sets MapFieldFunc func for Seedr
type Factory ¶
type Factory struct { FactoryConfig Relations Traits }
Factory is a container for factory definition
type FactoryConfig ¶
type FactoryConfig struct { // Entity is a name of database schema/table, index, etc. // Entity may not be required, depends on driver. // If not given, factory name is used. Entity string // PrimaryKey is a name of primary key field. // It is required if you define any relations. // It also may be required by your Driver. PrimaryKey string // contains filtered or unexported fields }
FactoryConfig ...
type Generator ¶
type Generator interface { // Next must return any value of supported type: // - any numeric type // - time.Time // - sql.Scanner // - nil value (interface{}(nil)) Next() interface{} }
Generator provides a way to generate dynamic field values for every instance created by seedr.
func Auto ¶
func Auto() Generator
Auto is a special kind of Generator. "Auto" field must be initialized by Driver on Create. May be useful for fields like "id", "created_at", etc.
func ChainInt ¶
ChainInt creates Generator that yields result of `f` with result of previous call to `f` as parameter (prev). For initial call `init` value is used.
func CreateRelated ¶
CreateRelated is a special Generator that will create related trait
func CreateRelatedBatch ¶
CreateRelatedBatch is a special Generator that will create a batch of related traits
func CreateRelatedCustom ¶
CreateRelatedCustom is a special Generator that will create related trait with additional changes.
func CreateRelatedCustomBatch ¶
CreateRelatedCustomBatch is a special Generator that will create a batch of related traits with additional changes.
func DummyText ¶
DummyText returns meaningless text up to `limit` bytes Limit 0 means 'no limit' (returns full length of hardcoded "Lorem Ipsum ....")
func Loop ¶
func Loop(slice interface{}) Generator
Loop returns values from given slice sequentially. When last element is reached, it starts from beginning.
func PickRandom ¶
func PickRandom(slice interface{}) Generator
PickRandom picks value by random index from given slice/array/string.
func SequenceFunc ¶
SequenceFunc creates generator from given func. On n'th .Next call it calls given func with startFrom + n argument. By default startFrom == 1
func SequenceInt ¶
SequenceInt generates sequence of ints starting from `startFrom` (1 by default)
func SequenceString ¶
SequenceString generates strings by given template `fmtStr`. Example: `SequenceString("MyString-%d")` => "MyString-1", "MyString-2",...
type MapFieldFunc ¶
type MapFieldFunc func(reflect.StructField) (traitFieldName string, err error)
MapFieldFunc returns name of Trait's field based on StructField
func NoopFieldMapper ¶
func NoopFieldMapper() MapFieldFunc
NoopFieldMapper returns field's `Name` without changes
func RegexpTagFieldMapper ¶
func RegexpTagFieldMapper(regex string, fallback MapFieldFunc) MapFieldFunc
RegexpTagFieldMapper applies regex to whole struct tag string and uses first match as result. If it fails fallback mapper is used.
func SnakeFieldMapper ¶
func SnakeFieldMapper() MapFieldFunc
SnakeFieldMapper converts field name to snake case; acronyms are converted to lower-case and preceded by an underscore. Note that multiple consequent acronyms will be considered a single acronym (e.g. ACDCTime will become acdc_time, not ac_dc_time)
func TagFieldMapper ¶
func TagFieldMapper(tag string, fallback MapFieldFunc) MapFieldFunc
TagFieldMapper looks for specific struct field tag or uses fallback mapper if tag is not found.
type Relation ¶
type Relation struct {
// contains filtered or unexported fields
}
Relation defines Factory relation. Can be defined using one of:
- BelongsTo
- HasMany
- HasManyThrough
func BelongsTo ¶
BelongsTo defines "belogs to" relation. By default joinField is eql to relation name (key of this relation in Relations map of Factory)
func HasManyThrough ¶
HasManyThrough defines "M2M" relation through joinFactory, where lfield is a foreign key on this factory, and rfield is a foreignKey on related factory. By default relatedFactory is eql to relation name (key of this relation in Relations map of Factory)
type Seedr ¶
type Seedr struct {
// contains filtered or unexported fields
}
Seedr is a collection of factories.
func New ¶
func New(name string, config ...ConfigFunc) *Seedr
New creates Seedr instance with NoopFieldMapper and NoopDriver for create and build methods by default.
func (*Seedr) Build ¶
func (sdr *Seedr) Build(traitName string) TraitInstance
Build builds trait. It uses "build" driver (see SetBuildDriver)
func (*Seedr) BuildBatch ¶
func (sdr *Seedr) BuildBatch(traitName string, n int) *TraitInstances
BuildBatch builds n trait instances. It uses "build" driver (see SetBuildDriver)
func (*Seedr) BuildCustom ¶
func (sdr *Seedr) BuildCustom(traitName string, override Trait) TraitInstance
BuildCustom builds trait with additional changes. It uses "build" driver (see SetBuildDriver)
func (*Seedr) BuildCustomBatch ¶
func (sdr *Seedr) BuildCustomBatch(traitName string, n int, override Trait) *TraitInstances
BuildCustomBatch builds n trait instances with additional changes. It uses "build" driver (see SetBuildDriver)
func (*Seedr) Create ¶
func (sdr *Seedr) Create(traitName string) TraitInstance
Create creates an instance of trait It uses "create" driver (see SetCreateDriver)
func (*Seedr) CreateBatch ¶
func (sdr *Seedr) CreateBatch(traitName string, n int) *TraitInstances
CreateBatch creates n instances of trait It uses "create" driver (see SetCreateDriver)
func (*Seedr) CreateCustom ¶
func (sdr *Seedr) CreateCustom(traitName string, override Trait) TraitInstance
CreateCustom overrides values of trait definition and creates resulting trait. It uses "create" driver (see SetCreateDriver)
func (*Seedr) CreateCustomBatch ¶
func (sdr *Seedr) CreateCustomBatch(traitName string, n int, override Trait) *TraitInstances
CreateCustomBatch overrides values of trait definition and creates n instances of resulting trait. It uses "create" driver (see SetCreateDriver)
type Trait ¶
type Trait map[string]interface{}
Trait contains factory fields definitions. Key is a field name and value is any of supported:
- any numeric type
- time.Time
- sql.Scanner
- Generator
- nil (interface{}(nil))
Special key is `Include` constant that defines on what traits this Trait is based on. Every field of trait is a name of respective field in database.
type TraitInstance ¶
type TraitInstance struct {
// contains filtered or unexported fields
}
TraitInstance is a created trait instance
func (TraitInstance) CreateRelated ¶
func (ti TraitInstance) CreateRelated(relation, traitName string) TraitInstance
CreateRelated creates single related instance. Works for FK (child only) and M2M. It returns original TraitInstance, not one that was created. Fetch created one using Related method.
func (TraitInstance) CreateRelatedBatch ¶
func (ti TraitInstance) CreateRelatedBatch(relation, traitName string, n int) TraitInstance
CreateRelatedBatch creates n related instances. Works for FK (child only) and M2M It returns original TraitInstance, not one that was created. Fetch created one using Related method.
func (TraitInstance) CreateRelatedCustom ¶
func (ti TraitInstance) CreateRelatedCustom(relation, traitName string, overrides Trait) TraitInstance
CreateRelatedCustom creates single related instance with additional changes. Works for FK (child only) and M2M It returns original TraitInstance, not one that was created. Fetch created one using Related method.
func (TraitInstance) CreateRelatedCustomBatch ¶
func (ti TraitInstance) CreateRelatedCustomBatch(relation, traitName string, n int, overrides Trait) TraitInstance
CreateRelatedCustomBatch creates n related instances with additional changes. Works for FK (child only) and M2M It returns original TraitInstance, not one that was created. Fetch created one using Related method.
func (TraitInstance) Related ¶
func (ti TraitInstance) Related(relationName string) *TraitInstances
Related returns related TraitInstances (that was created by CreateRelated*)
func (TraitInstance) Scan ¶
func (ti TraitInstance) Scan(v interface{}) TraitInstance
Scan initializes given struct instance `v` by TraitInstance's values. `v` must be a pointer to struct.
func (TraitInstance) ScanRelated ¶
func (ti TraitInstance) ScanRelated(relationName string, v interface{}) TraitInstance
ScanRelated scans related TraitInstance(s) into v and returns this TraitInstance.
type TraitInstances ¶
type TraitInstances struct {
// contains filtered or unexported fields
}
TraitInstances is a collection of created trait instances
func (*TraitInstances) Index ¶
func (ti *TraitInstances) Index(index int) TraitInstance
Index returns TraitInstance by index
func (*TraitInstances) Len ¶
func (ti *TraitInstances) Len() int
Len returns total count of trait instances in this collection
func (*TraitInstances) Scan ¶
func (ti *TraitInstances) Scan(dest interface{}) (ret *TraitInstances)
Scan initializes given list of struct instances `dest` by TraitInstance's values. `v` must be a pointer to slice of structs. But, if there is only one instance, dest can be pointer to struct.
func (*TraitInstances) ScanRelated ¶
func (ti *TraitInstances) ScanRelated(relationName string, dest interface{})
ScanRelated is a shortcut for #Index(0).ScanRelated() It panics if there is not exactly one instance in this TraitInstances.
Source Files
¶
Directories
¶
Path | Synopsis |
---|---|
sql/mysql
Package mysql is a MySQL driver for Seedr.
|
Package mysql is a MySQL driver for Seedr. |
sql/mysql/internal/tests/models
Package test_models is an example of basic models setup.
|
Package test_models is an example of basic models setup. |