-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into default-value-types
- Loading branch information
Showing
10 changed files
with
195 additions
and
144 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -198,21 +198,18 @@ func (r *RelationTestSuite) TestPostgresEventWithSchemaAndTimestampNoTZ() { | |
assert.Equal(r.T(), evtData["id"], int64(1001)) | ||
assert.Equal(r.T(), evtData["another_id"], int64(333)) | ||
assert.Equal(r.T(), typing.ParseValue(typing.Settings{}, "another_id", evt.GetOptionalSchema(), evtData["another_id"]), typing.Integer) | ||
|
||
assert.Equal(r.T(), evtData["email"], "[email protected]") | ||
|
||
// Datetime without TZ is emitted in microseconds which is 1000x larger than nanoseconds. | ||
td := time.Date(2023, time.February, 2, 17, 51, 35, 175445*1000, time.UTC) | ||
assert.Equal(r.T(), evtData["ts_no_tz1"], &ext.ExtendedTime{ | ||
Time: td, | ||
NestedKind: ext.NestedKind{ | ||
Type: ext.DateTimeKindType, | ||
Format: time.RFC3339Nano, | ||
}, | ||
}) | ||
|
||
assert.Equal(r.T(), time.Date(2023, time.February, 2, | ||
17, 54, 11, 451000000, time.UTC), evt.GetExecutionTime()) | ||
assert.Equal( | ||
r.T(), | ||
ext.NewExtendedTime( | ||
time.Date(2023, time.February, 2, 17, 51, 35, 175445*1000, time.UTC), | ||
ext.DateTimeKindType, ext.RFC3339Microsecond, | ||
), | ||
evtData["ts_no_tz1"], | ||
) | ||
assert.Equal(r.T(), time.Date(2023, time.February, 2, 17, 54, 11, 451000000, time.UTC), evt.GetExecutionTime()) | ||
assert.Equal(r.T(), "customers", evt.GetTableName()) | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package converters | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/artie-labs/transfer/lib/typing" | ||
"github.com/artie-labs/transfer/lib/typing/ext" | ||
) | ||
|
||
type Timestamp struct{} | ||
|
||
func (Timestamp) ToKindDetails() typing.KindDetails { | ||
return typing.NewKindDetailsFromTemplate(typing.ETime, ext.DateTimeKindType) | ||
} | ||
|
||
func (Timestamp) Convert(value any) (any, error) { | ||
castedValue, err := typing.AssertType[int64](value) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// Represents the number of milliseconds since the epoch, and does not include timezone information. | ||
return ext.NewExtendedTime(time.UnixMilli(castedValue).In(time.UTC), ext.DateTimeKindType, ext.RFC3339Millisecond), nil | ||
} | ||
|
||
type MicroTimestamp struct{} | ||
|
||
func (MicroTimestamp) ToKindDetails() typing.KindDetails { | ||
return typing.NewKindDetailsFromTemplate(typing.ETime, ext.DateTimeKindType) | ||
} | ||
|
||
func (MicroTimestamp) Convert(value any) (any, error) { | ||
castedValue, err := typing.AssertType[int64](value) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// Represents the number of microseconds since the epoch, and does not include timezone information. | ||
return ext.NewExtendedTime(time.UnixMicro(castedValue).In(time.UTC), ext.DateTimeKindType, ext.RFC3339Microsecond), nil | ||
} | ||
|
||
type NanoTimestamp struct{} | ||
|
||
func (NanoTimestamp) ToKindDetails() typing.KindDetails { | ||
return typing.NewKindDetailsFromTemplate(typing.ETime, ext.DateTimeKindType) | ||
} | ||
|
||
func (NanoTimestamp) Convert(value any) (any, error) { | ||
castedValue, err := typing.AssertType[int64](value) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// Represents the number of nanoseconds since the epoch, and does not include timezone information. | ||
return ext.NewExtendedTime(time.UnixMicro(castedValue/1_000).In(time.UTC), ext.DateTimeKindType, ext.RFC3339Nanosecond), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package converters | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/artie-labs/transfer/lib/typing" | ||
"github.com/artie-labs/transfer/lib/typing/ext" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestTimestamp_Converter(t *testing.T) { | ||
assert.Equal(t, typing.NewKindDetailsFromTemplate(typing.ETime, ext.DateTimeKindType), Timestamp{}.ToKindDetails()) | ||
{ | ||
// Invalid conversion | ||
_, err := Timestamp{}.Convert("invalid") | ||
assert.ErrorContains(t, err, "expected type int64, got string") | ||
} | ||
{ | ||
// Valid conversion | ||
converted, err := Timestamp{}.Convert(int64(1_725_058_799_089)) | ||
assert.NoError(t, err) | ||
assert.Equal(t, "2024-08-30T22:59:59.089Z", converted.(*ext.ExtendedTime).String("")) | ||
} | ||
{ | ||
// ms is preserved despite it being all zeroes. | ||
converted, err := Timestamp{}.Convert(int64(1_725_058_799_000)) | ||
assert.NoError(t, err) | ||
assert.Equal(t, "2024-08-30T22:59:59.000Z", converted.(*ext.ExtendedTime).String("")) | ||
} | ||
} | ||
|
||
func TestMicroTimestamp_Converter(t *testing.T) { | ||
assert.Equal(t, typing.NewKindDetailsFromTemplate(typing.ETime, ext.DateTimeKindType), MicroTimestamp{}.ToKindDetails()) | ||
{ | ||
// Invalid conversion | ||
_, err := MicroTimestamp{}.Convert("invalid") | ||
assert.ErrorContains(t, err, "expected type int64, got string") | ||
} | ||
{ | ||
// Valid conversion | ||
converted, err := MicroTimestamp{}.Convert(int64(1_712_609_795_827_923)) | ||
assert.NoError(t, err) | ||
assert.Equal(t, "2024-04-08T20:56:35.827923Z", converted.(*ext.ExtendedTime).String("")) | ||
} | ||
{ | ||
// micros is preserved despite it being all zeroes. | ||
converted, err := MicroTimestamp{}.Convert(int64(1_712_609_795_820_000)) | ||
assert.NoError(t, err) | ||
assert.Equal(t, "2024-04-08T20:56:35.820000Z", converted.(*ext.ExtendedTime).String("")) | ||
} | ||
} | ||
|
||
func TestNanoTimestamp_Converter(t *testing.T) { | ||
assert.Equal(t, typing.NewKindDetailsFromTemplate(typing.ETime, ext.DateTimeKindType), NanoTimestamp{}.ToKindDetails()) | ||
{ | ||
// Invalid conversion | ||
_, err := NanoTimestamp{}.Convert("invalid") | ||
assert.ErrorContains(t, err, "expected type int64, got string") | ||
} | ||
{ | ||
// Valid conversion | ||
converted, err := NanoTimestamp{}.Convert(int64(1_712_609_795_827_001_000)) | ||
assert.NoError(t, err) | ||
assert.Equal(t, "2024-04-08T20:56:35.827001000Z", converted.(*ext.ExtendedTime).String("")) | ||
} | ||
{ | ||
// nanos is preserved despite it being all zeroes. | ||
converted, err := NanoTimestamp{}.Convert(int64(1_712_609_795_827_000_000)) | ||
assert.NoError(t, err) | ||
assert.Equal(t, "2024-04-08T20:56:35.827000000Z", converted.(*ext.ExtendedTime).String("")) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.