Skip to content

Commit

Permalink
Add methods to manage parameter types as pointers
Browse files Browse the repository at this point in the history
  • Loading branch information
remi-stripe committed Jun 6, 2018
1 parent fdd38c4 commit afadd09
Show file tree
Hide file tree
Showing 20 changed files with 190 additions and 111 deletions.
15 changes: 7 additions & 8 deletions account.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,12 @@ type AccountParams struct {
BusinessPrimaryColor string `form:"business_primary_color"`
BusinessURL string `form:"business_url"`
Country string `form:"country"`
DebitNegativeBalances bool `form:"debit_negative_balances"`
DebitNegativeBalances *bool `form:"debit_negative_balances"`
DefaultCurrency string `form:"default_currency"`
Email string `form:"email"`
ExternalAccount *AccountExternalAccountParams `form:"external_account"`
FromRecipient string `form:"from_recipient"`
LegalEntity *LegalEntity `form:"legal_entity"`
NoDebitNegativeBalances bool `form:"debit_negative_balances,invert"`
PayoutSchedule *PayoutScheduleParams `form:"payout_schedule"`
PayoutStatementDescriptor string `form:"payout_statement_descriptor"`
ProductDescription string `form:"product_description"`
Expand Down Expand Up @@ -116,15 +115,15 @@ func (p *AccountExternalAccountParams) AppendTo(body *form.Values, keyParts []st

// PayoutScheduleParams are the parameters allowed for payout schedules.
type PayoutScheduleParams struct {
DelayDays uint64 `form:"delay_days"`
Interval Interval `form:"interval"`
MinimumDelay bool `form:"-"` // See custom AppendTo
MonthlyAnchor uint64 `form:"monthly_anchor"`
WeeklyAnchor string `form:"weekly_anchor"`
DelayDays uint64 `form:"delay_days"`
DelayDaysMinimum *bool `form:"-"` // See custom AppendTo
Interval Interval `form:"interval"`
MonthlyAnchor uint64 `form:"monthly_anchor"`
WeeklyAnchor string `form:"weekly_anchor"`
}

func (p *PayoutScheduleParams) AppendTo(body *form.Values, keyParts []string) {
if p.MinimumDelay {
if BoolValue(p.DelayDaysMinimum) {
body.Add(form.FormatKey(append(keyParts, "delay_days")), "minimum")
}
}
Expand Down
2 changes: 1 addition & 1 deletion account/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func TestAccountNew(t *testing.T) {
BusinessURL: "www.stripe.com",
BusinessName: "Stripe",
BusinessPrimaryColor: "#ffffff",
DebitNegativeBalances: true,
DebitNegativeBalances: stripe.Bool(true),
SupportEmail: "[email protected]",
SupportURL: "www.stripe.com",
SupportPhone: "4151234567",
Expand Down
2 changes: 1 addition & 1 deletion account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func TestIdentityDocument_Appendto(t *testing.T) {

func TestPayoutScheduleParams_AppendTo(t *testing.T) {
{
params := &PayoutScheduleParams{MinimumDelay: true}
params := &PayoutScheduleParams{DelayDaysMinimum: Bool(true)}
body := &form.Values{}
form.AppendTo(body, params)
t.Logf("body = %+v", body)
Expand Down
10 changes: 5 additions & 5 deletions bankaccount.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type BankAccountParams struct {
Country string `form:"country"`
Currency string `form:"currency"`
Customer string `form:"-"`
DefaultForCurrency bool `form:"default_for_currency"`
DefaultForCurrency *bool `form:"default_for_currency"`
RoutingNumber string `form:"routing_number"`

// Token is a token referencing an external account like one returned from
Expand Down Expand Up @@ -67,8 +67,8 @@ func (a *BankAccountParams) AppendToAsSourceOrExternalAccount(body *form.Values)
if len(a.Token) > 0 {
body.Add(sourceType, a.Token)

if a.DefaultForCurrency {
body.Add("default_for_currency", strconv.FormatBool(a.DefaultForCurrency))
if a.DefaultForCurrency != nil {
body.Add("default_for_currency", strconv.FormatBool(BoolValue(a.DefaultForCurrency)))
}
} else {
body.Add(sourceType+"[object]", "bank_account")
Expand All @@ -91,8 +91,8 @@ func (a *BankAccountParams) AppendToAsSourceOrExternalAccount(body *form.Values)
body.Add(sourceType+"[routing_number]", a.RoutingNumber)
}

if a.DefaultForCurrency {
body.Add(sourceType+"[default_for_currency]", strconv.FormatBool(a.DefaultForCurrency))
if a.DefaultForCurrency != nil {
body.Add(sourceType+"[default_for_currency]", strconv.FormatBool(BoolValue(a.DefaultForCurrency)))
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions bankaccount/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func TestBankAccountList_ByCustomer(t *testing.T) {
func TestBankAccountNew_ByAccount(t *testing.T) {
bankAcount, err := New(&stripe.BankAccountParams{
Account: "acct_123",
DefaultForCurrency: true,
DefaultForCurrency: stripe.Bool(true),
Token: "tok_123",
})
assert.Nil(t, err)
Expand All @@ -76,7 +76,7 @@ func TestBankAccountNew_ByCustomer(t *testing.T) {
func TestBankAccountUpdate_ByAccount(t *testing.T) {
bankAcount, err := Update("ba_123", &stripe.BankAccountParams{
Account: "acct_123",
DefaultForCurrency: true,
DefaultForCurrency: stripe.Bool(true),
})
assert.Nil(t, err)
assert.NotNil(t, bankAcount)
Expand Down
6 changes: 3 additions & 3 deletions bitcoinreceiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import (
// For more details see https://stripe.com/docs/api/#list_bitcoin_receivers.
type BitcoinReceiverListParams struct {
ListParams `form:"*"`
NotActive bool `form:"active,invert"`
NotFilled bool `form:"filled,invert"`
Uncaptured bool `form:"uncaptured_funds"`
Active *bool `form:"active"`
Filled *bool `form:"filled"`
Uncaptured bool `form:"uncaptured_funds"`
}

// BitcoinReceiverParams is the set of parameters that can be used when creating a BitcoinReceiver.
Expand Down
2 changes: 1 addition & 1 deletion charge.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ type ChargeParams struct {
Params `form:"*"`
Amount uint64 `form:"amount"`
ApplicationFee uint64 `form:"application_fee"`
Capture *bool `form:"capture"`
Currency Currency `form:"currency"`
Customer string `form:"customer"`
Description string `form:"description"`
Destination *DestinationParams `form:"destination"`
ExchangeRate float64 `form:"exchange_rate"`
FraudDetails *FraudDetailsParams `form:"fraud_details"`
NoCapture bool `form:"capture,invert"`
OnBehalfOf string `form:"on_behalf_of"`
ReceiptEmail string `form:"receipt_email"`
Shipping *ShippingDetails `form:"shipping"`
Expand Down
2 changes: 1 addition & 1 deletion charge/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func (c Client) Update(id string, params *stripe.ChargeParams) (*stripe.Charge,
return charge, err
}

// Capture captures a previously created charge with NoCapture set to true.
// Capture captures a charge not yet captured.
// For more details see https://stripe.com/docs/api#charge_capture.
func Capture(id string, params *stripe.CaptureParams) (*stripe.Charge, error) {
return getC().Capture(id, params)
Expand Down
31 changes: 14 additions & 17 deletions customer.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,20 @@ import (
// CustomerParams is the set of parameters that can be used when creating or updating a customer.
// For more details see https://stripe.com/docs/api#create_customer and https://stripe.com/docs/api#update_customer.
type CustomerParams struct {
Params `form:"*"`
AccountBalance int64 `form:"account_balance"`
AccountBalanceZero bool `form:"account_balance,zero"`
BusinessVatID string `form:"business_vat_id"`
Coupon string `form:"coupon"`
CouponEmpty bool `form:"coupon,empty"`
DefaultSource string `form:"default_source"`
Description string `form:"description"`
Email string `form:"email"`
Plan string `form:"plan"`
Quantity uint64 `form:"quantity"`
Shipping *CustomerShippingDetails `form:"shipping"`
Source *SourceParams `form:"*"` // SourceParams has custom encoding so brought to top level with "*"
TaxPercent float64 `form:"tax_percent"`
TaxPercentZero bool `form:"tax_percent,zero"`
Token string `form:"-"` // This doesn't seem to be used?
TrialEnd int64 `form:"trial_end"`
Params `form:"*"`
AccountBalance *int64 `form:"account_balance"`
BusinessVatID string `form:"business_vat_id"`
Coupon *string `form:"coupon"`
DefaultSource string `form:"default_source"`
Description string `form:"description"`
Email string `form:"email"`
Plan string `form:"plan"`
Quantity uint64 `form:"quantity"`
Shipping *CustomerShippingDetails `form:"shipping"`
Source *SourceParams `form:"*"` // SourceParams has custom encoding so brought to top level with "*"
TaxPercent *float64 `form:"tax_percent"`
Token string `form:"-"` // This doesn't seem to be used?
TrialEnd int64 `form:"trial_end"`
}

// SetSource adds valid sources to a CustomerParams object,
Expand Down
2 changes: 1 addition & 1 deletion dispute.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type DisputeStatus string
type DisputeParams struct {
Params `form:"*"`
Evidence *DisputeEvidenceParams `form:"evidence"`
NoSubmit bool `form:"submit,invert"`
Submit *bool `form:"submit"`
}

// DisputeEvidenceParams is the set of parameters that can be used when submitting
Expand Down
18 changes: 2 additions & 16 deletions form/form.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,6 @@ type formOptions struct {
// empty string is a string's zero value and wouldn't normally be encoded.
Empty bool

// Invert indicates that a boolean field's value should be inverted. False
// is the zero value for a boolean so it's convention in the library to
// specify a `No*` field to allow a false to be passed to the API. These
// fields should be annotated with `invert`.
Invert bool

// Zero indicates a field that's specifically defined to workaround the
// fact because 0 is the "zero value" of all int/float types, we can't
// properly encode an explicit 0. It indicates that an explicit zero should
Expand Down Expand Up @@ -159,8 +153,6 @@ func boolEncoder(values *Values, v reflect.Value, keyParts []string, encodeZero
switch {
case options.Empty:
values.Add(FormatKey(keyParts), "")
case options.Invert:
values.Add(FormatKey(keyParts), strconv.FormatBool(false))
case options.Zero:
values.Add(FormatKey(keyParts), "0")
}
Expand Down Expand Up @@ -387,11 +379,11 @@ func makeStructEncoder(t reflect.Type) *structEncoder {
fldKind := fldTyp.Kind()

if Strict && options != nil &&
(options.Empty || options.Invert || options.Zero) &&
(options.Empty || options.Zero) &&
fldKind != reflect.Bool {

panic(fmt.Sprintf(
"Cannot specify `empty`, `invert`, or `zero` for non-boolean field; on: %s/%s",
"Cannot specify `empty`, or `zero` for non-boolean field; on: %s/%s",
t.Name(), reflectField.Name,
))
}
Expand Down Expand Up @@ -468,12 +460,6 @@ func parseTag(tag string) (string, *formOptions) {
}
options.IndexedArray = true

case "invert":
if options == nil {
options = &formOptions{}
}
options.Invert = true

case "zero":
if options == nil {
options = &formOptions{}
Expand Down
4 changes: 0 additions & 4 deletions form/form_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,6 @@ type testStruct struct {
Int64 int64 `form:"int64"`
Int64Ptr *int64 `form:"int64_ptr"`

Inverted bool `form:"inverted,invert"`

Map map[string]interface{} `form:"map"`

Slice []string `form:"slice"`
Expand Down Expand Up @@ -218,8 +216,6 @@ func TestAppendTo(t *testing.T) {
{"int64_ptr", &testStruct{Int64Ptr: &int64Val0}, "0"},
{"int64_ptr", &testStruct{}, ""},

{"inverted", &testStruct{Inverted: true}, "false"},

// Tests map
{
"map[foo]",
Expand Down
14 changes: 5 additions & 9 deletions invoice.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,26 @@ type InvoiceBilling string
// For more details see https://stripe.com/docs/api#create_invoice, https://stripe.com/docs/api#update_invoice.
type InvoiceParams struct {
Params `form:"*"`
ApplicationFee uint64 `form:"application_fee"`
ApplicationFeeZero bool `form:"application_fee,zero"`
ApplicationFee *uint64 `form:"application_fee"`
Billing InvoiceBilling `form:"billing"`
Closed bool `form:"closed"`
Closed *bool `form:"closed"`
Customer string `form:"customer"`
DaysUntilDue uint64 `form:"days_until_due"`
Description string `form:"description"`
DueDate int64 `form:"due_date"`
Forgiven bool `form:"forgiven"`
NoClosed bool `form:"closed,invert"`
Paid bool `form:"paid"`
StatementDescriptor string `form:"statement_descriptor"`
Subscription string `form:"subscription"`
TaxPercent float64 `form:"tax_percent"`
TaxPercentZero bool `form:"tax_percent,zero"`
TaxPercent *float64 `form:"tax_percent"`

// These are all for exclusive use by GetNext.

SubscriptionItems []*SubscriptionItemsParams `form:"subscription_items,indexed"`
SubscriptionNoProrate bool `form:"subscription_prorate,invert"`
SubscriptionPlan string `form:"subscription_plan"`
SubscriptionProrate *bool `form:"subscription_prorate"`
SubscriptionProrationDate int64 `form:"subscription_proration_date"`
SubscriptionQuantity uint64 `form:"subscription_quantity"`
SubscriptionQuantityZero bool `form:"subscription_quantity,zero"`
SubscriptionQuantity *uint64 `form:"subscription_quantity"`
SubscriptionTrialEnd int64 `form:"subscription_trial_end"`
}

Expand Down
2 changes: 1 addition & 1 deletion invoice/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func TestInvoicePay(t *testing.T) {

func TestInvoiceUpdate(t *testing.T) {
invoice, err := Update("in_123", &stripe.InvoiceParams{
Closed: true,
Closed: stripe.Bool(true),
})
assert.Nil(t, err)
assert.NotNil(t, invoice)
Expand Down
18 changes: 8 additions & 10 deletions invoiceitem.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,14 @@ import "encoding/json"
// InvoiceItemParams is the set of parameters that can be used when creating or updating an invoice item.
// For more details see https://stripe.com/docs/api#create_invoiceitem and https://stripe.com/docs/api#update_invoiceitem.
type InvoiceItemParams struct {
Params `form:"*"`
Amount int64 `form:"amount"`
AmountZero bool `form:"amount,zero"`
Currency Currency `form:"currency"`
Customer string `form:"customer"`
Description string `form:"description"`
Discountable bool `form:"discountable"`
Invoice string `form:"invoice"`
NoDiscountable bool `form:"discountable,invert"`
Subscription string `form:"subscription"`
Params `form:"*"`
Amount int64 `form:"amount"`
Currency Currency `form:"currency"`
Customer string `form:"customer"`
Description string `form:"description"`
Discountable *bool `form:"discountable"`
Invoice string `form:"invoice"`
Subscription string `form:"subscription"`
}

// InvoiceItemListParams is the set of parameters that can be used when listing invoice items.
Expand Down
Loading

0 comments on commit afadd09

Please sign in to comment.