Commit a86c2d2e authored by Aaron Raddon's avatar Aaron Raddon

update CoerceString to support empty string

parent fa458351
......@@ -59,6 +59,9 @@ func CoerceStringShort(v interface{}) string {
func CoerceStrings(v interface{}) []string {
switch val := v.(type) {
case string:
if val == "" {
return nil
}
return strings.Split(val, ",")
case []string:
return val
......
......@@ -9,11 +9,12 @@ import (
func TestCoerce(t *testing.T) {
data := map[string]interface{}{
"int": 4,
"float": 45.3,
"string": "22",
"stringf": "22.2",
"boolt": true,
"int": 4,
"float": 45.3,
"string": "22",
"stringf": "22.2",
"boolt": true,
"emptystring": "",
}
assert.True(t, CoerceStringShort(data["int"]) == "4", "get int as string")
assert.True(t, CoerceStringShort(data["float"]) == "45.3", "get float as string: %v", data["float"])
......@@ -25,4 +26,8 @@ func TestCoerce(t *testing.T) {
assert.True(t, CoerceIntShort(data["float"]) == 45, "get float as int: %v", data["float"])
assert.True(t, CoerceIntShort(data["string"]) == 22, "get string as int: %v", data["string"])
assert.True(t, CoerceIntShort(data["stringf"]) == 22, "get stringf as int: %v", data["stringf"])
assert.Equal(t, 0, len(CoerceStrings(data["emptystring"])), "get emptystring as []string: %v", data["emptystring"])
assert.Equal(t, []string{"22"}, CoerceStrings(data["string"]), "get string as []string: %v", data["emptystring"])
assert.Equal(t, []string{"4"}, CoerceStrings(data["int"]), "get int as []string: %v", data["emptystring"])
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment