1
0
mirror of https://github.com/go-kratos/kratos.git synced 2026-05-22 10:15:24 +02:00

add readValue (#1161)

This commit is contained in:
Kagaya
2021-07-07 23:16:30 +08:00
committed by GitHub
parent 26e94aa2ad
commit 9280af7165
+29 -23
View File
@@ -48,29 +48,7 @@ func (r *reader) Merge(kvs ...*KeyValue) error {
}
func (r *reader) Value(path string) (Value, bool) {
var (
next = r.values
keys = strings.Split(path, ".")
last = len(keys) - 1
)
for idx, key := range keys {
value, ok := next[key]
if !ok {
return nil, false
}
if idx == last {
av := &atomicValue{}
av.Store(value)
return av, true
}
switch vm := value.(type) {
case map[string]interface{}:
next = vm
default:
return nil, false
}
}
return nil, false
return readValue(r.values, path)
}
func (r *reader) Source() ([]byte, error) {
@@ -114,6 +92,34 @@ func convertMap(src interface{}) interface{} {
}
}
// readValue read Value in given map[string]interface{}
// by the given path, will return false if not found.
func readValue(values map[string]interface{}, path string) (Value, bool) {
var (
next = values
keys = strings.Split(path, ".")
last = len(keys) - 1
)
for idx, key := range keys {
value, ok := next[key]
if !ok {
return nil, false
}
if idx == last {
av := &atomicValue{}
av.Store(value)
return av, true
}
switch vm := value.(type) {
case map[string]interface{}:
next = vm
default:
return nil, false
}
}
return nil, false
}
func marshalJSON(v interface{}) ([]byte, error) {
if m, ok := v.(proto.Message); ok {
return protojson.MarshalOptions{EmitUnpopulated: true}.Marshal(m)