2019-05-31 01:11:13 +03:00
|
|
|
package consul
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/hashicorp/consul/api"
|
|
|
|
"github.com/micro/go-micro/config/encoder"
|
|
|
|
)
|
|
|
|
|
2019-06-20 19:25:39 +03:00
|
|
|
type jsonValue interface {
|
|
|
|
Value() interface{}
|
|
|
|
Decode(encoder.Encoder, []byte) (jsonValue, error)
|
|
|
|
}
|
|
|
|
type jsonArrayValue []interface{}
|
|
|
|
type jsonMapValue map[string]interface{}
|
|
|
|
|
|
|
|
func (a jsonArrayValue) Value() interface{} { return a }
|
|
|
|
func (a jsonArrayValue) Decode(e encoder.Encoder, b []byte) (jsonValue, error) {
|
|
|
|
v := jsonArrayValue{}
|
|
|
|
err := e.Decode(b, &v)
|
|
|
|
return v, err
|
|
|
|
}
|
|
|
|
func (m jsonMapValue) Value() interface{} { return m }
|
|
|
|
func (m jsonMapValue) Decode(e encoder.Encoder, b []byte) (jsonValue, error) {
|
|
|
|
v := jsonMapValue{}
|
|
|
|
err := e.Decode(b, &v)
|
|
|
|
return v, err
|
|
|
|
}
|
|
|
|
|
2019-05-31 01:11:13 +03:00
|
|
|
func makeMap(e encoder.Encoder, kv api.KVPairs, stripPrefix string) (map[string]interface{}, error) {
|
2019-06-20 19:25:39 +03:00
|
|
|
|
2019-05-31 01:11:13 +03:00
|
|
|
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, stripPrefix), "/")
|
2019-06-20 19:25:39 +03:00
|
|
|
if pathString == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
var val jsonValue
|
|
|
|
var err error
|
2019-05-31 01:11:13 +03:00
|
|
|
|
|
|
|
// ensure a valid value is stored at this location
|
|
|
|
if len(v.Value) > 0 {
|
2019-06-20 19:25:39 +03:00
|
|
|
// check whether this is an array
|
|
|
|
if v.Value[0] == 91 && v.Value[len(v.Value)-1] == 93 {
|
|
|
|
val = jsonArrayValue{}
|
|
|
|
if val, err = val.Decode(e, v.Value); err != nil {
|
|
|
|
return nil, fmt.Errorf("faild decode value. path: %s, error: %s", pathString, err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
val = jsonMapValue{}
|
|
|
|
if val, err = val.Decode(e, v.Value); err != nil {
|
|
|
|
return nil, fmt.Errorf("faild decode value. path: %s, error: %s", pathString, err)
|
|
|
|
}
|
2019-05-31 01:11:13 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
target := data
|
2019-06-20 19:25:39 +03: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-31 01:11:13 +03:00
|
|
|
}
|
2019-06-20 19:25:39 +03:00
|
|
|
target = target[dir].(map[string]interface{})
|
2019-05-31 01:11:13 +03:00
|
|
|
}
|
|
|
|
|
2019-06-20 19:25:39 +03:00
|
|
|
leafDir := path[len(path)-1]
|
|
|
|
|
2019-05-31 01:11:13 +03:00
|
|
|
// copy over the keys from the value
|
2019-06-20 19:25:39 +03:00
|
|
|
switch val.(type) {
|
|
|
|
case jsonArrayValue:
|
|
|
|
target[leafDir] = val.Value()
|
|
|
|
case jsonMapValue:
|
|
|
|
target[leafDir] = make(map[string]interface{})
|
|
|
|
target = target[leafDir].(map[string]interface{})
|
|
|
|
mapv := val.Value().(jsonMapValue)
|
|
|
|
for k := range mapv {
|
|
|
|
target[k] = mapv[k]
|
|
|
|
}
|
2019-05-31 01:11:13 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return data, nil
|
|
|
|
}
|