Skip to content

Commit

Permalink
support set opts for each key (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
ringsaturn authored Apr 13, 2022
1 parent 49f9bb5 commit 8898935
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
9 changes: 7 additions & 2 deletions oap.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
}
Expand Down
3 changes: 2 additions & 1 deletion oap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"testing"

gomock "github.com/golang/mock/gomock"
agollo "github.com/philchia/agollo/v4"
"github.com/ringsaturn/oap"
)

Expand All @@ -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" {
Expand Down

0 comments on commit 8898935

Please sign in to comment.