diff --git a/lib/typing/converters/converters.go b/lib/typing/converters/converters.go deleted file mode 100644 index a01558b73..000000000 --- a/lib/typing/converters/converters.go +++ /dev/null @@ -1,33 +0,0 @@ -package converters - -import ( - "fmt" - - "github.com/artie-labs/transfer/lib/typing" - - "github.com/artie-labs/transfer/lib/typing/ext" -) - -type StringConverter interface { - Convert(value any) (string, error) -} - -func GetStringConverter(kd typing.KindDetails) (StringConverter, error) { - switch kd.Kind { - case typing.Date.Kind: - return DateStringConverter{}, nil - } - - return nil, nil -} - -type DateStringConverter struct{} - -func (DateStringConverter) Convert(value any) (string, error) { - _time, err := ext.ParseDateFromAny(value) - if err != nil { - return "", fmt.Errorf("failed to cast colVal as date, colVal: '%v', err: %w", value, err) - } - - return _time.Format(ext.PostgresDateFormat), nil -} diff --git a/lib/typing/converters/string_converter.go b/lib/typing/converters/string_converter.go new file mode 100644 index 000000000..83ecaafc5 --- /dev/null +++ b/lib/typing/converters/string_converter.go @@ -0,0 +1,86 @@ +package converters + +import ( + "encoding/json" + "fmt" + "time" + + "github.com/artie-labs/transfer/lib/typing" + "github.com/artie-labs/transfer/lib/typing/ext" +) + +type StringConverter interface { + Convert(value any) (string, error) +} + +func GetStringConverter(kd typing.KindDetails) (StringConverter, error) { + switch kd.Kind { + case typing.Date.Kind: + return DateConverter{}, nil + case typing.Time.Kind: + return TimeConverter{}, nil + case typing.TimestampNTZ.Kind: + return TimestampNTZConverter{}, nil + case typing.TimestampTZ.Kind: + return TimestampTZConverter{}, nil + case typing.Array.Kind: + return ArrayConverter{}, nil + } + + return nil, nil +} + +type DateConverter struct{} + +func (DateConverter) Convert(value any) (string, error) { + _time, err := ext.ParseDateFromAny(value) + if err != nil { + return "", fmt.Errorf("failed to cast colVal as date, colVal: '%v', err: %w", value, err) + } + + return _time.Format(ext.PostgresDateFormat), nil +} + +type TimeConverter struct{} + +func (TimeConverter) Convert(value any) (string, error) { + _time, err := ext.ParseTimeFromAny(value) + if err != nil { + return "", fmt.Errorf("failed to cast colVal as time, colVal: '%v', err: %w", value, err) + } + + return _time.Format(ext.PostgresTimeFormatNoTZ), nil +} + +type TimestampNTZConverter struct{} + +func (TimestampNTZConverter) Convert(value any) (string, error) { + _time, err := ext.ParseTimestampNTZFromAny(value) + if err != nil { + return "", fmt.Errorf("failed to cast colVal as timestampNTZ, colVal: '%v', err: %w", value, err) + } + + return _time.Format(ext.RFC3339NoTZ), nil +} + +type TimestampTZConverter struct{} + +func (TimestampTZConverter) Convert(value any) (string, error) { + _time, err := ext.ParseTimestampTZFromAny(value) + if err != nil { + return "", fmt.Errorf("failed to cast colVal as timestampTZ, colVal: '%v', err: %w", value, err) + } + + return _time.Format(time.RFC3339Nano), nil +} + +type ArrayConverter struct{} + +func (ArrayConverter) Convert(value any) (string, error) { + colValBytes, err := json.Marshal(value) + if err != nil { + return "", err + } + + return string(colValBytes), nil +} diff --git a/lib/typing/values/string.go b/lib/typing/values/string.go index 472371ac3..5d24e4398 100644 --- a/lib/typing/values/string.go +++ b/lib/typing/values/string.go @@ -6,7 +6,6 @@ import ( "reflect" "strconv" "strings" - "time" "github.com/artie-labs/transfer/lib/typing/converters" @@ -14,7 +13,6 @@ import ( "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" ) func Float64ToString(value float64) string { @@ -43,33 +41,11 @@ func ToString(colVal any, colKind typing.KindDetails) (string, error) { return "", fmt.Errorf("failed to get string converter: %w", err) } - // Prioritize converters if sv != nil { return sv.Convert(colVal) } switch colKind.Kind { - case typing.Time.Kind: - _time, err := ext.ParseTimeFromAny(colVal) - if err != nil { - return "", fmt.Errorf("failed to cast colVal as time, colVal: '%v', err: %w", colVal, err) - } - - return _time.Format(ext.PostgresTimeFormatNoTZ), nil - case typing.TimestampNTZ.Kind: - _time, err := ext.ParseTimestampNTZFromAny(colVal) - if err != nil { - return "", fmt.Errorf("failed to cast colVal as timestampNTZ, colVal: '%v', err: %w", colVal, err) - } - - return _time.Format(ext.RFC3339NoTZ), nil - case typing.TimestampTZ.Kind: - _time, err := ext.ParseTimestampTZFromAny(colVal) - if err != nil { - return "", fmt.Errorf("failed to cast colVal as timestampTZ, colVal: '%v', err: %w", colVal, err) - } - - return _time.Format(time.RFC3339Nano), nil case typing.String.Kind: isArray := reflect.ValueOf(colVal).Kind() == reflect.Slice _, isMap := colVal.(map[string]any) @@ -101,13 +77,6 @@ func ToString(colVal any, colKind typing.KindDetails) (string, error) { return string(colValBytes), nil } } - case typing.Array.Kind: - colValBytes, err := json.Marshal(colVal) - if err != nil { - return "", err - } - - return string(colValBytes), nil case typing.Float.Kind: switch parsedVal := colVal.(type) { case float32: