2019-05-30 23:11:13 +01:00
|
|
|
package consul
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/hashicorp/consul/api"
|
|
|
|
"github.com/micro/go-micro/config/encoder"
|
|
|
|
)
|
|
|
|
|
2019-06-21 15:30:45 +08:00
|
|
|
type configValue interface {
|
2019-06-21 00:25:39 +08:00
|
|
|
Value() interface{}
|
2019-06-21 15:30:45 +08:00
|
|
|
Decode(encoder.Encoder, []byte) error
|
|
|
|
}
|
|
|
|
type configArrayValue struct {
|
|
|
|
v []interface{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *configArrayValue) Value() interface{} { return a.v }
|
|
|
|
func (a *configArrayValue) Decode(e encoder.Encoder, b []byte) error {
|
|
|
|
return e.Decode(b, &a.v)
|
2019-06-21 00:25:39 +08:00
|
|
|
}
|
|
|
|
|
2019-06-21 15:30:45 +08:00
|
|
|
type configMapValue struct {
|
|
|
|
v map[string]interface{}
|
2019-06-21 00:25:39 +08:00
|
|
|
}
|
2019-06-21 15:30:45 +08:00
|
|
|
|
|
|
|
func (m *configMapValue) Value() interface{} { return m.v }
|
|
|
|
func (m *configMapValue) Decode(e encoder.Encoder, b []byte) error {
|
|
|
|
return e.Decode(b, &m.v)
|
2019-06-21 00:25:39 +08:00
|
|
|
}
|
|
|
|
|
2019-05-30 23:11:13 +01:00
|
|
|
func makeMap(e encoder.Encoder, kv api.KVPairs, stripPrefix string) (map[string]interface{}, error) {
|
2019-06-21 00:25:39 +08:00
|
|
|
|
2019-05-30 23:11:13 +01:00
|
|
|
data := make(map[string]interface{})
|
|
|
|
|
|
|
|
// consul guarantees lexicographic order, so no need to sort
|
|
|
|
for _, v := range kv {
|
2019-06-21 16:35:48 +08:00
|
|
|
pathString := strings.TrimPrefix(strings.TrimPrefix(v.Key, strings.TrimPrefix(stripPrefix, "/")), "/")
|
2019-06-21 00:25:39 +08:00
|
|
|
if pathString == "" {
|
|
|
|
continue
|
|
|
|
}
|
2019-06-21 15:30:45 +08:00
|
|
|
var val configValue
|
2019-06-21 00:25:39 +08:00
|
|
|
var err error
|
2019-05-30 23:11:13 +01:00
|
|
|
|
|
|
|
// ensure a valid value is stored at this location
|
|
|
|
if len(v.Value) > 0 {
|
2019-06-21 15:30:45 +08:00
|
|
|
// try to decode into map value or array value
|
|
|
|
arrayV := &configArrayValue{v: []interface{}{}}
|
|
|
|
mapV := &configMapValue{v: map[string]interface{}{}}
|
|
|
|
switch {
|
|
|
|
case arrayV.Decode(e, v.Value) == nil:
|
|
|
|
val = arrayV
|
|
|
|
case mapV.Decode(e, v.Value) == nil:
|
|
|
|
val = mapV
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("faild decode value. path: %s, error: %s", pathString, err)
|
2019-05-30 23:11:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// set target at the root
|
|
|
|
target := data
|
2019-06-21 00:25:39 +08:00
|
|
|
path := strings.Split(pathString, "/")
|
|
|
|
// find (or create) the leaf node we want to put this value at
|
|
|
|
for _, dir := range path[:len(path)-1] {
|
|
|
|
if _, ok := target[dir]; !ok {
|
|
|
|
target[dir] = make(map[string]interface{})
|
2019-05-30 23:11:13 +01:00
|
|
|
}
|
2019-06-21 00:25:39 +08:00
|
|
|
target = target[dir].(map[string]interface{})
|
2019-05-30 23:11:13 +01:00
|
|
|
}
|
|
|
|
|
2019-06-21 00:25:39 +08:00
|
|
|
leafDir := path[len(path)-1]
|
|
|
|
|
2019-05-30 23:11:13 +01:00
|
|
|
// copy over the keys from the value
|
2019-06-21 00:25:39 +08:00
|
|
|
switch val.(type) {
|
2019-06-21 15:30:45 +08:00
|
|
|
case *configArrayValue:
|
2019-06-21 00:25:39 +08:00
|
|
|
target[leafDir] = val.Value()
|
2019-06-21 15:30:45 +08:00
|
|
|
case *configMapValue:
|
2019-06-21 00:25:39 +08:00
|
|
|
target[leafDir] = make(map[string]interface{})
|
|
|
|
target = target[leafDir].(map[string]interface{})
|
2019-06-21 15:30:45 +08:00
|
|
|
mapv := val.Value().(map[string]interface{})
|
2019-06-21 00:25:39 +08:00
|
|
|
for k := range mapv {
|
|
|
|
target[k] = mapv[k]
|
|
|
|
}
|
2019-05-30 23:11:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return data, nil
|
|
|
|
}
|