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.
Этот коммит содержится в:
George Goldberg
2019-06-21 17:38:26 +01:00
коммит произвёл GitHub
родитель e3504398c7
Коммит 0d8377cc59
3 изменённых файлов: 19 добавлений и 7 удалений

Просмотреть файл

@@ -203,7 +203,7 @@ func printConfigValues(configMap map[string]interface{}, configSetting []string,
switch value.Kind() {
case reflect.Map:
if len(configSetting) == 1 {
return printMap(value, 0), nil
return printStringMap(value, 0), nil
}
return printConfigValues(res.(map[string]interface{}), configSetting[1:], name)
default:

Просмотреть файл

@@ -7,6 +7,7 @@ import (
"bytes"
"fmt"
"reflect"
"sort"
"strings"
"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
func prettyPrintMap(configMap map[string]interface{}) string {
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
func printMap(value reflect.Value, tabVal int) string {
// printStringMap takes a reflect.Value and prints it out alphabetically based on key values, which must be strings.
// 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{}
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)
if newVal, ok := val.Interface().(map[string]interface{}); !ok {
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())
// going one level in, increase the tab
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
tabVal--
}

Просмотреть файл

@@ -139,7 +139,7 @@ func TestPrintMap(t *testing.T) {
for _, test := range cases {
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
slice1 := strings.Split(res, "\n")