variable rename to abstract encoder

This commit is contained in:
magodo 2019-06-21 15:30:45 +08:00
parent 92b998c3ab
commit 1983b4ae92

View File

@ -8,24 +8,26 @@ import (
"github.com/micro/go-micro/config/encoder" "github.com/micro/go-micro/config/encoder"
) )
type jsonValue interface { type configValue interface {
Value() interface{} Value() interface{}
Decode(encoder.Encoder, []byte) (jsonValue, error) Decode(encoder.Encoder, []byte) error
}
type configArrayValue struct {
v []interface{}
} }
type jsonArrayValue []interface{}
type jsonMapValue map[string]interface{}
func (a jsonArrayValue) Value() interface{} { return a } func (a *configArrayValue) Value() interface{} { return a.v }
func (a jsonArrayValue) Decode(e encoder.Encoder, b []byte) (jsonValue, error) { func (a *configArrayValue) Decode(e encoder.Encoder, b []byte) error {
v := jsonArrayValue{} return e.Decode(b, &a.v)
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) { type configMapValue struct {
v := jsonMapValue{} v map[string]interface{}
err := e.Decode(b, &v) }
return v, err
func (m *configMapValue) Value() interface{} { return m.v }
func (m *configMapValue) Decode(e encoder.Encoder, b []byte) error {
return e.Decode(b, &m.v)
} }
func makeMap(e encoder.Encoder, kv api.KVPairs, stripPrefix string) (map[string]interface{}, error) { func makeMap(e encoder.Encoder, kv api.KVPairs, stripPrefix string) (map[string]interface{}, error) {
@ -38,23 +40,22 @@ func makeMap(e encoder.Encoder, kv api.KVPairs, stripPrefix string) (map[string]
if pathString == "" { if pathString == "" {
continue continue
} }
var val jsonValue var val configValue
var err error var err error
// ensure a valid value is stored at this location // ensure a valid value is stored at this location
if len(v.Value) > 0 { if len(v.Value) > 0 {
// check whether this is an array // try to decode into map value or array value
if v.Value[0] == 91 && v.Value[len(v.Value)-1] == 93 { arrayV := &configArrayValue{v: []interface{}{}}
val = jsonArrayValue{} mapV := &configMapValue{v: map[string]interface{}{}}
if val, err = val.Decode(e, v.Value); err != nil { 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) 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)
}
}
} }
target := data target := data
@ -71,12 +72,12 @@ func makeMap(e encoder.Encoder, kv api.KVPairs, stripPrefix string) (map[string]
// copy over the keys from the value // copy over the keys from the value
switch val.(type) { switch val.(type) {
case jsonArrayValue: case *configArrayValue:
target[leafDir] = val.Value() target[leafDir] = val.Value()
case jsonMapValue: case *configMapValue:
target[leafDir] = make(map[string]interface{}) target[leafDir] = make(map[string]interface{})
target = target[leafDir].(map[string]interface{}) target = target[leafDir].(map[string]interface{})
mapv := val.Value().(jsonMapValue) mapv := val.Value().(map[string]interface{})
for k := range mapv { for k := range mapv {
target[k] = mapv[k] target[k] = mapv[k]
} }