config

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Sep 20, 2026 License: MIT Imports: 8 Imported by: 0

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 RegisterSource

func RegisterSource(name string, f SourceFactory)

RegisterSource registers a SourceFactory under name. Source packages call this from init().

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 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 raw bytes.
	Source() ([]byte, error)
}

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