options

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Jul 13, 2026 License: MIT Imports: 2 Imported by: 3

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Apply

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

Apply applies functional options to a config struct.

Example:

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

func Merge added in v1.0.0

func Merge[T any](cfgs ...T) T

Merge combines multiple configurations of the same type. Later values always overwrite earlier ones (LastWins semantics). For SkipZero semantics, use MergeConfig from options_ext.go.

Example:

cfg := options.Merge(defaultCfg, userCfg)

func MergeConfig added in v1.0.0

func MergeConfig[T any](semantics MergeWithSemantics, cfgs ...T) T

MergeConfig combines configs with explicit merge semantics.

func MustValidate added in v1.0.0

func MustValidate[T any](cfg T)

MustValidate panics if the struct contains zero-valued required fields. Required fields are marked with `required:"true"` tag.

Types

type Builder

type Builder[T any] struct {
	// contains filtered or unexported fields
}

Builder provides a fluent interface for building config with options.

Example:

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

func NewBuilder

func NewBuilder[T any](defaults T) *Builder[T]

NewBuilder creates a new options builder with default config.

func (*Builder[T]) Build

func (b *Builder[T]) Build() T

Build applies all options and returns the final config.

func (*Builder[T]) Ptr

func (b *Builder[T]) Ptr() *T

Ptr returns a pointer to the built config.

func (*Builder[T]) With

func (b *Builder[T]) With(opt Option[T]) *Builder[T]

With adds an option to the builder.

Returns:

The builder instance for chaining.

type MergeWithSemantics added in v1.0.0

type MergeWithSemantics int

MergeWithSemantics controls how Merge handles zero values.

const (
	// LastWins means the last config always overwrites, even with zero values.
	LastWins MergeWithSemantics = iota
	// SkipZero means zero values in later configs do not overwrite non-zero values.
	SkipZero
)

type Option added in v0.2.1

type Option[T any] func(*T)

Option is a functional option for configuration.

type Options added in v1.0.0

type Options[T any] struct {
	Value T
}

Options wraps a validated configuration section for DI consumption. Similar to .NET IOptions<T>.

func Configure added in v1.0.0

func Configure[T any](value T) Options[T]

Configure registers an Options[T] in the DI container by binding from the given configuration section at startup.

Usage:

di.Configure[AppOptions](builder, cfg.GetSection("app"))

Jump to

Keyboard shortcuts

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