From 47943cfb05e591078b6be358c12d582eb21c1ec4 Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Mon, 15 Jan 2024 00:46:00 +0300 Subject: [PATCH] config: add conditions Signed-off-by: Vasiliy Tolstov --- config/default.go | 10 +++++++++- config/options.go | 4 ++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/config/default.go b/config/default.go index 179fc9ba..12840450 100644 --- a/config/default.go +++ b/config/default.go @@ -39,6 +39,10 @@ func (c *defaultConfig) Init(opts ...Option) 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 { return err } @@ -291,7 +295,11 @@ func fillValues(valueOf reflect.Value, tname string) error { 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 { return err } diff --git a/config/options.go b/config/options.go index 66c86f90..67074bdc 100644 --- a/config/options.go +++ b/config/options.go @@ -42,6 +42,10 @@ type Options struct { AfterInit []func(context.Context, Config) error // AllowFail flag to allow fail in config source 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