add readValue (#1161)

pull/1163/head
Kagaya 4 years ago committed by GitHub
parent 26e94aa2ad
commit 9280af7165
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 52
      config/reader.go

@ -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)

Loading…
Cancel
Save