Skip to content

Commit

Permalink
Add splitWithSeparator func
Browse files Browse the repository at this point in the history
Signed-off-by: Rick Brouwer <[email protected]>
  • Loading branch information
rickbrouwer committed Sep 6, 2024
1 parent 51a1765 commit 9a3d8ea
Showing 1 changed file with 13 additions and 20 deletions.
33 changes: 13 additions & 20 deletions pkg/scalers/scalersconfig/typed_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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] {
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 9a3d8ea

Please sign in to comment.