Documentation
¶
Overview ¶
Package config provides source-based configuration loading, merging, placeholder resolution, value lookup, scanning, and watching.
Use config.New when composing explicit sources such as config/file, config/env, and config/inmem. Use config/loader for the repository's opinionated defaults-plus-profile loading flow.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrNotFound is key not found. ErrNotFound = errors.New("key not found") // ErrTypeAssert is type assert error. ErrTypeAssert = errors.New("type assert error") )
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config interface {
Load() error
Scan(v interface{}) error
Value(key string) Value
Watch(key string, o Observer) error
Close() error
}
Config is a config interface.
func New ¶
New new a config with options.
Example ¶
package main
import (
"fmt"
"github.com/go-jimu/components/config"
"github.com/go-jimu/components/config/inmem"
)
func main() {
cfg := config.New(config.WithSource(inmem.NewSource("app", []byte(`{
"server": {
"addr": "127.0.0.1",
"port": 8080
}
}`), "json")))
if err := cfg.Load(); err != nil {
panic(err)
}
defer cfg.Close()
addr, err := cfg.Value("server.addr").String()
if err != nil {
panic(err)
}
port, err := cfg.Value("server.port").Int()
if err != nil {
panic(err)
}
fmt.Println(addr, port)
}
Output: 127.0.0.1 8080
type Option ¶
type Option func(*options)
Option is config option.
func WithDecoder ¶
WithDecoder with config decoder. DefaultDecoder behavior: If KeyValue.Format is non-empty, then KeyValue.Value will be deserialized into map[string]interface{} and stored in the config cache(map[string]interface{}) if KeyValue.Format is empty,{KeyValue.Key : KeyValue.Value} will be stored in config cache(map[string]interface{})
func WithResolveActualTypes ¶
WithResolveActualTypes with config resolver. bool input will enable conversion of config to data types
type Reader ¶
type Reader interface {
Merge(...*KeyValue) error
Value(string) (Value, bool)
Source() ([]byte, error)
Resolve() error
}
Reader is config reader.
type Value ¶
type Value interface {
Bool() (bool, error)
Int() (int64, error)
Float() (float64, error)
String() (string, error)
Duration() (time.Duration, error)
Slice() ([]Value, error)
Map() (map[string]Value, error)
Scan(interface{}) error
Load() interface{}
Store(interface{})
}
Value is config value interface.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package env provides a config.Source backed by environment variables.
|
Package env provides a config.Source backed by environment variables. |
|
Package file provides a config.Source backed by files or directories.
|
Package file provides a config.Source backed by files or directories. |
|
Package inmem provides an in-memory config.Source.
|
Package inmem provides an in-memory config.Source. |
|
Package loader provides an opinionated application configuration loader.
|
Package loader provides an opinionated application configuration loader. |