settings

package
v0.2.6 Latest Latest
Warning

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

Go to latest
Published: Apr 23, 2025 License: MIT Imports: 4 Imported by: 0

README

Settings

This is a simple example of using generics in Go.

Example

type Setting = settings.Setting[Serialize]

func WithValue(value string) Setting {
	return func(o *Serialize) {
		o.Value = value
	}
}

type Serialize struct {
	Value    string
}

var (
	defaultSerialize = Serialize{
		Value:    "hello",
}

func NewSerialize(ts ...Setting) *Serialize {
	serialize := settings.Apply(&defaultSerialize, ts)
	return serialize
}

func main() {
	serialize = NewSerialize()
	fmt.Println(serialize.Value)
	// Output: hello
	serialize := NewSerialize(WithValue("world"))
	fmt.Println(serialize.Value)
	// Output: world
}

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

View Source
const (
	ErrUnsupportedType = iota
	ErrExecutionFailed
	ErrEmptyTargetValue
)

Error types for configuration errors

Variables

This section is empty.

Functions

func Apply

func Apply[S any, F FuncType[S]](target *S, settings []F) *S

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

func ApplyAny[S any](target *S, settings []any) *S

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

func ApplyDefault[S any, F FuncType[S]](s S, settings []F) *S

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

func ApplyDefaultE[S any](target S, settings []FuncE[S]) (*S, error)

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

func ApplyDefaultsOrError[S any](s *S, fs ...func(*S)) (*S, error)

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

func ApplyE[S any, Func FuncEType[S]](target *S, settings []Func) (*S, error)

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

func ApplyErrorDefaults[S any](s *S, fs []func(*S)) (*S, error)

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

func ApplyErrorDefaultsOr[S any](s *S, fs ...func(*S)) (*S, error)

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

func ApplyErrorDefaultsOrZero[S any](fs ...func(*S)) (*S, error)

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

func ApplyMixed[S any](target *S, settings []any) (*S, error)

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

func ApplyStrict[S any](target *S, settings []any) *S

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

func ApplyStrictE[S any](target *S, settings []any) (*S, error)

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

func ApplyWith[S any, F FuncType[S]](target *S, settings ...F) *S

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

func ApplyWithDefault[S any, F FuncType[S]](s S, settings ...F) *S

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

func ApplyWithDefaultE[S any](target S, settings ...FuncE[S]) (*S, error)

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

func ApplyWithE[S any, Func FuncEType[S]](target *S, settings ...Func) (*S, error)

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

func ApplyWithZero[S any, F FuncType[S]](settings ...F) *S

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

func ApplyWithZeroE[S any](settings ...FuncE[S]) (*S, error)

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

func ApplyZero[S any, F FuncType[S]](settings []F) *S

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

func ApplyZeroE[S any](settings []FuncE[S]) (*S, error)

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

func CoalesceBool[T types.Boolean](a, b T) T

CoalesceBool provides type-safe coalescing for boolean types Returns the first non-zero boolean value

func CoalesceFloat added in v0.2.5

func CoalesceFloat[T types.Float](a, b T) T

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

func CoalesceInt[T types.Integer](a, b T) T

CoalesceInt provides type-safe coalescing for integer types Returns the first non-zero integer value

func CoalesceString added in v0.2.5

func CoalesceString[T types.String](a, b T) T

CoalesceString handles string coalescing with type safety Converts parameters to string type before comparison

func IsConfigError added in v0.2.0

func IsConfigError(err error) bool

func IsEmptyTargetValueError added in v0.2.0

func IsEmptyTargetValueError(err error) bool

func IsExecutionFailedError added in v0.2.0

func IsExecutionFailedError(err error) bool

func IsUnsupportedTypeError added in v0.2.0

func IsUnsupportedTypeError(err error) bool

func New added in v0.2.0

func New[S any, F FuncType[S]](settings []F) *S

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

func WithDefault[S any](target S, settings ...any) *S

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

func WithDefaultE[S any](target *S, settings ...any) (*S, error)

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

func WithMixed[S any](target *S, settings ...any) (*S, error)

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

func WithMixedZero[S any](settings ...any) (*S, error)

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

func WithZero added in v0.2.0

func WithZero[S any](settings ...any) *S

WithZero applies settings and handles default values. Parameters:

  • settings: Configuration functions to apply

Returns:

  • *S: Configured struct pointer

func WithZeroE added in v0.2.0

func WithZeroE[S any](settings ...any) (*S, error)

WithZeroE 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

type ApplierE[S any] interface {
	Apply(target *S) error
}

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

func (ApplyFunc[S]) Apply

func (s ApplyFunc[S]) Apply(v *S)

Apply executes the configuration function with nil safety. 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.

func (Func[S]) Apply added in v0.2.0

func (f Func[S]) Apply(target *S)

Apply executes the configuration function with nil safety.

type FuncE added in v0.2.0

type FuncE[S any] func(*S) error

FuncE defines enhanced configuration function with error return

func (FuncE[S]) Apply added in v0.2.0

func (f FuncE[S]) Apply(target *S) error

Apply executes the configuration function with nil safety. Returns:

  • error: Any error encountered during execution
  • nil: Success or non-error configuration applied

type FuncEType added in v0.2.2

type FuncEType[S any] interface {
	~func(*S) error
}

FuncEType represents a union type for configuration options.

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 NewErrorResult[S any](err error) Result[S]

func NewResult added in v0.2.0

func NewResult[S any](val *S, err error) Result[S]

func NewValueResult added in v0.2.0

func NewValueResult[S any](val *S) Result[S]

func ResultMixed added in v0.2.0

func ResultMixed[S any](target *S, settings []any) Result[S]

func ResultWith added in v0.2.0

func ResultWith[S any](target *S, settings []func(*S)) Result[S]

func ResultWithE added in v0.2.0

func ResultWithE[S any](target *S, settings []func(*S) error) Result[S]

func ResultWithZero added in v0.2.0

func ResultWithZero[S any](settings []func(*S)) Result[S]

ResultWithZero creates a zero-value instance and applies settings. Parameters:

  • settings: Configuration functions to apply

Returns:

  • *S: New configured instance

func ResultWithZeroE added in v0.2.0

func ResultWithZeroE[S any](settings []func(*S) error) Result[S]

func ResultZeroMixed added in v0.2.0

func ResultZeroMixed[S any](settings []any) Result[S]

Jump to

Keyboard shortcuts

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