factory

package
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 5, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DefaultFaker

type DefaultFaker struct {
	// contains filtered or unexported fields
}

DefaultFaker implements the Faker interface using a seeded random source.

func NewFaker

func NewFaker(seed int64) *DefaultFaker

NewFaker creates a new DefaultFaker with the given seed for reproducibility.

func NewFakerWithRand

func NewFakerWithRand(rng *mathrand.Rand) *DefaultFaker

NewFakerWithRand creates a new DefaultFaker using the provided *math/rand.Rand.

func (*DefaultFaker) Address

func (f *DefaultFaker) Address() string

func (*DefaultFaker) Bool

func (f *DefaultFaker) Bool() bool

func (*DefaultFaker) City

func (f *DefaultFaker) City() string

func (*DefaultFaker) Country

func (f *DefaultFaker) Country() string

func (*DefaultFaker) Date

func (f *DefaultFaker) Date() time.Time

func (*DefaultFaker) DateBetween

func (f *DefaultFaker) DateBetween(start, end time.Time) time.Time

func (*DefaultFaker) Email

func (f *DefaultFaker) Email() string

func (*DefaultFaker) FirstName

func (f *DefaultFaker) FirstName() string

func (*DefaultFaker) Float64Between

func (f *DefaultFaker) Float64Between(min, max float64) float64

func (*DefaultFaker) IntBetween

func (f *DefaultFaker) IntBetween(min, max int) int

func (*DefaultFaker) LastName

func (f *DefaultFaker) LastName() string

func (*DefaultFaker) Name

func (f *DefaultFaker) Name() string

func (*DefaultFaker) Paragraph

func (f *DefaultFaker) Paragraph() string

func (*DefaultFaker) Phone

func (f *DefaultFaker) Phone() string

func (*DefaultFaker) Pick

func (f *DefaultFaker) Pick(items []string) string

func (*DefaultFaker) Sentence

func (f *DefaultFaker) Sentence() string

func (*DefaultFaker) UUID

func (f *DefaultFaker) UUID() string

UUID generates a v4 UUID using crypto/rand for proper randomness.

func (*DefaultFaker) Word

func (f *DefaultFaker) Word() string

type Factory

type Factory[T any] struct {
	// contains filtered or unexported fields
}

Factory is a generic builder for generating model instances with fake data. It supports a default definition, named states that override fields, and batch creation via MakeMany.

func NewFactory

func NewFactory[T any](definition func(faker Faker) T) *Factory[T]

NewFactory creates a new Factory with the given definition function. A default Faker seeded from the current time is used.

func (*Factory[T]) Make

func (f *Factory[T]) Make() T

Make creates a single instance using the definition, then applies any active states in the order they were added.

func (*Factory[T]) MakeMany

func (f *Factory[T]) MakeMany(count int) []T

MakeMany creates count instances. Each instance is independently generated through Make.

func (*Factory[T]) State

func (f *Factory[T]) State(name string, fn func(faker Faker, base T) T) *Factory[T]

State registers a named state modifier on the factory. The modifier receives the Faker and the base instance produced by the definition, and returns a modified instance. State returns the same factory for chaining.

func (*Factory[T]) WithFaker

func (f *Factory[T]) WithFaker(faker Faker) *Factory[T]

WithFaker returns a copy of the factory that uses the provided Faker.

func (*Factory[T]) WithState

func (f *Factory[T]) WithState(name string) *Factory[T]

WithState returns a new Factory copy that will apply the named state when Make or MakeMany is called. The original factory is not modified.

type Faker

type Faker interface {
	// Name returns a full name (first + last).
	Name() string
	// FirstName returns a random first name.
	FirstName() string
	// LastName returns a random last name.
	LastName() string
	// Email returns a random email address.
	Email() string
	// Phone returns a random phone number string.
	Phone() string
	// Address returns a random street address.
	Address() string
	// City returns a random city name.
	City() string
	// Country returns a random country name.
	Country() string
	// UUID returns a random v4 UUID string.
	UUID() string
	// Paragraph returns a random paragraph of text.
	Paragraph() string
	// Sentence returns a random sentence.
	Sentence() string
	// Word returns a random word.
	Word() string
	// IntBetween returns a random integer in [min, max].
	IntBetween(min, max int) int
	// Float64Between returns a random float64 in [min, max).
	Float64Between(min, max float64) float64
	// Bool returns a random boolean.
	Bool() bool
	// Date returns a random date.
	Date() time.Time
	// DateBetween returns a random date between start and end.
	DateBetween(start, end time.Time) time.Time
	// Pick returns a random element from the given slice.
	Pick(items []string) string
}

Faker defines the contract for generating fake data values. Implementations provide realistic-looking data for use in factories and seeders.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL