Skip to content

Commit

Permalink
String converters.
Browse files Browse the repository at this point in the history
  • Loading branch information
Tang8330 committed Dec 16, 2024
1 parent a725cd4 commit 8d6f16e
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 7 deletions.
33 changes: 33 additions & 0 deletions lib/typing/converters/converters.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
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
}
19 changes: 12 additions & 7 deletions lib/typing/values/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"strings"
"time"

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

"github.com/artie-labs/transfer/lib/config/constants"
"github.com/artie-labs/transfer/lib/stringutil"
"github.com/artie-labs/transfer/lib/typing"
Expand Down Expand Up @@ -36,14 +38,17 @@ func ToString(colVal any, colKind typing.KindDetails) (string, error) {
return "", fmt.Errorf("colVal is nil")
}

switch colKind.Kind {
case typing.Date.Kind:
_time, err := ext.ParseDateFromAny(colVal)
if err != nil {
return "", fmt.Errorf("failed to cast colVal as date, colVal: '%v', err: %w", colVal, err)
}
sv, err := converters.GetStringConverter(colKind)
if err != nil {
return "", fmt.Errorf("failed to get string converter: %w", err)
}

// Prioritize converters
if sv != nil {
return sv.Convert(colVal)
}

return _time.Format(ext.PostgresDateFormat), nil
switch colKind.Kind {
case typing.Time.Kind:
_time, err := ext.ParseTimeFromAny(colVal)
if err != nil {
Expand Down

0 comments on commit 8d6f16e

Please sign in to comment.