inference

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Aug 18, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package inference generates field values through gofakeit, seeded hierarchically from an autoseed.SeededSource, so autoseed's core stays testable without a value generator: the same separation the .NET sibling keeps between AutoSeed.Core and AutoSeed.Inference.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsNullableWrapper

func IsNullableWrapper(t reflect.Type) bool

IsNullableWrapper reports whether t is one of the database/sql "nullable primitive" wrapper types — the Go-level signal an adapter can use to mark a Field Nullable, since these types (unlike a plain string or int) can actually represent a real database NULL.

Types

type AddressRule

type AddressRule struct{}

AddressRule infers a street name or city for a string field ending in Street, StreetName, Address or City.

func (AddressRule) CanInfer

func (AddressRule) CanInfer(field autoseed.Field) bool

CanInfer matches a string field ending in Street, StreetName, Address or City, except EmailAddress: that suffix also ends in Address, but belongs to EmailRule.

func (AddressRule) Infer

func (AddressRule) Infer(field autoseed.Field, seed *autoseed.SeededSource, _ map[string]any) any

Infer generates a city name for a field ending in City, a street name otherwise.

func (AddressRule) Priority

func (AddressRule) Priority() int

Priority runs AddressRule before generic string rules.

type CorrelatedTotalRule

type CorrelatedTotalRule struct{}

CorrelatedTotalRule infers a floating-point field ending in Total or Subtotal as Price times Quantity when both were already generated on the same row, so the numbers actually add up, falling back to an independent price-range draw otherwise.

func (CorrelatedTotalRule) CanInfer

func (CorrelatedTotalRule) CanInfer(field autoseed.Field) bool

CanInfer matches a floating-point field ending in Total or Subtotal.

func (CorrelatedTotalRule) Infer

func (CorrelatedTotalRule) Infer(_ autoseed.Field, seed *autoseed.SeededSource, generated map[string]any) any

Infer generates Price times Quantity, rounded to two decimal places, from a same-row Price and Quantity when both were already generated, otherwise an independent amount in PriceRule's own range.

func (CorrelatedTotalRule) Priority

func (CorrelatedTotalRule) Priority() int

Priority runs CorrelatedTotalRule after PriceRule and QuantityRule, so a same-row Price and Quantity are already generated when Infer looks for them.

type CreatedAtRule

type CreatedAtRule struct{}

CreatedAtRule infers a creation timestamp for a time.Time field ending in CreatedAt, within the two years before a fixed reference time.

func (CreatedAtRule) CanInfer

func (CreatedAtRule) CanInfer(field autoseed.Field) bool

CanInfer matches a time.Time field ending in CreatedAt.

func (CreatedAtRule) Infer

func (CreatedAtRule) Infer(_ autoseed.Field, seed *autoseed.SeededSource, _ map[string]any) any

Infer generates a timestamp within the two years before a fixed reference time.

func (CreatedAtRule) Priority

func (CreatedAtRule) Priority() int

Priority runs CreatedAtRule before UpdatedAtRule, which reads its output back.

type DocumentRule

type DocumentRule struct{}

DocumentRule infers a Brazilian CPF or CNPJ, digits only, with a valid check digit, for a string field named Cpf or Cnpj.

func (DocumentRule) CanInfer

func (DocumentRule) CanInfer(field autoseed.Field) bool

CanInfer matches a string field ending in Cpf or Cnpj.

func (DocumentRule) Infer

func (DocumentRule) Infer(field autoseed.Field, seed *autoseed.SeededSource, _ map[string]any) any

Infer generates digits-only, with a valid check digit: 11 digits for a Cpf field, 14 for a Cnpj field.

func (DocumentRule) Priority

func (DocumentRule) Priority() int

Priority runs DocumentRule before generic string rules.

type EmailRule

type EmailRule struct{}

EmailRule infers an email address for a string field ending in Email or EmailAddress. When FirstName and LastName were already generated on the same row, the address is built from them so the two agree.

func (EmailRule) CanInfer

func (EmailRule) CanInfer(field autoseed.Field) bool

CanInfer matches a string field ending in Email or EmailAddress.

func (EmailRule) Infer

func (EmailRule) Infer(_ autoseed.Field, seed *autoseed.SeededSource, generated map[string]any) any

Infer builds firstname.lastname@domain from a same-row FirstName and LastName when both were already generated, otherwise draws an independent address.

func (EmailRule) Priority

func (EmailRule) Priority() int

Priority runs EmailRule after NameRule, so a same-row FirstName and LastName are already generated when Infer looks for them.

type Generator

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

Generator produces a full row of values for an entity. Rules run in ascending Priority order, and within a tier in registration order; a rule completes across every field in the entity before the next rule runs, so a higher-priority rule can read a value a lower-priority rule already produced this row (FirstName before Email, CreatedAt before UpdatedAt), never the reverse.

func NewDefaultGenerator

func NewDefaultGenerator() *Generator

NewDefaultGenerator returns a Generator with every built-in rule.

func NewGenerator

func NewGenerator(rules ...Rule) *Generator

NewGenerator returns a Generator trying rules in ascending Priority order, with no null rate — every Nullable field always gets a generated value. Use WithNilRate to change that.

func (*Generator) GenerateRow

func (g *Generator) GenerateRow(entity autoseed.Entity, seed *autoseed.SeededSource) (map[string]any, error)

GenerateRow produces a value for every field on entity that isn't database-generated: a foreign key column (copied from its parent at the persistence stage, never from inference) or an auto-increment primary key. A primary key field that is neither — a natural, non-auto-increment column, whether it's an entity's whole key or, in a composite key, the one part not already covered by a reference — is generated like any other field, since leaving it at its Go zero value would make every row collide on it.

A field typed as one of database/sql's nullable wrappers (sql.NullString, sql.NullInt64, sql.NullBool, sql.NullFloat64, sql.NullTime, and the narrower NullInt32/NullInt16/NullByte) is matched against rules by its wrapped value's type, so a field named Email of type sql.NullString still gets EmailRule's treatment, not just generic text.

Before any rule runs, a field.Nullable field has its own chance, set by WithNilRate, of being left out of the row entirely instead of matched to a rule at all — that field's value comes back nil, which the caller must persist as a real database NULL. A field also flagged SoftDelete is never subject to this: SoftDeleteRule already owns that field's own null-vs-not split.

seed must already be scoped to the row, typically source.Entity(entity.Name).Row(index). It returns autoseed.ErrUnsupportedField, naming the entity and field, if no rule claims a field. A string value longer than field.Size is truncated before it reaches the caller, regardless of which rule produced it, so a named rule can never hand a sized column a value the database rejects.

func (*Generator) WithLocale

func (g *Generator) WithLocale(locale string) *Generator

WithLocale adds locale's own name/address/phone rules, ahead of the generic ones already registered, and returns g for chaining. An unrecognized locale, including "", leaves g unchanged — the generic rules already cover every field a locale rule would otherwise claim, so there is nothing to fall back to.

func (*Generator) WithNilRate

func (g *Generator) WithNilRate(rate float64) *Generator

WithNilRate sets the per-row probability GenerateRow leaves a Nullable field's value out entirely instead of running its matched rule, and returns g for chaining. A field the adapter also flagged SoftDelete is never subject to this: SoftDeleteRule already owns that field's own, more deliberate null-vs-not split. The roll is independent of whatever randomness a surviving field's own rule draws — surviving a rate below 1.0 never biases that rule's output toward a particular range.

type NameRule

type NameRule struct{}

NameRule infers a person's first or last name for a string field ending in FirstName or LastName.

func (NameRule) CanInfer

func (NameRule) CanInfer(field autoseed.Field) bool

CanInfer matches a string field ending in FirstName or LastName.

func (NameRule) Infer

func (NameRule) Infer(field autoseed.Field, seed *autoseed.SeededSource, _ map[string]any) any

Infer generates a first name, or a last name for a field ending in LastName.

func (NameRule) Priority

func (NameRule) Priority() int

Priority runs NameRule before EmailRule, which reads its output back.

type PhoneRule

type PhoneRule struct{}

PhoneRule infers a phone number for a string field ending in Phone or PhoneNumber.

func (PhoneRule) CanInfer

func (PhoneRule) CanInfer(field autoseed.Field) bool

CanInfer matches a string field ending in Phone or PhoneNumber.

func (PhoneRule) Infer

func (PhoneRule) Infer(_ autoseed.Field, seed *autoseed.SeededSource, _ map[string]any) any

Infer generates a phone number.

func (PhoneRule) Priority

func (PhoneRule) Priority() int

Priority runs PhoneRule before generic string rules.

type PriceRule

type PriceRule struct{}

PriceRule infers a reasonable monetary amount for a floating-point field ending in Price, Amount or Cost.

func (PriceRule) CanInfer

func (PriceRule) CanInfer(field autoseed.Field) bool

CanInfer matches a floating-point field ending in Price, Amount or Cost.

func (PriceRule) Infer

func (PriceRule) Infer(_ autoseed.Field, seed *autoseed.SeededSource, _ map[string]any) any

Infer generates an amount between 1.00 and 999.99.

func (PriceRule) Priority

func (PriceRule) Priority() int

Priority runs PriceRule before CorrelatedTotalRule, which reads its output back.

type PtBRAddressRule

type PtBRAddressRule struct{}

PtBRAddressRule infers a city or street name from a pool of real Brazilian cities and common street-name conventions, for a string field ending in Street, StreetName, Address or City.

func (PtBRAddressRule) CanInfer

func (PtBRAddressRule) CanInfer(field autoseed.Field) bool

CanInfer matches the same fields AddressRule does: a string field ending in Street, StreetName, Address or City, except EmailAddress.

func (PtBRAddressRule) Infer

func (PtBRAddressRule) Infer(field autoseed.Field, seed *autoseed.SeededSource, _ map[string]any) any

Infer picks a city for a field ending in City, or a "<street type> <street name>" pair (e.g. "Rua das Flores") otherwise.

func (PtBRAddressRule) Priority

func (PtBRAddressRule) Priority() int

Priority runs PtBRAddressRule ahead of AddressRule (Priority 0), so it claims a matching field first when the locale is registered.

type PtBRNameRule

type PtBRNameRule struct{}

PtBRNameRule infers a person's first or last name from a pool of common Brazilian names, for a string field ending in FirstName or LastName. Names are ASCII (no diacritics) so a downstream rule built from them, like EmailRule lowercasing FirstName and LastName into an address, never has to decide whether to strip an accent.

func (PtBRNameRule) CanInfer

func (PtBRNameRule) CanInfer(field autoseed.Field) bool

CanInfer matches a string field ending in FirstName or LastName, the same fields NameRule claims.

func (PtBRNameRule) Infer

func (PtBRNameRule) Infer(field autoseed.Field, seed *autoseed.SeededSource, _ map[string]any) any

Infer picks a first name, or a last name for a field ending in LastName, from ptBRFirstNames/ptBRLastNames.

func (PtBRNameRule) Priority

func (PtBRNameRule) Priority() int

Priority runs PtBRNameRule ahead of NameRule (Priority 0), so it claims a matching field first when the locale is registered.

type PtBRPhoneRule

type PtBRPhoneRule struct{}

PtBRPhoneRule infers a Brazilian mobile phone number, for a string field ending in Phone or PhoneNumber.

func (PtBRPhoneRule) CanInfer

func (PtBRPhoneRule) CanInfer(field autoseed.Field) bool

CanInfer matches a string field ending in Phone or PhoneNumber, the same fields PhoneRule claims.

func (PtBRPhoneRule) Infer

func (PtBRPhoneRule) Infer(_ autoseed.Field, seed *autoseed.SeededSource, _ map[string]any) any

Infer generates a Brazilian mobile number in "+55 (DDD) 9XXXX-XXXX" form, DDD drawn from ptBRAreaCodes.

func (PtBRPhoneRule) Priority

func (PtBRPhoneRule) Priority() int

Priority runs PtBRPhoneRule ahead of PhoneRule (Priority 0), so it claims a matching field first when the locale is registered.

type QuantityRule

type QuantityRule struct{}

QuantityRule infers a small positive count for an integer field ending in Quantity or Qty.

func (QuantityRule) CanInfer

func (QuantityRule) CanInfer(field autoseed.Field) bool

CanInfer matches an integer field ending in Quantity or Qty.

func (QuantityRule) Infer

func (QuantityRule) Infer(_ autoseed.Field, seed *autoseed.SeededSource, _ map[string]any) any

Infer generates a count between 1 and 20.

func (QuantityRule) Priority

func (QuantityRule) Priority() int

Priority runs QuantityRule before CorrelatedTotalRule, which reads its output back.

type Rule

type Rule interface {
	Priority() int
	CanInfer(field autoseed.Field) bool
	Infer(field autoseed.Field, seed *autoseed.SeededSource, generated map[string]any) any
}

Rule infers a value for one field. CanInfer is a pure predicate over the field's name and type; once it claims a field for a row, no other rule gets a turn at it, so Infer must always return something usable.

type SoftDeleteRule

type SoftDeleteRule struct{}

SoftDeleteRule infers a value for the field an adapter flagged as an entity's soft-delete column (Field.SoftDelete): nil for the large majority of rows, so a default query filter still finds them, and a timestamp for a small minority, so the deleted branch of the model has real rows to test against. Same 90/10 split the .NET sibling defaults its query-filter pass rate to.

func (SoftDeleteRule) CanInfer

func (SoftDeleteRule) CanInfer(field autoseed.Field) bool

CanInfer matches any field the adapter flagged as the soft-delete column, regardless of its name or Go type.

func (SoftDeleteRule) Infer

func (SoftDeleteRule) Infer(_ autoseed.Field, seed *autoseed.SeededSource, generated map[string]any) any

Infer returns nil for most rows. For the rest, it returns a timestamp at or after a same-row UpdatedAt, or CreatedAt if there is no UpdatedAt, otherwise an independent past timestamp. The field's own Set method (schema.Field.Set, backed by sql.Scanner for GORM's gorm.DeletedAt) turns nil into SQL NULL and a time.Time into a valid deleted-at timestamp.

func (SoftDeleteRule) Priority

func (SoftDeleteRule) Priority() int

Priority runs SoftDeleteRule after CreatedAtRule and UpdatedAtRule, so a same-row CreatedAt or UpdatedAt is already generated when Infer looks for it: a row cannot be deleted before it was last touched.

type URLRule

type URLRule struct{}

URLRule infers a URL for a string field ending in URL, URI or Website.

func (URLRule) CanInfer

func (URLRule) CanInfer(field autoseed.Field) bool

CanInfer matches a string field ending in URL, URI or Website.

func (URLRule) Infer

func (URLRule) Infer(_ autoseed.Field, seed *autoseed.SeededSource, _ map[string]any) any

Infer generates a URL.

func (URLRule) Priority

func (URLRule) Priority() int

Priority runs URLRule before generic string rules.

type UpdatedAtRule

type UpdatedAtRule struct{}

UpdatedAtRule infers an update timestamp for a time.Time field ending in UpdatedAt: at or after a same-row CreatedAt when one was generated, otherwise drawn independently from the same window CreatedAtRule uses.

func (UpdatedAtRule) CanInfer

func (UpdatedAtRule) CanInfer(field autoseed.Field) bool

CanInfer matches a time.Time field ending in UpdatedAt.

func (UpdatedAtRule) Infer

func (UpdatedAtRule) Infer(_ autoseed.Field, seed *autoseed.SeededSource, generated map[string]any) any

Infer generates a timestamp at or after a same-row CreatedAt when one was already generated, otherwise independently within the same window CreatedAtRule uses.

func (UpdatedAtRule) Priority

func (UpdatedAtRule) Priority() int

Priority runs UpdatedAtRule after CreatedAtRule, so a same-row CreatedAt is already generated when Infer looks for it.

Jump to

Keyboard shortcuts

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