options API

options

package

API reference for the options package.

Imports

(1)
T
type

Option

Option is a functional option for configuration.

pkg/options/options.go:4-4
type Option func(*T)
F
function

Apply

Apply applies functional options to a config struct.

Parameters

cfg
*T
opts
...Option[T]
pkg/options/options.go:12-16
func Apply[T any](cfg *T, opts ...Option[T])

{
	for _, opt := range opts {
		opt(cfg)
	}
}

Example

cfg := &Config{}
options.Apply(cfg, WithHost("localhost"))
S
struct

Builder

Builder provides a fluent interface for building config with options.

pkg/options/options.go:25-28
type Builder struct

Example

cfg := options.NewBuilder(defaultCfg).
	With(WithHost("localhost")).
	Build()

Fields

Name Type Description
cfg T
opts []Option[T]
F
function

NewBuilder

NewBuilder creates a new options builder with default config.

Parameters

defaults
T

Returns

*Builder[T]
pkg/options/options.go:31-33
func NewBuilder[T any](defaults T) *Builder[T]

{
	return &Builder[T]{cfg: defaults}
}
S
struct

ServerConfig

pkg/options/options_test.go:5-9
type ServerConfig struct

Fields

Name Type Description
Host string
Port int
Timeout int
F
function

WithHost

Parameters

h
string

Returns

func(*ServerConfig)
pkg/options/options_test.go:11-13
func WithHost(h string) func(*ServerConfig)

{
	return func(c *ServerConfig) { c.Host = h }
}
F
function

WithPort

Parameters

p
int

Returns

func(*ServerConfig)
pkg/options/options_test.go:15-17
func WithPort(p int) func(*ServerConfig)

{
	return func(c *ServerConfig) { c.Port = p }
}
F
function

WithTimeout

Parameters

t
int

Returns

func(*ServerConfig)
pkg/options/options_test.go:19-21
func WithTimeout(t int) func(*ServerConfig)

{
	return func(c *ServerConfig) { c.Timeout = t }
}
F
function

TestApply

Parameters

pkg/options/options_test.go:23-34
func TestApply(t *testing.T)

{
	cfg := &ServerConfig{Host: "localhost", Port: 8080}

	Apply(cfg, WithHost("0.0.0.0"), WithPort(9000))

	if cfg.Host != "0.0.0.0" {
		t.Errorf("Host: got %q, want %q", cfg.Host, "0.0.0.0")
	}
	if cfg.Port != 9000 {
		t.Errorf("Port: got %d, want %d", cfg.Port, 9000)
	}
}
F
function

TestBuilder

Parameters

pkg/options/options_test.go:36-53
func TestBuilder(t *testing.T)

{
	defaults := ServerConfig{Host: "localhost", Port: 8080, Timeout: 30}

	cfg := NewBuilder(defaults).
		With(WithHost("127.0.0.1")).
		With(WithTimeout(60)).
		Build()

	if cfg.Host != "127.0.0.1" {
		t.Errorf("Host: got %q, want %q", cfg.Host, "127.0.0.1")
	}
	if cfg.Port != 8080 {
		t.Errorf("Port should remain default: got %d, want %d", cfg.Port, 8080)
	}
	if cfg.Timeout != 60 {
		t.Errorf("Timeout: got %d, want %d", cfg.Timeout, 60)
	}
}
F
function

TestBuilder_Ptr

Parameters

pkg/options/options_test.go:55-68
func TestBuilder_Ptr(t *testing.T)

{
	defaults := ServerConfig{Port: 3000}

	cfg := NewBuilder(defaults).
		With(WithHost("api.example.com")).
		Ptr()

	if cfg.Host != "api.example.com" {
		t.Errorf("Host: got %q, want %q", cfg.Host, "api.example.com")
	}
	if cfg.Port != 3000 {
		t.Errorf("Port: got %d, want %d", cfg.Port, 3000)
	}
}