diff --git a/config/options.go b/config/options.go index 6a340b994cb..3574e974477 100644 --- a/config/options.go +++ b/config/options.go @@ -61,7 +61,17 @@ 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 { + target[k] = 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..c3769166be0 --- /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": []byte("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": []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 }