Documentation
¶
Overview ¶
Package typedcfg loads single-source configurations into atomic snapshots. JSON, HTTP, polling, defaults and custom validation use only the standard library.
Index ¶
- Variables
- type ConfigError
- type ConfigManager
- func Load[T any](path string, opts ...Option) (*ConfigManager[T], error)
- func LoadContext[T any](ctx context.Context, path string, opts ...Option) (*ConfigManager[T], error)
- func LoadSource[T any](ctx context.Context, source Source, opts ...Option) (*ConfigManager[T], error)
- func MustLoad[T any](path string, opts ...Option) *ConfigManager[T]
- type Decoder
- type Encoder
- type ErrorType
- type HTTPOptions
- type Option
- type Source
- type Subscribe
Constants ¶
This section is empty.
Variables ¶
var ErrClosed = errors.New("configuration manager is closed")
Functions ¶
This section is empty.
Types ¶
type ConfigError ¶
type ConfigError struct {
// Type categorizes the kind of error that occurred
Type ErrorType
// Source identifies where the error originated (file path, provider name, etc.)
Source string
// Message provides a human-readable description of the error
Message string
// Cause holds the underlying error that triggered this configuration error
Cause error
}
ConfigError identifies a configuration failure and wraps its underlying cause.
func NewConfigError ¶
func NewConfigError(errType ErrorType, source, message string, cause error) *ConfigError
NewConfigError constructs a categorized error.
func NewParseError ¶
func NewParseError(source, message string, cause error) *ConfigError
NewParseError wraps a source, decoding, or default-value error.
func NewValidationError ¶
func NewValidationError(source, message string, cause error) *ConfigError
NewValidationError wraps a validation error.
func (*ConfigError) Error ¶
func (e *ConfigError) Error() string
Error includes the category, source, message, and underlying cause.
func (*ConfigError) Is ¶
func (e *ConfigError) Is(target error) bool
Is matches another ConfigError by category.
func (*ConfigError) Unwrap ¶
func (e *ConfigError) Unwrap() error
Unwrap returns the underlying cause.
type ConfigManager ¶
type ConfigManager[T any] struct { // contains filtered or unexported fields }
ConfigManager publishes validated snapshots. Get's entire object graph is read-only.
func Load ¶
func Load[T any](path string, opts ...Option) (*ConfigManager[T], error)
Load reads one JSON file or HTTP(S) URL. An empty path loads only defaults. Use LoadContext to bind refresh to an application context and Close to stop it.
func LoadContext ¶
func LoadSource ¶
func LoadSource[T any](ctx context.Context, source Source, opts ...Option) (*ConfigManager[T], error)
LoadSource loads a caller-owned source. It does not close that source.
func (*ConfigManager[T]) Close ¶
func (cm *ConfigManager[T]) Close() error
Close cancels in-flight HTTP reads, joins subscriptions and retains the last snapshot.
func (*ConfigManager[T]) Errors ¶
func (cm *ConfigManager[T]) Errors() <-chan error
Errors buffers one asynchronous error; additional errors may be dropped.
func (*ConfigManager[T]) Get ¶
func (cm *ConfigManager[T]) Get() *T
Get returns the shared snapshot without copying. Do not modify any reachable fields, slices, maps or pointers; use Snapshot for a mutable private copy.
func (*ConfigManager[T]) Reload ¶
func (cm *ConfigManager[T]) Reload() error
Reload builds a fresh object and atomically publishes only after validation. Manual and automatic reloads are serialized. Errors preserve the previous version.
func (*ConfigManager[T]) Snapshot ¶
func (cm *ConfigManager[T]) Snapshot() *T
Snapshot deep-copies the current version for independent local modification.
type Decoder ¶
Decoder decodes into a pointer to the configuration, already populated with defaults.
type ErrorType ¶
type ErrorType int
ErrorType categorizes a configuration failure. Loading currently wraps source and decoding failures as ParseFailure and validation failures as ValidationFailure; other categories are available to callers constructing their own ConfigError.
const ( // ErrorTypeUnknown represents an unclassified error ErrorTypeUnknown ErrorType = iota // ErrorTypeFileNotFound indicates a configuration file could not be found ErrorTypeFileNotFound // ErrorTypeParseFailure indicates failure to parse configuration data ErrorTypeParseFailure // ErrorTypeValidationFailure indicates configuration validation failed ErrorTypeValidationFailure // ErrorTypeWatchFailure indicates failure in file watching functionality ErrorTypeWatchFailure // ErrorTypeMergeFailure indicates failure to merge configuration sources ErrorTypeMergeFailure )
type HTTPOptions ¶
type HTTPOptions = httpconfig.Options
HTTPOptions controls bounded HTTP reads; headers and the client are copied at Load.
type Option ¶
type Option func(*options)
func WithCodec ¶
WithCodec selects direct decoding and optional encoding for default-file creation. Core JSON uses json tags; other codecs define their own tag and conversion rules.
func WithCreateIfMissing ¶
func WithCreateIfMissing() Option
func WithHTTP ¶
func WithHTTP(opts HTTPOptions) Option
func WithPollInterval ¶
func WithSubscription ¶
WithSubscription enables notifications plus periodic refresh as a fallback.
func WithValidation ¶
WithValidation adds validation before the configuration's own Validate method. Use the optional validator adapter to enable validate tags.