source

package
v0.3.28 Latest Latest
Warning

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

Go to latest
Published: Mar 15, 2026 License: MIT Imports: 22 Imported by: 2

Documentation

Index

Constants

View Source
const WatchAllChanges = "*"

WatchAllChanges is a special value that indicates all configuration values may have changed. This should be used when the source detects a broad change but can't determine exactly which keys changed.

Variables

View Source
var AllChanges = []string{WatchAllChanges}

AllChanges is a predefined slice containing the WatchAllChanges constant for convenience.

Functions

func FindSourceByType

func FindSourceByType[T ConfigSource](sources []ConfigSource) (T, error)

FindSourceByType iterates through a slice of ConfigSource and returns the first source that matches the specified type T. If no source of the specified type is found, it returns the zero value for T and an error.

Types

type ArrayStrategy added in v0.3.27

type ArrayStrategy int

ArrayStrategy defines how environment variable values are parsed as arrays.

const (
	// ArrayStrategyAuto automatically detects the best parsing strategy.
	// Tries in order: index-based → JSON arrays → delimited (comma).
	ArrayStrategyAuto ArrayStrategy = iota

	// ArrayStrategyIndex uses index-based environment variables (APP_KEY_0, APP_KEY_1).
	// Clear ordering but requires multiple env vars.
	ArrayStrategyIndex

	// ArrayStrategyDelimited uses a delimiter to split values (e.g., "value1,value2,value3").
	// Delimiter defaults to comma but can be configured.
	ArrayStrategyDelimited

	// ArrayStrategyJSON parses values as JSON arrays.
	// Example: '["value1","value2"]' → []string{"value1", "value2"}
	ArrayStrategyJSON
)

type ConfigDefaults

type ConfigDefaults interface {
	Defaults() map[string]any
}

type ConfigSource

type ConfigSource interface {
	// Load loads configuration data from a specific source into the config manager.
	Load(ctx context.Context, cm configManager) error
	// Watch watches for changes in the configuration source and triggers the onChange function with the keys that changed.
	// If the source does not support watching, this method should return nil.
	Watch(ctx context.Context, cm configManager, onChange WatchOnChangeCallback) error
}

ConfigSource abstracts the loading of configuration data from different sources.

func NewFileSource

func NewFileSource(path string, opts ...FileSourceOption) ConfigSource

type DefaultConfigOption added in v0.3.2

type DefaultConfigOption func(*DefaultConfigOptions)

DefaultConfigOption defines the option function type

func WithDefaultSourceDefaults added in v0.3.13

func WithDefaultSourceDefaults(defaults map[string]any) DefaultConfigOption

WithDefaultSourceDefaults sets the default values map

func WithDefaultSourceGlobal added in v0.3.13

func WithDefaultSourceGlobal() DefaultConfigOption

WithDefaultSourceGlobal marks this source to be loaded globally

func WithDefaultSourceTagName added in v0.3.13

func WithDefaultSourceTagName(tagName string) DefaultConfigOption

WithDefaultSourceTagName sets the struct tag name to use (default: "config")

type DefaultConfigOptions added in v0.3.2

type DefaultConfigOptions struct {
	// contains filtered or unexported fields
}

DefaultConfigOptions holds configuration options for DefaultConfigSource

type DefaultConfigSource

type DefaultConfigSource struct {
	// contains filtered or unexported fields
}

DefaultConfigSource loads default configuration values.

func NewDefaultConfigSource

func NewDefaultConfigSource(manager manager, opts ...DefaultConfigOption) *DefaultConfigSource

NewDefaultConfigSource creates a new DefaultConfigSource. The defaults map can contain nested values using dot notation keys (e.g. "database.host").

func (*DefaultConfigSource) IsGlobal added in v0.3.12

func (d *DefaultConfigSource) IsGlobal() bool

IsGlobal implements GlobalConfigSource

func (*DefaultConfigSource) Load

func (d *DefaultConfigSource) Load(ctx context.Context, cm configManager) error

Load loads the default configuration values into the config manager.

func (*DefaultConfigSource) Watch

func (d *DefaultConfigSource) Watch(ctx context.Context, cm configManager, onChange WatchOnChangeCallback) error

Watch does not support watching default values, so it returns nil.

type EnvConfigOption added in v0.3.12

type EnvConfigOption func(*EnvConfigSource)

NewEnvConfigSource creates a new EnvConfigSource with optional prefix and delimiter. The prefix is prepended to environment variable names (e.g. "APP_"). The delimiter is used to split nested keys (e.g. "_" for "APP_DB_HOST").

func WithEnvEnvironFunc added in v0.3.27

func WithEnvEnvironFunc(fn func() []string) EnvConfigOption

WithEnvEnvironFunc provides a custom environment variable function (for testing).

func WithEnvSourceArrayStrategy added in v0.3.27

func WithEnvSourceArrayStrategy(strategy ArrayStrategy, delimiter string) EnvConfigOption

WithEnvSourceArrayStrategy configures how array values are parsed from environment variables.

func WithEnvSourceGlobal added in v0.3.13

func WithEnvSourceGlobal() EnvConfigOption

func WithEnvTransformFunc added in v0.3.27

func WithEnvTransformFunc(fn func(k, v string) (string, any)) EnvConfigOption

WithEnvTransformFunc provides a custom transform callback.

type EnvConfigSource

type EnvConfigSource struct {
	// contains filtered or unexported fields
}

EnvConfigSource loads configuration from environment variables.

func NewEnvConfigSource

func NewEnvConfigSource(prefix, delimiter string, opts ...EnvConfigOption) *EnvConfigSource

func (*EnvConfigSource) IsGlobal added in v0.3.12

func (e *EnvConfigSource) IsGlobal() bool

IsGlobal implements GlobalConfigSource

func (*EnvConfigSource) Load

func (e *EnvConfigSource) Load(ctx context.Context, cm configManager) error

Load loads the configuration from environment variables into the config manager.

func (*EnvConfigSource) Watch

func (e *EnvConfigSource) Watch(_ context.Context, _ configManager, _ WatchOnChangeCallback) error

Watch watches for changes in the environment variables and triggers the onChange function when a change occurs. Environment variables cannot be watched in a cross-platform way, so this is a no-op.

type EtcdConfigSource

type EtcdConfigSource struct {
	// contains filtered or unexported fields
}

EtcdConfigSource loads configuration from etcd.

func NewEtcdConfigSource

func NewEtcdConfigSource(config *config.EtcdConfig, opts ...EtcdConfigSourceOption) (*EtcdConfigSource, error)

NewEtcdConfigSource creates a new EtcdConfigSource.

func (*EtcdConfigSource) Load

func (e *EtcdConfigSource) Load(ctx context.Context, cm configManager) error

Load loads the configuration from etcd into the config manager.

func (*EtcdConfigSource) Persist

func (e *EtcdConfigSource) Persist(ctx context.Context, cm configManager, keyPrefix ...string) error

Persist writes configuration changes back to etcd.

func (*EtcdConfigSource) Stop

func (e *EtcdConfigSource) Stop() error

Stop stops the etcd watcher if it's running.

func (*EtcdConfigSource) Watch

func (e *EtcdConfigSource) Watch(ctx context.Context, cm configManager, onChange WatchOnChangeCallback) error

Watch watches for changes in etcd and triggers the onChange function when a change occurs.

type EtcdConfigSourceOption

type EtcdConfigSourceOption func(*EtcdConfigSource)

func WithEtcdSourceEtcdManager

func WithEtcdSourceEtcdManager(manager etcd.EtcdManager) EtcdConfigSourceOption

WithEtcdSourceEtcdManager injects a custom EtcdManager

func WithEtcdSourceLogger

func WithEtcdSourceLogger(logger *zap.Logger) EtcdConfigSourceOption

WithEtcdSourceLogger sets a custom logger

type FileSourceOption

type FileSourceOption func(*fileSource)

func WithChangedThreshold

func WithChangedThreshold(threshold float64) FileSourceOption

WithChangedThreshold sets the percentage (0-1) of keys that must change to trigger full reload

func WithFileSourceLogger

func WithFileSourceLogger(logger *zap.Logger) FileSourceOption

type GlobalConfigSource added in v0.3.12

type GlobalConfigSource interface {
	ConfigSource
	// IsGlobal returns true if this source should be loaded globally
	IsGlobal() bool
}

GlobalConfigSource marks a ConfigSource that should be loaded globally without namespace isolation

type MemoryConfigSource

type MemoryConfigSource struct {
	// contains filtered or unexported fields
}

MemoryConfigSource is an in-memory configuration source that can be used for testing or temporary configuration storage.

func NewMemoryConfigSource

func NewMemoryConfigSource(initialData map[string]any, opts ...MemoryConfigSourceOption) *MemoryConfigSource

NewMemoryConfigSource creates a new MemoryConfigSource with optional initial data.

func (*MemoryConfigSource) Clear

func (m *MemoryConfigSource) Clear()

Clear removes all data from the memory source.

func (*MemoryConfigSource) Delete

func (m *MemoryConfigSource) Delete(key string)

Delete removes a key from the memory source.

func (*MemoryConfigSource) DeleteFromManager added in v0.3.0

func (m *MemoryConfigSource) DeleteFromManager(key string, cm configManager)

DeleteFromManager deletes a key from the passed config manager and notifies watchers.

func (*MemoryConfigSource) Load

func (m *MemoryConfigSource) Load(ctx context.Context, cm configManager) error

Load loads the in-memory configuration into the config manager.

func (*MemoryConfigSource) Persist

func (m *MemoryConfigSource) Persist(ctx context.Context, cm configManager, keyPrefix ...string) error

Persist implements PersistableConfigSource but does nothing since memory is ephemeral.

func (*MemoryConfigSource) Set

func (m *MemoryConfigSource) Set(key string, value any)

Set sets a value in the memory source.

func (*MemoryConfigSource) Watch

func (m *MemoryConfigSource) Watch(ctx context.Context, cm configManager, cb WatchOnChangeCallback) error

Watch registers a callback to be notified when changes occur.

type MemoryConfigSourceOption added in v0.1.2

type MemoryConfigSourceOption func(*MemoryConfigSource)

func WithMemorySourceLogger added in v0.1.2

func WithMemorySourceLogger(logger *zap.Logger) MemoryConfigSourceOption

type PersistableConfigSource

type PersistableConfigSource interface {
	ConfigSource
	// Persist writes configuration changes back to the source.
	Persist(cm configManager, namespace string, keys ...string) error
}

PersistableConfigSource is a ConfigSource that can persist configuration changes.

type StoppableConfigSource

type StoppableConfigSource interface {
	ConfigSource
	// Stop stops any background processes or watchers associated with the source.
	Stop() error
}

StoppableConfigSource is a ConfigSource that can be stopped.

type TestingInvalidSource added in v0.3.0

type TestingInvalidSource struct {
	// contains filtered or unexported fields
}

TestingInvalidSource is a ConfigSource that always returns errors. Useful for testing error handling.

func New added in v0.3.0

func New(loadError, watchError string) *TestingInvalidSource

New creates a new TestingInvalidSource with custom error messages. If empty strings are provided, default error messages will be used.

func (*TestingInvalidSource) Load added in v0.3.0

func (i *TestingInvalidSource) Load(ctx context.Context, cm configManager) error

Load always returns an error.

func (*TestingInvalidSource) Watch added in v0.3.0

func (i *TestingInvalidSource) Watch(ctx context.Context, cm configManager, cb WatchOnChangeCallback) error

Watch always returns an error.

type WatchOnChangeCallback

type WatchOnChangeCallback func(changedKeys []string, err error)

WatchOnChangeCallback defines the signature for the callback function used in Watch. The callback receives a slice of changed keys: - An empty slice []string{} means no changes were detected - A slice containing WatchAllChanges (["*"]) means all configuration may have changed - A slice of specific keys means those keys changed

Jump to

Keyboard shortcuts

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