MM-29709 Client side feature flags (#16212)

* Client side feature flags.

* Fix test.

Co-authored-by: Mattermod <mattermod@users.noreply.github.com>
Этот коммит содержится в:
Christopher Speller
2020-11-18 11:11:15 -08:00
коммит произвёл GitHub
родитель 213345fa2c
Коммит ceb1422a5c
4 изменённых файлов: 59 добавлений и 0 удалений

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

@@ -334,5 +334,9 @@ func GenerateLimitedClientConfig(c *model.Config, telemetryID string, license *m
}
}
for key, value := range featureFlagsToMap(c.FeatureFlags) {
props["FeatureFlag"+key] = value
}
return props
}

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

@@ -257,6 +257,19 @@ func TestGetLimitedClientConfig(t *testing.T) {
"PasswordRequireSymbol": "false",
},
},
{
"Feature Flags",
&model.Config{
FeatureFlags: &model.FeatureFlags{
TestFeature: "myvalue",
},
},
"",
nil,
map[string]string{
"FeatureFlagTestFeature": "myvalue",
},
},
}
for _, testCase := range testCases {

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

@@ -88,6 +88,24 @@ func featureFlagsFromMap(featuresMap map[string]string, baseFeatureFlags model.F
return baseFeatureFlags
}
// featureFlagsToMap returns the feature flags as a map[string]string
// Currently assumes that all feature flags are strings for now.
func featureFlagsToMap(featureFlags *model.FeatureFlags) map[string]string {
refStructVal := reflect.ValueOf(*featureFlags)
refStructType := reflect.TypeOf(*featureFlags)
ret := make(map[string]string)
for i := 0; i < refStructVal.NumField(); i++ {
refFieldVal := refStructVal.Field(i)
refFieldType := refStructType.Field(i)
if !refFieldVal.IsValid() {
continue
}
ret[refFieldType.Name] = refFieldVal.String()
}
return ret
}
func getStructFields(s interface{}) []string {
structType := reflect.TypeOf(s)
fieldNames := make([]string, 0, structType.NumField())

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

@@ -73,3 +73,27 @@ func TestFeatureFlagsFromMap(t *testing.T) {
})
}
}
func TestFeatureFlagsToMap(t *testing.T) {
for name, tc := range map[string]struct {
Flags model.FeatureFlags
TestFeatureValue string
}{
"empty": {
TestFeatureValue: "",
Flags: model.FeatureFlags{},
},
"simple value": {
TestFeatureValue: "expectedvalue",
Flags: model.FeatureFlags{TestFeature: "expectedvalue"},
},
"empty value": {
TestFeatureValue: "",
Flags: model.FeatureFlags{TestFeature: ""},
},
} {
t.Run(name, func(t *testing.T) {
require.Equal(t, tc.TestFeatureValue, featureFlagsToMap(&tc.Flags)["TestFeature"])
})
}
}