From 8f36e9db7923f21519795ab2c6a20a0455407fed Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Tue, 14 Mar 2023 13:58:03 +0300 Subject: [PATCH] config: proper handle AllowFail option Signed-off-by: Vasiliy Tolstov --- env.go | 26 ++++++++++---------------- go.mod | 2 +- go.sum | 4 ++-- 3 files changed, 13 insertions(+), 19 deletions(-) diff --git a/env.go b/env.go index d18ce5f..ce3ea92 100644 --- a/env.go +++ b/env.go @@ -28,11 +28,11 @@ func (c *envConfig) Init(opts ...config.Option) error { o(&c.opts) } - if err := config.DefaultBeforeInit(c.opts.Context, c); err != nil { + if err := config.DefaultBeforeInit(c.opts.Context, c); err != nil && !c.opts.AllowFail { return err } - if err := config.DefaultAfterInit(c.opts.Context, c); err != nil { + if err := config.DefaultAfterInit(c.opts.Context, c); err != nil && !c.opts.AllowFail { return err } @@ -40,7 +40,7 @@ func (c *envConfig) Init(opts ...config.Option) error { } func (c *envConfig) Load(ctx context.Context, opts ...config.LoadOption) error { - if err := config.DefaultBeforeLoad(ctx, c); err != nil { + if err := config.DefaultBeforeLoad(ctx, c); err != nil && !c.opts.AllowFail { return err } @@ -60,14 +60,11 @@ func (c *envConfig) Load(ctx context.Context, opts ...config.LoadOption) error { } } - if err != nil { - c.opts.Logger.Errorf(c.opts.Context, "env load err: %v", err) - if !c.opts.AllowFail { - return err - } + if err != nil && !c.opts.AllowFail { + return err } - if err := config.DefaultAfterLoad(ctx, c); err != nil { + if err := config.DefaultAfterLoad(ctx, c); err != nil && !c.opts.AllowFail { return err } @@ -314,18 +311,15 @@ func fillValues(ctx context.Context, valueOf reflect.Value, structTag string) er } func (c *envConfig) Save(ctx context.Context, opts ...config.SaveOption) error { - if err := config.DefaultBeforeSave(ctx, c); err != nil { + if err := config.DefaultBeforeSave(ctx, c); err != nil && !c.opts.AllowFail { return err } - if err := c.setValues(ctx, reflect.ValueOf(c.opts.Struct)); err != nil { - c.opts.Logger.Errorf(c.opts.Context, "env save error: %v", err) - if !c.opts.AllowFail { - return err - } + if err := c.setValues(ctx, reflect.ValueOf(c.opts.Struct)); err != nil && !c.opts.AllowFail { + return err } - if err := config.DefaultAfterSave(ctx, c); err != nil { + if err := config.DefaultAfterSave(ctx, c); err != nil && !c.opts.AllowFail { return err } diff --git a/go.mod b/go.mod index 05315e6..9e308ad 100644 --- a/go.mod +++ b/go.mod @@ -4,5 +4,5 @@ go 1.18 require ( github.com/imdario/mergo v0.3.13 - go.unistack.org/micro/v3 v3.10.15 + go.unistack.org/micro/v3 v3.10.16 ) diff --git a/go.sum b/go.sum index 3c24125..8ff5437 100644 --- a/go.sum +++ b/go.sum @@ -2,8 +2,8 @@ github.com/imdario/mergo v0.3.13 h1:lFzP57bqS/wsqKssCGmtLAb8A0wKjLGrve2q3PPVcBk= github.com/imdario/mergo v0.3.13/go.mod h1:4lJ1jqUDcsbIECGy0RUJAXNIhg+6ocWgb1ALK2O4oXg= github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ= github.com/silas/dag v0.0.0-20211117232152-9d50aa809f35/go.mod h1:7RTUFBdIRC9nZ7/3RyRNH1bdqIShrDejd1YbLwgPS+I= -go.unistack.org/micro/v3 v3.10.15 h1:vliCUhERMuDB08+XX5V6I+9YZl4jPUaBo3S+Jx1JLSo= -go.unistack.org/micro/v3 v3.10.15/go.mod h1:uMAc0U/x7dmtICCrblGf0ZLgYegu3VwQAquu+OFCw1Q= +go.unistack.org/micro/v3 v3.10.16 h1:2er/SKKYbV60M+UuJM4eYCF0MZYAIq/yNUrAbTfgq8Q= +go.unistack.org/micro/v3 v3.10.16/go.mod h1:uMAc0U/x7dmtICCrblGf0ZLgYegu3VwQAquu+OFCw1Q= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.0/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=