From 68c1048a7d081f097adb3d2f6388c2977aeca3d9 Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Tue, 14 Mar 2023 10:38:24 +0300 Subject: [PATCH] config: add Before/After Init funcs Signed-off-by: Vasiliy Tolstov --- config/config.go | 76 +++++++++++++++++++++++++++++++---------------- config/options.go | 30 +++++++++++++++---- 2 files changed, 74 insertions(+), 32 deletions(-) diff --git a/config/config.go b/config/config.go index 0e29af6e..6469cbc7 100644 --- a/config/config.go +++ b/config/config.go @@ -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 + } ) diff --git a/config/options.go b/config/options.go index ffada114..66c86f90 100644 --- a/config/options.go +++ b/config/options.go @@ -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) { -- 2.45.2