2019-05-31 01:11:13 +03:00
|
|
|
// Package config is an interface for dynamic configuration.
|
2023-04-11 22:20:37 +03:00
|
|
|
package config // import "go.unistack.org/micro/v4/config"
|
2019-05-31 01:11:13 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-12-04 02:28:45 +03:00
|
|
|
"errors"
|
2022-03-22 14:54:43 +03:00
|
|
|
"reflect"
|
2021-08-03 00:24:40 +03:00
|
|
|
"time"
|
2023-07-29 00:40:58 +03:00
|
|
|
|
|
|
|
"go.unistack.org/micro/v4/options"
|
2020-12-04 02:28:45 +03:00
|
|
|
)
|
2019-05-31 01:11:13 +03:00
|
|
|
|
2022-03-22 14:54:43 +03:00
|
|
|
type Validator interface {
|
|
|
|
Validate() error
|
|
|
|
}
|
|
|
|
|
2021-04-27 08:32:47 +03:00
|
|
|
// DefaultConfig default config
|
2022-03-22 14:54:43 +03:00
|
|
|
var DefaultConfig = NewConfig()
|
2019-05-31 01:11:13 +03:00
|
|
|
|
2021-08-04 00:37:56 +03:00
|
|
|
// DefaultWatcherMinInterval default min interval for poll changes
|
|
|
|
var DefaultWatcherMinInterval = 5 * time.Second
|
|
|
|
|
2021-09-28 23:43:43 +03:00
|
|
|
// DefaultWatcherMaxInterval default max interval for poll changes
|
2021-08-04 00:37:56 +03:00
|
|
|
var DefaultWatcherMaxInterval = 9 * time.Second
|
2021-08-03 00:24:40 +03:00
|
|
|
|
2020-08-29 18:08:21 +03:00
|
|
|
var (
|
2020-12-13 13:26:44 +03:00
|
|
|
// ErrCodecMissing is returned when codec needed and not specified
|
|
|
|
ErrCodecMissing = errors.New("codec missing")
|
2020-12-13 13:10:04 +03:00
|
|
|
// ErrInvalidStruct is returned when the target struct is invalid
|
|
|
|
ErrInvalidStruct = errors.New("invalid struct specified")
|
2020-12-04 02:28:45 +03:00
|
|
|
// ErrWatcherStopped is returned when source watcher has been stopped
|
|
|
|
ErrWatcherStopped = errors.New("watcher stopped")
|
2021-11-30 07:34:49 +03:00
|
|
|
// ErrWatcherNotImplemented returned when config does not implement watch
|
|
|
|
ErrWatcherNotImplemented = errors.New("watcher not implemented")
|
2020-08-29 18:08:21 +03:00
|
|
|
)
|
|
|
|
|
2019-05-31 01:11:13 +03:00
|
|
|
// Config is an interface abstraction for dynamic configuration
|
|
|
|
type Config interface {
|
2021-06-20 23:57:13 +03:00
|
|
|
// Name returns name of config
|
2021-01-29 16:18:17 +03:00
|
|
|
Name() string
|
2020-03-12 21:13:03 +03:00
|
|
|
// Init the config
|
2023-07-29 00:40:58 +03:00
|
|
|
Init(opts ...options.Option) error
|
2020-03-31 19:13:21 +03:00
|
|
|
// Options in the config
|
|
|
|
Options() Options
|
2020-12-04 02:28:45 +03:00
|
|
|
// Load config from sources
|
2023-07-29 00:40:58 +03:00
|
|
|
Load(context.Context, ...options.Option) error
|
2020-12-04 02:28:45 +03:00
|
|
|
// Save config to sources
|
2023-07-29 00:40:58 +03:00
|
|
|
Save(context.Context, ...options.Option) error
|
2021-08-03 00:24:40 +03:00
|
|
|
// Watch a config for changes
|
2023-07-29 00:40:58 +03:00
|
|
|
Watch(context.Context, ...options.Option) (Watcher, error)
|
2021-06-20 23:57:13 +03:00
|
|
|
// String returns config type name
|
2020-12-04 02:28:45 +03:00
|
|
|
String() string
|
2019-05-31 01:11:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Watcher is the config watcher
|
2021-06-20 23:57:13 +03:00
|
|
|
type Watcher interface {
|
2021-08-03 00:24:40 +03:00
|
|
|
// Next blocks until update happens or error returned
|
|
|
|
Next() (map[string]interface{}, error)
|
|
|
|
// Stop stops watcher
|
2021-06-20 23:57:13 +03:00
|
|
|
Stop() error
|
|
|
|
}
|
2020-12-18 03:37:18 +03:00
|
|
|
|
|
|
|
// Load loads config from config sources
|
2023-07-29 00:40:58 +03:00
|
|
|
func Load(ctx context.Context, cs []Config, opts ...options.Option) error {
|
2020-12-18 03:37:18 +03:00
|
|
|
var err error
|
|
|
|
for _, c := range cs {
|
2020-12-20 23:08:40 +03:00
|
|
|
if err = c.Init(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-07-01 15:56:22 +03:00
|
|
|
if err = c.Load(ctx, opts...); err != nil {
|
2020-12-18 03:37:18 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2021-09-30 01:24:16 +03:00
|
|
|
|
2022-03-22 14:54:43 +03:00
|
|
|
// Validate runs Validate() error func for each struct field
|
|
|
|
func Validate(ctx context.Context, cfg interface{}) error {
|
|
|
|
if cfg == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if v, ok := cfg.(Validator); ok {
|
|
|
|
if err := v.Validate(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sv := reflect.ValueOf(cfg)
|
|
|
|
if sv.Kind() == reflect.Ptr {
|
|
|
|
sv = sv.Elem()
|
|
|
|
}
|
|
|
|
if sv.Kind() != reflect.Struct {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
typ := sv.Type()
|
|
|
|
for idx := 0; idx < typ.NumField(); idx++ {
|
|
|
|
fld := typ.Field(idx)
|
|
|
|
val := sv.Field(idx)
|
|
|
|
if !val.IsValid() || len(fld.PkgPath) != 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if v, ok := val.Interface().(Validator); ok {
|
|
|
|
if err := v.Validate(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch val.Kind() {
|
|
|
|
case reflect.Ptr:
|
|
|
|
if reflect.Indirect(val).Kind() == reflect.Struct {
|
|
|
|
if err := Validate(ctx, val.Interface()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case reflect.Struct:
|
|
|
|
if err := Validate(ctx, val.Interface()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-09-30 01:24:16 +03:00
|
|
|
var (
|
2023-03-14 10:38:24 +03:00
|
|
|
// DefaultBeforeLoad default func that runs before config Load
|
|
|
|
DefaultBeforeLoad = func(ctx context.Context, c Config) error {
|
|
|
|
for _, fn := range c.Options().BeforeLoad {
|
2023-03-16 07:14:21 +03:00
|
|
|
if fn == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2023-03-14 10:38:24 +03:00
|
|
|
if err := fn(ctx, c); err != nil {
|
2023-10-15 00:12:28 +03:00
|
|
|
c.Options().Logger.Error(ctx, c.String()+" BeforeLoad error "+err.Error())
|
2023-03-14 10:38:24 +03:00
|
|
|
if !c.Options().AllowFail {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
// DefaultAfterLoad default func that runs after config Load
|
2021-09-30 01:24:16 +03:00
|
|
|
DefaultAfterLoad = func(ctx context.Context, c Config) error {
|
|
|
|
for _, fn := range c.Options().AfterLoad {
|
2023-03-16 07:14:21 +03:00
|
|
|
if fn == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2021-09-30 01:24:16 +03:00
|
|
|
if err := fn(ctx, c); err != nil {
|
2023-10-15 00:12:28 +03:00
|
|
|
c.Options().Logger.Error(ctx, c.String()+" AfterLoad error "+err.Error())
|
2021-09-30 01:24:16 +03:00
|
|
|
if !c.Options().AllowFail {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2023-03-14 10:38:24 +03:00
|
|
|
// DefaultBeforeSave default func that runs befora config Save
|
|
|
|
DefaultBeforeSave = func(ctx context.Context, c Config) error {
|
|
|
|
for _, fn := range c.Options().BeforeSave {
|
2023-03-16 07:14:21 +03:00
|
|
|
if fn == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2023-03-14 10:38:24 +03:00
|
|
|
if err := fn(ctx, c); err != nil {
|
2023-10-15 00:12:28 +03:00
|
|
|
c.Options().Logger.Error(ctx, c.String()+" BeforeSave error "+err.Error())
|
2023-03-14 10:38:24 +03:00
|
|
|
if !c.Options().AllowFail {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
// DefaultAfterSave default func that runs after config Save
|
2021-09-30 01:24:16 +03:00
|
|
|
DefaultAfterSave = func(ctx context.Context, c Config) error {
|
|
|
|
for _, fn := range c.Options().AfterSave {
|
2023-03-16 07:14:21 +03:00
|
|
|
if fn == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2021-09-30 01:24:16 +03:00
|
|
|
if err := fn(ctx, c); err != nil {
|
2023-10-15 00:12:28 +03:00
|
|
|
c.Options().Logger.Error(ctx, c.String()+" AfterSave error "+err.Error())
|
2021-09-30 01:24:16 +03:00
|
|
|
if !c.Options().AllowFail {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2023-03-14 10:38:24 +03:00
|
|
|
// DefaultBeforeInit default func that runs befora config Init
|
|
|
|
DefaultBeforeInit = func(ctx context.Context, c Config) error {
|
|
|
|
for _, fn := range c.Options().BeforeInit {
|
2023-03-16 07:14:21 +03:00
|
|
|
if fn == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2021-09-30 01:24:16 +03:00
|
|
|
if err := fn(ctx, c); err != nil {
|
2023-10-15 00:12:28 +03:00
|
|
|
c.Options().Logger.Error(ctx, c.String()+" BeforeInit error "+err.Error())
|
2021-09-30 01:24:16 +03:00
|
|
|
if !c.Options().AllowFail {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2023-03-14 10:38:24 +03:00
|
|
|
// DefaultAfterInit default func that runs after config Init
|
|
|
|
DefaultAfterInit = func(ctx context.Context, c Config) error {
|
|
|
|
for _, fn := range c.Options().AfterSave {
|
2023-03-16 07:14:21 +03:00
|
|
|
if fn == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2021-09-30 01:24:16 +03:00
|
|
|
if err := fn(ctx, c); err != nil {
|
2023-10-15 00:12:28 +03:00
|
|
|
c.Options().Logger.Error(ctx, c.String()+" AfterInit error "+err.Error())
|
2021-09-30 01:24:16 +03:00
|
|
|
if !c.Options().AllowFail {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
)
|