Skip to content

Commit

Permalink
[Extended Time] Additional follow up (#999)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tang8330 authored Oct 31, 2024
1 parent a34dac1 commit 7b59d85
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
12 changes: 11 additions & 1 deletion clients/bigquery/converters/converters.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package converters
import (
"fmt"
"strconv"
"time"

"github.com/artie-labs/transfer/lib/typing"
"github.com/artie-labs/transfer/lib/typing/decimal"
Expand All @@ -25,14 +26,23 @@ func (s StringConverter) Convert(value any) (any, error) {
return castedValue.String(), nil
case bool, int64:
return fmt.Sprint(castedValue), nil
case time.Time:
switch s.kd {
case typing.Date:
return castedValue.Format(ext.PostgresDateFormat), nil
case typing.TimestampNTZ:
return castedValue.Format(ext.RFC3339NoTZ), nil
default:
return nil, fmt.Errorf("unexpected kind details: %q", s.kd.Kind)
}
case *ext.ExtendedTime:
if err := s.kd.EnsureExtendedTimeDetails(); err != nil {
return nil, err
}

return castedValue.GetTime().Format(s.kd.ExtendedTimeDetails.Format), nil
default:
return nil, fmt.Errorf("expected string/*decimal.Decimal/bool/int64 received %T with value %v", value, value)
return nil, fmt.Errorf("unexpected data type: %T with value: %v", value, value)
}
}

Expand Down
22 changes: 21 additions & 1 deletion clients/bigquery/converters/converters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,27 @@ func TestStringConverter_Convert(t *testing.T) {
{
// Invalid
_, err := NewStringConverter(typing.Integer).Convert(123)
assert.ErrorContains(t, err, "expected string/*decimal.Decimal/bool/int64 received int with value 123")
assert.ErrorContains(t, err, "unexpected data type: int with value: 123")
}
{
// time.Time
{
// Date
val, err := NewStringConverter(typing.Date).Convert(time.Date(2021, 1, 1, 0, 0, 0, 0, time.UTC))
assert.NoError(t, err)
assert.Equal(t, "2021-01-01", val)
}
{
// Timestamp NTZ
val, err := NewStringConverter(typing.TimestampNTZ).Convert(time.Date(2021, 1, 1, 9, 10, 12, 400_123_991, time.UTC))
assert.NoError(t, err)
assert.Equal(t, "2021-01-01T09:10:12.400123991", val)
}
{
// Invalid
_, err := NewStringConverter(typing.String).Convert(time.Date(2021, 1, 1, 9, 10, 12, 400_123_991, time.UTC))
assert.ErrorContains(t, err, `unexpected kind details: "string"`)
}
}
{
// Extended time
Expand Down

0 comments on commit 7b59d85

Please sign in to comment.