diff --git a/pkg/scalers/scalersconfig/typed_config.go b/pkg/scalers/scalersconfig/typed_config.go index a2a9ec91a57..e4d353decb4 100644 --- a/pkg/scalers/scalersconfig/typed_config.go +++ b/pkg/scalers/scalersconfig/typed_config.go @@ -212,11 +212,7 @@ func (sc *ScalerConfig) setValue(field reflect.Value, params Params) error { enumMap[e] = true } missingMap := make(map[string]bool) - separator := elemSeparator - if params.Separator != "" { - separator = params.Separator - } - split := strings.Split(valFromConfig, separator) + split := splitWithSeparator(valFromConfig, elemSeparator, params.Separator) for _, s := range split { s := strings.TrimSpace(s) if !enumMap[s] { @@ -232,11 +228,7 @@ func (sc *ScalerConfig) setValue(field reflect.Value, params Params) error { for _, e := range params.ExclusiveSet { exclusiveMap[e] = true } - separator := elemSeparator - if params.Separator != "" { - separator = params.Separator - } - split := strings.Split(valFromConfig, separator) + split := splitWithSeparator(valFromConfig, elemSeparator, params.Separator) exclusiveCount := 0 for _, s := range split { s := strings.TrimSpace(s) @@ -288,11 +280,7 @@ func setConfigValueURLParams(params Params, valFromConfig string, field reflect. // setConfigValueMap is a function that sets the value of the map field func setConfigValueMap(params Params, valFromConfig string, field reflect.Value) error { field.Set(reflect.MakeMap(reflect.MapOf(field.Type().Key(), field.Type().Elem()))) - separator := elemSeparator - if params.Separator != "" { - separator = params.Separator - } - split := strings.Split(valFromConfig, separator) + split := splitWithSeparator(valFromConfig, elemSeparator, params.Separator) for _, s := range split { s := strings.TrimSpace(s) kv := strings.Split(s, elemKeyValSeparator) @@ -330,6 +318,15 @@ func canRange(valFromConfig, elemRangeSeparator string, field reflect.Value) boo return strings.Contains(valFromConfig, elemRangeSeparator) } +// splitWithSeparator is a function that splits on default or custom separator +func splitWithSeparator(valFromConfig, defaultSeparator, customSeparator string) []string { + separator := defaultSeparator + if customSeparator != "" { + separator = customSeparator + } + return strings.Split(valFromConfig, separator) +} + // setConfigValueRange is a function that sets the value of the range field func setConfigValueRange(params Params, valFromConfig string, field reflect.Value) error { rangeSplit := strings.Split(valFromConfig, params.RangeSeparator) @@ -358,11 +355,7 @@ func setConfigValueRange(params Params, valFromConfig string, field reflect.Valu // setConfigValueSlice is a function that sets the value of the slice field func setConfigValueSlice(params Params, valFromConfig string, field reflect.Value) error { elemIfc := reflect.New(field.Type().Elem()).Interface() - separator := elemSeparator - if params.Separator != "" { - separator = params.Separator - } - split := strings.Split(valFromConfig, separator) + split := splitWithSeparator(valFromConfig, elemSeparator, params.Separator) for i, s := range split { s := strings.TrimSpace(s) if canRange(s, params.RangeSeparator, field) {