config: proper handle AllowFail option

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2023-03-14 13:23:41 +03:00
parent 3950f2ed86
commit 33a7feb970
1 changed files with 16 additions and 8 deletions

View File

@ -24,11 +24,20 @@ func (c *defaultConfig) Init(opts ...Option) error {
for _, o := range opts { for _, o := range opts {
o(&c.opts) o(&c.opts)
} }
if err := DefaultBeforeInit(c.opts.Context, c); err != nil && !c.opts.AllowFail {
return err
}
if err := DefaultAfterInit(c.opts.Context, c); err != nil && !c.opts.AllowFail {
return err
}
return nil return nil
} }
func (c *defaultConfig) Load(ctx context.Context, opts ...LoadOption) error { func (c *defaultConfig) Load(ctx context.Context, opts ...LoadOption) error {
if err := DefaultBeforeLoad(ctx, c); err != nil { if err := DefaultBeforeLoad(ctx, c); err != nil && !c.opts.AllowFail {
return err return err
} }
@ -51,21 +60,20 @@ func (c *defaultConfig) Load(ctx context.Context, opts ...LoadOption) error {
if !c.opts.AllowFail { if !c.opts.AllowFail {
return err return err
} }
return DefaultAfterLoad(ctx, c) if err = DefaultAfterLoad(ctx, c); err != nil && !c.opts.AllowFail {
return err
}
} }
if err = fillValues(reflect.ValueOf(src), c.opts.StructTag); err == nil { if err = fillValues(reflect.ValueOf(src), c.opts.StructTag); err == nil {
err = mergo.Merge(dst, src, mopts...) err = mergo.Merge(dst, src, mopts...)
} }
if err != nil { if err != nil && !c.opts.AllowFail {
c.opts.Logger.Errorf(ctx, "default load error: %v", err) return err
if !c.opts.AllowFail {
return err
}
} }
if err := DefaultAfterLoad(ctx, c); err != nil { if err := DefaultAfterLoad(ctx, c); err != nil && !c.opts.AllowFail {
return err return err
} }