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

Add helper to go from []T to []*T for string, int64, float64, bool #837

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions account/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ func TestAccountNew(t *testing.T) {
ExternalAccount: &stripe.AccountExternalAccountParams{
Token: stripe.String("tok_123"),
},
RequestedCapabilities: []*string{
stripe.String("card_payments"),
},
RequestedCapabilities: stripe.StringSlice([]string{
"card_payments",
}),
Settings: &stripe.AccountSettingsParams{
Branding: &stripe.AccountSettingsBrandingParams{
Icon: stripe.String("file_123"),
Expand Down
12 changes: 6 additions & 6 deletions checkout/session/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ func TestCheckoutSessionNew(t *testing.T) {
Amount: stripe.Int64(1234),
Currency: stripe.String(string(stripe.CurrencyUSD)),
Description: stripe.String("description"),
Images: []*string{
stripe.String("https://stripe.com/image1"),
},
Images: stripe.StringSlice([]string{
"https://stripe.com/image1",
}),
Name: stripe.String("name"),
Quantity: stripe.Int64(2),
},
Expand All @@ -41,9 +41,9 @@ func TestCheckoutSessionNew(t *testing.T) {
Name: stripe.String("name"),
},
},
PaymentMethodTypes: []*string{
stripe.String("card"),
},
PaymentMethodTypes: stripe.StringSlice([]string{
"card",
}),
SuccessURL: stripe.String("https://stripe.com/success"),
})
assert.Nil(t, err)
Expand Down
6 changes: 3 additions & 3 deletions issuing/card/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ func TestIssuingCardNew(t *testing.T) {
SpendingLimits: []*stripe.IssuingAuthorizationControlsSpendingLimitsParams{
{
Amount: stripe.Int64(1000),
Categories: []*string{
stripe.String("financial_institutions"),
},
Categories: stripe.StringSlice([]string{
"financial_institutions",
}),
Interval: stripe.String(string(stripe.IssuingSpendingLimitIntervalAllTime)),
},
},
Expand Down
6 changes: 3 additions & 3 deletions paymentintent/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ func TestPaymentIntentNew(t *testing.T) {
intent, err := New(&stripe.PaymentIntentParams{
Amount: stripe.Int64(123),
Currency: stripe.String(string(stripe.CurrencyUSD)),
PaymentMethodTypes: []*string{
stripe.String("card"),
},
PaymentMethodTypes: stripe.StringSlice([]string{
"card",
}),
})
assert.Nil(t, err)
assert.NotNil(t, intent)
Expand Down
8 changes: 4 additions & 4 deletions paymentsource/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ func TestSourceVerify(t *testing.T) {

func TestSourceObjectVerify(t *testing.T) {
source, err := Verify("src_123", &stripe.SourceVerifyParams{
Values: []*string{
stripe.String("32"),
stripe.String("45"),
},
Values: stripe.StringSlice([]string{
"32",
"45",
}),
})
assert.Nil(t, err)
assert.NotNil(t, source)
Expand Down
8 changes: 4 additions & 4 deletions product/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ func TestProductNew(t *testing.T) {
Name: stripe.String("Test Name"),
Description: stripe.String("This is a description"),
Caption: stripe.String("This is a caption"),
Attributes: []*string{
stripe.String("Attr1"),
stripe.String("Attr2"),
},
Attributes: stripe.StringSlice([]string{
"Attr1",
"Attr2",
}),
URL: stripe.String("http://example.com"),
Shippable: stripe.Bool(true),
PackageDimensions: &stripe.PackageDimensionsParams{
Expand Down
36 changes: 36 additions & 0 deletions stripe.go
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,15 @@ func BoolValue(v *bool) bool {
return false
}

// BoolSlice returns a slice of bool pointers given a slice of bools.
func BoolSlice(v []bool) []*bool {
out := make([]*bool, len(v))
for i := range v {
out[i] = &v[i]
}
return out
}

// Float64 returns a pointer to the float64 value passed in.
func Float64(v float64) *float64 {
return &v
Expand All @@ -620,6 +629,15 @@ func Float64Value(v *float64) float64 {
return 0
}

// Float64Slice returns a slice of float64 pointers given a slice of float64s.
func Float64Slice(v []float64) []*float64 {
out := make([]*float64, len(v))
for i := range v {
out[i] = &v[i]
}
return out
}

// FormatURLPath takes a format string (of the kind used in the fmt package)
// representing a URL path with a number of parameters that belong in the path
// and returns a formatted string.
Expand Down Expand Up @@ -741,6 +759,15 @@ func Int64Value(v *int64) int64 {
return 0
}

// Int64Slice returns a slice of int64 pointers given a slice of int64s.
func Int64Slice(v []int64) []*int64 {
out := make([]*int64, len(v))
for i := range v {
out[i] = &v[i]
}
return out
}

// NewBackends creates a new set of backends with the given HTTP client. You
// should only need to use this for testing purposes or on App Engine.
func NewBackends(httpClient *http.Client) *Backends {
Expand Down Expand Up @@ -816,6 +843,15 @@ func StringValue(v *string) string {
return ""
}

// StringSlice returns a slice of string pointers given a slice of strings.
func StringSlice(v []string) []*string {
out := make([]*string, len(v))
for i := range v {
out[i] = &v[i]
}
return out
}

//
// Private constants
//
Expand Down
45 changes: 45 additions & 0 deletions stripe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -744,6 +744,51 @@ func TestResponseToError(t *testing.T) {
assert.Equal(t, expectedDeclineCode, cardErr.DeclineCode)
}

func TestStringSlice(t *testing.T) {
input := []string{"a", "b", "c"}
result := stripe.StringSlice(input)

assert.Equal(t, "a", *result[0])
assert.Equal(t, "b", *result[1])
assert.Equal(t, "c", *result[2])

assert.Equal(t, 0, len(stripe.StringSlice(nil)))
}

func TestInt64Slice(t *testing.T) {
input := []int64{8, 7, 6}
result := stripe.Int64Slice(input)

assert.Equal(t, int64(8), *result[0])
assert.Equal(t, int64(7), *result[1])
assert.Equal(t, int64(6), *result[2])

assert.Equal(t, 0, len(stripe.Int64Slice(nil)))
}

func TestFloat64Slice(t *testing.T) {
input := []float64{8, 7, 6}
result := stripe.Float64Slice(input)

assert.Equal(t, float64(8), *result[0])
assert.Equal(t, float64(7), *result[1])
assert.Equal(t, float64(6), *result[2])

assert.Equal(t, 0, len(stripe.Float64Slice(nil)))
}

func TestBoolSlice(t *testing.T) {
input := []bool{true, false, true, false}
result := stripe.BoolSlice(input)

assert.Equal(t, true, *result[0])
assert.Equal(t, false, *result[1])
assert.Equal(t, true, *result[2])
assert.Equal(t, false, *result[3])

assert.Equal(t, 0, len(stripe.BoolSlice(nil)))
}

//
// ---
//
Expand Down
12 changes: 6 additions & 6 deletions webhookendpoint/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ func TestWebhookEndpointList(t *testing.T) {

func TestWebhookEndpointNew(t *testing.T) {
endpoint, err := New(&stripe.WebhookEndpointParams{
EnabledEvents: []*string{
stripe.String("charge.succeeded"),
},
EnabledEvents: stripe.StringSlice([]string{
"charge.succeeded",
}),
URL: stripe.String("https://stripe.com"),
})
assert.Nil(t, err)
Expand All @@ -42,9 +42,9 @@ func TestWebhookEndpointNew(t *testing.T) {

func TestWebhookEndpointUpdate(t *testing.T) {
endpoint, err := Update("we_123", &stripe.WebhookEndpointParams{
EnabledEvents: []*string{
stripe.String("charge.succeeded"),
},
EnabledEvents: stripe.StringSlice([]string{
"charge.succeeded",
}),
})
assert.Nil(t, err)
assert.NotNil(t, endpoint)
Expand Down