From 6f44125bdf141cc605919157c22de6da0d464ba9 Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Mon, 21 Jun 2021 16:09:28 +0300 Subject: [PATCH] update Signed-off-by: Vasiliy Tolstov --- env.go | 16 +++++++++++++--- go.mod | 2 +- go.sum | 6 +++--- 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/env.go b/env.go index db5b253..ae12176 100644 --- a/env.go +++ b/env.go @@ -32,13 +32,22 @@ func (c *envConfig) Init(opts ...config.Option) error { return nil } -func (c *envConfig) Load(ctx context.Context) error { +func (c *envConfig) Load(ctx context.Context, opts ...config.LoadOption) error { for _, fn := range c.opts.BeforeLoad { if err := fn(ctx, c); err != nil && !c.opts.AllowFail { return err } } + options := config.NewLoadOptions(opts...) + mopts := []func(*mergo.Config){mergo.WithTypeCheck} + if options.Override { + mopts = append(mopts, mergo.WithOverride) + } + if options.Append { + mopts = append(mopts, mergo.WithAppendSlice) + } + src, err := rutil.Zero(c.opts.Struct) if err == nil { err = c.fillValues(ctx, reflect.ValueOf(src)) @@ -47,7 +56,8 @@ func (c *envConfig) Load(ctx context.Context) error { return err } - if err = mergo.Merge(c.opts.Struct, src, mergo.WithOverride, mergo.WithTypeCheck, mergo.WithAppendSlice); err != nil && !c.opts.AllowFail { + err = mergo.Merge(c.opts.Struct, src, mopts...) + if err != nil && !c.opts.AllowFail { return err } @@ -290,7 +300,7 @@ func (c *envConfig) fillValues(ctx context.Context, valueOf reflect.Value) error return nil } -func (c *envConfig) Save(ctx context.Context) error { +func (c *envConfig) Save(ctx context.Context, opts ...config.SaveOption) error { for _, fn := range c.opts.BeforeSave { if err := fn(ctx, c); err != nil && !c.opts.AllowFail { return err diff --git a/go.mod b/go.mod index 7cd8c6a..fab22d2 100644 --- a/go.mod +++ b/go.mod @@ -4,5 +4,5 @@ go 1.16 require ( github.com/imdario/mergo v0.3.12 - github.com/unistack-org/micro/v3 v3.3.17 + github.com/unistack-org/micro/v3 v3.4.0 ) diff --git a/go.sum b/go.sum index d490443..3e10b38 100644 --- a/go.sum +++ b/go.sum @@ -5,9 +5,9 @@ github.com/imdario/mergo v0.3.12 h1:b6R2BslTbIEToALKP7LxUvijTsNI9TAe80pLWN2g/HU= github.com/imdario/mergo v0.3.12/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA= github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ= github.com/silas/dag v0.0.0-20210121180416-41cf55125c34/go.mod h1:7RTUFBdIRC9nZ7/3RyRNH1bdqIShrDejd1YbLwgPS+I= -github.com/unistack-org/micro/v3 v3.3.17 h1:WcyS7InP0DlS/JpRQGLh5sG6VstkdHJbgpMp+gmHmwg= -github.com/unistack-org/micro/v3 v3.3.17/go.mod h1:022EOEZZ789hZY3yB5ZSMXU6jLiadBgcNB/cpediV3c= -golang.org/x/net v0.0.0-20210423184538-5f58ad60dda6/go.mod h1:OJAsFXCWl8Ukc7SiCT/9KSuxbyM7479/AVlXFRxuMCk= +github.com/unistack-org/micro/v3 v3.4.0 h1:z9F3lgAb2j4cZ1ib5qBj66JPYUAzR4sNIJqUDjVwyVQ= +github.com/unistack-org/micro/v3 v3.4.0/go.mod h1:LXmPfbJnJNvL0kQs8HfnkV3Wya2Wb+C7keVq++RCZnk= +golang.org/x/net v0.0.0-20210510120150-4163338589ed/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=