From b57c149c7fd19d2cf1542a27cc1cbf0ecba18c34 Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Sun, 20 Dec 2020 00:36:07 +0300 Subject: [PATCH] fix merge Signed-off-by: Vasiliy Tolstov --- file.go | 9 ++++++++- go.mod | 5 ++++- go.sum | 4 ++++ 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/file.go b/file.go index 9dff097..7ac7910 100644 --- a/file.go +++ b/file.go @@ -7,6 +7,7 @@ import ( "io/ioutil" "os" + "github.com/imdario/mergo" "github.com/unistack-org/micro/v3/codec" "github.com/unistack-org/micro/v3/config" ) @@ -62,7 +63,13 @@ func (c *fileConfig) Load(ctx context.Context) error { var buf []byte buf, err = ioutil.ReadAll(io.LimitReader(fp, int64(codec.DefaultMaxMsgSize))) if err == nil { - err = c.opts.Codec.Unmarshal(buf, c.opts.Struct) + src, err := config.Zero(c.opts.Struct) + if err == nil { + err = c.opts.Codec.Unmarshal(buf, src) + if err == nil { + err = mergo.Merge(c.opts.Struct, src, mergo.WithOverride, mergo.WithTypeCheck, mergo.WithAppendSlice) + } + } } if err != nil && !c.opts.AllowFail { return err diff --git a/go.mod b/go.mod index a1429a7..12b29f6 100644 --- a/go.mod +++ b/go.mod @@ -2,4 +2,7 @@ module github.com/unistack-org/micro-config-file go 1.15 -require github.com/unistack-org/micro/v3 v3.0.2-0.20201219202205-150e8ad698bb +require ( + github.com/imdario/mergo v0.3.11 + github.com/unistack-org/micro/v3 v3.0.2-0.20201219211529-b50855855b58 +) diff --git a/go.sum b/go.sum index 50d3b5c..906795c 100644 --- a/go.sum +++ b/go.sum @@ -136,6 +136,8 @@ github.com/hashicorp/golang-lru v0.5.3 h1:YPkqC67at8FYaadspW/6uE0COsBxS2656RLEr8 github.com/hashicorp/golang-lru v0.5.3/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4= github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= github.com/iij/doapi v0.0.0-20190504054126-0bbf12d6d7df/go.mod h1:QMZY7/J/KSQEhKWFeDesPjMj+wCHReeknARU3wqlyN4= +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/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k= github.com/json-iterator/go v1.1.5/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU= github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU= @@ -243,6 +245,8 @@ github.com/unistack-org/micro/v3 v3.0.2-0.20201213102644-92aec349c3d4 h1:SmwEx/E github.com/unistack-org/micro/v3 v3.0.2-0.20201213102644-92aec349c3d4/go.mod h1:0DgOy4OdJxQCDER8YSKitZugd2+1bddrRSNfeooTHDc= github.com/unistack-org/micro/v3 v3.0.2-0.20201219202205-150e8ad698bb h1:cJGF8j30idReDyjRCvEJN5waWCh/AKuiKEqLA8OLMlY= github.com/unistack-org/micro/v3 v3.0.2-0.20201219202205-150e8ad698bb/go.mod h1:0DgOy4OdJxQCDER8YSKitZugd2+1bddrRSNfeooTHDc= +github.com/unistack-org/micro/v3 v3.0.2-0.20201219211529-b50855855b58 h1:cxBL1qxHsugjajAf/N14Lmk8V1KL1LH9rMGnYEokrpI= +github.com/unistack-org/micro/v3 v3.0.2-0.20201219211529-b50855855b58/go.mod h1:0DgOy4OdJxQCDER8YSKitZugd2+1bddrRSNfeooTHDc= github.com/urfave/cli v1.22.1/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0= github.com/vultr/govultr v0.1.4/go.mod h1:9H008Uxr/C4vFNGLqKx232C206GL0PBHzOP0809bGNA= github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f/go.mod h1:N2zxlSyiKSe5eX1tZViRH5QA0qijqEDrYZiPEAiq3wU=