Skip to content

Commit

Permalink
Support time.Time
Browse files Browse the repository at this point in the history
  • Loading branch information
mbroshi-stripe committed Jan 22, 2025
1 parent b384f33 commit aa2e73f
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 11 deletions.
5 changes: 3 additions & 2 deletions account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package stripe
import (
"encoding/json"
"testing"
"time"

assert "github.com/stretchr/testify/require"
"github.com/stripe/stripe-go/v81/form"
Expand Down Expand Up @@ -138,7 +139,7 @@ func TestAccount_Unmarshal(t *testing.T) {
assert.Equal(t, "value1", account.Metadata["key1"])
assert.Equal(t, "value2", account.Metadata["key2"])

assert.Equal(t, int64(1234567890), account.Requirements.CurrentDeadline)
assert.Equal(t, time.Unix(1234567890, 0), account.Requirements.CurrentDeadline)
assert.Equal(t, 2, len(account.Requirements.CurrentlyDue))
assert.Equal(t, AccountRequirementsDisabledReasonRejectedFraud, account.Requirements.DisabledReason)
assert.Equal(t, 1, len(account.Requirements.Errors))
Expand All @@ -156,7 +157,7 @@ func TestAccount_Unmarshal(t *testing.T) {
assert.Equal(t, int64(2), account.Settings.Payouts.Schedule.DelayDays)
assert.Equal(t, AccountSettingsPayoutsScheduleIntervalWeekly, account.Settings.Payouts.Schedule.Interval)

assert.Equal(t, int64(1528573382), account.TOSAcceptance.Date)
assert.Equal(t, time.Unix(1528573382, 0), account.TOSAcceptance.Date)
assert.Equal(t, "127.0.0.1", account.TOSAcceptance.IP)
assert.Equal(t, "user agent", account.TOSAcceptance.UserAgent)
assert.Equal(t, AccountTOSAcceptanceServiceAgreementRecipient, account.TOSAcceptance.ServiceAgreement)
Expand Down
16 changes: 15 additions & 1 deletion form/form.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"strconv"
"strings"
"sync"
"time"
)

const tagName = "form"
Expand Down Expand Up @@ -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)
if val.IsZero() && !encodeZero {
return
}
values.Add(FormatKey(keyParts), strconv.FormatInt(val.Unix(), 10))
}

func interfaceEncoder(values *Values, v reflect.Value, keyParts []string, encodeZero bool, _ *formOptions) {
// interfaceEncoder never encodes a `nil`, but it will pass through an
// `encodeZero` value into its chained encoder
Expand Down Expand Up @@ -383,7 +392,6 @@ func uintEncoder(values *Values, v reflect.Value, keyParts []string, encodeZero
// interface{}.
func reflectValue(values *Values, v reflect.Value, encodeZero bool, keyParts []string) {
t := v.Type()

f := getCachedOrBuildTypeEncoder(t)
if f != nil {
f(values, v, keyParts, encodeZero || v.Kind() == reflect.Ptr, nil)
Expand Down Expand Up @@ -461,6 +469,12 @@ func makeStructEncoder(t reflect.Type) *structEncoder {
}

func makeTypeEncoder(t reflect.Type) encoderFunc {
// For time.Time, we want to encode it as a Unix timestamp,
// and don't want to recurse into it.
if t == reflect.TypeOf(time.Time{}) {
return timeEncoder
}

switch t.Kind() {
case reflect.Array, reflect.Slice:
return buildArrayOrSliceEncoder(t)
Expand Down
6 changes: 3 additions & 3 deletions issuing/dispute/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ func TestIssuingDisputeNew(t *testing.T) {
Evidence: &stripe.IssuingDisputeEvidenceParams{
Canceled: &stripe.IssuingDisputeEvidenceCanceledParams{
AdditionalDocumentation: stripe.String("file_123"),
CanceledAt: stripe.Int64(1577836800),
CanceledAt: stripe.UnixTime(1528573382),

Check failure on line 38 in issuing/dispute/client_test.go

View workflow job for this annotation

GitHub Actions / Lint

cannot use stripe.UnixTime(1528573382) (value of type *time.Time) as *int64 value in struct literal

Check failure on line 38 in issuing/dispute/client_test.go

View workflow job for this annotation

GitHub Actions / Test: go v1.17

cannot use stripe.UnixTime(1528573382) (type *time.Time) as type *int64 in field value

Check failure on line 38 in issuing/dispute/client_test.go

View workflow job for this annotation

GitHub Actions / Test: go v1.15

cannot use stripe.UnixTime(1528573382) (type *time.Time) as type *int64 in field value

Check failure on line 38 in issuing/dispute/client_test.go

View workflow job for this annotation

GitHub Actions / Test: go v1.16

cannot use stripe.UnixTime(1528573382) (type *time.Time) as type *int64 in field value

Check failure on line 38 in issuing/dispute/client_test.go

View workflow job for this annotation

GitHub Actions / Test: go v1.18

cannot use stripe.UnixTime(1528573382) (value of type *time.Time) as type *int64 in struct literal

Check failure on line 38 in issuing/dispute/client_test.go

View workflow job for this annotation

GitHub Actions / Test: go v1.19

cannot use stripe.UnixTime(1528573382) (value of type *time.Time) as type *int64 in struct literal
CancellationPolicyProvided: stripe.Bool(true),
CancellationReason: stripe.String("reason for cancellation"),
ExpectedAt: stripe.Int64(1577836800),
ExpectedAt: stripe.UnixTime(1528573382),

Check failure on line 41 in issuing/dispute/client_test.go

View workflow job for this annotation

GitHub Actions / Test: go v1.17

cannot use stripe.UnixTime(1528573382) (type *time.Time) as type *int64 in field value

Check failure on line 41 in issuing/dispute/client_test.go

View workflow job for this annotation

GitHub Actions / Test: go v1.15

cannot use stripe.UnixTime(1528573382) (type *time.Time) as type *int64 in field value

Check failure on line 41 in issuing/dispute/client_test.go

View workflow job for this annotation

GitHub Actions / Test: go v1.16

cannot use stripe.UnixTime(1528573382) (type *time.Time) as type *int64 in field value

Check failure on line 41 in issuing/dispute/client_test.go

View workflow job for this annotation

GitHub Actions / Test: go v1.18

cannot use stripe.UnixTime(1528573382) (value of type *time.Time) as type *int64 in struct literal

Check failure on line 41 in issuing/dispute/client_test.go

View workflow job for this annotation

GitHub Actions / Test: go v1.19

cannot use stripe.UnixTime(1528573382) (value of type *time.Time) as type *int64 in struct literal
Explanation: stripe.String("explanation"),
ProductDescription: stripe.String("product description"),
ProductType: stripe.String(string(stripe.IssuingDisputeEvidenceCanceledProductTypeMerchandise)),
ReturnStatus: stripe.String(string(stripe.IssuingDisputeEvidenceCanceledReturnStatusMerchantRejected)),
ReturnedAt: stripe.Int64(1577836800),
ReturnedAt: stripe.UnixTime(1528573382),

Check failure on line 46 in issuing/dispute/client_test.go

View workflow job for this annotation

GitHub Actions / Test: go v1.17

cannot use stripe.UnixTime(1528573382) (type *time.Time) as type *int64 in field value

Check failure on line 46 in issuing/dispute/client_test.go

View workflow job for this annotation

GitHub Actions / Test: go v1.15

cannot use stripe.UnixTime(1528573382) (type *time.Time) as type *int64 in field value

Check failure on line 46 in issuing/dispute/client_test.go

View workflow job for this annotation

GitHub Actions / Test: go v1.16

cannot use stripe.UnixTime(1528573382) (type *time.Time) as type *int64 in field value

Check failure on line 46 in issuing/dispute/client_test.go

View workflow job for this annotation

GitHub Actions / Test: go v1.18

cannot use stripe.UnixTime(1528573382) (value of type *time.Time) as type *int64 in struct literal

Check failure on line 46 in issuing/dispute/client_test.go

View workflow job for this annotation

GitHub Actions / Test: go v1.19

cannot use stripe.UnixTime(1528573382) (value of type *time.Time) as type *int64 in struct literal
},
Reason: stripe.String(string(stripe.IssuingDisputeEvidenceReasonCanceled)),
},
Expand Down
4 changes: 2 additions & 2 deletions source/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ func TestSourceNew(t *testing.T) {
Mandate: &stripe.SourceMandateParams{
Amount: stripe.Int64(1000),
Acceptance: &stripe.SourceMandateAcceptanceParams{
Date: stripe.Int64(1528573382),
Date: stripe.UnixTime(1528573382),

Check failure on line 26 in source/client_test.go

View workflow job for this annotation

GitHub Actions / Lint

cannot use stripe.UnixTime(1528573382) (value of type *time.Time) as *int64 value in struct literal

Check failure on line 26 in source/client_test.go

View workflow job for this annotation

GitHub Actions / Test: go v1.17

cannot use stripe.UnixTime(1528573382) (type *time.Time) as type *int64 in field value

Check failure on line 26 in source/client_test.go

View workflow job for this annotation

GitHub Actions / Test: go v1.15

cannot use stripe.UnixTime(1528573382) (type *time.Time) as type *int64 in field value

Check failure on line 26 in source/client_test.go

View workflow job for this annotation

GitHub Actions / Test: go v1.16

cannot use stripe.UnixTime(1528573382) (type *time.Time) as type *int64 in field value

Check failure on line 26 in source/client_test.go

View workflow job for this annotation

GitHub Actions / Test: go v1.18

cannot use stripe.UnixTime(1528573382) (value of type *time.Time) as type *int64 in struct literal

Check failure on line 26 in source/client_test.go

View workflow job for this annotation

GitHub Actions / Test: go v1.19

cannot use stripe.UnixTime(1528573382) (value of type *time.Time) as type *int64 in struct literal
IP: stripe.String("127.0.0.1"),
Online: &stripe.SourceMandateAcceptanceOnlineParams{
Date: stripe.Int64(1528573382),
Date: stripe.UnixTime(1528573382),

Check failure on line 29 in source/client_test.go

View workflow job for this annotation

GitHub Actions / Test: go v1.17

cannot use stripe.UnixTime(1528573382) (type *time.Time) as type *int64 in field value

Check failure on line 29 in source/client_test.go

View workflow job for this annotation

GitHub Actions / Test: go v1.15

cannot use stripe.UnixTime(1528573382) (type *time.Time) as type *int64 in field value

Check failure on line 29 in source/client_test.go

View workflow job for this annotation

GitHub Actions / Test: go v1.16

cannot use stripe.UnixTime(1528573382) (type *time.Time) as type *int64 in field value

Check failure on line 29 in source/client_test.go

View workflow job for this annotation

GitHub Actions / Test: go v1.18

cannot use stripe.UnixTime(1528573382) (value of type *time.Time) as type *int64 in struct literal

Check failure on line 29 in source/client_test.go

View workflow job for this annotation

GitHub Actions / Test: go v1.19

cannot use stripe.UnixTime(1528573382) (value of type *time.Time) as type *int64 in struct literal
IP: stripe.String("127.0.0.1"),
UserAgent: stripe.String("User-Agent"),
},
Expand Down
26 changes: 26 additions & 0 deletions stripe.go
Original file line number Diff line number Diff line change
Expand Up @@ -1381,6 +1381,32 @@ func StringSlice(v []string) []*string {
return out
}

// Time returns a pointer to the time.Time value passed in.
func Time(v time.Time) *time.Time {
return &v
}

// TimeValue returns the value of the time.Time pointer passed in or
// time.Time{} if the pointer is nil.
func TimeValue(v *time.Time) time.Time {
if v != nil {
return *v
}
return time.Time{}
}

// UnixTime returns a pointer to the time.Time value associated to the
// Unix epoch timestamp passed in.
func UnixTime(v int64) *time.Time {
return Time(time.Unix(v, 0))
}

// UnixTimeValue returns the Unix epoch timestamp value of the time.Time
// pointer passed in or 0 if the pointer is nil.
func UnixTimeValue(v *time.Time) int64 {
return TimeValue(v).Unix()
}

//
// Private constants
//
Expand Down
3 changes: 1 addition & 2 deletions usagerecord/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@ import (
)

func TestUsageRecordNew(t *testing.T) {
now := int64(time.Now().Unix())
usageRecord, err := New(&stripe.UsageRecordParams{
Quantity: stripe.Int64(123),
Timestamp: stripe.Int64(now),
Timestamp: stripe.Time(time.Now()),

Check failure on line 15 in usagerecord/client_test.go

View workflow job for this annotation

GitHub Actions / Lint

cannot use stripe.Time(time.Now()) (value of type *time.Time) as *int64 value in struct literal

Check failure on line 15 in usagerecord/client_test.go

View workflow job for this annotation

GitHub Actions / Test: go v1.17

cannot use stripe.Time(time.Now()) (type *time.Time) as type *int64 in field value

Check failure on line 15 in usagerecord/client_test.go

View workflow job for this annotation

GitHub Actions / Test: go v1.15

cannot use stripe.Time(time.Now()) (type *time.Time) as type *int64 in field value

Check failure on line 15 in usagerecord/client_test.go

View workflow job for this annotation

GitHub Actions / Test: go v1.16

cannot use stripe.Time(time.Now()) (type *time.Time) as type *int64 in field value

Check failure on line 15 in usagerecord/client_test.go

View workflow job for this annotation

GitHub Actions / Test: go v1.18

cannot use stripe.Time(time.Now()) (value of type *time.Time) as type *int64 in struct literal

Check failure on line 15 in usagerecord/client_test.go

View workflow job for this annotation

GitHub Actions / Test: go v1.19

cannot use stripe.Time(time.Now()) (value of type *time.Time) as type *int64 in struct literal
Action: stripe.String(stripe.UsageRecordActionIncrement),
SubscriptionItem: stripe.String("si_123"),
})
Expand Down
2 changes: 1 addition & 1 deletion usagerecord_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func TestUsageRecordParams_AppendTo(t *testing.T) {
{"action", &UsageRecordParams{Action: String("increment")}, "increment"},
{"quantity", &UsageRecordParams{Quantity: Int64(2000)}, strconv.FormatUint(2000, 10)},
{"quantity", &UsageRecordParams{Quantity: Int64(0)}, strconv.FormatUint(0, 10)},
{"timestamp", &UsageRecordParams{Timestamp: Int64(123123123)}, strconv.FormatUint(123123123, 10)},
{"timestamp", &UsageRecordParams{Timestamp: UnixTime(123123123)}, strconv.FormatUint(123123123, 10)},

Check failure on line 20 in usagerecord_test.go

View workflow job for this annotation

GitHub Actions / Lint

cannot use UnixTime(123123123) (value of type *time.Time) as type *int64 in struct literal

Check failure on line 20 in usagerecord_test.go

View workflow job for this annotation

GitHub Actions / Test: go v1.17

cannot use UnixTime(123123123) (type *time.Time) as type *int64 in field value

Check failure on line 20 in usagerecord_test.go

View workflow job for this annotation

GitHub Actions / Test: go v1.15

cannot use UnixTime(123123123) (type *time.Time) as type *int64 in field value

Check failure on line 20 in usagerecord_test.go

View workflow job for this annotation

GitHub Actions / Test: go v1.16

cannot use UnixTime(123123123) (type *time.Time) as type *int64 in field value

Check failure on line 20 in usagerecord_test.go

View workflow job for this annotation

GitHub Actions / Test: go v1.18

cannot use UnixTime(123123123) (value of type *time.Time) as type *int64 in struct literal

Check failure on line 20 in usagerecord_test.go

View workflow job for this annotation

GitHub Actions / Test: go v1.19

cannot use UnixTime(123123123) (value of type *time.Time) as type *int64 in struct literal

Check failure on line 20 in usagerecord_test.go

View workflow job for this annotation

GitHub Actions / Test: go v1.22

cannot use UnixTime(123123123) (value of type *time.Time) as *int64 value in struct literal

Check failure on line 20 in usagerecord_test.go

View workflow job for this annotation

GitHub Actions / Test: go v1.23

cannot use UnixTime(123123123) (value of type *time.Time) as *int64 value in struct literal

Check failure on line 20 in usagerecord_test.go

View workflow job for this annotation

GitHub Actions / Test: go v1.20

cannot use UnixTime(123123123) (value of type *time.Time) as *int64 value in struct literal

Check failure on line 20 in usagerecord_test.go

View workflow job for this annotation

GitHub Actions / Test: go v1.21

cannot use UnixTime(123123123) (value of type *time.Time) as *int64 value in struct literal
}
for _, tc := range testCases {
t.Run(tc.field, func(t *testing.T) {
Expand Down

0 comments on commit aa2e73f

Please sign in to comment.