7acd249147
`config.NewConfig()` with consul source will both read from consul and watch consul for changes. Hence, the `prefix` is used in these 2 cases: - read case: it is used to strip path based on the `KVPair` returned from consul `kv.List()` method - watch case: it is used as the `key` of watch query (`keyprefix` type) So for *watch case*, the `key` is leagal to be `/` for watching change on root. While for *read case*, because `KVPair.Key` is always stripped off the leading slash, so if user specified some `prefix` with leading slash, we should strip it also. An extream case would be: user want's to read & watch node in root dir. One would specify `prefix` as `/`, and it should work then.
50 lines
1.2 KiB
Go
50 lines
1.2 KiB
Go
package consul
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
|
|
"github.com/hashicorp/consul/api"
|
|
"github.com/micro/go-micro/config/encoder"
|
|
)
|
|
|
|
func makeMap(e encoder.Encoder, kv api.KVPairs, stripPrefix string) (map[string]interface{}, error) {
|
|
data := make(map[string]interface{})
|
|
|
|
// consul guarantees lexicographic order, so no need to sort
|
|
for _, v := range kv {
|
|
pathString := strings.TrimPrefix(strings.TrimPrefix(v.Key, strings.TrimPrefix(stripPrefix, "/")), "/")
|
|
var val map[string]interface{}
|
|
|
|
// ensure a valid value is stored at this location
|
|
if len(v.Value) > 0 {
|
|
if err := e.Decode(v.Value, &val); err != nil {
|
|
return nil, fmt.Errorf("faild decode value. path: %s, error: %s", pathString, err)
|
|
}
|
|
}
|
|
|
|
// set target at the root
|
|
target := data
|
|
|
|
// then descend to the target location, creating as we go, if need be
|
|
if pathString != "" {
|
|
path := strings.Split(pathString, "/")
|
|
// find (or create) the location we want to put this value at
|
|
for _, dir := range path {
|
|
if _, ok := target[dir]; !ok {
|
|
target[dir] = make(map[string]interface{})
|
|
}
|
|
target = target[dir].(map[string]interface{})
|
|
}
|
|
|
|
}
|
|
|
|
// copy over the keys from the value
|
|
for k := range val {
|
|
target[k] = val[k]
|
|
}
|
|
}
|
|
|
|
return data, nil
|
|
}
|