options

package
v0.11.0 Latest Latest
Warning

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

Go to latest
Published: May 8, 2026 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

Package options provides a set of options for the client.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Apply

func Apply[T any](v T, opts ...Option[T])

Apply applies a slice of functions to a value.

Example
package main

import (
	"fmt"

	"github.com/foomo/go/options"
)

type Server struct {
	Name string
}

func WithName(p string) options.Option[*Server] {
	return func(s *Server) {
		s.Name = p
	}
}

func main() {
	s := &Server{}

	options.Apply(
		s,
		WithName("localhost"),
	)

	fmt.Printf("%s\n", s.Name)

}
Output:
localhost

func ApplyE

func ApplyE[T any](v T, opts ...OptionE[T]) error

ApplyE applies a slice of error-returning functions to a value and stops on the first encountered error.

Example
package main

import (
	"errors"
	"fmt"

	"github.com/foomo/go/options"
)

type Server struct {
	Name string
}

func WithNameE(p string) options.OptionE[*Server] {
	return func(s *Server) error {
		if len(p) == 0 {
			return errors.New("invalid name")
		}

		s.Name = p

		return nil
	}
}

func main() {
	s := &Server{}

	err := options.ApplyE(
		s,
		WithNameE("localhost"),
	)

	fmt.Println(err)
	fmt.Println(s.Name)

}
Output:
<nil>
localhost

func Build

func Build[T any](v T, builders ...interface{ List() []Option[T] })

Build applies a slice of functions to a value.

func BuildE

func BuildE[T any](v T, builders ...interface{ List() []OptionE[T] }) error

BuildE applies a slice of error-returning functions to a value and stops on the first encountered error.

Types

type Builder

type Builder[T any] struct {
	Opts []Option[T]
}

Builder is a generic option builder that collects functional options.

Example
package main

import (
	"fmt"

	"github.com/foomo/go/options"
)

type MyOptions struct {
	Name string
}

type MyOptionsBuilder struct {
	options.Builder[*MyOptions]
}

func NewMyOptionsBuilder() *MyOptionsBuilder {
	return &MyOptionsBuilder{
		options.Builder[*MyOptions]{},
	}
}

func (b *MyOptionsBuilder) WithName(name string) *MyOptionsBuilder {
	b.Opts = append(b.Opts, func(o *MyOptions) {
		o.Name = name
	})

	return b
}

func main() {
	b := NewMyOptionsBuilder()
	b.WithName("example")

	o := MyOptions{}
	options.Build(&o, b)

	fmt.Println(o)

}
Output:
{example}

func (*Builder[T]) List

func (b *Builder[T]) List() []Option[T]

List returns the collected options.

type BuilderE

type BuilderE[T any] struct {
	Opts []OptionE[T]
}

BuilderE is a generic option builder that collects functional error-returning options.

Example
package main

import (
	"fmt"

	"github.com/foomo/go/options"
)

type MyOptions struct {
	Name string
}

type MyOptionsBuilderE struct {
	options.BuilderE[*MyOptions]
}

func MyBuilderE() *MyOptionsBuilderE {
	return &MyOptionsBuilderE{
		options.BuilderE[*MyOptions]{},
	}
}

func (b *MyOptionsBuilderE) WithName(name string) *MyOptionsBuilderE {
	b.Opts = append(b.Opts, func(o *MyOptions) error {
		o.Name = name
		return nil
	})

	return b
}

func main() {
	b := MyBuilderE()
	b.WithName("example")

	o := MyOptions{}
	if err := options.BuildE(&o, b); err != nil {
		panic(err)
	}

	fmt.Println(o)

}
Output:
{example}

func (*BuilderE[T]) List

func (b *BuilderE[T]) List() []OptionE[T]

List returns the collected options.

type Option

type Option[T any] func(T)

Option is a functional option.

type OptionE

type OptionE[T any] func(T) error

OptionE is a functional error-returning option.

Jump to

Keyboard shortcuts

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