From c65c85b08b8b5b88c8586fd4075dc73f8f9ddc5b Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Fri, 18 Dec 2020 03:40:34 +0300 Subject: [PATCH] before/after config stuff handled by implementations Signed-off-by: Vasiliy Tolstov --- env.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/env.go b/env.go index f4eb680..f9603a1 100644 --- a/env.go +++ b/env.go @@ -30,12 +30,24 @@ func (c *envConfig) Init(opts ...config.Option) error { } func (c *envConfig) Load(ctx context.Context) error { + for _, fn := range c.opts.BeforeLoad { + if err := fn(ctx, c); err != nil { + return err + } + } + valueOf := reflect.ValueOf(c.opts.Struct) if err := c.fillValues(ctx, valueOf); err != nil { return err } + for _, fn := range c.opts.AfterLoad { + if err := fn(ctx, c); err != nil { + return err + } + } + return nil } @@ -219,6 +231,18 @@ func (c *envConfig) fillValues(ctx context.Context, valueOf reflect.Value) error } func (c *envConfig) 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 }