Skip to content

Commit

Permalink
fix: use comma without a space as separator to parse array value
Browse files Browse the repository at this point in the history
Signed-off-by: FelixTing <[email protected]>
  • Loading branch information
FelixTing committed Sep 14, 2023
1 parent 446f517 commit 2452e70
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
3 changes: 2 additions & 1 deletion dtos/reading.go
Original file line number Diff line number Diff line change
Expand Up @@ -407,9 +407,10 @@ func parseSimpleValue(valueType string, value string) (err error) {
}

func parseArrayValue(valueType string, value string) (err error) {
arrayValue := strings.Split(value[1:len(value)-1], ", ") // trim "[" and "]"
arrayValue := strings.Split(value[1:len(value)-1], ",") // trim "[" and "]"

for _, v := range arrayValue {
v = strings.TrimSpace(v)
switch valueType {
case common.ValueTypeBoolArray:
err = parseSimpleValue(common.ValueTypeBool, v)
Expand Down
24 changes: 24 additions & 0 deletions dtos/reading_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,30 @@ func TestValidateValue(t *testing.T) {
}
}

func TestValidateArrayValue(t *testing.T) {
tests := []struct {
name string
valueType string
value string
expectError bool
}{
{"Valid separator (comma followed by a space)", common.ValueTypeBoolArray, "[true, false]", false},
{"Valid separator (comma)", common.ValueTypeBoolArray, "[true,false]", false},
{"Invalid separator", common.ValueTypeBoolArray, "[true@false]", true},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := ValidateValue(tt.valueType, tt.value)
if tt.expectError {
require.Error(t, err)
} else {
require.NoError(t, err)
}
})
}
}

func TestValidateValueError(t *testing.T) {
invalidValue := "invalid"
tests := []struct {
Expand Down

0 comments on commit 2452e70

Please sign in to comment.