Skip to content

Commit

Permalink
Clean up and adding Boolean Converter.
Browse files Browse the repository at this point in the history
  • Loading branch information
Tang8330 committed Dec 28, 2024
1 parent edb0514 commit f24f314
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 5 deletions.
25 changes: 20 additions & 5 deletions lib/typing/converters/string_converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ type Converter interface {

func GetStringConverter(kd typing.KindDetails) (Converter, error) {
switch kd.Kind {
// String
// Base types
case typing.Boolean.Kind:
return BooleanConverter{}, nil
case typing.String.Kind:
return StringConverter{}, nil
// Time
// Time types
case typing.Date.Kind:
return DateConverter{}, nil
case typing.Time.Kind:
Expand All @@ -33,23 +35,36 @@ func GetStringConverter(kd typing.KindDetails) (Converter, error) {
return TimestampNTZConverter{}, nil
case typing.TimestampTZ.Kind:
return TimestampTZConverter{}, nil
// Array and struct types
case typing.Array.Kind:
return ArrayConverter{}, nil
// Numbers
case typing.Struct.Kind:
return StructConverter{}, nil
// Numbers types
case typing.EDecimal.Kind:
return DecimalConverter{}, nil
case typing.Integer.Kind:
return IntegerConverter{}, nil
case typing.Float.Kind:
return FloatConverter{}, nil
case typing.Struct.Kind:
return StructConverter{}, nil

default:
slog.Warn("[GetStringConverter] - Unsupported type", slog.String("kind", kd.Kind))
return nil, nil
}
}

type BooleanConverter struct{}

func (BooleanConverter) Convert(value any) (string, error) {
booleanValue, isOk := value.(bool)
if !isOk {
return "", fmt.Errorf("failed to cast colVal as boolean, colVal: '%v', type: %T", value, value)
}

return fmt.Sprint(booleanValue), nil
}

type StringConverter struct{}

func (StringConverter) Convert(value any) (string, error) {
Expand Down
20 changes: 20 additions & 0 deletions lib/typing/converters/string_converter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,26 @@ import (
"github.com/artie-labs/transfer/lib/typing/decimal"
)

func TestBooleanConverter_Convert(t *testing.T) {
{
// Not boolean
_, err := BooleanConverter{}.Convert("foo")
assert.ErrorContains(t, err, `failed to cast colVal as boolean, colVal: 'foo', type: string`)
}
{
// True
val, err := BooleanConverter{}.Convert(true)
assert.NoError(t, err)
assert.Equal(t, "true", val)
}
{
// False
val, err := BooleanConverter{}.Convert(false)
assert.NoError(t, err)
assert.Equal(t, "false", val)
}
}

func TestArrayConverter_Convert(t *testing.T) {
// Array
{
Expand Down

0 comments on commit f24f314

Please sign in to comment.