Documentation ¶
Overview ¶
Example ¶
package main import ( "fmt" "os" "path" "github.com/sraphs/config" "github.com/sraphs/config/env" "github.com/sraphs/config/file" "github.com/sraphs/config/flag" testData "github.com/sraphs/config/internal/testdata" ) func main() { os.Setenv("sraph_log_level", "warn") p := path.Join("internal", "testdata", "yaml") c := config.New( config.WithSource( env.NewSource("sraph_"), file.NewSource(p), flag.NewSource(), ), ) if err := c.Load(); err != nil { panic(err) } var conf testData.Conf if err := c.Scan(&conf); err != nil { panic(err) } c.Watch(func(c config.Config) { c.Scan(&conf) }) fmt.Println(&conf) c.Get("log.level").String() // Outputs: // log:{level:"warn"} server:{http:{addr:"0.0.0.0:8000" timeout:{seconds:1}} grpc:{addr:"0.0.0.0:9000" timeout:{seconds:1}}} data:{database:{driver:"mysql"} redis:{addr:"mysql:6379" read_timeout:{nanos:200000000} write_timeout:{nanos:200000000}}} }
Output:
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var ( ErrUnsupportedFormat = errors.New("unsupported format") ErrScanNeedPtr = errors.New("scan need ptr") ErrNotFound = errors.New("key not found") ErrTypeAssert = errors.New("type assert error") )
View Source
var (
SupportedFormats = []string{"env", "json", "xml", "yaml", "yml"}
)
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config interface { Load() error Scan(v interface{}) error Watch(o Observer) error Close() error Get(key string) Value }
Config is a config interface.
type Decoder ¶ added in v1.0.1
type Decoder func(*Descriptor, map[string]interface{}) error
Decoder is config decoder.
type Descriptor ¶
Descriptor is file or env or flag descriptor.
func (*Descriptor) GetCodec ¶
func (d *Descriptor) GetCodec() encoding.Codec
type Option ¶
type Option func(*options)
Option is config option.
func WithDecoder ¶ added in v1.0.1
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 WithResolver ¶ added in v1.0.1
WithResolver with config resolver.
type Reader ¶ added in v1.0.1
type Reader interface { Merge(...*Descriptor) error Value(string) (Value, bool) Source() ([]byte, error) Resolve() error }
Reader is config reader.
type Source ¶
type Source interface { Load() ([]*Descriptor, error) Watch() (Watcher, error) }
Source is config source.
type Value ¶ added in v1.0.1
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.
type Watcher ¶
type Watcher interface { Next() ([]*Descriptor, error) Stop() error }
Watcher watches a source for changes.
Click to show internal directories.
Click to hide internal directories.