Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrating the last converter over #1096

Merged
merged 4 commits into from
Jan 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 48 additions & 8 deletions lib/typing/converters/string_converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,30 @@ package converters
import (
"encoding/json"
"fmt"
"log/slog"
"reflect"
"strings"
"time"

"github.com/artie-labs/transfer/lib/config/constants"
"github.com/artie-labs/transfer/lib/stringutil"
"github.com/artie-labs/transfer/lib/typing"
"github.com/artie-labs/transfer/lib/typing/decimal"
"github.com/artie-labs/transfer/lib/typing/ext"
)

type StringConverter interface {
type Converter interface {
Convert(value any) (string, error)
}

func GetStringConverter(kd typing.KindDetails) (StringConverter, error) {
func GetStringConverter(kd typing.KindDetails) (Converter, error) {
switch kd.Kind {
// Time
// Base types
case typing.Boolean.Kind:
return BooleanConverter{}, nil
case typing.String.Kind:
return StringConverter{}, nil
// Time types
case typing.Date.Kind:
return DateConverter{}, nil
case typing.Time.Kind:
Expand All @@ -27,21 +35,53 @@ func GetStringConverter(kd typing.KindDetails) (StringConverter, 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) {
// TODO Simplify this function
isArray := reflect.ValueOf(value).Kind() == reflect.Slice
_, isMap := value.(map[string]any)
// If colVal is either an array or a JSON object, we should run JSON parse.
if isMap || isArray {
colValBytes, err := json.Marshal(value)
if err != nil {
return "", err
}

return string(colValBytes), nil
}

// TODO: Return an error when all the types are implemented.
return nil, nil
return stringutil.EscapeBackslashes(fmt.Sprint(value)), nil
}

type DateConverter struct{}
Expand Down
97 changes: 97 additions & 0 deletions lib/typing/converters/string_converter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,110 @@ package converters
import (
"testing"

"github.com/artie-labs/transfer/lib/typing"

"github.com/stretchr/testify/assert"

"github.com/artie-labs/transfer/lib/config/constants"
"github.com/artie-labs/transfer/lib/numbers"
"github.com/artie-labs/transfer/lib/typing/decimal"
)

func TestGetStringConverter(t *testing.T) {
{
// Boolean
converter, err := GetStringConverter(typing.Boolean)
assert.NoError(t, err)
assert.IsType(t, BooleanConverter{}, converter)
}
{
// String
converter, err := GetStringConverter(typing.String)
assert.NoError(t, err)
assert.IsType(t, StringConverter{}, converter)
}
{
// Date
converter, err := GetStringConverter(typing.Date)
assert.NoError(t, err)
assert.IsType(t, DateConverter{}, converter)
}
{
// Time
converter, err := GetStringConverter(typing.Time)
assert.NoError(t, err)
assert.IsType(t, TimeConverter{}, converter)
}
{
// TimestampNTZ
converter, err := GetStringConverter(typing.TimestampNTZ)
assert.NoError(t, err)
assert.IsType(t, TimestampNTZConverter{}, converter)
}
{
// TimestampTZ
converter, err := GetStringConverter(typing.TimestampTZ)
assert.NoError(t, err)
assert.IsType(t, TimestampTZConverter{}, converter)
}
{
// Array
converter, err := GetStringConverter(typing.Array)
assert.NoError(t, err)
assert.IsType(t, ArrayConverter{}, converter)
}
{
// Struct
converter, err := GetStringConverter(typing.Struct)
assert.NoError(t, err)
assert.IsType(t, StructConverter{}, converter)
}
{
// EDecimal
converter, err := GetStringConverter(typing.EDecimal)
assert.NoError(t, err)
assert.IsType(t, DecimalConverter{}, converter)
}
{
// Integer
converter, err := GetStringConverter(typing.Integer)
assert.NoError(t, err)
assert.IsType(t, IntegerConverter{}, converter)
}
{
// Float
converter, err := GetStringConverter(typing.Float)
assert.NoError(t, err)
assert.IsType(t, FloatConverter{}, converter)
}
{
// Invalid
converter, err := GetStringConverter(typing.Invalid)
assert.NoError(t, err)
assert.Nil(t, converter)
}
}

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
21 changes: 1 addition & 20 deletions lib/typing/values/string.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package values

import (
"encoding/json"
"fmt"
"reflect"

"github.com/artie-labs/transfer/lib/stringutil"
"github.com/artie-labs/transfer/lib/typing"
"github.com/artie-labs/transfer/lib/typing/converters"
)
Expand All @@ -20,26 +17,10 @@ func ToString(colVal any, colKind typing.KindDetails) (string, error) {
return "", fmt.Errorf("failed to get string converter: %w", err)
}

// TODO: Simplify this block
if sv != nil {
return sv.Convert(colVal)
}

// TODO: Move all of this into converter function
switch colKind.Kind {
case typing.String.Kind:
isArray := reflect.ValueOf(colVal).Kind() == reflect.Slice
_, isMap := colVal.(map[string]any)
// If colVal is either an array or a JSON object, we should run JSON parse.
if isMap || isArray {
colValBytes, err := json.Marshal(colVal)
if err != nil {
return "", err
}

return string(colValBytes), nil
}

return stringutil.EscapeBackslashes(fmt.Sprint(colVal)), nil
}
return fmt.Sprint(colVal), nil
}
Loading