Documentation ¶
Overview ¶
Package config is an interface for dynamic configuration.
Index ¶
- Variables
- func AfterInit(fn ...func(context.Context, Config) error) options.Option
- func AfterLoad(fn ...func(context.Context, Config) error) options.Option
- func AfterSave(fn ...func(context.Context, Config) error) options.Option
- func AllowFail(b bool) options.Option
- func BeforeInit(fn ...func(context.Context, Config) error) options.Option
- func BeforeLoad(fn ...func(context.Context, Config) error) options.Option
- func BeforeSave(fn ...func(context.Context, Config) error) options.Option
- func Load(ctx context.Context, cs []Config, opts ...options.Option) error
- func LoadAppend(b bool) options.Option
- func LoadOverride(b bool) options.Option
- func NewContext(ctx context.Context, c Config) context.Context
- func Struct(v interface{}) options.Option
- func StructTag(name string) options.Option
- func Validate(ctx context.Context, cfg interface{}) error
- func WatchCoalesce(b bool) options.Option
- func WatchInterval(min, max time.Duration) options.Option
- type Config
- type LoadOptions
- type Options
- type SaveOptions
- type Validator
- type WatchOptions
- type Watcher
Constants ¶
This section is empty.
Variables ¶
var ( // ErrCodecMissing is returned when codec needed and not specified ErrCodecMissing = errors.New("codec missing") // ErrInvalidStruct is returned when the target struct is invalid ErrInvalidStruct = errors.New("invalid struct specified") // ErrWatcherStopped is returned when source watcher has been stopped ErrWatcherStopped = errors.New("watcher stopped") // ErrWatcherNotImplemented returned when config does not implement watch ErrWatcherNotImplemented = errors.New("watcher not implemented") )
var ( // DefaultBeforeLoad default func that runs before config Load DefaultBeforeLoad = func(ctx context.Context, c Config) error { for _, fn := range c.Options().BeforeLoad { if fn == nil { return nil } if err := fn(ctx, c); err != nil { c.Options().Logger.Error(ctx, c.String()+" BeforeLoad error "+err.Error()) if !c.Options().AllowFail { return err } } } return nil } // DefaultAfterLoad default func that runs after config Load DefaultAfterLoad = func(ctx context.Context, c Config) error { for _, fn := range c.Options().AfterLoad { if fn == nil { return nil } if err := fn(ctx, c); err != nil { c.Options().Logger.Error(ctx, c.String()+" AfterLoad error "+err.Error()) if !c.Options().AllowFail { return err } } } return nil } // DefaultBeforeSave default func that runs befora config Save DefaultBeforeSave = func(ctx context.Context, c Config) error { for _, fn := range c.Options().BeforeSave { if fn == nil { return nil } if err := fn(ctx, c); err != nil { c.Options().Logger.Error(ctx, c.String()+" BeforeSave error "+err.Error()) if !c.Options().AllowFail { return err } } } return nil } // DefaultAfterSave default func that runs after config Save DefaultAfterSave = func(ctx context.Context, c Config) error { for _, fn := range c.Options().AfterSave { if fn == nil { return nil } if err := fn(ctx, c); err != nil { c.Options().Logger.Error(ctx, c.String()+" AfterSave error "+err.Error()) if !c.Options().AllowFail { return err } } } return nil } // DefaultBeforeInit default func that runs befora config Init DefaultBeforeInit = func(ctx context.Context, c Config) error { for _, fn := range c.Options().BeforeInit { if fn == nil { return nil } if err := fn(ctx, c); err != nil { c.Options().Logger.Error(ctx, c.String()+" BeforeInit error "+err.Error()) if !c.Options().AllowFail { return err } } } return nil } // DefaultAfterInit default func that runs after config Init DefaultAfterInit = func(ctx context.Context, c Config) error { for _, fn := range c.Options().AfterSave { if fn == nil { return nil } if err := fn(ctx, c); err != nil { c.Options().Logger.Error(ctx, c.String()+" AfterInit error "+err.Error()) if !c.Options().AllowFail { return err } } } return nil } )
var DefaultConfig = NewConfig()
DefaultConfig default config
var DefaultWatcherMaxInterval = 9 * time.Second
DefaultWatcherMaxInterval default max interval for poll changes
var DefaultWatcherMinInterval = 5 * time.Second
DefaultWatcherMinInterval default min interval for poll changes
Functions ¶
func BeforeInit ¶
BeforeInit run funcs before config Init
func BeforeLoad ¶
BeforeLoad run funcs before config load
func BeforeSave ¶
BeforeSave run funcs before save
func NewContext ¶
NewContext put store in context
func WatchCoalesce ¶
Coalesce controls watch event combining
Types ¶
type Config ¶
type Config interface { // Name returns name of config Name() string // Init the config Init(opts ...options.Option) error // Options in the config Options() Options // Load config from sources Load(context.Context, ...options.Option) error // Save config to sources Save(context.Context, ...options.Option) error // Watch a config for changes Watch(context.Context, ...options.Option) (Watcher, error) // String returns config type name String() string }
Config is an interface abstraction for dynamic configuration
func FromContext ¶
FromContext returns store from context
type LoadOptions ¶
LoadOptions struct
func NewLoadOptions ¶
func NewLoadOptions(opts ...options.Option) LoadOptions
NewLoadOptions create LoadOptions struct with provided opts
type Options ¶
type Options struct { // Struct holds the destination config struct Struct interface{} // Codec that used for load/save Codec codec.Codec // Tracer that will be used Tracer tracer.Tracer // Meter that will be used Meter meter.Meter // Logger that will be used Logger logger.Logger // Context used for external options Context context.Context // Name of the config Name string // StructTag name StructTag string // BeforeSave contains slice of funcs that runs before Save BeforeSave []func(context.Context, Config) error // AfterSave contains slice of funcs that runs after Save AfterSave []func(context.Context, Config) error // BeforeLoad contains slice of funcs that runs before Load BeforeLoad []func(context.Context, Config) error // AfterLoad contains slice of funcs that runs after Load AfterLoad []func(context.Context, Config) error // BeforeInit contains slice of funcs that runs before Init BeforeInit []func(context.Context, Config) error // AfterInit contains slice of funcs that runs after Init AfterInit []func(context.Context, Config) error // AllowFail flag to allow fail in config source AllowFail bool // SkipLoad runs only if condition returns true SkipLoad func(context.Context, Config) bool // SkipSave runs only if condition returns true SkipSave func(context.Context, Config) bool }
Options hold the config options
func NewOptions ¶
NewOptions new options struct with filed values
type SaveOptions ¶
SaveOptions struct
func NewSaveOptions ¶
func NewSaveOptions(opts ...options.Option) SaveOptions
NewSaveOptions fill SaveOptions struct
type WatchOptions ¶
type WatchOptions struct { // Context used by non default options Context context.Context // Struct for filling Struct interface{} // MinInterval specifies the min time.Duration interval for poll changes MinInterval time.Duration // MaxInterval specifies the max time.Duration interval for poll changes MaxInterval time.Duration // Coalesce multiple events to one Coalesce bool }
WatchOptions struuct
func NewWatchOptions ¶
func NewWatchOptions(opts ...options.Option) WatchOptions
NewWatchOptions create WatchOptions struct with provided opts