factory

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jul 30, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package factory builds deterministic test data without requiring an ORM.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Builder

type Builder[T any] func(sequence uint64) T

Builder creates one value for a sequence number.

type Factory

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

Factory builds values from a concurrency-safe sequence.

func New

func New[T any](builder Builder[T]) *Factory[T]

New creates a factory whose first sequence number is 1.

It panics when builder is nil because a factory without a definition is a programming error.

func NewSequence

func NewSequence[T any](start uint64, builder Builder[T]) *Factory[T]

NewSequence creates a factory with an explicit first sequence number.

func (*Factory[T]) Build

func (f *Factory[T]) Build(states ...State[T]) T

Build creates one value and applies states in order.

func (*Factory[T]) BuildN

func (f *Factory[T]) BuildN(count int, states ...State[T]) ([]T, error)

BuildN creates count values.

func (*Factory[T]) Create

func (f *Factory[T]) Create(
	ctx context.Context,
	persist Persister[T],
	states ...State[T],
) (T, error)

Create builds and persists one value.

func (*Factory[T]) CreateN

func (f *Factory[T]) CreateN(
	ctx context.Context,
	count int,
	persist Persister[T],
	states ...State[T],
) ([]T, error)

CreateN builds and persists count values, stopping at the first error.

The returned slice contains only values persisted successfully.

type Persister

type Persister[T any] func(context.Context, T) error

Persister stores a built value.

type State

type State[T any] func(*T)

State customizes a value after its defaults are built.

Jump to

Keyboard shortcuts

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