Documentation
¶
Overview ¶
Package config provides a layered configuration abstraction inspired by go-micro: Source (data origin) -> Loader (decode + merge) -> Reader (random access) -> Value (typed access). Backends such as local files and the Polaris config center implement Source.
Index ¶
- func BackendNames() []string
- func DecodeOptions(raw map[string]any, out any) error
- func RegisterBackend(name string, f BackendFactory)
- func RegisterSource(name string, f SourceFactory)
- func RegisteredBackend(name string) bool
- func RegisteredSource(name string) bool
- type Backend
- type BackendFactory
- type Config
- type KeyValue
- type Loader
- type Option
- type Reader
- type Source
- type SourceFactory
- type Value
- type Watcher
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BackendNames ¶ added in v0.0.6
func BackendNames() []string
BackendNames returns the sorted names of all registered backends. It is used by validation to avoid hardcoding a whitelist, and by AddFlags so the flag set does not depend on which type is selected.
func DecodeOptions ¶ added in v0.0.6
DecodeOptions overlays configuration-file values onto a backend's typed Options.
It exists because a backend's typed Options is deliberately not a field of the option struct that owns the section: that is the dependency the backend registry is there to avoid. So the values a file carries under, say, config-source.polaris.* have nowhere to land unless the backend is handed them and decodes them itself. This is the single implementation behind every backend's Decode, so a backend gains file configuration with one line rather than a hand-written field mapping per option.
The matcher ignores case and hyphens, so the key "file-group" and the Go field FileGroup are the same option. Without that every multi-word option would decode to nothing and leave its default in place — a failure with no symptom, since the service starts and merely ignores what it was configured with. That is the same shape of mistake this layer exists to remove.
The decoder config mirrors viper's, so a value written in a config file behaves here as it does everywhere else in the application: a number or a boolean may arrive as a string, and a duration may be written "10s".
func RegisterBackend ¶ added in v0.0.6
func RegisterBackend(name string, f BackendFactory)
RegisterBackend registers a BackendFactory under name. Backend packages call this from init().
func RegisterSource ¶
func RegisterSource(name string, f SourceFactory)
RegisterSource registers a SourceFactory under name. Source packages call this from init().
func RegisteredBackend ¶ added in v0.0.6
RegisteredBackend reports whether a Backend is registered under name.
func RegisteredSource ¶
RegisteredSource reports whether a source is registered under name. It is used by validation to avoid hardcoding source whitelists.
Types ¶
type Backend ¶ added in v0.0.6
type Backend interface {
// Name is the backend identifier, used as the configured type and as the
// nested flag prefix (e.g. "polaris" -> --config-source.polaris.addresses).
Name() string
// AddFlags binds the backend's configuration to flags under prefix. The
// backend owns a typed Options struct and binds directly to it.
AddFlags(fs *pflag.FlagSet, prefix string)
// Decode overlays configuration-file values onto the backend's typed
// Options. Keys are flag names with the prefix removed, e.g. "addresses" for
// --config-source.polaris.addresses. Implementations are one call to
// DecodeOptions.
Decode(raw map[string]any) error
// NewSource builds the Source from the parsed options.
NewSource() (Source, error)
}
Backend is a self-describing configuration source. It contributes its own CLI flags, holds a typed options value, and constructs its Source from them. Concrete backends register a BackendFactory via RegisterBackend in init(); the options layer discovers them through this registry rather than importing each backend, so adding a source no longer requires touching the options package (open/closed principle).
It is the same shape as registry.Backend, one layer up: Source is what reads the configuration — and is what a caller who already has its options uses directly through CreateSource — while Backend is what lets a *configuration file* address a source whose settings the options layer cannot know. A source that only ever needs to be constructed in code registers only a SourceFactory.
func NewBackend ¶ added in v0.0.6
NewBackend constructs a fresh Backend by name.
type BackendFactory ¶ added in v0.0.6
type BackendFactory func() Backend
BackendFactory constructs a Backend with its default options.
type Config ¶
type Config interface {
// Load loads from the configured sources.
Load() error
// Watch invokes fn whenever the configuration changes.
Watch(fn func(Config)) error
// Close releases resources.
Close() error
Value(path string) Value
Scan(v interface{}) error
Reader() Reader
}
Config is the top-level entry point. An App typically owns one Config.
type KeyValue ¶
type KeyValue struct {
Key string
Value []byte
Format string // "json", "yaml", "toml"; empty means auto-detect.
}
KeyValue is a raw configuration key/value returned by a Source.
type Reader ¶
type Reader interface {
// Merge decodes and merges raw key/values.
Merge(kvs ...*KeyValue) error
// Value returns the value at a dot-separated path, e.g. "a.b.c".
Value(path string) Value
// Source returns the merged configuration as JSON bytes.
Source() ([]byte, error)
// Map returns the merged configuration as a nested map.
//
// It exists alongside Source because JSON has a single number type: a value
// written as an integer comes back from Source as a float64 and then needs
// weakly-typed decoding to land in an int field. A caller that is going to
// decode the result itself, or hand it to something that will, should take
// the map and keep the types the source declared.
Map() map[string]any
}
Reader provides random access to merged configuration.
type Source ¶
type Source interface {
// Load returns the current raw key/values.
Load() ([]*KeyValue, error)
// Watch returns a Watcher that emits raw changes.
Watch() (Watcher, error)
}
Source reads raw configuration from a backend (file, remote config center, environment, ...).
type SourceFactory ¶
SourceFactory constructs a Source from a backend-specific options value. Concrete sources register their factory in init() and type-assert the options to their own typed Options struct, mirroring registry.RegisterRegistrar.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
source
|
|
|
file
Package file implements a local-file config.Source with polling-based hot-reload.
|
Package file implements a local-file config.Source with polling-based hot-reload. |
|
polaris
Package polaris implements a config.Source backed by the Polaris config center (github.com/polarismesh/polaris-go).
|
Package polaris implements a config.Source backed by the Polaris config center (github.com/polarismesh/polaris-go). |