Skip to content
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

Support time.Time instead of int64 for date fields #1972

Open
wants to merge 3 commits into
base: beta
Choose a base branch
from

Conversation

mbroshi-stripe
Copy link

@mbroshi-stripe mbroshi-stripe commented Jan 22, 2025

Why?

This change supports codegen changes that converts datetime fields from int64 to time.Time as brought up in this issue: #1205.

What?

  • The main change is in the form serialization in form.go for time.Time fields. Serialization is performed by reflection, and we do not want the default behavior of inspecting into a time.Time type, rather just convert it to an int64
  • While I was in there, I also created convenience method Time, TimeValue, UnixTime and UnixValue to help in creating time.Time and *time.Time parameter fields
  • I also updated unit tests expecting an int64 type

See Also

@mbroshi-stripe mbroshi-stripe changed the title Support time.Time Support time.Time instead of int64 for date fields Jan 23, 2025
@mbroshi-stripe mbroshi-stripe marked this pull request as ready for review January 23, 2025 02:30
@@ -325,6 +326,14 @@ func intEncoder(values *Values, v reflect.Value, keyParts []string, encodeZero b
values.Add(FormatKey(keyParts), strconv.FormatInt(val, 10))
}

func timeEncoder(values *Values, v reflect.Value, keyParts []string, encodeZero bool, _ *formOptions) {
val := v.Interface().(time.Time)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what happens if this is called with a v other than a time.Time; does this return an error we need to look for, or will val just be Zero (and is that swallowing an error?)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It panics. I think that's OK, but open for other thoughts. It's only called in one location surrounded by

if t == reflect.TypeOf(time.Time{}) {
	return timeEncoder
}

so logically, the type assertion v.Interface().(time.Time) must succeed. I would definitely rather fail fast here rather than silently. The alternative would be to bubble up an error, but it feels a little weird to return an error due to a bug.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In fact, I'm realizing we have the same panic behavior in the rest of type assertions in form.go, so this is really no different than current behavior (where we do a type assertion to an integer:

val := v.Int()

form/form.go Outdated Show resolved Hide resolved
stripe.go Show resolved Hide resolved
@mbroshi-stripe mbroshi-stripe force-pushed the mbroshi/time-dot-time-support branch from aa2e73f to e0418a5 Compare January 23, 2025 17:16
@helenye-stripe
Copy link
Contributor

helenye-stripe commented Jan 23, 2025

:lurk: I think the merge-base for this PR should be the beta branch!

@mbroshi-stripe mbroshi-stripe changed the base branch from master to beta January 23, 2025 17:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants