Skip to content

Commit

Permalink
Clean up.
Browse files Browse the repository at this point in the history
  • Loading branch information
Tang8330 committed Oct 24, 2024
1 parent 5ee1966 commit eb48521
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 26 deletions.
17 changes: 1 addition & 16 deletions lib/typing/ext/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ func NewNestedKind(kindType ExtendedTimeKindType, optionalFormat string) (Nested
// ExtendedTime is created because Golang's time.Time does not allow us to explicitly cast values as a date, or time
// and only allows timestamp expressions.
type ExtendedTime struct {
ts time.Time
nestedKind NestedKind
ts time.Time
}

// MarshalJSON is a custom JSON marshaller for ExtendedTime.
Expand All @@ -58,26 +57,12 @@ func (e ExtendedTime) MarshalJSON() ([]byte, error) {
return e.ts.UTC().MarshalJSON()
}

// TODO: Have this return an error instead of nil
func NewExtendedTime(t time.Time, kindType ExtendedTimeKindType, originalFormat string) *ExtendedTime {
defaultLayout, err := kindType.defaultLayout()
if err != nil {
return nil
}

return &ExtendedTime{
ts: t,
nestedKind: NestedKind{
Type: kindType,
Format: cmp.Or(originalFormat, defaultLayout),
},
}
}

func (e *ExtendedTime) GetTime() time.Time {
return e.ts
}

func (e *ExtendedTime) GetNestedKind() NestedKind {
return e.nestedKind
}
6 changes: 2 additions & 4 deletions lib/typing/mongo/bson.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ import (
"github.com/google/uuid"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"

"github.com/artie-labs/transfer/lib/typing/ext"
)

// JSONEToMap - Takes a JSON Extended string and converts it to a map[string]any
Expand Down Expand Up @@ -89,9 +87,9 @@ func bsonBinaryValueToMap(value primitive.Binary) (any, error) {
func bsonValueToGoValue(value any) (any, error) {
switch v := value.(type) {
case primitive.DateTime:
return ext.NewExtendedTime(v.Time().UTC(), ext.TimestampTZKindType, ext.ISO8601), nil
return v.Time().UTC(), nil
case primitive.Timestamp:
return ext.NewExtendedTime(time.Unix(int64(v.T), 0).UTC(), ext.TimestampTZKindType, ext.ISO8601), nil
return time.Unix(int64(v.T), 0).UTC(), nil
case primitive.ObjectID:
return v.Hex(), nil
case primitive.Binary:
Expand Down
11 changes: 5 additions & 6 deletions lib/typing/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package typing
import (
"fmt"
"reflect"
"time"

"github.com/artie-labs/transfer/lib/typing/decimal"
"github.com/artie-labs/transfer/lib/typing/ext"
Expand Down Expand Up @@ -48,12 +49,10 @@ func ParseValue(key string, optionalSchema map[string]KindDetails, val any) (Kin
Kind: EDecimal.Kind,
ExtendedDecimalDetails: &extendedDetails,
}, nil
case *ext.ExtendedTime:
nestedKind := convertedVal.GetNestedKind()
return KindDetails{
Kind: ETime.Kind,
ExtendedTimeDetails: &nestedKind,
}, nil
case time.Time:
// time.Time should only appear for bson.Timestamp and bson.DateTime
// All other sources will have an optional schema
return NewExtendedTimeDetails(ETime, ext.TimestampTZKindType, "")
default:
// Check if the val is one of our custom-types
if reflect.TypeOf(val).Kind() == reflect.Slice {
Expand Down

0 comments on commit eb48521

Please sign in to comment.