Skip to content

Commit

Permalink
Clean up and add test.
Browse files Browse the repository at this point in the history
  • Loading branch information
Tang8330 committed Jan 4, 2025
1 parent 38a692c commit c620001
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 20 deletions.
5 changes: 3 additions & 2 deletions lib/typing/converters/string_converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,10 @@ func (BooleanConverter) Convert(value any) (string, error) {
return fmt.Sprint(castedValue), nil
default:
// First try to cast the value into a string and see if we can parse it
if fmt.Sprint(value) == "0" {
stringValue := strings.ToLower(fmt.Sprint(value))
if stringValue == "0" || stringValue == "false" {
return "false", nil
} else if fmt.Sprint(value) == "1" {
} else if stringValue == "1" || stringValue == "true" {
return "true", nil
} else {
return "", fmt.Errorf("failed to cast colVal as boolean, colVal: '%v', type: %T", value, value)
Expand Down
16 changes: 10 additions & 6 deletions lib/typing/converters/string_converter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,19 @@ func TestBooleanConverter_Convert(t *testing.T) {
}
{
// True
val, err := BooleanConverter{}.Convert(true)
assert.NoError(t, err)
assert.Equal(t, "true", val)
for _, possibleValue := range []any{1, true, "1", "true", "TRUE", "True"} {
val, err := BooleanConverter{}.Convert(possibleValue)
assert.NoError(t, err)
assert.Equal(t, "true", val)
}
}
{
// False
val, err := BooleanConverter{}.Convert(false)
assert.NoError(t, err)
assert.Equal(t, "false", val)
for _, possibleValue := range []any{0, false, "0", "false", "FALSE", "False"} {
val, err := BooleanConverter{}.Convert(possibleValue)
assert.NoError(t, err)
assert.Equal(t, "false", val)
}
}
}

Expand Down
12 changes: 0 additions & 12 deletions lib/typing/converters/util.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package converters

import (
"fmt"
"strconv"
)

Expand All @@ -20,14 +19,3 @@ func BooleanToBit(val bool) int {
return 0
}
}

func BitToBoolean[T int | int8 | int16 | int32 | int64](value T) (bool, error) {
switch value {
case 0:
return false, nil
case 1:
return true, nil
default:
return false, fmt.Errorf("unexpected value: %d, expected: [0, 1]", value)
}
}

0 comments on commit c620001

Please sign in to comment.