builder

package
v2.1.20 Latest Latest
Warning

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

Go to latest
Published: Jan 22, 2026 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Overview

Package builder demonstrates a functional builder pattern using fp-go. It shows how to construct and validate Person objects using lenses, prisms, and functional composition, separating the building phase (PartialPerson) from the validated result (Person).

Package builder demonstrates the builder pattern using functional programming concepts from fp-go, including validation and transformation of data structures.

Index

Constants

This section is empty.

Variables

View Source
var (

	// WithName is a builder function that sets the Name field of a PartialPerson.
	// It returns an endomorphism that can be composed with other builder operations.
	//
	// Example:
	//   builder := WithName("Alice")
	//   person := builder(&PartialPerson{})
	WithName = partialPersonLenses.Name.Set

	// WithAge is a builder function that sets the Age field of a PartialPerson.
	// It returns an endomorphism that can be composed with other builder operations.
	//
	// Example:
	//   builder := WithAge(25)
	//   person := builder(&PartialPerson{})
	WithAge = partialPersonLenses.Age.Set

	// PersonPrism is a prism that converts between a builder pattern (Endomorphism[*PartialPerson])
	// and a validated Person in both directions.
	//
	// Forward direction (buildPerson): Endomorphism[*PartialPerson] -> Option[*Person]
	//   - Applies the builder to an empty PartialPerson
	//   - Validates all fields using namePrism and agePrism
	//   - Returns Some(*Person) if all validations pass, None otherwise
	//
	// Reverse direction (buildEndomorphism): *Person -> Endomorphism[*PartialPerson]
	//   - Extracts validated fields from Person
	//   - Converts them back to raw types
	//   - Returns a builder that reconstructs the PartialPerson
	//
	// This enables bidirectional conversion with validation in the forward direction.
	PersonPrism = prism.MakePrismWithName(buildPerson(), buildEndomorphism(), "Person")
)

Functions

This section is empty.

Types

type AdultAge

type AdultAge uint

AdultAge is an unsigned integer type that represents a validated age that meets adult criteria (typically >= 18).

type Endomorphism

type Endomorphism[A any] = endomorphism.Endomorphism[A]

Endomorphism represents a function from type A to type A. It is an alias for endomorphism.Endomorphism[A].

func MakePerson

func MakePerson(name string, age int) Endomorphism[*PartialPerson]

MakePerson creates a builder (endomorphism) that sets both name and age fields on a PartialPerson. This is a convenience function that combines WithName and WithAge into a single builder operation.

Parameters:

  • name: The name to set (will be validated later when converting to Person)
  • age: The age to set (will be validated later when converting to Person)

Returns:

An endomorphism that applies both field setters to a PartialPerson

Example:

builder := MakePerson("Alice", 25)
partial := builder(&PartialPerson{})
// partial now has Name="Alice" and Age=25

type Lens

type Lens[S, A any] = lens.Lens[S, A]

type NonEmptyString

type NonEmptyString string

NonEmptyString is a string type that represents a validated non-empty string. It is used to ensure that string fields contain meaningful data.

type Option

type Option[A any] = option.Option[A]

Option represents an optional value of type A that may or may not be present. It is an alias for option.Option[A].

type PartialPerson

type PartialPerson struct {
	// Name is the person's name as a raw string, which may be empty or invalid.
	Name string

	// Age is the person's age as a raw integer, which may be negative or otherwise invalid.
	Age int
}

PartialPerson represents a person record with unvalidated fields. This type is typically used as an intermediate representation before validation is applied to create a Person instance.

The fp-go:Lens directive generates lens functions for accessing and modifying the fields of this struct in a functional way.

fp-go:Lens

type PartialPersonLenses

type PartialPersonLenses struct {
	// mandatory fields
	Name __lens.Lens[PartialPerson, string]
	Age  __lens.Lens[PartialPerson, int]
	// optional fields
	NameO __lens_option.LensO[PartialPerson, string]
	AgeO  __lens_option.LensO[PartialPerson, int]
}

PartialPersonLenses provides lenses for accessing fields of PartialPerson

func MakePartialPersonLenses

func MakePartialPersonLenses() PartialPersonLenses

MakePartialPersonLenses creates a new PartialPersonLenses with lenses for all fields

type PartialPersonPrisms

type PartialPersonPrisms struct {
	Name __prism.Prism[PartialPerson, string]
	Age  __prism.Prism[PartialPerson, int]
}

PartialPersonPrisms provides prisms for accessing fields of PartialPerson

func MakePartialPersonPrisms

func MakePartialPersonPrisms() PartialPersonPrisms

MakePartialPersonPrisms creates a new PartialPersonPrisms with prisms for all fields

type PartialPersonRefLenses

type PartialPersonRefLenses struct {
	// mandatory fields
	Name __lens.Lens[*PartialPerson, string]
	Age  __lens.Lens[*PartialPerson, int]
	// optional fields
	NameO __lens_option.LensO[*PartialPerson, string]
	AgeO  __lens_option.LensO[*PartialPerson, int]
	// prisms
	NameP __prism.Prism[*PartialPerson, string]
	AgeP  __prism.Prism[*PartialPerson, int]
}

PartialPersonRefLenses provides lenses for accessing fields of PartialPerson via a reference to PartialPerson

func MakePartialPersonRefLenses

func MakePartialPersonRefLenses() PartialPersonRefLenses

MakePartialPersonRefLenses creates a new PartialPersonRefLenses with lenses for all fields

type Person

type Person struct {
	// Name is the person's validated name, guaranteed to be non-empty.
	Name NonEmptyString

	// Age is the person's validated age, guaranteed to meet adult criteria.
	Age AdultAge
}

Person represents a person record with validated fields. All fields in this type have been validated and are guaranteed to meet specific business rules (non-empty name, adult age).

The fp-go:Lens directive generates lens functions for accessing and modifying the fields of this struct in a functional way.

fp-go:Lens

type PersonLenses

type PersonLenses struct {
	// mandatory fields
	Name __lens.Lens[Person, NonEmptyString]
	Age  __lens.Lens[Person, AdultAge]
	// optional fields
	NameO __lens_option.LensO[Person, NonEmptyString]
	AgeO  __lens_option.LensO[Person, AdultAge]
}

PersonLenses provides lenses for accessing fields of Person

func MakePersonLenses

func MakePersonLenses() PersonLenses

MakePersonLenses creates a new PersonLenses with lenses for all fields

type PersonPrisms

type PersonPrisms struct {
	Name __prism.Prism[Person, NonEmptyString]
	Age  __prism.Prism[Person, AdultAge]
}

PersonPrisms provides prisms for accessing fields of Person

func MakePersonPrisms

func MakePersonPrisms() PersonPrisms

MakePersonPrisms creates a new PersonPrisms with prisms for all fields

type PersonRefLenses

type PersonRefLenses struct {
	// mandatory fields
	Name __lens.Lens[*Person, NonEmptyString]
	Age  __lens.Lens[*Person, AdultAge]
	// optional fields
	NameO __lens_option.LensO[*Person, NonEmptyString]
	AgeO  __lens_option.LensO[*Person, AdultAge]
	// prisms
	NameP __prism.Prism[*Person, NonEmptyString]
	AgeP  __prism.Prism[*Person, AdultAge]
}

PersonRefLenses provides lenses for accessing fields of Person via a reference to Person

func MakePersonRefLenses

func MakePersonRefLenses() PersonRefLenses

MakePersonRefLenses creates a new PersonRefLenses with lenses for all fields

type Prism

type Prism[S, A any] = prism.Prism[S, A]

type Reader

type Reader[R, A any] = reader.Reader[R, A]

Reader represents a computation that depends on an environment R and produces a value of type A. It is an alias for reader.Reader[R, A].

type ReaderOption

type ReaderOption[R, A any] = readeroption.ReaderOption[R, A]

ReaderOption represents a computation that depends on an environment R and produces an optional value of type A. It is an alias for readeroption.ReaderOption[R, A].

type Result

type Result[A any] = result.Result[A]

Result represents a computation that may succeed with a value of type A or fail with an error. It is an alias for result.Result[A].

Jump to

Keyboard shortcuts

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