From 24c962cc4e159b16f12956bb6199f3cd37d8e0c0 Mon Sep 17 00:00:00 2001 From: longXboy Date: Wed, 21 Jul 2021 15:53:25 +0800 Subject: [PATCH 1/2] support expand config keys --- config/options.go | 14 +++++++++++++- config/options_test.go | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 config/options_test.go diff --git a/config/options.go b/config/options.go index 6a340b994cb..914c257a54b 100644 --- a/config/options.go +++ b/config/options.go @@ -61,7 +61,19 @@ func WithLogger(l log.Logger) Option { // to target map[string]interface{} using src.Format codec. func defaultDecoder(src *KeyValue, target map[string]interface{}) error { if src.Format == "" { - target[src.Key] = src.Value + // expand key "aaa.bbb" into map[aaa]map[bbb]interface{} + keys := strings.Split(src.Key, ".") + for i, k := range keys { + if i == len(keys)-1 { + // all kv data without format should be treated as string, + // otherwise there will be problems with json parsing + target[k] = string(src.Value) + } else { + sub := make(map[string]interface{}) + target[k] = sub + target = sub + } + } return nil } if codec := encoding.GetCodec(src.Format); codec != nil { diff --git a/config/options_test.go b/config/options_test.go new file mode 100644 index 00000000000..39b58b2b92d --- /dev/null +++ b/config/options_test.go @@ -0,0 +1,37 @@ +package config + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestDefaultDecoder(t *testing.T) { + src := &KeyValue{ + Key: "service", + Value: []byte("config"), + Format: "", + } + target := make(map[string]interface{}, 0) + err := defaultDecoder(src, target) + assert.Nil(t, err) + assert.Equal(t, map[string]interface{}{ + "service": "config", + }, target) + + src = &KeyValue{ + Key: "service.name.alias", + Value: []byte("2233"), + Format: "", + } + target = make(map[string]interface{}, 0) + err = defaultDecoder(src, target) + assert.Nil(t, err) + assert.Equal(t, map[string]interface{}{ + "service": map[string]interface{}{ + "name": map[string]interface{}{ + "alias": "2233", + }, + }, + }, target) +} From 07147e85fdc9f31379933a9f85036bde5b160a64 Mon Sep 17 00:00:00 2001 From: longXboy Date: Wed, 21 Jul 2021 19:19:33 +0800 Subject: [PATCH 2/2] fix bytes in convertMap --- config/options.go | 4 +--- config/options_test.go | 4 ++-- config/reader.go | 3 +++ 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/config/options.go b/config/options.go index 914c257a54b..3574e974477 100644 --- a/config/options.go +++ b/config/options.go @@ -65,9 +65,7 @@ func defaultDecoder(src *KeyValue, target map[string]interface{}) error { keys := strings.Split(src.Key, ".") for i, k := range keys { if i == len(keys)-1 { - // all kv data without format should be treated as string, - // otherwise there will be problems with json parsing - target[k] = string(src.Value) + target[k] = src.Value } else { sub := make(map[string]interface{}) target[k] = sub diff --git a/config/options_test.go b/config/options_test.go index 39b58b2b92d..c3769166be0 100644 --- a/config/options_test.go +++ b/config/options_test.go @@ -16,7 +16,7 @@ func TestDefaultDecoder(t *testing.T) { err := defaultDecoder(src, target) assert.Nil(t, err) assert.Equal(t, map[string]interface{}{ - "service": "config", + "service": []byte("config"), }, target) src = &KeyValue{ @@ -30,7 +30,7 @@ func TestDefaultDecoder(t *testing.T) { assert.Equal(t, map[string]interface{}{ "service": map[string]interface{}{ "name": map[string]interface{}{ - "alias": "2233", + "alias": []byte("2233"), }, }, }, target) diff --git a/config/reader.go b/config/reader.go index 833508b4033..40e460b3e61 100644 --- a/config/reader.go +++ b/config/reader.go @@ -99,6 +99,9 @@ func convertMap(src interface{}) interface{} { dst[k] = convertMap(v) } return dst + case []byte: + // there will be no binary data in the config data + return string(m) default: return src }