config: add conditions
Some checks failed
lint / lint (pull_request) Successful in 1m28s
pr / test (pull_request) Failing after 1m5s

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2024-01-15 00:46:00 +03:00
parent ed4e9d54b1
commit 47943cfb05
2 changed files with 13 additions and 1 deletions

View File

@ -39,6 +39,10 @@ func (c *defaultConfig) Init(opts ...Option) error {
} }
func (c *defaultConfig) Load(ctx context.Context, opts ...LoadOption) error { func (c *defaultConfig) Load(ctx context.Context, opts ...LoadOption) error {
if c.opts.SkipLoad != nil && c.opts.SkipLoad(ctx, c) {
return nil
}
if err := DefaultBeforeLoad(ctx, c); err != nil && !c.opts.AllowFail { if err := DefaultBeforeLoad(ctx, c); err != nil && !c.opts.AllowFail {
return err return err
} }
@ -291,7 +295,11 @@ func fillValues(valueOf reflect.Value, tname string) error {
return nil return nil
} }
func (c *defaultConfig) Save(ctx context.Context, opts ...SaveOption) error { func (c *defaultConfig) Save(ctx context.Context, _ ...SaveOption) error {
if c.opts.SkipSave != nil && c.opts.SkipSave(ctx, c) {
return nil
}
if err := DefaultBeforeSave(ctx, c); err != nil { if err := DefaultBeforeSave(ctx, c); err != nil {
return err return err
} }

View File

@ -42,6 +42,10 @@ type Options struct {
AfterInit []func(context.Context, Config) error 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
// SkipLoad runs only if condition returns true
SkipLoad func(context.Context, Config) bool
// SkipSave runs only if condition returns true
SkipSave func(context.Context, Config) bool
} }
// Option function signature // Option function signature