MM-10189 Fixed inconsistency when using environment variables for MessageExportSettings (#8705)
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
ffb834ec3c
Коммит
3b138c8b16
@@ -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
|
// 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.
|
// 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 {
|
for key, value := range defaults {
|
||||||
v.SetDefault(key, value)
|
v.SetDefault(key, value)
|
||||||
@@ -226,6 +226,10 @@ func newViper(allowEnvironmentOverrides bool) *viper.Viper {
|
|||||||
return v
|
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
|
// 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.
|
// matching the zeroed value of the corresponding field.
|
||||||
func structToMap(t reflect.Type) (out map[string]interface{}) {
|
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:
|
case reflect.Struct:
|
||||||
value = structToMap(field.Type)
|
value = structToMap(field.Type)
|
||||||
case reflect.Ptr:
|
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:
|
default:
|
||||||
value = reflect.Zero(field.Type).Interface()
|
value = reflect.Zero(field.Type).Interface()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -396,3 +396,25 @@ func sToP(s string) *string {
|
|||||||
func bToP(b bool) *bool {
|
func bToP(b bool) *bool {
|
||||||
return &b
|
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)
|
||||||
|
}
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user