MM-16439: Print config ordered by key alphabetically for easy diff. (#11349)
* MM-16439: Print config ordered by key alphabetically for easy diff. * Clarify comment.
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
e3504398c7
Коммит
0d8377cc59
@@ -203,7 +203,7 @@ func printConfigValues(configMap map[string]interface{}, configSetting []string,
|
|||||||
switch value.Kind() {
|
switch value.Kind() {
|
||||||
case reflect.Map:
|
case reflect.Map:
|
||||||
if len(configSetting) == 1 {
|
if len(configSetting) == 1 {
|
||||||
return printMap(value, 0), nil
|
return printStringMap(value, 0), nil
|
||||||
}
|
}
|
||||||
return printConfigValues(res.(map[string]interface{}), configSetting[1:], name)
|
return printConfigValues(res.(map[string]interface{}), configSetting[1:], name)
|
||||||
default:
|
default:
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/mattermost/mattermost-server/mlog"
|
"github.com/mattermost/mattermost-server/mlog"
|
||||||
@@ -62,14 +63,25 @@ func structToMap(t interface{}) map[string]interface{} {
|
|||||||
// prettyPrintMap will return a prettyPrint version of a given map
|
// prettyPrintMap will return a prettyPrint version of a given map
|
||||||
func prettyPrintMap(configMap map[string]interface{}) string {
|
func prettyPrintMap(configMap map[string]interface{}) string {
|
||||||
value := reflect.ValueOf(configMap)
|
value := reflect.ValueOf(configMap)
|
||||||
return printMap(value, 0)
|
return printStringMap(value, 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
// printMap takes a reflect.Value and prints it out, recursively if it's a map with the given tab settings
|
// printStringMap takes a reflect.Value and prints it out alphabetically based on key values, which must be strings.
|
||||||
func printMap(value reflect.Value, tabVal int) string {
|
// This is done recursively if it's a map, and uses the given tab settings.
|
||||||
|
func printStringMap(value reflect.Value, tabVal int) string {
|
||||||
out := &bytes.Buffer{}
|
out := &bytes.Buffer{}
|
||||||
|
|
||||||
for _, key := range value.MapKeys() {
|
var sortedKeys []string
|
||||||
|
stringToKeyMap := make(map[string]reflect.Value)
|
||||||
|
for _, k := range value.MapKeys() {
|
||||||
|
sortedKeys = append(sortedKeys, k.String())
|
||||||
|
stringToKeyMap[k.String()] = k
|
||||||
|
}
|
||||||
|
|
||||||
|
sort.Strings(sortedKeys)
|
||||||
|
|
||||||
|
for _, keyString := range sortedKeys {
|
||||||
|
key := stringToKeyMap[keyString]
|
||||||
val := value.MapIndex(key)
|
val := value.MapIndex(key)
|
||||||
if newVal, ok := val.Interface().(map[string]interface{}); !ok {
|
if newVal, ok := val.Interface().(map[string]interface{}); !ok {
|
||||||
fmt.Fprintf(out, "%s", strings.Repeat("\t", tabVal))
|
fmt.Fprintf(out, "%s", strings.Repeat("\t", tabVal))
|
||||||
@@ -79,7 +91,7 @@ func printMap(value reflect.Value, tabVal int) string {
|
|||||||
fmt.Fprintf(out, "%v:\n", key.Interface())
|
fmt.Fprintf(out, "%v:\n", key.Interface())
|
||||||
// going one level in, increase the tab
|
// going one level in, increase the tab
|
||||||
tabVal++
|
tabVal++
|
||||||
fmt.Fprintf(out, "%s", printMap(reflect.ValueOf(newVal), tabVal))
|
fmt.Fprintf(out, "%s", printStringMap(reflect.ValueOf(newVal), tabVal))
|
||||||
// coming back one level, decrease the tab
|
// coming back one level, decrease the tab
|
||||||
tabVal--
|
tabVal--
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -139,7 +139,7 @@ func TestPrintMap(t *testing.T) {
|
|||||||
|
|
||||||
for _, test := range cases {
|
for _, test := range cases {
|
||||||
t.Run(test.Name, func(t *testing.T) {
|
t.Run(test.Name, func(t *testing.T) {
|
||||||
res := printMap(test.Input, 0)
|
res := printStringMap(test.Input, 0)
|
||||||
|
|
||||||
// create two slice of string formed by splitting our strings on \n
|
// create two slice of string formed by splitting our strings on \n
|
||||||
slice1 := strings.Split(res, "\n")
|
slice1 := strings.Split(res, "\n")
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user