Skip to content

Commit

Permalink
Make seperator configurable
Browse files Browse the repository at this point in the history
Signed-off-by: Rick Brouwer <[email protected]>
  • Loading branch information
rickbrouwer committed Sep 4, 2024
1 parent c8d137f commit 6ec9b58
Showing 1 changed file with 28 additions and 13 deletions.
41 changes: 28 additions & 13 deletions pkg/scalers/scalersconfig/typed_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ const (

// separators for map and slice elements
const (
elemSeparator = ",;"
elemKeyValSeparator = "="
)

Expand All @@ -76,6 +75,7 @@ const (
enumTag = "enum"
exclusiveSetTag = "exclusiveSet"
rangeTag = "range"
seperatorTag = "separator"
)

// Params is a struct that represents the parameter list that can be used in the keda tag
Expand Down Expand Up @@ -109,6 +109,9 @@ type Params struct {

// RangeSeparator is the 'range' tag parameter defining the separator for range values
RangeSeparator string

// Separator is the tag parameter to define which separator will be used
Separator string
}

// IsNested is a function that returns true if the parameter is nested
Expand Down Expand Up @@ -208,9 +211,11 @@ func (sc *ScalerConfig) setValue(field reflect.Value, params Params) error {
enumMap[e] = true
}
missingMap := make(map[string]bool)
split := strings.FieldsFunc(valFromConfig, func(r rune) bool {
return strings.ContainsRune(elemSeparator, r)
})
separator := ","
if params.Separator != "" {
separator = params.Separator
}
split := strings.Split(valFromConfig, separator)
for _, s := range split {
s := strings.TrimSpace(s)
if !enumMap[s] {
Expand All @@ -226,9 +231,11 @@ func (sc *ScalerConfig) setValue(field reflect.Value, params Params) error {
for _, e := range params.ExclusiveSet {
exclusiveMap[e] = true
}
split := strings.FieldsFunc(valFromConfig, func(r rune) bool {
return strings.ContainsRune(elemSeparator, r)
})
separator := ","
if params.Separator != "" {
separator = params.Separator
}
split := strings.Split(valFromConfig, separator)
exclusiveCount := 0
for _, s := range split {
s := strings.TrimSpace(s)
Expand Down Expand Up @@ -280,9 +287,11 @@ 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())))
split := strings.FieldsFunc(valFromConfig, func(r rune) bool {
return strings.ContainsRune(elemSeparator, r)
})
separator := ","
if params.Separator != "" {
separator = params.Separator
}
split := string.Split(valFromConfig, separator)

Check failure on line 294 in pkg/scalers/scalersconfig/typed_config.go

View workflow job for this annotation

GitHub Actions / Static Checks

string.Split undefined (type string has no field or method Split)

Check failure on line 294 in pkg/scalers/scalersconfig/typed_config.go

View workflow job for this annotation

GitHub Actions / Static Checks

string.Split undefined (type string has no field or method Split)

Check failure on line 294 in pkg/scalers/scalersconfig/typed_config.go

View workflow job for this annotation

GitHub Actions / Static Checks

string.Split undefined (type string has no field or method Split)

Check failure on line 294 in pkg/scalers/scalersconfig/typed_config.go

View workflow job for this annotation

GitHub Actions / Static Checks

string.Split undefined (type string has no field or method Split)
for _, s := range split {
s := strings.TrimSpace(s)
kv := strings.Split(s, elemKeyValSeparator)
Expand Down Expand Up @@ -348,9 +357,11 @@ 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()
split := strings.FieldsFunc(valFromConfig, func(r rune) bool {
return strings.ContainsRune(elemSeparator, r)
})
separator := ","
if params.Separator != "" {
separator = params.Separator
}
split := strings.Split(valFromConfig, separator)
for i, s := range split {
s := strings.TrimSpace(s)
if canRange(s, params.RangeSeparator, field) {
Expand Down Expand Up @@ -481,6 +492,10 @@ func paramsFromTag(tag string, field reflect.StructField) (Params, error) {
if len(tsplit) == 2 {
params.RangeSeparator = strings.TrimSpace(tsplit[1])
}
case separatorTag:

Check failure on line 495 in pkg/scalers/scalersconfig/typed_config.go

View workflow job for this annotation

GitHub Actions / Static Checks

undefined: separatorTag) (typecheck)

Check failure on line 495 in pkg/scalers/scalersconfig/typed_config.go

View workflow job for this annotation

GitHub Actions / Static Checks

undefined: separatorTag) (typecheck)

Check failure on line 495 in pkg/scalers/scalersconfig/typed_config.go

View workflow job for this annotation

GitHub Actions / Static Checks

undefined: separatorTag) (typecheck)

Check failure on line 495 in pkg/scalers/scalersconfig/typed_config.go

View workflow job for this annotation

GitHub Actions / Static Checks

undefined: separatorTag (typecheck)
if len(tsplit) > 1 {
params.Separator = strings.TrimSpace(tsplit[1])
}
case "":
continue
default:
Expand Down

0 comments on commit 6ec9b58

Please sign in to comment.