-
Notifications
You must be signed in to change notification settings - Fork 461
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
[WIP/RFC] Begin custom unmarshalling for events, do not use maps #729
Conversation
Yeah, this would definitely be a nice shortcut most of the time — the one consideration here though is that it's possible to receive both webhooks and events that are of a different version than the one that stripe-go is pegged to, and when that happens, unmarshaling may fail. If we brought something like this in, we'd have to be really careful to specify that it's a "best effort" approach and that unmarshaling is not guaranteed to succeed. That makes this addition a bit of a trade off unfortunately. It will mostly provide better usability, but will also be more fragile. If you didn't see it already, you should also take a look at the provided With regards to the patch: in the custom @remi-stripe @ob-stripe Do either of you have any feelings around whether we should pursue this or not? |
@brahn-stripe WRT the last bit (just looking at Another reason why I chose to be explicit is so I'd have a |
But the
Yeah, having the map as a backup would help with versioning et all. I'd move that out of the |
@brandur-stripe If we should always unmarshal into a map and additionally try to unmarshal into a typed object, then there's very little point to me pursuing this further, really... the |
Maybe some helper functions to get the type and make the unmarshalling a bit easier and an example for the godoc documentation on how to use them? That way each user will not have to come up with their own way of doing it. |
Personally, I wouldn't worry about the performance aspects of the approach too much ... recall that unmarshaling JSON is really, really fast compared to the network call you just made. So much so that it's practically negligible by comparison.
I'm in fully support of this idea — basically leave the default as an untyped map, but have a utility function that will try to transform it into a typed structure, and documented carefully in such a way that it indicates there's some possibility of failure in case of divergent version representations, etc. |
I'm looking into handling certain events again (after finishing off some other stuff that came up). There seems to be 2 slight misunderstandings at play here:
parts := strings.Split(evet.Type, ".")
eventType := parts[0] Sadly, there are events like eventType := parts[len(parts)-2] // next to last substring This would indeed work, but the problem with the type cardOrBankAccount struct {
*Card
*BankAccount
// whatever other types are possible
}
switch event.Type {
case "some.external_account.action", "another.external_account.action":
var typed cardOrBankAccount
if err := json.Unmarshal([]byte(event.Raw), &typed); err != nil {
return err
}
event.setExternalAccount(typed)
}
func (e *Event) setExternalAccount(data cardOrBankAccount) {
if data.Card != nil {
e.Card = data.Card
return
}
e.BankAccount = data.BankAccount
} This PR was clearly not in a state where my intentions were clear to everyone. Hopefully this cleared up any confusion, in which case, I'd like to know if this is still something worth pursuing. |
Hey @EVODelavega, I'd still say that decoding object type based on If you reference our Stripe implementation for Ruby or for PHP, you'll see that they already have a function that does this exact thing of converting an |
@brahn-stripe I've not run into problems so far. In fact, yesterday I've used the API doc page to extract some JSON to map various events onto specific types, and wrote a little generator to generate a typed event wrapper. It basically unmarshals everything into the existing stripe.Event type, whereas my event hook handler is unmarshalling the body in this wrapped type. Due to the custom unmarshaller, I can unmarshal the raw body into the specific type. That's how I'm handing things now, and I'm not experiencing any issues. I can change this generator somewhat, and use the API specs as a source I think (although I've not checked yet whether or not the events are in there). Here's the gist of the generator I hacked together: https://gist.github.com/EVODelavega/288a109d8ed9e908c99175b75cfb70a8 |
Elias Van Ootegem seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account. You have signed the CLA already but the status is still pending? Let us recheck it. |
Hi, I'm sorry it's been so long since you've opened the pull request without us getting back. We are looking at improving event handling experience across languages but don't have a full design for it yet. I'm going to close this PR because of it's age. My apologies once again. |
Bumps [sequel](https://github.com/jeremyevans/sequel) from 5.59.0 to 5.60.1. - [Release notes](https://github.com/jeremyevans/sequel/releases) - [Changelog](https://github.com/jeremyevans/sequel/blob/master/CHANGELOG) - [Commits](jeremyevans/sequel@5.59.0...5.60.1) --- updated-dependencies: - dependency-name: sequel dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] <[email protected]> Signed-off-by: dependabot[bot] <[email protected]> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
I'm in the process of implementing webhooks, and in doing so noticed that all events are essentially unmarshalled into a
map[string]interface{}
type. This makes code messy, error-prone, and due to the insane amount of type assertions might tempt people to simply check the event type, and proceed to do something like:Essentially unmarshalling the same payload twice (I know I briefly considered this). Rather than adding all the listed events right now, I thought I'd open this PR as an RFC, and see if this would be a welcome change. If it isn't, then I'd rather not waste too much time going through the entire list before being told it's not worth the effort.