Skip to content

Commit

Permalink
Merge branch 'master' into pull-timestamp-ntz
Browse files Browse the repository at this point in the history
  • Loading branch information
Tang8330 committed Oct 31, 2024
2 parents e28b6b1 + b67409b commit e8c5564
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 3 deletions.
7 changes: 7 additions & 0 deletions clients/mssql/values.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 19 additions & 3 deletions clients/mssql/values_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)
Expand Down
7 changes: 7 additions & 0 deletions lib/typing/values/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions lib/typing/values/string_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down

0 comments on commit e8c5564

Please sign in to comment.