-
Notifications
You must be signed in to change notification settings - Fork 30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[ExtendedTime] More clean up #969
Changes from 5 commits
b2a3bd2
5a59a7d
58aebf5
c02893e
777191e
80f299b
034ee78
078562e
f1a3345
1663e02
884472c
fbe42a6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,11 +3,10 @@ package ext | |
import ( | ||
"cmp" | ||
"encoding/json" | ||
"fmt" | ||
"time" | ||
) | ||
|
||
// TODO: This package should have a concept of default formats for each type. | ||
|
||
type ExtendedTimeKindType string | ||
|
||
const ( | ||
|
@@ -17,6 +16,21 @@ const ( | |
TimeKindType ExtendedTimeKindType = "time" | ||
) | ||
|
||
func (e ExtendedTimeKindType) defaultLayout() (string, error) { | ||
switch e { | ||
case TimestampTZKindType: | ||
return time.RFC3339Nano, nil | ||
case TimestampNTZKindType: | ||
return RFC3339NanosecondNoTZ, nil | ||
case DateKindType: | ||
return PostgresDateFormat, nil | ||
case TimeKindType: | ||
return PostgresTimeFormat, nil | ||
default: | ||
return "", fmt.Errorf("unknown kind type: %q", e) | ||
} | ||
} | ||
|
||
type NestedKind struct { | ||
Type ExtendedTimeKindType | ||
Format string | ||
|
@@ -51,29 +65,23 @@ type ExtendedTime struct { | |
nestedKind NestedKind | ||
} | ||
|
||
// MarshalJSON is a custom JSON marshaller for ExtendedTime. This is only by MongoDB where we are recursively parsing a nested object. | ||
func (e ExtendedTime) MarshalJSON() ([]byte, error) { | ||
return json.Marshal(e.String("")) | ||
return json.Marshal(e.ts) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this not taking the NestedKind's type and format into account? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah yeah. I'm purposefully moving away from using the ExtendedTime's nested type and format into account. My goal is to eventually make ExtendedTime just an alias for MarshalJSON was created specifically for nested MongoDB objects and this code is consistent with how Mongo marshals Datetime https://github.com/mongodb/mongo-go-driver/blob/c91b204cc72025ea0c8d0845b84816b8c9e88903/bson/primitive.go#L59-L61 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually, let me get it to look identical to MongoDB's |
||
} | ||
|
||
// TODO: Have this return an error instead of nil | ||
func NewExtendedTime(t time.Time, kindType ExtendedTimeKindType, originalFormat string) *ExtendedTime { | ||
if originalFormat == "" { | ||
switch kindType { | ||
case TimestampTZKindType: | ||
originalFormat = TimestampTZ.Format | ||
case TimestampNTZKindType: | ||
originalFormat = TimestampNTZ.Format | ||
case DateKindType: | ||
originalFormat = Date.Format | ||
case TimeKindType: | ||
originalFormat = Time.Format | ||
} | ||
format, err := kindType.defaultLayout() | ||
if err != nil { | ||
return nil | ||
} | ||
|
||
return &ExtendedTime{ | ||
ts: t, | ||
nestedKind: NestedKind{ | ||
Type: kindType, | ||
Format: originalFormat, | ||
Format: cmp.Or(originalFormat, format), | ||
}, | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Addresses #967 (comment)