MM-27918 In-Product notices support (#15316)
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
43ed6ad690
Коммит
4e9ddd4686
@@ -5,6 +5,7 @@ package config
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"reflect"
|
||||
"strings"
|
||||
|
||||
"github.com/mattermost/mattermost-server/v5/mlog"
|
||||
@@ -198,3 +199,48 @@ func JSONToLogTargetCfg(data []byte) (mlog.LogTargetCfg, error) {
|
||||
}
|
||||
return cfg, nil
|
||||
}
|
||||
|
||||
func GetValueByPath(path []string, obj interface{}) (interface{}, bool) {
|
||||
r := reflect.ValueOf(obj)
|
||||
var val reflect.Value
|
||||
if r.Kind() == reflect.Map {
|
||||
val = r.MapIndex(reflect.ValueOf(path[0]))
|
||||
if val.IsValid() {
|
||||
val = val.Elem()
|
||||
}
|
||||
} else {
|
||||
val = r.FieldByName(path[0])
|
||||
}
|
||||
|
||||
if !val.IsValid() {
|
||||
return nil, false
|
||||
}
|
||||
|
||||
switch {
|
||||
case len(path) == 1:
|
||||
return val.Interface(), true
|
||||
case val.Kind() == reflect.Struct:
|
||||
return GetValueByPath(path[1:], val.Interface())
|
||||
case val.Kind() == reflect.Map:
|
||||
remainingPath := strings.Join(path[1:], ".")
|
||||
mapIter := val.MapRange()
|
||||
for mapIter.Next() {
|
||||
key := mapIter.Key().String()
|
||||
if strings.HasPrefix(remainingPath, key) {
|
||||
i := strings.Count(key, ".") + 2 // number of dots + a dot on each side
|
||||
mapVal := mapIter.Value()
|
||||
// if no sub field path specified, return the object
|
||||
if len(path[i:]) == 0 {
|
||||
return mapVal.Interface(), true
|
||||
}
|
||||
data := mapVal.Interface()
|
||||
if mapVal.Kind() == reflect.Ptr {
|
||||
data = mapVal.Elem().Interface() // if value is a pointer, dereference it
|
||||
}
|
||||
// pass subpath
|
||||
return GetValueByPath(path[i:], data)
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil, false
|
||||
}
|
||||
|
||||
Ссылка в новой задаче
Block a user