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:
+29
-23
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user