config: fix Default funcs #192

Merged
vtolstov merged 1 commits from config-fix into v3 2023-03-15 22:53:21 +03:00

View File

@ -126,6 +126,9 @@ func Validate(ctx context.Context, cfg interface{}) error {
var ( var (
// DefaultBeforeLoad default func that runs before config Load // DefaultBeforeLoad default func that runs before config Load
DefaultBeforeLoad = func(ctx context.Context, c Config) error { DefaultBeforeLoad = func(ctx context.Context, c Config) error {
if c.Options().BeforeLoad == nil {
return nil
}
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 {
c.Options().Logger.Errorf(ctx, "%s BeforeLoad err: %v", c.String(), err) c.Options().Logger.Errorf(ctx, "%s BeforeLoad err: %v", c.String(), err)
@ -138,6 +141,9 @@ var (
} }
// DefaultAfterLoad default func that runs after config Load // DefaultAfterLoad default func that runs after config Load
DefaultAfterLoad = func(ctx context.Context, c Config) error { DefaultAfterLoad = func(ctx context.Context, c Config) error {
if c.Options().AfterLoad == nil {
return nil
}
for _, fn := range c.Options().AfterLoad { for _, fn := range c.Options().AfterLoad {
if err := fn(ctx, c); err != nil { if err := fn(ctx, c); err != nil {
c.Options().Logger.Errorf(ctx, "%s AfterLoad err: %v", c.String(), err) c.Options().Logger.Errorf(ctx, "%s AfterLoad err: %v", c.String(), err)
@ -150,6 +156,9 @@ var (
} }
// DefaultBeforeSave default func that runs befora config Save // DefaultBeforeSave default func that runs befora config Save
DefaultBeforeSave = func(ctx context.Context, c Config) error { DefaultBeforeSave = func(ctx context.Context, c Config) error {
if c.Options().BeforeSave == nil {
return nil
}
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 {
c.Options().Logger.Errorf(ctx, "%s BeforeSave err: %v", c.String(), err) c.Options().Logger.Errorf(ctx, "%s BeforeSave err: %v", c.String(), err)
@ -162,6 +171,9 @@ var (
} }
// DefaultAfterSave default func that runs after config Save // DefaultAfterSave default func that runs after config Save
DefaultAfterSave = func(ctx context.Context, c Config) error { DefaultAfterSave = func(ctx context.Context, c Config) error {
if c.Options().AfterSave == nil {
return nil
}
for _, fn := range c.Options().AfterSave { for _, fn := range c.Options().AfterSave {
if err := fn(ctx, c); err != nil { if err := fn(ctx, c); err != nil {
c.Options().Logger.Errorf(ctx, "%s AfterSave err: %v", c.String(), err) c.Options().Logger.Errorf(ctx, "%s AfterSave err: %v", c.String(), err)
@ -174,6 +186,9 @@ var (
} }
// DefaultBeforeInit default func that runs befora config Init // DefaultBeforeInit default func that runs befora config Init
DefaultBeforeInit = func(ctx context.Context, c Config) error { DefaultBeforeInit = func(ctx context.Context, c Config) error {
if c.Options().BeforeInit == nil {
return nil
}
for _, fn := range c.Options().BeforeInit { for _, fn := range c.Options().BeforeInit {
if err := fn(ctx, c); err != nil { if err := fn(ctx, c); err != nil {
c.Options().Logger.Errorf(ctx, "%s BeforeInit err: %v", c.String(), err) c.Options().Logger.Errorf(ctx, "%s BeforeInit err: %v", c.String(), err)
@ -186,6 +201,9 @@ var (
} }
// DefaultAfterInit default func that runs after config Init // DefaultAfterInit default func that runs after config Init
DefaultAfterInit = func(ctx context.Context, c Config) error { DefaultAfterInit = func(ctx context.Context, c Config) error {
if c.Options().AfterInit == nil {
return nil
}
for _, fn := range c.Options().AfterSave { for _, fn := range c.Options().AfterSave {
if err := fn(ctx, c); err != nil { if err := fn(ctx, c); err != nil {
c.Options().Logger.Errorf(ctx, "%s AfterInit err: %v", c.String(), err) c.Options().Logger.Errorf(ctx, "%s AfterInit err: %v", c.String(), err)