Documentation
¶
Overview ¶
Package settings provides type-safe configuration management with default value handling.
Package settings provides utility functions for coalescing values of various types.
Package settings implements the functions, types, and interfaces for the module.
Package settings implements the functions, types, and interfaces for the module.
Package settings implements the functions, types, and interfaces for the module.
Index ¶
- Constants
- func Apply[S any, F FuncType[S]](target *S, settings []F) *S
- func ApplyAny[S any](target *S, settings []any) *S
- func ApplyDefault[S any, F FuncType[S]](s S, settings []F) *S
- func ApplyDefaultE[S any](target S, settings []FuncE[S]) (*S, error)
- func ApplyDefaults[S any](s *S, fs []func(*S)) *S
- func ApplyDefaultsOr[S any](s *S, fs ...func(*S)) *S
- func ApplyDefaultsOrError[S any](s *S, fs ...func(*S)) (*S, error)
- func ApplyDefaultsOrZero[S any](fs ...func(*S)) *S
- func ApplyE[S any, Func FuncEType[S]](target *S, settings []Func) (*S, error)
- func ApplyErrorDefaults[S any](s *S, fs []func(*S)) (*S, error)
- func ApplyErrorDefaultsOr[S any](s *S, fs ...func(*S)) (*S, error)
- func ApplyErrorDefaultsOrZero[S any](fs ...func(*S)) (*S, error)
- func ApplyMixed[S any](target *S, settings []any) (*S, error)
- func ApplyOr[S any](s *S, fs ...func(*S)) *S
- func ApplyOrZero[S any](fs ...func(*S)) *S
- func ApplyStrict[S any](target *S, settings []any) *S
- func ApplyStrictE[S any](target *S, settings []any) (*S, error)
- func ApplyWith[S any, F FuncType[S]](target *S, settings ...F) *S
- func ApplyWithDefault[S any, F FuncType[S]](s S, settings ...F) *S
- func ApplyWithDefaultE[S any](target S, settings ...FuncE[S]) (*S, error)
- func ApplyWithE[S any, Func FuncEType[S]](target *S, settings ...Func) (*S, error)
- func ApplyWithZero[S any, F FuncType[S]](settings ...F) *S
- func ApplyWithZeroE[S any](settings ...FuncE[S]) (*S, error)
- func ApplyZero[S any, F FuncType[S]](settings []F) *S
- func ApplyZeroE[S any](settings []FuncE[S]) (*S, error)
- func Coalesce[T comparable](a, b T) T
- func CoalesceBool[T types.Boolean](a, b T) T
- func CoalesceFloat[T types.Float](a, b T) T
- func CoalesceInt[T types.Integer](a, b T) T
- func CoalesceString[T types.String](a, b T) T
- func IsConfigError(err error) bool
- func IsEmptyTargetValueError(err error) bool
- func IsExecutionFailedError(err error) bool
- func IsUnsupportedTypeError(err error) bool
- func New[S any, F FuncType[S]](settings []F) *S
- func WithDefault[S any](target S, settings ...any) *S
- func WithDefaultE[S any](target *S, settings ...any) (*S, error)
- func WithMixed[S any](target *S, settings ...any) (*S, error)
- func WithMixedZero[S any](settings ...any) (*S, error)
- func WithZero[S any](settings ...any) *S
- func WithZeroE[S any](settings ...any) (*S, error)
- type Applier
- type ApplierE
- type ApplyFunc
- type ApplySetting
- type ConfigError
- type Defaulter
- type ErrorDefaulter
- type Func
- type FuncE
- type FuncEType
- type FuncType
- type Result
- func NewErrorResult[S any](err error) Result[S]
- func NewResult[S any](val *S, err error) Result[S]
- func NewValueResult[S any](val *S) Result[S]
- func ResultMixed[S any](target *S, settings []any) Result[S]
- func ResultWith[S any](target *S, settings []func(*S)) Result[S]
- func ResultWithE[S any](target *S, settings []func(*S) error) Result[S]
- func ResultWithZero[S any](settings []func(*S)) Result[S]
- func ResultWithZeroE[S any](settings []func(*S) error) Result[S]
- func ResultZeroMixed[S any](settings []any) Result[S]
Constants ¶
const ( ErrUnsupportedType = iota ErrExecutionFailed ErrEmptyTargetValue )
Error types for configuration errors
Variables ¶
This section is empty.
Functions ¶
func Apply ¶
Apply configures a target struct with ordered settings. Parameters:
- target: Pointer to the struct being configured (non-nil)
- settings: Ordered list of configuration functions
Returns:
- *S: Configured struct pointer (same as input)
func ApplyAny ¶
ApplyAny applies a list of settings to a target struct. Deprecated: Use ApplyMixed with slice syntax. Will be removed in v0.3.0
Before: ApplyAny(&obj, []interface{}{f1, f2}) After: ApplyMixed(&obj, []interface{}{f1, f2}) (*obj, error)
func ApplyDefault ¶ added in v0.2.2
ApplyDefault applies a list of settings to a target struct. Parameters:
- s: Struct to be configured
- settings: Ordered list of configuration functions
Returns:
- *S: Configured struct pointer (same as input)
func ApplyDefaultE ¶ added in v0.2.2
ApplyDefaultE is an apply settings with Parameters:
- target: Pointer to the struct being configured (non-nil)
- settings: Ordered list of configuration functions
Returns:
- *S: Configured struct pointer (same as input)
- error: Any error encountered during configuration
func ApplyDefaults ¶ added in v0.1.2
func ApplyDefaults[S any](s *S, fs []func(*S)) *S
ApplyDefaults applies the given settings and default settings to the provided value. Decrypted: use Apply instead of ApplyDefaults
It first applies the given settings using the Apply function, then checks if the value implements the Defaulter interface. If it does, it calls the ApplyDefaults method to apply the default settings.
func ApplyDefaultsOr ¶ added in v0.1.2
func ApplyDefaultsOr[S any](s *S, fs ...func(*S)) *S
ApplyDefaultsOr applies the given settings and default settings to the provided value. Decrypted: use WithZeroE instead of ApplyDefaultsOr
It is a convenience wrapper around ApplyDefaults that accepts a variable number of setting functions.
func ApplyDefaultsOrError ¶ added in v0.1.3
ApplyDefaultsOrError applies the given settings and default settings to the provided value. Decrypted: use WithDefaultE instead of ApplyDefaultsOrError
It is a convenience wrapper around ApplyDefaults that accepts a variable number of setting functions.
func ApplyDefaultsOrZero ¶ added in v0.1.2
func ApplyDefaultsOrZero[S any](fs ...func(*S)) *S
ApplyDefaultsOrZero applies the given settings and default settings to a zero value of the type. Decrypted: use WithZero instead of ApplyDefaultsOrZero
It creates a zero value of the type, then calls ApplyDefaults to apply the given settings and default settings.
func ApplyE ¶ added in v0.2.0
ApplyE applies a list of settings to a target struct. Parameters:
- target: Pointer to the struct being configured (non-nil)
- settings: Ordered list of configuration functions
Returns:
- *S: Configured struct pointer (same as input)
- error: Any error encountered during configuration
func ApplyErrorDefaults ¶ added in v0.1.3
ApplyErrorDefaults applies the given settings and default settings to the provided value. Decrypted: use WithDefaultE instead of ApplyErrorDefaults
It first applies the given settings using the Apply function, then checks if the value implements the ErrorDefaulter interface. If it does, it calls the ApplyDefaults method
func ApplyErrorDefaultsOr ¶ added in v0.1.3
ApplyErrorDefaultsOr applies the given settings and default settings to the provided value. Decrypted: use WithDefaultE instead of ApplyErrorDefaultsOr
It is a convenience wrapper around ApplyDefaults that accepts a variable number of interface{} values.
func ApplyErrorDefaultsOrZero ¶ added in v0.1.3
ApplyErrorDefaultsOrZero applies the given settings and default settings to a zero value of the type. Decrypted: use WithZeroE instead of ApplyErrorDefaultsOrZero
It creates a zero value of the type, then calls ApplyDefaults to apply the given settings and default settings.
func ApplyMixed ¶ added in v0.2.0
ApplyMixed applies a list of settings to a target struct. Parameters:
- target: Pointer to the struct being configured (non-nil)
- settings: Ordered list of configuration functions
Returns:
- *S: Configured struct pointer (same as input)
func ApplyOr ¶
func ApplyOr[S any](s *S, fs ...func(*S)) *S
ApplyOr is an apply settings with defaults Decrypted: use WithDefault instead of ApplyOr. Will be removed in v0.3.0
func ApplyOrZero ¶
func ApplyOrZero[S any](fs ...func(*S)) *S
ApplyOrZero is an apply settings with defaults Decrypted: use WithZero instead of ApplyOrZero. Will be removed in v0.3.0
func ApplyStrict ¶ added in v0.2.0
ApplyStrict is a version for strict type safety Parameters:
- target: Pointer to the struct being configured (non-nil)
- settings: Ordered list of configuration functions
Returns:
- *S: Configured struct pointer (same as input)
Panics:
- If target is nil
- If any setting is not a supported type
func ApplyStrictE ¶ added in v0.2.0
ApplyStrictE applies a list of settings to a target struct. Parameters:
- target: Pointer to the struct being configured (non-nil)
- settings: Ordered list of configuration functions
Returns:
- *S: Configured struct pointer (same as input)
func ApplyWith ¶ added in v0.2.2
ApplyWith applies a list of settings to a target struct. Parameters:
- target: Pointer to the struct being configured (non-nil)
- settings: Ordered list of configuration functions
Returns:
- *S: Configured struct pointer (same as input)
func ApplyWithDefault ¶ added in v0.2.2
ApplyWithDefault applies a list of settings to a target struct. Parameters:
- s: Struct to be configured
- settings: Ordered list of configuration functions
Returns:
- *S: Configured struct pointer (same as input)
func ApplyWithDefaultE ¶ added in v0.2.2
ApplyWithDefaultE is an apply settings with defaults Parameters:// Parameters:
- settings: Ordered list of configuration functions
Returns:
- *S: Configured struct pointer (same as input)
func ApplyWithE ¶ added in v0.2.2
ApplyWithE applies a list of settings to a target struct. Parameters:
- target: Pointer to the struct being configured (non-nil)
- settings: Ordered list of configuration functions
Returns:
- *S: Configured struct pointer (same as input)
- error: Any error encountered during configuration
func ApplyWithZero ¶ added in v0.2.2
ApplyWithZero applies a list of settings to a target struct. Parameters:
- settings: Ordered list of configuration functions
Returns:
- *S: Configured struct pointer (same as input)
func ApplyWithZeroE ¶ added in v0.2.2
ApplyWithZeroE is an apply settings with defaults Parameters:
- settings: Ordered list of configuration functions
Returns:
- *S: Configured struct pointer (same as input)
func ApplyZero ¶ added in v0.2.2
ApplyZero applies a list of settings to a target struct. Parameters:
- settings: Ordered list of configuration functions
Returns:
- *S: Configured struct pointer (same as input)
func ApplyZeroE ¶ added in v0.2.2
ApplyZeroE is an apply settings with defaults Parameters:
- settings: Ordered list of configuration functions
Returns:
- *S: Configured struct pointer (same as input)
func Coalesce ¶ added in v0.2.5
func Coalesce[T comparable](a, b T) T
Coalesce returns the first non-zero value of comparable type T If a is non-zero, returns a; otherwise returns b
func CoalesceBool ¶ added in v0.2.5
CoalesceBool provides type-safe coalescing for boolean types Returns the first non-zero boolean value
func CoalesceFloat ¶ added in v0.2.5
CoalesceFloat handles floating point coalescing with epsilon comparison Returns b if a is NaN or its absolute value is below epsilon threshold
func CoalesceInt ¶ added in v0.2.5
CoalesceInt provides type-safe coalescing for integer types Returns the first non-zero integer value
func CoalesceString ¶ added in v0.2.5
CoalesceString handles string coalescing with type safety Converts parameters to string type before comparison
func IsConfigError ¶ added in v0.2.0
func IsEmptyTargetValueError ¶ added in v0.2.0
func IsExecutionFailedError ¶ added in v0.2.0
func IsUnsupportedTypeError ¶ added in v0.2.0
func New ¶ added in v0.2.0
New creates a zero-value instance and applies settings. Parameters:
- settings: Configuration functions to apply
Retur Returns:
- *S: New configured instance
func WithDefault ¶ added in v0.2.0
WithDefault applies settings and handles default values. Parameters:
- target: Struct pointer to configure
- settings: Configuration functions to apply
Returns:
- *S: Configured struct pointer
func WithDefaultE ¶ added in v0.2.0
WithDefaultE applies settings and handles default values. Parameters:
- target: Struct pointer to configure
- settings: Configuration functions to apply
Returns:
- *S: Configured struct pointer
- error: Error if any occurred during the configuration process
func WithMixed ¶ added in v0.2.0
WithMixed applies settings and handles default values. Parameters:
- target: Struct pointer to configure
- settings: Configuration functions to apply
Returns:
- *S: Configured struct pointer
- error: Error if any occurred during the configuration process
func WithMixedZero ¶ added in v0.2.0
WithMixedZero applies settings and handles default values. Parameters:
- settings: Configuration functions to apply
Returns:
- *S: Configured struct pointer
- error: Error if any occurred during the configuration process
Types ¶
type Applier ¶ added in v0.2.0
type Applier[S any] interface { // Apply modifies the target struct with specific settings Apply(target *S) }
Applier defines the interface for configuration application mechanisms.
type ApplierE ¶ added in v0.2.0
ApplierE defines enhanced configuration interface with error handling
type ApplyFunc ¶
type ApplyFunc[S any] func(*S)
ApplyFunc is a ApplyFunc function for Apply Decrypted: use Func instead of ApplyFunc. Will be removed in v0.3.0
type ApplySetting ¶
type ApplySetting[S any] interface { Apply(v *S) }
ApplySetting defines the interface for configuration settings. Decrypted: use Applier instead of ApplySetting. Will be removed in v0.3.0
type ConfigError ¶ added in v0.2.0
type ConfigError struct { Type int // Error category TypeString string // Setting type info Err error // Original error }
func (*ConfigError) Error ¶ added in v0.2.0
func (e *ConfigError) Error() string
Error message display is enhanced
func (*ConfigError) Unwrap ¶ added in v0.2.0
func (e *ConfigError) Unwrap() error
type Defaulter ¶ added in v0.1.2
type Defaulter interface {
// ApplyDefaults applies the default settings to the implementing type.
ApplyDefaults()
}
Defaulter is an interface that provides a method to apply default settings. Decrypted: use Apply instead of Defaulter
type ErrorDefaulter ¶ added in v0.1.3
type ErrorDefaulter interface { // ApplyDefaults applies the default settings to the implementing type and returns an error. ApplyDefaults() error }
ErrorDefaulter is an interface that provides a method to apply default settings and return an error. Decrypted: use ApplyE instead of ErrorDefaulter
type Func ¶ added in v0.2.0
type Func[S any] func(*S)
Func defines the standard function type for configuration operations.
type FuncType ¶ added in v0.2.2
type FuncType[S any] interface { ~func(*S) }
FuncType represents a union type for configuration options.
type Result ¶ added in v0.2.0
type Result[S any] interface { // Value returns the configured struct pointer Value() *S // Err returns any error occurred during configuration Err() error // Unwrap returns both value and error for direct handling Unwrap() (*S, error) // MustUnwrap panics if error exists MustUnwrap() *S }
Result defines the unified return type for configuration operations
func NewErrorResult ¶ added in v0.2.0
func NewValueResult ¶ added in v0.2.0
func ResultMixed ¶ added in v0.2.0
func ResultWith ¶ added in v0.2.0
func ResultWithE ¶ added in v0.2.0
func ResultWithZero ¶ added in v0.2.0
ResultWithZero creates a zero-value instance and applies settings. Parameters:
- settings: Configuration functions to apply
Returns:
- *S: New configured instance