From 8898935a9e21dddc88db1501aa482746002bab5a Mon Sep 17 00:00:00 2001 From: ringsaturn Date: Wed, 13 Apr 2022 20:35:33 +0800 Subject: [PATCH] support set opts for each key (#2) --- oap.go | 9 +++++++-- oap_test.go | 3 ++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/oap.go b/oap.go index 882fd83..3c73c38 100644 --- a/oap.go +++ b/oap.go @@ -8,7 +8,7 @@ import ( "github.com/philchia/agollo/v4" ) -func Decode(ptr interface{}, client agollo.Client, opts ...agollo.OpOption) error { +func Decode(ptr interface{}, client agollo.Client, keyOpts map[string][]agollo.OpOption) error { v := reflect.ValueOf(ptr).Elem() for i := 0; i < v.NumField(); i++ { structField := v.Type().Field(i) @@ -18,7 +18,12 @@ func Decode(ptr interface{}, client agollo.Client, opts ...agollo.OpOption) erro // Ignore empty key continue } - confV := client.GetString(apolloKey, opts...) + var confV string + if opts, ok := keyOpts[apolloKey]; ok { + confV = client.GetString(apolloKey, opts...) + } else { + confV = client.GetString(apolloKey) + } if confV == "" { continue } diff --git a/oap_test.go b/oap_test.go index df01438..7293147 100644 --- a/oap_test.go +++ b/oap_test.go @@ -4,6 +4,7 @@ import ( "testing" gomock "github.com/golang/mock/gomock" + agollo "github.com/philchia/agollo/v4" "github.com/ringsaturn/oap" ) @@ -29,7 +30,7 @@ func TestDo(t *testing.T) { client.EXPECT().GetString(gomock.Eq("boolField")).Return("true") conf := &DemoConfig{} - if err := oap.Decode(conf, client); err != nil { + if err := oap.Decode(conf, client, make(map[string][]agollo.OpOption)); err != nil { panic(err) } if conf.Foo != "bar" {