config

package
v2.0.0-alpha.1 Latest Latest
Warning

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

Go to latest
Published: Feb 17, 2021 License: MIT Imports: 13 Imported by: 435

README

Config

Example Usage

可以指定多个配置源,config 会进行合并成 map[string]interface{},然后通过 Scan 或者 Value 获取值内容;

c := config.New(
    config.WithSource(
        file.NewSource(path),
    ),
    config.WithDecoder(func(kv *config.KeyValue, v map[string]interface{}) error {
        // kv.Key
        // kv.Value
        // kv.Metadata
        // 自定义实现对应的数据源解析,如果是配置中心数据源也可以指定metadata进行识别配置类型
        return yaml.Unmarshal(kv.Value, v)
    }),
)
// 加载配置源:
if err := c.Load(); err != nil {
    panic(err)
}
// 获取对应的值内容:
name, err := c.Value("service").String()
// 解析到结构体(由于已经合并到map[string]interface{},所以需要指定 jsonName 进行解析):
var v struct {
    Service string `json:"service"`
    Version string `json:"version"`
}
if err := c.Scan(&v); err != nil {
    panic(err)
}
// 监听值内容变更
c.Watch("service.name", func(key string, value config.Value) {
    // 值内容变更
})

Documentation

Index

Constants

This section is empty.

Variables

View Source
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

func New(opts ...Option) Config

New new a config with options.

type Decoder

type Decoder func(*KeyValue, map[string]interface{}) error

Decoder is config decoder.

type KeyValue

type KeyValue struct {
	Key      string
	Value    []byte
	Metadata map[string]string
}

KeyValue is config key value.

type Observer

type Observer func(string, Value)

Observer is config observer.

type Option

type Option func(*options)

Option is config option.

func WithDecoder

func WithDecoder(d Decoder) Option

WithDecoder with config decoder.

func WithLogger

func WithLogger(l log.Logger) Option

WithLogger with config loogger.

func WithSource

func WithSource(s ...Source) Option

WithSource with config source.

type Reader

type Reader interface {
	Merge(...*KeyValue) error
	Value(string) (Value, bool)
	Source() ([]byte, error)
}

Reader is config reader.

type Source

type Source interface {
	Load() ([]*KeyValue, error)
	Watch() (Watcher, error)
}

Source is config source.

type Value

type Value interface {
	Bool() (bool, error)
	Int() (int64, error)
	Float() (float64, error)
	String() (string, error)
	Duration() (time.Duration, error)
	Scan(interface{}) error
	Load() interface{}
	Store(interface{})
}

Value is config value interface.

type Watcher

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

Watcher watches a source for changes.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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