gory: github.com/modocache/gory Index | Files

package gory

import "github.com/modocache/gory"

gory is a fixtures replacement with a straightforward definition syntax. It's to Go what thoughtbot/factory_girl is to Ruby.

Index

Package Files

definitions.go factory.go gory.go lazy.go sequence.go

func Build Uses

func Build(definitionName string) interface{}

Returns an instance of the struct defined using the definitionName parameter and the Define() function. If no matching definition exists, this function panics. It also panics if an attempt to set an invalid field was made from within the Define() function.

Example:

person := gory.Build("person").(*Person)
fmt.Println(person.FirstName) // "Jane"

func BuildWithParams Uses

func BuildWithParams(definitionName string, params Factory) interface{}

Returns an instance of the struct defined by definitionName, but using the specified parameters instead of the defined factory's values. If names of fields that don't exist or are not exported are specified, the function panics.

Example:

person := gory.BuildWithParams("person", Person{}, {
	"Name": "John"
}).(*Person)
fmt.Println(person.Name) // "John" instead of Factory value

func Define Uses

func Define(definitionName string, instance interface{}, builder FactoryBuilder)

Define a factory. Factories can be retrieved later by using the definitionName parameter. You cannot define two factories with identical names.

The instance parameter must be a struct literal of the type you'd like the factory to return when building.

The builder parameter is a function executed when defining factories. Use the factory parameter as a map to store values for your struct fields. If this is nil, no fields on the returned struct are set.

func Lazy Uses

func Lazy(callback func() interface{}) next

Used to set a lazily evaluated value on a Factory.

Example:

factory["Created"] = gory.Lazy(func() interface{} {
    return time.Now()
})

In the above example, time.Now() is not evaluated until the Factory is built, using the Build() function.

func Sequence Uses

func Sequence(sequencer Sequencer) next

Used to set an sequenced value on a Factory.

Example:

factory["Email"] = gory.Sequence(func(n int) interface{} {
    return fmt.Sprintf("john-doe-%d@example.com", n)
})

Each time Build() is called, the sequenced value will use an incremented value for n.

type Factory Uses

type Factory map[string]interface{}

A Factory maps field names to values.

type FactoryBuilder Uses

type FactoryBuilder func(factory Factory)

A function executed when defining factories. Use the factory parameter as a map to store values for your struct fields.

Example:

gory.Define("person", Person{}, func(factory gory.Factory) {
	factory["FirstName"] = "Jane"
	factory["LastName"] = "Doe"
	factory["Admin"] = false
})

Later, when you build the object, the FirstName, LastName, and Admin fields will be set to the parameters you specified. If you attempt to set fields that don't exist or are not exported, the Build() function will panic once called.

type Sequencer Uses

type Sequencer func(n int) interface{}

A function that returns a value to be set on a Factory. n is incremented each time the Factory is built.

var IntSequencer Sequencer = func(n int) interface{} {
    return n
}

A Sequencer that simply returns the int value of n. In other words, the first time the factory is built, it will return 0, then 1, then 2, and so on.

Example:

factory["NumberOfChildren"] = gory.Sequence(gory.IntSequencer)

Package gory imports 3 packages (graph). Updated 2018-03-27. Refresh now. Tools for package owners.