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 codec-based validation and encoding/decoding for Person objects using fp-go's optics and validation framework.
This file extends the builder pattern with codec functionality, enabling:
- Bidirectional transformation between PartialPerson and Person
- Validation with detailed error reporting
- Type-safe encoding and decoding operations
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 Encode
- type Endomorphism
- type Lens
- type NonEmptyString
- type Option
- type PartialPerson
- type PartialPersonLenses
- type PartialPersonPrisms
- type PartialPersonRefLenses
- type Person
- type PersonCodec
- type PersonLenses
- type PersonPrisms
- type PersonRefLenses
- type Prism
- type Reader
- type ReaderOption
- type Result
- type Type
- type Validate
- type Validation
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 {
// contains filtered or unexported fields
}
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 {
// contains filtered or unexported fields
}
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 {
// contains filtered or unexported fields
}
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 {
// contains filtered or unexported fields
}
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 PersonCodec ¶ added in v2.1.21
type PersonCodec = Type[*Person, Endomorphism[*PartialPerson], Endomorphism[*PartialPerson]]
PersonCodec is a codec type that handles bidirectional transformation between Person and PartialPerson using endomorphisms.
Type parameters:
- A: *Person - The validated target type
- O: Endomorphism[*PartialPerson] - The output encoding type (builder)
- I: Endomorphism[*PartialPerson] - The input decoding type (builder)
This codec enables:
- Validation: Converting a PartialPerson builder to a validated Person
- Encoding: Converting a Person back to a PartialPerson builder
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].
type Result ¶
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].
type Validation ¶ added in v2.1.21
type Validation[A any] = validation.Validation[A]