From 92e6fd036e43dd7120d2adaa23bb1facc953c123 Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Thu, 21 Jan 2021 02:20:06 +0300 Subject: [PATCH] config: merge default not overwrite Signed-off-by: Vasiliy Tolstov --- config/default.go | 11 +++++++++-- go.mod | 1 + go.sum | 4 ++++ 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/config/default.go b/config/default.go index 59f7d230..aef35317 100644 --- a/config/default.go +++ b/config/default.go @@ -6,6 +6,7 @@ import ( "strconv" "strings" + "github.com/imdario/mergo" rutil "github.com/unistack-org/micro/v3/util/reflect" ) @@ -31,9 +32,15 @@ func (c *defaultConfig) Load(ctx context.Context) error { } } - valueOf := reflect.ValueOf(c.opts.Struct) + src, err := rutil.Zero(c.opts.Struct) + if err == nil { + valueOf := reflect.ValueOf(src) + if err = c.fillValues(ctx, valueOf); err == nil { + err = mergo.Merge(c.opts.Struct, src, mergo.WithOverride, mergo.WithTypeCheck, mergo.WithAppendSlice) + } + } - if err := c.fillValues(ctx, valueOf); err != nil && !c.opts.AllowFail { + if err != nil && !c.opts.AllowFail { return err } diff --git a/go.mod b/go.mod index 6746e093..d9c056ec 100644 --- a/go.mod +++ b/go.mod @@ -8,6 +8,7 @@ require ( github.com/ef-ds/deque v1.0.4 github.com/golang/protobuf v1.4.3 github.com/google/uuid v1.1.5 + github.com/imdario/mergo v0.3.11 github.com/kr/text v0.2.0 // indirect github.com/miekg/dns v1.1.35 github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect diff --git a/go.sum b/go.sum index 16a642ad..2169ca48 100644 --- a/go.sum +++ b/go.sum @@ -33,6 +33,8 @@ github.com/google/go-cmp v0.5.0 h1:/QaMHBdZ26BB3SSst0Iwl10Epc+xhTquomWX0oZEB6w= github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/uuid v1.1.5 h1:kxhtnfFVi+rYdOALN0B3k9UT86zVJKfBimRaciULW4I= github.com/google/uuid v1.1.5/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/imdario/mergo v0.3.11 h1:3tnifQM4i+fbajXKBHXWEH+KvNHqojZ778UH75j3bGA= +github.com/imdario/mergo v0.3.11/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= @@ -116,6 +118,8 @@ google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlba gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f h1:BLraFXnmrev5lT+xlilqcH8XK9/i0At2xKjWk4p6zsU= gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU= +gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=