Merge pull request 'config: add Before/After Init funcs' (#190) from config-improve into v3
Reviewed-on: #190
This commit is contained in:
commit
3950f2ed86
@ -124,31 +124,7 @@ func Validate(ctx context.Context, cfg interface{}) error {
|
||||
}
|
||||
|
||||
var (
|
||||
// DefaultAfterLoad default func that runs after config load
|
||||
DefaultAfterLoad = func(ctx context.Context, c Config) error {
|
||||
for _, fn := range c.Options().AfterLoad {
|
||||
if err := fn(ctx, c); err != nil {
|
||||
c.Options().Logger.Errorf(ctx, "%s AfterLoad err: %v", c.String(), err)
|
||||
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 err := fn(ctx, c); err != nil {
|
||||
c.Options().Logger.Errorf(ctx, "%s AfterSave err: %v", c.String(), err)
|
||||
if !c.Options().AllowFail {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
// DefaultBeforeLoad default func that runs before config load
|
||||
// DefaultBeforeLoad default func that runs before config Load
|
||||
DefaultBeforeLoad = func(ctx context.Context, c Config) error {
|
||||
for _, fn := range c.Options().BeforeLoad {
|
||||
if err := fn(ctx, c); err != nil {
|
||||
@ -160,7 +136,19 @@ var (
|
||||
}
|
||||
return nil
|
||||
}
|
||||
// DefaultBeforeSave default func that runs befora config save
|
||||
// DefaultAfterLoad default func that runs after config Load
|
||||
DefaultAfterLoad = func(ctx context.Context, c Config) error {
|
||||
for _, fn := range c.Options().AfterLoad {
|
||||
if err := fn(ctx, c); err != nil {
|
||||
c.Options().Logger.Errorf(ctx, "%s AfterLoad err: %v", c.String(), err)
|
||||
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 err := fn(ctx, c); err != nil {
|
||||
@ -172,4 +160,40 @@ var (
|
||||
}
|
||||
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 err := fn(ctx, c); err != nil {
|
||||
c.Options().Logger.Errorf(ctx, "%s AfterSave err: %v", c.String(), err)
|
||||
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 err := fn(ctx, c); err != nil {
|
||||
c.Options().Logger.Errorf(ctx, "%s BeforeInit err: %v", c.String(), err)
|
||||
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 err := fn(ctx, c); err != nil {
|
||||
c.Options().Logger.Errorf(ctx, "%s AfterInit err: %v", c.String(), err)
|
||||
if !c.Options().AllowFail {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
)
|
||||
|
@ -28,14 +28,18 @@ type Options struct {
|
||||
Name string
|
||||
// StructTag name
|
||||
StructTag string
|
||||
// BeforeSave contains slice of funcs that runs before save
|
||||
// BeforeSave contains slice of funcs that runs before Save
|
||||
BeforeSave []func(context.Context, Config) error
|
||||
// AfterLoad contains slice of funcs that runs after load
|
||||
AfterLoad []func(context.Context, Config) error
|
||||
// BeforeLoad contains slice of funcs that runs before load
|
||||
BeforeLoad []func(context.Context, Config) error
|
||||
// AfterSave contains slice of funcs that runs after save
|
||||
// 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
|
||||
}
|
||||
@ -131,6 +135,20 @@ func AllowFail(b bool) Option {
|
||||
}
|
||||
}
|
||||
|
||||
// BeforeInit run funcs before config Init
|
||||
func BeforeInit(fn ...func(context.Context, Config) error) Option {
|
||||
return func(o *Options) {
|
||||
o.BeforeInit = fn
|
||||
}
|
||||
}
|
||||
|
||||
// AfterInit run funcs after config Init
|
||||
func AfterInit(fn ...func(context.Context, Config) error) Option {
|
||||
return func(o *Options) {
|
||||
o.AfterInit = fn
|
||||
}
|
||||
}
|
||||
|
||||
// BeforeLoad run funcs before config load
|
||||
func BeforeLoad(fn ...func(context.Context, Config) error) Option {
|
||||
return func(o *Options) {
|
||||
|
Loading…
Reference in New Issue
Block a user