merge instead of overwrite

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2020-12-20 00:26:18 +03:00
parent 2b8c1d6c4a
commit ab6b82304a
4 changed files with 37 additions and 26 deletions

11
env.go
View File

@ -7,6 +7,7 @@ import (
"strconv" "strconv"
"strings" "strings"
"github.com/imdario/mergo"
"github.com/unistack-org/micro/v3/config" "github.com/unistack-org/micro/v3/config"
) )
@ -36,9 +37,15 @@ func (c *envConfig) Load(ctx context.Context) error {
} }
} }
valueOf := reflect.ValueOf(c.opts.Struct) dst, err := config.Zero(c.opts.Struct)
if err == nil {
err = c.fillValues(ctx, reflect.ValueOf(dst))
}
if err != nil && !c.opts.AllowFail {
return err
}
if err := c.fillValues(ctx, valueOf); err != nil && !c.opts.AllowFail { if err = mergo.Merge(c.opts.Struct, dst, mergo.WithOverride, mergo.WithTypeCheck, mergo.WithAppendSlice); err != nil && !c.opts.AllowFail {
return err return err
} }

View File

@ -1,11 +1,12 @@
package env_test package env_test
import ( import (
"github.com/unistack-org/micro/v3/config"
env "github.com/unistack-org/micro-config-env"
"testing"
"context" "context"
"os" "os"
"testing"
env "github.com/unistack-org/micro-config-env"
"github.com/unistack-org/micro/v3/config"
) )
type Config struct { type Config struct {
@ -31,10 +32,9 @@ func TestEnv(t *testing.T) {
} }
if conf.StringValue != "before_load" { if conf.StringValue != "before_load" {
t.Fatalf("something wrong with env config: %v", conf) t.Fatalf("something wrong with env config: %#+v", conf)
} }
os.Setenv("STRING_VALUE", "STRING_VALUE") os.Setenv("STRING_VALUE", "STRING_VALUE")
os.Setenv("BOOL_VALUE", "true") os.Setenv("BOOL_VALUE", "true")
os.Setenv("STRING_SLICE", "STRING_SLICE1,STRING_SLICE2;STRING_SLICE3") os.Setenv("STRING_SLICE", "STRING_SLICE1,STRING_SLICE2;STRING_SLICE3")
@ -46,23 +46,24 @@ func TestEnv(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
if conf.StringValue != "STRING_VALUE" { if conf.StringValue != "STRING_VALUE" {
t.Fatalf("something wrong with env config: %v", conf) t.Fatalf("something wrong with env config: %#+v", conf)
} }
if !conf.BoolValue { if !conf.BoolValue {
t.Fatalf("something wrong with env config: %v", conf) t.Fatalf("something wrong with env config: %#+v", conf)
} }
if len(conf.StringSlice) != 3 { if len(conf.StringSlice) != 3 {
t.Fatalf("something wrong with env config: %v", conf) t.Fatalf("something wrong with env config: %#+v", conf.StringSlice)
} }
if len(conf.MapStringValue) != 2 { if len(conf.MapStringValue) != 2 {
t.Fatalf("something wrong with env config: %v", conf) t.Fatalf("something wrong with env config: %#+v", conf.MapStringValue)
} }
if len(conf.MapIntValue) != 2 { if len(conf.MapIntValue) != 2 {
t.Fatalf("something wrong with env config: %v", conf) t.Fatalf("something wrong with env config: %#+v", conf.MapIntValue)
} }
t.Logf("cfg %#+v", conf)
} }

5
go.mod
View File

@ -2,4 +2,7 @@ module github.com/unistack-org/micro-config-env
go 1.15 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
)

8
go.sum
View File

@ -134,6 +134,8 @@ github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ
github.com/hashicorp/golang-lru v0.5.3/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4= 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/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/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/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.5/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU= github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
@ -235,10 +237,8 @@ github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5
github.com/timewasted/linode v0.0.0-20160829202747-37e84520dcf7/go.mod h1:imsgLplxEC/etjIhdr3dNzV3JeT27LbVu5pYWm0JCBY= github.com/timewasted/linode v0.0.0-20160829202747-37e84520dcf7/go.mod h1:imsgLplxEC/etjIhdr3dNzV3JeT27LbVu5pYWm0JCBY=
github.com/transip/gotransip v0.0.0-20190812104329-6d8d9179b66f/go.mod h1:i0f4R4o2HM0m3DZYQWsj6/MEowD57VzoH0v3d7igeFY= github.com/transip/gotransip v0.0.0-20190812104329-6d8d9179b66f/go.mod h1:i0f4R4o2HM0m3DZYQWsj6/MEowD57VzoH0v3d7igeFY=
github.com/uber-go/atomic v1.3.2/go.mod h1:/Ct5t2lcmbJ4OSe/waGBoaVvVqtO0bmtfVNex1PFV8g= github.com/uber-go/atomic v1.3.2/go.mod h1:/Ct5t2lcmbJ4OSe/waGBoaVvVqtO0bmtfVNex1PFV8g=
github.com/unistack-org/micro/v3 v3.0.2-0.20201213101738-2dcd30b21c0f h1:VTnea0NAgE0LXQNBpiemP/O6G0Uns6sl5KkyeAKodEU= 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.20201213101738-2dcd30b21c0f/go.mod h1:0DgOy4OdJxQCDER8YSKitZugd2+1bddrRSNfeooTHDc= github.com/unistack-org/micro/v3 v3.0.2-0.20201219211529-b50855855b58/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/urfave/cli v1.22.1/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0= 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/vultr/govultr v0.1.4/go.mod h1:9H008Uxr/C4vFNGLqKx232C206GL0PBHzOP0809bGNA=
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f/go.mod h1:N2zxlSyiKSe5eX1tZViRH5QA0qijqEDrYZiPEAiq3wU= github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f/go.mod h1:N2zxlSyiKSe5eX1tZViRH5QA0qijqEDrYZiPEAiq3wU=