Last active
February 26, 2019 14:14
-
-
Save EVODelavega/288a109d8ed9e908c99175b75cfb70a8 to your computer and use it in GitHub Desktop.
Typed stripe event generator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"encoding/json" | |
"flag" | |
"fmt" | |
"html/template" | |
"io/ioutil" | |
"os" | |
"sort" | |
) | |
const ( | |
templateStr = `// Code generated by go generate; DO NOT EDIT. | |
// generated from template in generate/events.go | |
package quote | |
import ( | |
"encoding/json" | |
"github.com/stripe/stripe-go" | |
) | |
type Event struct { | |
ObjectSet bool | |
Event stripe.Event | |
{{ range $t := .Types }}{{ $t }} *stripe.{{ $t }} | |
{{ end }} | |
} | |
func (e *Event) UnmarshalJSON(data []byte) error { | |
if err := json.Unmarshal(data, &e.Event); err != nil { | |
return err | |
} | |
switch e.Event.Type { {{ range $key, $types := .Data }} | |
case "{{ $key }}": | |
{{ $len := len $types }}{{ if eq $len 1 }}{{ range $field := $types }} | |
if err := json.Unmarshal([]byte(e.Event.Data.Raw), e.{{ $field }}); err != nil { | |
return err | |
} | |
e.ObjectSet = true{{ end }} {{ else }} | |
var err error{{ range $field := $types }} | |
err = json.Unmarshal([]byte(e.Event.Data.Raw), e.{{ $field }}) | |
// correct type {{ $field }}, return early | |
if err == nil { | |
e.ObjectSet = true | |
return nil | |
}{{ end }} {{ end }} {{ end }} | |
} | |
return nil | |
} | |
func (e Event) MarshalJSON() ([]byte, error) { | |
type mEvent struct { | |
stripe.Event | |
Object interface{} ` + "`json:" + `"object"` + "`" + ` | |
} | |
me := mEvent{ | |
Event: e.Event, | |
} | |
{{ range $_, $types := .Data }} | |
{{ range $field := $types }} | |
if e.{{ $field }} != nil { | |
me.Object = e.{{ $field }} | |
return json.Marshal(me) | |
}{{ end }} {{ end }} | |
// fallback to default -> marshal raw stripe event | |
return json.Marshal(me.Event) | |
} | |
` | |
) | |
type templateVars struct { | |
Data map[string][]string | |
Types []string | |
} | |
func main() { | |
var ( | |
inPath, out string | |
) | |
flag.StringVar(&inPath, "input", "./events.json", "input file to generate code from") | |
flag.StringVar(&out, "output", "", "output file") | |
flag.Parse() | |
jsonStr, err := ioutil.ReadFile(inPath) | |
if err != nil { | |
fmt.Sprintf("Error reading file: %+v\n", err) | |
os.Exit(1) | |
} | |
vars := templateVars{} | |
if err := json.Unmarshal(jsonStr, &vars.Data); err != nil { | |
fmt.Sprintf("Error unmarshalling file contents: %+v\n", err) | |
os.Exit(1) | |
} | |
stripeTypes := map[string]struct{}{} | |
for _, types := range vars.Data { | |
for _, t := range types { | |
if _, ok := stripeTypes[t]; !ok { | |
stripeTypes[t] = struct{}{} | |
vars.Types = append(vars.Types, t) | |
} | |
} | |
} | |
sort.Strings(vars.Types) | |
to := os.Stdout | |
if out != "" { | |
f, err := os.Create(out) | |
if err != nil { | |
fmt.Sprintf("Could not create file %s: %+v\n", out, err) | |
os.Exit(1) | |
} | |
defer f.Close() | |
to = f | |
} | |
t := template.Must(template.New("quote").Parse(templateStr)) | |
t.Execute(to, vars) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"account.updated": [ | |
"Account" | |
], | |
"account.application.authorized": [ | |
"Application" | |
], | |
"account.application.deauthorized": [ | |
"Application" | |
], | |
"account.external_account.created": [ | |
"Card", | |
"BankAccount" | |
], | |
"account.external_account.deleted": [ | |
"Card", | |
"BankAccount" | |
], | |
"account.external_account.updated": [ | |
"Card", | |
"BankAccount" | |
], | |
"application_fee.created": [ | |
"ApplicationFee" | |
], | |
"application_fee.refunded": [ | |
"ApplicationFee" | |
], | |
"application_fee.refund.updated": [ | |
"FeeRefund" | |
], | |
"balance.available": [ | |
"Balance" | |
], | |
"charge.captured": [ | |
"Charge" | |
], | |
"charge.expired": [ | |
"Charge" | |
], | |
"charge.failed": [ | |
"Charge" | |
], | |
"charge.pending": [ | |
"Charge" | |
], | |
"charge.refunded": [ | |
"Charge" | |
], | |
"charge.succeeded": [ | |
"Charge" | |
], | |
"charge.updated": [ | |
"Charge" | |
], | |
"charge.dispute.closed": [ | |
"Dispute" | |
], | |
"charge.dispute.created": [ | |
"Dispute" | |
], | |
"charge.dispute.funds_reinstated": [ | |
"Dispute" | |
], | |
"charge.dispute.funds_withdrawn": [ | |
"Dispute" | |
], | |
"charge.dispute.updated": [ | |
"Dispute" | |
], | |
"charge.refund.updated": [ | |
"Refund" | |
], | |
"coupon.created": [ | |
"Coupon" | |
], | |
"coupon.deleted": [ | |
"Coupon" | |
], | |
"coupon.updated": [ | |
"Coupon" | |
], | |
"customer.created": [ | |
"Customer" | |
], | |
"customer.deleted": [ | |
"Customer" | |
], | |
"customer.updated": [ | |
"Customer" | |
], | |
"customer.discount.created": [ | |
"Discount" | |
], | |
"customer.discount.deleted": [ | |
"Discount" | |
], | |
"customer.discount.updated": [ | |
"Discount" | |
], | |
"customer.source.created": [ | |
"Card" | |
], | |
"customer.source.deleted": [ | |
"Card" | |
], | |
"customer.source.expiring": [ | |
"Card" | |
], | |
"customer.source.updated": [ | |
"Card" | |
], | |
"customer.subscription.created": [ | |
"Subscription" | |
], | |
"customer.subscription.deleted": [ | |
"Subscription" | |
], | |
"customer.subscription.trial_will_end": [ | |
"Subscription" | |
], | |
"customer.subscription.updated": [ | |
"Subscription" | |
], | |
"file.created": [ | |
"File" | |
], | |
"invoice.created": [ | |
"Invoice" | |
], | |
"invoice.deleted": [ | |
"Invoice" | |
], | |
"invoice.finalized": [ | |
"Invoice" | |
], | |
"invoice.marked_uncollectible": [ | |
"Invoice" | |
], | |
"invoice.payment_failed": [ | |
"Invoice" | |
], | |
"invoice.payment_succeeded": [ | |
"Invoice" | |
], | |
"invoice.sent": [ | |
"Invoice" | |
], | |
"invoice.upcoming": [ | |
"Invoice" | |
], | |
"invoice.updated": [ | |
"Invoice" | |
], | |
"invoice.voided": [ | |
"Invoice" | |
], | |
"invoiceitem.created": [ | |
"InvoiceItem" | |
], | |
"invoiceitem.deleted": [ | |
"InvoiceItem" | |
], | |
"invoiceitem.updated": [ | |
"InvoiceItem" | |
], | |
"issuing_authorization.created": [ | |
"IssuingAuthorization" | |
], | |
"issuing_authorization.request": [ | |
"IssuingAuthorization" | |
], | |
"issuing_authorization.updated": [ | |
"IssuingAuthorization" | |
], | |
"issuing_card.created": [ | |
"IssuingCard" | |
], | |
"issuing_card.updated": [ | |
"IssuingCard" | |
], | |
"issuing_cardholder.created": [ | |
"IssuingCardholder" | |
], | |
"issuing_cardholder.updated": [ | |
"IssuingCardholder" | |
], | |
"issuing_dispute.created": [ | |
"IssuingDispute" | |
], | |
"issuing_dispute.updated": [ | |
"IssuingDispute" | |
], | |
"issuing_transaction.created": [ | |
"IssuingTransaction" | |
], | |
"issuing_transaction.updated": [ | |
"IssuingTransaction" | |
], | |
"order.created": [ | |
"Order" | |
], | |
"order.payment_failed": [ | |
"Order" | |
], | |
"order.payment_succeeded": [ | |
"Order" | |
], | |
"order.updated": [ | |
"Order" | |
], | |
"order_return.created": [ | |
"OrderReturn" | |
], | |
"payment_intent.amount_capturable_updated": [ | |
"PaymentIntent" | |
], | |
"payment_intent.created": [ | |
"PaymentIntent" | |
], | |
"payment_intent.payment_failed": [ | |
"PaymentIntent" | |
], | |
"payment_intent.requires_capture": [ | |
"PaymentIntent" | |
], | |
"payment_intent.succeeded": [ | |
"PaymentIntent" | |
], | |
"payout.canceled": [ | |
"Payout" | |
], | |
"payout.created": [ | |
"Payout" | |
], | |
"payout.failed": [ | |
"Payout" | |
], | |
"payout.paid": [ | |
"Payout" | |
], | |
"payout.updated": [ | |
"Payout" | |
], | |
"plan.created": [ | |
"Plan" | |
], | |
"plan.deleted": [ | |
"Plan" | |
], | |
"plan.updated": [ | |
"Plan" | |
], | |
"product.created": [ | |
"Product" | |
], | |
"product.deleted": [ | |
"Product" | |
], | |
"product.updated": [ | |
"Product" | |
], | |
"recipient.created": [ | |
"Recipient" | |
], | |
"recipient.deleted": [ | |
"Recipient" | |
], | |
"recipient.updated": [ | |
"Recipient" | |
], | |
"reporting.report_run.failed": [ | |
"ReportRun" | |
], | |
"reporting.report_run.succeeded": [ | |
"ReportRun" | |
], | |
"reporting.report_type.updated": [ | |
"ReportType" | |
], | |
"review.closed": [ | |
"Review" | |
], | |
"review.opened": [ | |
"Review" | |
], | |
"sigma.scheduled_query_run.created": [ | |
"SigmaScheduledQueryRun" | |
], | |
"sku.created": [ | |
"SKU" | |
], | |
"sku.deleted": [ | |
"SKU" | |
], | |
"sku.updated": [ | |
"SKU" | |
], | |
"source.canceled": [ | |
"Source" | |
], | |
"source.chargeable": [ | |
"Source" | |
], | |
"source.failed": [ | |
"Source" | |
], | |
"source.mandate_notification": [ | |
"Source" | |
], | |
"source.refund_attributes_required": [ | |
"Source" | |
], | |
"source.transaction.created": [ | |
"SourceTransaction" | |
], | |
"source.transaction.updated": [ | |
"SourceTransaction" | |
], | |
"topup.canceled": [ | |
"Topup" | |
], | |
"topup.created": [ | |
"Topup" | |
], | |
"topup.failed": [ | |
"Topup" | |
], | |
"topup.reversed": [ | |
"Topup" | |
], | |
"topup.succeeded": [ | |
"Topup" | |
], | |
"transfer.created": [ | |
"Transfer" | |
], | |
"transfer.reversed": [ | |
"Transfer" | |
], | |
"transfer.updated": [ | |
"Transfer" | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment