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

Config and Location swift decodable conformance #559

Merged
merged 17 commits into from
Feb 25, 2019

Conversation

Scollaco
Copy link
Contributor

📲 What

Makes the models Config, Location, and Project.Country conform to Swift.Decodable.

🤔 Why

This puts us one step closer to remove the Argo library from our codebase. Argo was included in our project before Swift Decodable be released by Apple and its removal will result in the deletion of tons of boilerplate code. Besides that, all the new models no longer conform to the Argo.Decodable protocol.

🛠 How

To make the models conform to Swift Decodable, I simply created an extension that conforms to the protocol with a init(decoder:) function. The unit tests make sure that the models are being decoded correctly. In the future, we will need to create a new fetch function on our Service+RequestHelpers with the correct return type (SignalProducer<A, ErrorEnvelope>) and maybe with a keyDecodingStrategy.

✅ Acceptance criteria

  • Config decoding shouldn't be affected, since we are not directly using Swift decoder yet.

  • ConfigTests should pass.

  • Location decoding shouldn't be affected.

  • LocationTests should pass.

  • Project.Country decoding shouldn't be affected.

  • Project.CountryTests should pass.

  • Got planned refactors to come

  • Will add more tests

  • Need feedback on a design

Copy link
Contributor

@justinswart justinswart left a comment

Choose a reason for hiding this comment

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

Looking good, had two suggestions 👍

self.launchedCountries = try values.decode([Project.Country].self, forKey: .launchedCountries)
self.locale = try values.decode(String.self, forKey: .locale)

let stripe = try values.decode([String: String].self, forKey: .stripe)
Copy link
Contributor

@justinswart justinswart Jan 23, 2019

Choose a reason for hiding this comment

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

I think we can replace the if/else here and set the property directly with:

self.stripePublishableKey = try values
  .nestedContainer(keyedBy: StripeCodingKeys.self, forKey: .stripe)
  .decode(String.self, forKey: .stripePublishableKey)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nice solution!

extension Location: Swift.Decodable {

enum CodingKeys: String, CodingKey {
case country, displayableName = "displayable_name", id, localizedName = "localized_name", name
Copy link
Contributor

Choose a reason for hiding this comment

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

I think let's put these on new lines for consistency?

@justinswart
Copy link
Contributor

So what if we tried to use these decodings in place of the Argo ones?

@Scollaco
Copy link
Contributor Author

So what if we tried to use these decodings in place of the Argo ones?

We would just need to create a new request function to return a SignalProducer<M, ErrorEnvelope> where M == Swift.Decodable and use JSONDecoder instead. I actually had to create this function temporarily to see if it was working. So this model would basically work with Argo and Swift.Decodable.

@Scollaco Scollaco closed this Jan 28, 2019
@Scollaco Scollaco reopened this Jan 28, 2019
@justinswart
Copy link
Contributor

@Scollaco I think we should merge this into a feature branch because it will be unused code until we have the request functions to handle it, what do you think?

@justinswart justinswart mentioned this pull request Feb 6, 2019
5 tasks
@Scollaco
Copy link
Contributor Author

Scollaco commented Feb 6, 2019

@Scollaco I think we should merge this into a feature branch because it will be unused code until we have the request functions to handle it, what do you think?

Yeah, I see your point. The only con I see in waiting is that we would have to do the same to all models (make them conform to both Argo and Swift decodable) before merge the feature branch, and this can take a long time and end up forgotten, like it happened in the past. And since these 2 models still conform Argo.Decodable, our codebase as it is wouldn't be affected, making possible to get away from argo progressively. We can also talk to the team and see what they think.

@justinswart justinswart added the blocked a PR that is blocked for external reasons label Feb 12, 2019
@justinswart justinswart removed the blocked a PR that is blocked for external reasons label Feb 22, 2019
@Scollaco Scollaco merged commit a7051b6 into master Feb 25, 2019
@Scollaco Scollaco deleted the config_swift_decodable_conformance branch February 25, 2019 16:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants