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 ¶
- Variables
- type AdultAge
- type Endomorphism
- type Lens
- type NonEmptyString
- type Option
- type PartialPerson
- type PartialPersonLenses
- type PartialPersonPrisms
- type PartialPersonRefLenses
- type Person
- type PersonLenses
- type PersonPrisms
- type PersonRefLenses
- type Prism
- type Reader
- type ReaderOption
- type Result
Constants ¶
This section is empty.
Variables ¶
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 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 ¶
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 Reader ¶
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].