config

package
v0.0.6 Latest Latest
Warning

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

Go to latest
Published: Oct 24, 2026 License: MIT Imports: 11 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

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

func DecodeOptions(raw map[string]any, out any) error

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

func RegisteredBackend(name string) bool

RegisteredBackend reports whether a Backend is registered under name.

func RegisteredSource

func RegisteredSource(name string) bool

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

func NewBackend(name string) (Backend, error)

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.

func New

func New(opts ...Option) Config

New builds a Config from the given options.

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 Loader

type Loader interface {
	Load(sources ...Source) error
}

Loader decodes and merges raw KeyValues into a reader.

type Option

type Option func(*config)

Option configures a Config.

func WithSource

func WithSource(s Source) Option

WithSource appends a configuration 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, ...).

func CreateSource

func CreateSource(name string, opts any) (Source, error)

CreateSource creates a Source by name. It returns an error when the name is not registered or the factory fails.

type SourceFactory

type SourceFactory func(opts any) (Source, error)

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.

type Value

type Value interface {
	String() string
	Int() int
	Int64() int64
	Float64() float64
	Bool() bool
	Duration() time.Duration
	Bytes() []byte
	// Scan unmarshals the value into v.
	Scan(v interface{}) error
}

Value provides typed access to a configuration value.

type Watcher

type Watcher interface {
	Next() ([]*KeyValue, error)
	Stop() error
}

Watcher streams raw configuration changes.

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).

Jump to

Keyboard shortcuts

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