From b67409bc87fe6ff4cf1398072872bd66f036baa7 Mon Sep 17 00:00:00 2001 From: Robin Tang Date: Wed, 30 Oct 2024 17:52:37 -0700 Subject: [PATCH] Date - Fast follow (#991) --- clients/mssql/values.go | 7 +++++++ clients/mssql/values_test.go | 22 +++++++++++++++++++--- lib/typing/values/string.go | 7 +++++++ lib/typing/values/string_test.go | 15 +++++++++++++++ 4 files changed, 48 insertions(+), 3 deletions(-) diff --git a/clients/mssql/values.go b/clients/mssql/values.go index 94174ceca..161aca329 100644 --- a/clients/mssql/values.go +++ b/clients/mssql/values.go @@ -27,6 +27,13 @@ func parseValue(colVal any, colKind columns.Column) (any, error) { colValString := fmt.Sprint(colVal) switch colKind.KindDetails.Kind { + case typing.Date.Kind: + _time, err := ext.ParseDateFromInterface(colVal) + if err != nil { + return "", fmt.Errorf("failed to cast colVal as time.Time, colVal: '%v', err: %w", colVal, err) + } + + return _time, nil case typing.ETime.Kind: if err := colKind.KindDetails.EnsureExtendedTimeDetails(); err != nil { return "", err diff --git a/clients/mssql/values_test.go b/clients/mssql/values_test.go index 6ed8e7af9..f9818534a 100644 --- a/clients/mssql/values_test.go +++ b/clients/mssql/values_test.go @@ -2,13 +2,14 @@ package mssql import ( "testing" + "time" - "github.com/artie-labs/transfer/lib/config/constants" + "github.com/stretchr/testify/assert" + "github.com/artie-labs/transfer/lib/config/constants" "github.com/artie-labs/transfer/lib/typing" - "github.com/artie-labs/transfer/lib/typing/columns" - "github.com/stretchr/testify/assert" + "github.com/artie-labs/transfer/lib/typing/ext" ) func TestParseValue(t *testing.T) { @@ -17,6 +18,21 @@ func TestParseValue(t *testing.T) { assert.NoError(t, err) assert.Nil(t, val) } + { + // Date + { + // String + val, err := parseValue("2021-01-01", columns.NewColumn("date", typing.Date)) + assert.NoError(t, err) + assert.Equal(t, "2021-01-01", val.(time.Time).Format(ext.PostgresDateFormat)) + } + { + // time.Time + val, err := parseValue(time.Date(2021, time.January, 1, 0, 0, 0, 0, time.UTC), columns.NewColumn("date", typing.Date)) + assert.NoError(t, err) + assert.Equal(t, "2021-01-01", val.(time.Time).Format(ext.PostgresDateFormat)) + } + } { val, err := parseValue("string value", columns.NewColumn("foo", typing.String)) assert.NoError(t, err) diff --git a/lib/typing/values/string.go b/lib/typing/values/string.go index 3eb4825c7..311d461e5 100644 --- a/lib/typing/values/string.go +++ b/lib/typing/values/string.go @@ -27,6 +27,13 @@ func ToString(colVal any, colKind typing.KindDetails) (string, error) { } switch colKind.Kind { + case typing.Date.Kind: + _time, err := ext.ParseDateFromInterface(colVal) + if err != nil { + return "", fmt.Errorf("failed to cast colVal as time.Time, colVal: '%v', err: %w", colVal, err) + } + + return _time.Format(ext.PostgresDateFormat), nil case typing.ETime.Kind: if err := colKind.EnsureExtendedTimeDetails(); err != nil { return "", err diff --git a/lib/typing/values/string_test.go b/lib/typing/values/string_test.go index 54738cd8d..7bfee5205 100644 --- a/lib/typing/values/string_test.go +++ b/lib/typing/values/string_test.go @@ -25,6 +25,21 @@ func TestToString(t *testing.T) { _, err := ToString(nil, typing.KindDetails{}) assert.ErrorContains(t, err, "colVal is nil") } + { + // Date + { + // time.Time + value, err := ToString(time.Date(2021, time.January, 1, 0, 0, 0, 0, time.UTC), typing.Date) + assert.NoError(t, err) + assert.Equal(t, "2021-01-01", value) + } + { + // String + value, err := ToString("2021-01-01", typing.Date) + assert.NoError(t, err) + assert.Equal(t, "2021-01-01", value) + } + } { // ETime {