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

Better serialization #1478

Merged
merged 1 commit into from
Jan 22, 2019
Merged

Better serialization #1478

merged 1 commit into from
Jan 22, 2019

Conversation

ob-stripe
Copy link
Contributor

@ob-stripe ob-stripe commented Jan 18, 2019

Summary of changes:

  • Renamed ParameterBuilder to FormEncoder
  • Greatly simplified the form-encoding logic by replacing a bunch of reflection and unsafe casts with pattern matching
  • New utility class StringUtils with one new method:
    • ToSnakeCase(): converts strings from CamelCase to snake_case. This is used for Expand* properties on services, to convert from the service's property name (e.g. ExpandFooBar) to the Stripe property name (e.g. foo_bar).
  • New extension method on BaseOptions:
    • ToQueryString(): returns a form-encoded query string containing all of the parameters in the options class, including the ones set in the ExtraParams dictionary and the Expand list.
  • New extension method on Service:
    • Expansions(): returns a list of strings with the properties to expand. E.g. if the service has three properties ExpandFoo, ExpandBar and ExpandAllTheThings and the last two are set to true, the method will return a list containing "bar" and "all_the_things".
  • Greatly simplified the existing extension method ApplyAllParameters() on Service
  • Moved the files that were at the root of Services/ to either Services/_base (for base classes meant to be derived from) or Services/_common (for options classes that can be used as-is and that are not specific to a single service)
  • Updated tests to be consistent with the changes above

Since we have pretty good test coverage for encoding (and I've added a few more tests) I'm fairly confident that I haven't broken anything, but there are a few changes:

  • square brackets characters are no longer URL-encoded. This is consistent with most of our other clients and is meant to make the query strings easier to read by humans (since the server doesn't care one way or the other)
  • expand parameters are now encoded with integer indices (e.g. expand[0]=foo&expand[1]=bar). Previously, they were encoded without indices (expand[]=foo&expand[]=bar). This was inconsistent with the way regular lists are encoded. Now all lists are encoded the same way (I've also checked that the server accepts this).

I'm targeting a new integration-v23 branch despite the fact that there are no breaking changes in this PR, since I have a few more updates planned. The breaking changes should be fairly limited this time around though!

@ob-stripe ob-stripe force-pushed the ob-better-serialization branch 2 times, most recently from 3144510 to 416578d Compare January 19, 2019 20:42
Copy link
Contributor

@remi-stripe remi-stripe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good to me and I like how it was re-organized! As usual though this is core to the library and I'm less comfortable approving myself so please be careful and all that :)

@ob-stripe ob-stripe force-pushed the ob-better-serialization branch 7 times, most recently from 6180511 to 357e31d Compare January 20, 2019 21:01
@ob-stripe ob-stripe changed the base branch from master to integration-v23 January 20, 2019 21:01
@ob-stripe ob-stripe force-pushed the ob-better-serialization branch from 357e31d to 74381f0 Compare January 20, 2019 21:49
@ob-stripe ob-stripe changed the title [wip] Better serialization Better serialization Jan 20, 2019
@ob-stripe
Copy link
Contributor Author

r? @brandur-stripe @remi-stripe
cc @stripe/api-libraries

Copy link
Contributor

@brandur-stripe brandur-stripe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow, awesome work here OB! Love the clarity, and love how many new tests are coming in for these primitives :)

Didn't find anything objectionable in here. Maybe one thing I'd be a little careful about is getting to overzealous with extension methods — they're super cool, but there is a clarity trade-off in that it's immediately clear where StringUtils.ToSnakeCase comes from, but not where str.ToSnakeCase comes from without going up to check the imports.

It's not a big deal in C# because probably everyone is using an IDE of some sort anyway, but something to think about, especially in instances where the extension methods aren't expected to have a whole lot of use, so you don't really need the short-hand that badly anyway.

LGTM.

@ob-stripe
Copy link
Contributor Author

but there is a clarity trade-off in that it's immediately clear where StringUtils.ToSnakeCase comes from, but not where str.ToSnakeCase comes from without going up to check the imports.

Great point, I did get a little carried away with the extension methods :) Looking at Newtonsoft.Json's source code, they do have a static method named exactly StringUtils.ToSnakeCase.

@ob-stripe ob-stripe force-pushed the ob-better-serialization branch from bd1d7b8 to 247a38f Compare January 22, 2019 23:13
@ob-stripe ob-stripe merged commit bbef0f0 into integration-v23 Jan 22, 2019
@ob-stripe ob-stripe deleted the ob-better-serialization branch January 22, 2019 23:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants