MM-30740 Add support for boolean feature flags. (#16311)

* Add support for boolean feature flags.

* Tests

* Use strconv.ParseBool to accept additional values as true.

* Update config/feature_flags.go

Co-authored-by: Christopher Poile <cpoile@gmail.com>

Co-authored-by: Christopher Poile <cpoile@gmail.com>
Co-authored-by: Mattermod <mattermod@users.noreply.github.com>
Этот коммит содержится в:
Christopher Speller
2020-11-25 07:27:06 -08:00
коммит произвёл GitHub
родитель faaac9cb74
Коммит a87e8d1c20
3 изменённых файлов: 78 добавлений и 4 удалений

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

@@ -6,6 +6,8 @@ package config
import (
"math"
"reflect"
"strconv"
"strings"
"github.com/mattermost/mattermost-server/v5/mlog"
"github.com/mattermost/mattermost-server/v5/model"
@@ -73,7 +75,8 @@ func (f *FeatureFlagSynchronizer) Close() {
// featureFlagsFromMap sets the feature flags from a map[string]string.
// It starts with baseFeatureFlags and only sets values that are
// given by the upstream management system.
// Makes the assumption that all feature flags are strings for now.
// Makes the assumption that all feature flags are strings or booleans.
// Strings are converted to booleans by considering case insensitive "on" or any value considered by strconv.ParseBool as true and any other value as false.
func featureFlagsFromMap(featuresMap map[string]string, baseFeatureFlags model.FeatureFlags) model.FeatureFlags {
refStruct := reflect.ValueOf(&baseFeatureFlags).Elem()
for fieldName, fieldValue := range featuresMap {
@@ -83,13 +86,20 @@ func featureFlagsFromMap(featuresMap map[string]string, baseFeatureFlags model.F
continue
}
refField.Set(reflect.ValueOf(fieldValue))
switch refField.Type().Kind() {
case reflect.Bool:
parsedBoolValue, _ := strconv.ParseBool(fieldValue)
refField.Set(reflect.ValueOf(strings.ToLower(fieldValue) == "on" || parsedBoolValue))
default:
refField.Set(reflect.ValueOf(fieldValue))
}
}
return baseFeatureFlags
}
// featureFlagsToMap returns the feature flags as a map[string]string
// Currently assumes that all feature flags are strings for now.
// Supports boolean and string feature flags.
func featureFlagsToMap(featureFlags *model.FeatureFlags) map[string]string {
refStructVal := reflect.ValueOf(*featureFlags)
refStructType := reflect.TypeOf(*featureFlags)
@@ -100,7 +110,12 @@ func featureFlagsToMap(featureFlags *model.FeatureFlags) map[string]string {
if !refFieldVal.IsValid() {
continue
}
ret[refFieldType.Name] = refFieldVal.String()
switch refFieldType.Type.Kind() {
case reflect.Bool:
ret[refFieldType.Name] = strconv.FormatBool(refFieldVal.Bool())
default:
ret[refFieldType.Name] = refFieldVal.String()
}
}
return ret