From b3f0be323c1b74ffe393328e85afb509f2d3db0c Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Fri, 18 Dec 2020 03:39:35 +0300 Subject: [PATCH] before/after config stuff handled by implementations Signed-off-by: Vasiliy Tolstov --- consul.go | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/consul.go b/consul.go index e52afa2..fbf684d 100644 --- a/consul.go +++ b/consul.go @@ -81,6 +81,12 @@ func (c *consulConfig) Init(opts ...config.Option) error { } func (c *consulConfig) Load(ctx context.Context) error { + for _, fn := range c.opts.BeforeLoad { + if err := fn(ctx, c); err != nil { + return err + } + } + pair, _, err := c.cli.KV().Get(c.path, nil) if err != nil { return fmt.Errorf("consul path load error: %v", err) @@ -88,10 +94,32 @@ func (c *consulConfig) Load(ctx context.Context) error { return fmt.Errorf("consul path not found %v", ErrPathNotExist) } - return c.opts.Codec.Unmarshal(pair.Value, c.opts.Struct) + if err = c.opts.Codec.Unmarshal(pair.Value, c.opts.Struct); err != nil { + return err + } + + for _, fn := range c.opts.AfterLoad { + if err := fn(ctx, c); err != nil { + return err + } + } + + return nil } func (c *consulConfig) Save(ctx context.Context) error { + for _, fn := range c.opts.BeforeSave { + if err := fn(ctx, c); err != nil { + return err + } + } + + for _, fn := range c.opts.AfterSave { + if err := fn(ctx, c); err != nil { + return err + } + } + return nil }