chain

package
v0.14.2 Latest Latest
Warning

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

Go to latest
Published: Oct 26, 2025 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

Package chain. chain provides fluent method chaining for Result and Option types. This enables a more readable, pipeline-style programming pattern similar to Rust's method chaining.

Benefits:

  • Fluent API: Chain multiple operations in a readable sequence
  • Type safety: Compiler tracks type transformations through the chain
  • No nesting: Avoids deep nesting of Map/AndThen calls
  • Better readability: Operations read left-to-right like a pipeline

Example - Traditional vs Chained:

// Traditional nested style
result := result.AndThen(
    result.Map(
        findUser(123),
        func(u User) string { return u.Name },
    ),
    func(name string) Result[Profile] { return findProfile(name) },
)

// Fluent chained style
result := chain.Chain(findUser(123)).
    Map(func(u User) string { return u.Name }).
    AndThen(findProfile).
    Unwrap()

Package chain. chain2 provides fluent method chaining for Result and Option types. This enables a more readable, pipeline-style programming pattern similar to Rust's method chaining.

Benefits:

  • Fluent API: Chain multiple operations in a readable sequence
  • Type safety: Compiler tracks type transformations through the chain
  • No nesting: Avoids deep nesting of Map/AndThen calls
  • Better readability: Operations read left-to-right like a pipeline

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ApplyToResult

type ApplyToResult[Out, In any] struct {
	// contains filtered or unexported fields
}

ApplyToResult [Out, In] is the first step in a Result chaining pipeline. It holds a Result[In] and provides methods that transform it to Result[Out].

func Chain

func Chain[Out, T any](r result.Result[T]) *ApplyToResult[Out, T]

Chain starts a new chaining pipeline with a Result[In]. Use this as the entry point for fluent Result operations.

Example:

 userResult := findUser(123)
	chain.Chain(userResult).
	    Map(func(u User) string { return u.Name }).
	    AndThen(validateName).
	    Unwrap()

func (*ApplyToResult[Out, In]) AndThen

func (applyToResult *ApplyToResult[Out, In]) AndThen(fn func(In) result.Result[Out]) result.Result[Out]

AndThen chains a Result-returning function. Similar to Map but for functions that can fail.

Example:

chain.Chain(validateEmail("test@example.com")).
    AndThen(func(email string) result.Result[User] {
        return createUser(email)
    }).
    Unwrap()

func (*ApplyToResult[Out, In]) Map

func (applyToResult *ApplyToResult[Out, In]) Map(fn func(In) Out) result.Result[Out]

Map transforms the value inside the Result using fn. Returns a new ApplyToResult that can continue the chain.

Example:

chain.Chain(Ok(42)).
    Map(func(x int) string { return fmt.Sprintf("value: %d", x) }).
    Unwrap() // Result[string] with "value: 42"

func (*ApplyToResult[Out, In]) MapError

func (applyToResult *ApplyToResult[Out, In]) MapError(fn func(error) error) *ApplyToResult[Out, In]

MapError transforms the error if the Result is in error state.

Example:

chain.Chain(dbQueryResult).
    MapError(func(err error) error {
        return fmt.Errorf("database error: %w", err)
    }).
    Unwrap()

func (*ApplyToResult[Out, In]) OrElse

func (applyToResult *ApplyToResult[Out, In]) OrElse(fallback Out) Out

OrElse terminates the chain and returns the value or fallback.

func (*ApplyToResult[Out, In]) OrElseGet

func (applyToResult *ApplyToResult[Out, In]) OrElseGet(fn func(error) Out) Out

OrElseGet terminates the chain and returns the value or computed fallback.

func (*ApplyToResult[Out, In]) Unwrap

func (applyToResult *ApplyToResult[Out, In]) Unwrap() result.Result[Out]

Unwrap terminates the chain and returns the final Result. This is usually the last call in a chain.

type ApplyToResult2

type ApplyToResult2[Out1, Out2, In any] struct {
	// contains filtered or unexported fields
}

ApplyToResult2 [Out1, Out2, In] represents a 2-step transformation pipeline.

func Chain2

func Chain2[Out2, Out1, T any](result result.Result[T]) *ApplyToResult2[Out1, Out2, T]

Chain2 starts a chain that expects exactly 2 transformations. Useful when you know the exact number of steps for type clarity.

func (ApplyToResult2[Out1, Out2, T]) AndThen

func (applyToResult2 ApplyToResult2[Out1, Out2, T]) AndThen(fn func(T) result.Result[Out1]) *ApplyToResult[Out2, Out1]

func (ApplyToResult2[Out1, Out2, T]) Map

func (applyToResult2 ApplyToResult2[Out1, Out2, T]) Map(fn func(T) Out1) *ApplyToResult[Out2, Out1]

Jump to

Keyboard shortcuts

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