From ffb834ec3ce048db341e2438c0116475297a6f74 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Espino?= Date: Thu, 3 May 2018 15:40:54 +0200 Subject: Fix TestSendNotifications test (#8712) --- utils/utils.go | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'utils') diff --git a/utils/utils.go b/utils/utils.go index 595a9d2ba..b156f9934 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -13,6 +13,15 @@ import ( "github.com/mattermost/mattermost-server/model" ) +func StringInSlice(a string, slice []string) bool { + for _, b := range slice { + if b == a { + return true + } + } + return false +} + func StringArrayIntersection(arr1, arr2 []string) []string { arrMap := map[string]bool{} result := []string{} -- cgit v1.2.3-1-g7c22 From 3b138c8b16668f17bfcd367d05b5f7846d185caf Mon Sep 17 00:00:00 2001 From: Harrison Healey Date: Thu, 3 May 2018 09:55:03 -0400 Subject: MM-10189 Fixed inconsistency when using environment variables for MessageExportSettings (#8705) --- utils/config.go | 15 +++++++++++++-- utils/config_test.go | 22 ++++++++++++++++++++++ 2 files changed, 35 insertions(+), 2 deletions(-) (limited to 'utils') diff --git a/utils/config.go b/utils/config.go index 34cd0ed9f..3827cf4ee 100644 --- a/utils/config.go +++ b/utils/config.go @@ -217,7 +217,7 @@ func newViper(allowEnvironmentOverrides bool) *viper.Viper { // Set zeroed defaults for all the config settings so that Viper knows what environment variables // it needs to be looking for. The correct defaults will later be applied using Config.SetDefaults. - defaults := flattenStructToMap(structToMap(reflect.TypeOf(model.Config{}))) + defaults := getDefaultsFromStruct(model.Config{}) for key, value := range defaults { v.SetDefault(key, value) @@ -226,6 +226,10 @@ func newViper(allowEnvironmentOverrides bool) *viper.Viper { return v } +func getDefaultsFromStruct(s interface{}) map[string]interface{} { + return flattenStructToMap(structToMap(reflect.TypeOf(s))) +} + // Converts a struct type into a nested map with keys matching the struct's fields and values // matching the zeroed value of the corresponding field. func structToMap(t reflect.Type) (out map[string]interface{}) { @@ -251,7 +255,14 @@ func structToMap(t reflect.Type) (out map[string]interface{}) { case reflect.Struct: value = structToMap(field.Type) case reflect.Ptr: - value = nil + indirectType := field.Type.Elem() + + if indirectType.Kind() == reflect.Struct { + // Follow pointers to structs since we need to define defaults for their fields + value = structToMap(indirectType) + } else { + value = nil + } default: value = reflect.Zero(field.Type).Interface() } diff --git a/utils/config_test.go b/utils/config_test.go index ec66a30f0..75bbc420f 100644 --- a/utils/config_test.go +++ b/utils/config_test.go @@ -396,3 +396,25 @@ func sToP(s string) *string { func bToP(b bool) *bool { return &b } + +func TestGetDefaultsFromStruct(t *testing.T) { + s := struct { + TestSettings struct { + IntValue int + BoolValue bool + StringValue string + } + PointerToTestSettings *struct { + Value int + } + }{} + + defaults := getDefaultsFromStruct(s) + + assert.Equal(t, defaults["TestSettings.IntValue"], 0) + assert.Equal(t, defaults["TestSettings.BoolValue"], false) + assert.Equal(t, defaults["TestSettings.StringValue"], "") + assert.Equal(t, defaults["PointerToTestSettings.Value"], 0) + assert.NotContains(t, defaults, "PointerToTestSettings") + assert.Len(t, defaults, 4) +} -- cgit v1.2.3-1-g7c22