builder

package
v2.1.11 Latest Latest
Warning

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

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

Documentation

Overview

Package builder provides a generic Builder pattern interface for constructing complex objects with validation.

The Builder pattern is useful when:

  • Object construction requires multiple steps
  • Construction may fail with validation errors
  • You want to separate construction logic from the object itself

Example usage:

type PersonBuilder struct {
    name string
    age  int
}

func (b PersonBuilder) Build() result.Result[Person] {
    if b.name == "" {
        return result.Error[Person](errors.New("name is required"))
    }
    if b.age < 0 {
        return result.Error[Person](errors.New("age must be non-negative"))
    }
    return result.Of(Person{Name: b.name, Age: b.age})
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Builder

type Builder[T any] interface {
	// Build constructs and validates an object of type T.
	//
	// Returns:
	//   - Result[T]: A Result containing either the successfully built object
	//     or an error if validation or construction fails.
	Build() Result[T]
}

Builder is a generic interface for the Builder pattern that constructs objects of type T with validation.

The Build method returns a Result[T] which can be either:

  • Success: containing the constructed object of type T
  • Error: containing an error if validation or construction fails

This allows builders to perform validation and return meaningful errors during the construction process, making it explicit that object creation may fail.

Type Parameters:

  • T: The type of object being built

Example:

type ConfigBuilder struct {
    host string
    port int
}

func (b ConfigBuilder) Build() result.Result[Config] {
    if b.host == "" {
        return result.Error[Config](errors.New("host is required"))
    }
    if b.port <= 0 || b.port > 65535 {
        return result.Error[Config](errors.New("invalid port"))
    }
    return result.Of(Config{Host: b.host, Port: b.port})
}

type Option

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

Option represents an optional value that may or may not be present.

type Prism

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

Prism is an optic that focuses on a case of a sum type. It provides a way to extract and construct values of a specific variant.

func BuilderPrism

func BuilderPrism[T any, B Builder[T]](creator func(T) B) Prism[B, T]

BuilderPrism creates a Prism that converts between a builder and its built type.

A Prism is an optic that focuses on a case of a sum type, providing bidirectional conversion with the possibility of failure. This function creates a prism that:

  • Extracts: Attempts to build the object from the builder (may fail)
  • Constructs: Creates a builder from a valid object (always succeeds)

The extraction direction (builder -> object) uses the Build method and converts the Result to an Option, where errors become None. The construction direction (object -> builder) uses the provided creator function.

Type Parameters:

  • T: The type of the object being built
  • B: The builder type that implements Builder[T]

Parameters:

  • creator: A function that creates a builder from a valid object of type T. This function should initialize the builder with all fields from the object.

Returns:

  • Prism[B, T]: A prism that can convert between the builder and the built type.

Example:

type Person struct {
    Name string
    Age  int
}

type PersonBuilder struct {
    name string
    age  int
}

func (b PersonBuilder) Build() result.Result[Person] {
    if b.name == "" {
        return result.Error[Person](errors.New("name required"))
    }
    return result.Of(Person{Name: b.name, Age: b.age})
}

func NewPersonBuilder(p Person) PersonBuilder {
    return PersonBuilder{name: p.Name, age: p.Age}
}

// Create a prism for PersonBuilder
prism := BuilderPrism(NewPersonBuilder)

// Use the prism to extract a Person from a valid builder
builder := PersonBuilder{name: "Alice", age: 30}
person := prism.GetOption(builder) // Some(Person{Name: "Alice", Age: 30})

// Use the prism to create a builder from a Person
p := Person{Name: "Bob", Age: 25}
b := prism.ReverseGet(p) // PersonBuilder{name: "Bob", age: 25}

type Result

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

Result represents a computation that may fail with an error. It's an alias for Either[error, T].

Jump to

Keyboard shortcuts

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