config: add Before/After Init funcs

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2023-03-14 10:38:24 +03:00
parent bff40bd317
commit 68c1048a7d
2 changed files with 74 additions and 32 deletions

View File

@ -124,31 +124,7 @@ func Validate(ctx context.Context, cfg interface{}) error {
} }
var ( var (
// DefaultAfterLoad default func that runs after config load // DefaultBeforeLoad default func that runs before 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 = func(ctx context.Context, c Config) error { DefaultBeforeLoad = func(ctx context.Context, c Config) error {
for _, fn := range c.Options().BeforeLoad { for _, fn := range c.Options().BeforeLoad {
if err := fn(ctx, c); err != nil { if err := fn(ctx, c); err != nil {
@ -160,7 +136,19 @@ var (
} }
return nil 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 { DefaultBeforeSave = func(ctx context.Context, c Config) error {
for _, fn := range c.Options().BeforeSave { for _, fn := range c.Options().BeforeSave {
if err := fn(ctx, c); err != nil { if err := fn(ctx, c); err != nil {
@ -172,4 +160,40 @@ var (
} }
return nil 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
}
) )

View File

@ -28,14 +28,18 @@ type Options struct {
Name string Name string
// StructTag name // StructTag name
StructTag string 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 BeforeSave []func(context.Context, Config) error
// AfterLoad contains slice of funcs that runs after load // AfterSave contains slice of funcs that runs after Save
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 []func(context.Context, Config) error 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 flag to allow fail in config source
AllowFail bool 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 // BeforeLoad run funcs before config load
func BeforeLoad(fn ...func(context.Context, Config) error) Option { func BeforeLoad(fn ...func(context.Context, Config) error) Option {
return func(o *Options) { return func(o *Options) {