core

package
v0.1.8 Latest Latest
Warning

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

Go to latest
Published: Oct 28, 2025 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BindOptions

type BindOptions struct {
	// DefaultValue to use when the key is not found or is nil
	DefaultValue interface{}

	// UseDefaults when true, uses struct field default values for missing keys
	UseDefaults bool

	// IgnoreCase when true, performs case-insensitive key matching
	IgnoreCase bool

	// TagName specifies which struct tag to use for field mapping (default: "yaml")
	TagName string

	// Required specifies field names that must be present in the configuration
	Required []string

	// ErrorOnMissing when true, returns error if any field cannot be bound
	ErrorOnMissing bool

	// DeepMerge when true, performs deep merging for nested structs
	DeepMerge bool
}

BindOptions provides flexible options for binding configuration to structs

func DefaultBindOptions

func DefaultBindOptions() BindOptions

DefaultBindOptions returns default bind options

type ChangeType

type ChangeType string

ChangeType represents the type of configuration change

const (
	ChangeTypeSet    ChangeType = "set"
	ChangeTypeUpdate ChangeType = "update"
	ChangeTypeDelete ChangeType = "delete"
	ChangeTypeReload ChangeType = "reload"
)

type ConfigChange

type ConfigChange struct {
	Source    string      `json:"source"`
	Type      ChangeType  `json:"type"`
	Key       string      `json:"key"`
	OldValue  interface{} `json:"old_value,omitempty"`
	NewValue  interface{} `json:"new_value,omitempty"`
	Timestamp time.Time   `json:"timestamp"`
}

ConfigChange represents a configuration change event

type ConfigManager

type ConfigManager interface {
	// Lifecycle
	Name() string
	SecretsManager() SecretsManager

	// Loading and management
	LoadFrom(sources ...ConfigSource) error
	Watch(ctx context.Context) error
	Reload() error
	ReloadContext(ctx context.Context) error
	Validate() error
	Stop() error

	// Basic getters with optional variadic defaults
	Get(key string) interface{}
	GetString(key string, defaultValue ...string) string
	GetInt(key string, defaultValue ...int) int
	GetInt8(key string, defaultValue ...int8) int8
	GetInt16(key string, defaultValue ...int16) int16
	GetInt32(key string, defaultValue ...int32) int32
	GetInt64(key string, defaultValue ...int64) int64
	GetUint(key string, defaultValue ...uint) uint
	GetUint8(key string, defaultValue ...uint8) uint8
	GetUint16(key string, defaultValue ...uint16) uint16
	GetUint32(key string, defaultValue ...uint32) uint32
	GetUint64(key string, defaultValue ...uint64) uint64
	GetFloat32(key string, defaultValue ...float32) float32
	GetFloat64(key string, defaultValue ...float64) float64
	GetBool(key string, defaultValue ...bool) bool
	GetDuration(key string, defaultValue ...time.Duration) time.Duration
	GetTime(key string, defaultValue ...time.Time) time.Time
	GetSizeInBytes(key string, defaultValue ...uint64) uint64

	// Collection getters with optional variadic defaults
	GetStringSlice(key string, defaultValue ...[]string) []string
	GetIntSlice(key string, defaultValue ...[]int) []int
	GetInt64Slice(key string, defaultValue ...[]int64) []int64
	GetFloat64Slice(key string, defaultValue ...[]float64) []float64
	GetBoolSlice(key string, defaultValue ...[]bool) []bool
	GetStringMap(key string, defaultValue ...map[string]string) map[string]string
	GetStringMapString(key string, defaultValue ...map[string]string) map[string]string
	GetStringMapStringSlice(key string, defaultValue ...map[string][]string) map[string][]string

	// Advanced getters with functional options
	GetWithOptions(key string, opts ...GetOption) (interface{}, error)
	GetStringWithOptions(key string, opts ...GetOption) (string, error)
	GetIntWithOptions(key string, opts ...GetOption) (int, error)
	GetBoolWithOptions(key string, opts ...GetOption) (bool, error)
	GetDurationWithOptions(key string, opts ...GetOption) (time.Duration, error)

	// Configuration modification
	Set(key string, value interface{})

	// Binding methods
	Bind(key string, target interface{}) error
	BindWithDefault(key string, target interface{}, defaultValue interface{}) error
	BindWithOptions(key string, target interface{}, options BindOptions) error

	// Watching and callbacks
	WatchWithCallback(key string, callback func(string, interface{}))
	WatchChanges(callback func(ConfigChange))

	// Metadata and introspection
	GetSourceMetadata() map[string]*SourceMetadata
	GetKeys() []string
	GetSection(key string) map[string]interface{}
	HasKey(key string) bool
	IsSet(key string) bool
	Size() int

	// Structure operations
	Sub(key string) ConfigManager
	MergeWith(other ConfigManager) error
	Clone() ConfigManager
	GetAllSettings() map[string]interface{}

	// Utility methods
	Reset()
	ExpandEnvVars() error
	SafeGet(key string, expectedType reflect.Type) (interface{}, error)

	// Compatibility aliases
	GetBytesSize(key string, defaultValue ...uint64) uint64
	InConfig(key string) bool
	UnmarshalKey(key string, rawVal interface{}) error
	Unmarshal(rawVal interface{}) error
	AllKeys() []string
	AllSettings() map[string]interface{}
	ReadInConfig() error
	SetConfigType(configType string)
	SetConfigFile(filePath string) error
	ConfigFileUsed() string
	WatchConfig() error
	OnConfigChange(callback func(ConfigChange))
}

ConfigManager defines the comprehensive interface for configuration management

type ConfigSource

type ConfigSource interface {
	// Name returns the unique name of the configuration source
	Name() string

	// Priority returns the priority of this source (higher = more important)
	Priority() int

	// Load loads configuration data from the source
	Load(ctx context.Context) (map[string]interface{}, error)

	// Watch starts watching for configuration changes
	Watch(ctx context.Context, callback func(map[string]interface{})) error

	// StopWatch stops watching for configuration changes
	StopWatch() error

	// Reload forces a reload of the configuration source
	Reload(ctx context.Context) error

	// IsWatchable returns true if the source supports watching for changes
	IsWatchable() bool

	// SupportsSecrets returns true if the source supports secret management
	SupportsSecrets() bool

	// GetSecret retrieves a secret value from the source
	GetSecret(ctx context.Context, key string) (string, error)

	// IsAvailable checks if the source is available
	IsAvailable(ctx context.Context) bool

	// GetName returns the name of the source (alias for Name)
	GetName() string

	// GetType returns the type of the source
	GetType() string
}

ConfigSource represents a source of configuration data

type ConfigSourceFactory

type ConfigSourceFactory interface {
	// CreateFileSource creates a file-based configuration source
	CreateFileSource(path string, options ConfigSourceOptions) (ConfigSource, error)

	// CreateEnvSource creates an environment variable configuration source
	CreateEnvSource(prefix string, options ConfigSourceOptions) (ConfigSource, error)

	// CreateMemorySource creates an in-memory configuration source
	CreateMemorySource(data map[string]interface{}, options ConfigSourceOptions) (ConfigSource, error)

	// RegisterCustomSource registers a custom configuration source
	RegisterCustomSource(name string, factory func(ConfigSourceOptions) (ConfigSource, error)) error
}

ConfigSourceFactory creates configuration sources

type ConfigSourceOptions

type ConfigSourceOptions struct {
	Name            string
	Priority        int
	WatchInterval   time.Duration
	EnvironmentVars map[string]string
	SecretKeys      []string
	Validation      ValidationOptions
}

ConfigSourceOptions contains options for creating a configuration source

type GetOption

type GetOption func(*GetOptions)

GetOption defines functional options for advanced get operations

type GetOptions

type GetOptions struct {
	Default    interface{}
	Required   bool
	Validator  func(interface{}) error
	Transform  func(interface{}) interface{}
	OnMissing  func(string) interface{}
	AllowEmpty bool
	CacheKey   string
}

GetOptions contains options for advanced get operations

type SecretProvider

type SecretProvider interface {
	// Name returns the provider name
	Name() string

	// GetSecret retrieves a secret
	GetSecret(ctx context.Context, key string) (string, error)

	// SetSecret stores a secret
	SetSecret(ctx context.Context, key, value string) error

	// DeleteSecret removes a secret
	DeleteSecret(ctx context.Context, key string) error

	// ListSecrets returns all secret keys
	ListSecrets(ctx context.Context) ([]string, error)

	// HealthCheck performs a health check
	HealthCheck(ctx context.Context) error

	// SupportsRotation returns true if the provider supports secret rotation
	SupportsRotation() bool

	// SupportsCaching returns true if the provider supports caching
	SupportsCaching() bool

	// Initialize initializes the provider
	Initialize(ctx context.Context, config map[string]interface{}) error

	// Close closes the provider
	Close(ctx context.Context) error
}

SecretProvider defines an interface for different secret backends

type SecretsManager

type SecretsManager interface {
	// GetSecret retrieves a secret by key
	GetSecret(ctx context.Context, key string) (string, error)

	// SetSecret stores a secret
	SetSecret(ctx context.Context, key, value string) error

	// DeleteSecret removes a secret
	DeleteSecret(ctx context.Context, key string) error

	// ListSecrets returns all secret keys
	ListSecrets(ctx context.Context) ([]string, error)

	// RotateSecret rotates a secret with a new value
	RotateSecret(ctx context.Context, key, newValue string) error

	// RegisterProvider registers a secrets provider
	RegisterProvider(name string, provider SecretProvider) error

	// GetProvider returns a secrets provider by name
	GetProvider(name string) (SecretProvider, error)

	// RefreshSecrets refreshes all cached secrets
	RefreshSecrets(ctx context.Context) error

	// Start starts the secrets manager
	Start(ctx context.Context) error

	// Stop stops the secrets manager
	Stop(ctx context.Context) error

	// HealthCheck performs a health check
	HealthCheck(ctx context.Context) error
}

SecretsManager manages secrets for configuration

type SourceConfig

type SourceConfig struct {
	Name          string            `yaml:"name" json:"name"`
	Type          string            `yaml:"type" json:"type"`
	Priority      int               `yaml:"priority" json:"priority"`
	WatchEnabled  bool              `yaml:"watch_enabled" json:"watch_enabled"`
	WatchInterval time.Duration     `yaml:"watch_interval" json:"watch_interval"`
	RetryAttempts int               `yaml:"retry_attempts" json:"retry_attempts"`
	RetryDelay    time.Duration     `yaml:"retry_delay" json:"retry_delay"`
	Properties    map[string]string `yaml:"properties" json:"properties"`
	Validation    ValidationConfig  `yaml:"validation" json:"validation"`
}

SourceConfig contains common configuration for all sources

type SourceEvent

type SourceEvent struct {
	SourceName string                 `json:"source_name"`
	EventType  string                 `json:"event_type"`
	Data       map[string]interface{} `json:"data"`
	Timestamp  time.Time              `json:"timestamp"`
	Error      error                  `json:"error,omitempty"`
}

SourceEvent represents an event from a configuration source

type SourceEventHandler

type SourceEventHandler interface {
	HandleEvent(event SourceEvent) error
}

SourceEventHandler handles events from configuration sources

type SourceMetadata

type SourceMetadata struct {
	Name         string                 `json:"name"`
	Priority     int                    `json:"priority"`
	Type         string                 `json:"type"`
	LastLoaded   time.Time              `json:"last_loaded"`
	LastModified time.Time              `json:"last_modified"`
	IsWatching   bool                   `json:"is_watching"`
	KeyCount     int                    `json:"key_count"`
	ErrorCount   int                    `json:"error_count"`
	LastError    string                 `json:"last_error,omitempty"`
	Properties   map[string]interface{} `json:"properties"`
}

SourceMetadata contains metadata about a configuration source

type SourceRegistry

type SourceRegistry interface {
	// RegisterSource registers a configuration source
	RegisterSource(source ConfigSource) error

	// UnregisterSource unregisters a configuration source
	UnregisterSource(name string) error

	// GetSource retrieves a configuration source by name
	GetSource(name string) (ConfigSource, error)

	// GetSources returns all registered sources ordered by priority
	GetSources() []ConfigSource

	// GetSourceMetadata returns metadata for a source
	GetSourceMetadata(name string) (*SourceMetadata, error)

	// GetAllMetadata returns metadata for all sources
	GetAllMetadata() map[string]*SourceMetadata
}

SourceRegistry manages registered configuration sources

type ValidationConfig

type ValidationConfig struct {
	Enabled     bool                   `yaml:"enabled" json:"enabled"`
	Required    []string               `yaml:"required" json:"required"`
	Schema      map[string]interface{} `yaml:"schema" json:"schema"`
	Constraints map[string]interface{} `yaml:"constraints" json:"constraints"`
}

ValidationConfig contains validation configuration for sources

type ValidationOptions

type ValidationOptions struct {
	Enabled     bool
	Schema      interface{}
	Required    []string
	Constraints map[string]interface{}
	CustomRules []ValidationRule
}

ValidationOptions contains validation configuration

type ValidationRule

type ValidationRule interface {
	Name() string
	Validate(key string, value interface{}) error
	AppliesTo(key string) bool
}

ValidationRule represents a custom validation rule

type WatchContext

type WatchContext struct {
	Source      ConfigSource
	Interval    time.Duration
	LastCheck   time.Time
	ChangeCount int64
	ErrorCount  int64
	Active      bool
}

WatchContext contains context for watching configuration changes

Jump to

Keyboard shortcuts

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