Skip to content

Added tests and fix to ensure RawDataContainer includes all data#1565

Merged
NachoSoto merged 3 commits into
mainfrom
raw-data-container-all-data
May 24, 2022
Merged

Added tests and fix to ensure RawDataContainer includes all data#1565
NachoSoto merged 3 commits into
mainfrom
raw-data-container-all-data

Conversation

@NachoSoto

@NachoSoto NachoSoto commented May 11, 2022

Copy link
Copy Markdown
Contributor

Follow up to #1496. Depends on #1567, #1568, #1604.

The solution implemented in #1496 was very simple, but as pointed out by @joshdholtz, it was only encoding the data that was formally decoded by the structs.
We had no test coverage for this, so it went unnoticed.

The new solution adds a new field:

@IgnoreEncodable @IgnoreHashable
var rawData: [String: Any]

Which, together with a new helper method decodeRawData(), allows storing all the data to be used by CustomerInfo's RawDataContainer implementation.

Unfortunately, despite many attempts, I realized that this can't be abstracted into a property wrapper, because a KeyedDecodingContainer can't access the parent Decoder.
That's why the CustomerInfoResponse and CustomerInfoResponse.Entitlement must manually call this method.

@NachoSoto NachoSoto force-pushed the customer-info-decodable branch from fb12779 to 74be49e Compare May 11, 2022 19:56
@NachoSoto NachoSoto force-pushed the raw-data-container-all-data branch 2 times, most recently from f9fb2d6 to 56a12b6 Compare May 11, 2022 22:56
@NachoSoto NachoSoto force-pushed the customer-info-decodable branch from 74be49e to db27ce0 Compare May 11, 2022 23:00
@NachoSoto NachoSoto force-pushed the raw-data-container-all-data branch from 56a12b6 to 2301693 Compare May 11, 2022 23:00
@NachoSoto NachoSoto changed the title [WIP] Include all JSON data in RawDataContainer Include all JSON data in RawDataContainer May 11, 2022
@NachoSoto NachoSoto changed the title Include all JSON data in RawDataContainer Added tests and fix to ensure RawDataContainer includes all data May 11, 2022
@NachoSoto NachoSoto force-pushed the raw-data-container-all-data branch from 2301693 to af2b181 Compare May 11, 2022 23:03
@NachoSoto NachoSoto requested a review from a team May 11, 2022 23:04
@NachoSoto NachoSoto marked this pull request as ready for review May 11, 2022 23:12
@NachoSoto NachoSoto force-pushed the customer-info-decodable branch from db27ce0 to 6242220 Compare May 12, 2022 21:50
@NachoSoto NachoSoto force-pushed the raw-data-container-all-data branch from af2b181 to 9e0a81e Compare May 12, 2022 21:53
@NachoSoto NachoSoto force-pushed the customer-info-decodable branch from 6242220 to d36135d Compare May 19, 2022 18:07
@NachoSoto NachoSoto force-pushed the raw-data-container-all-data branch 2 times, most recently from 38b6f32 to d9f8c16 Compare May 19, 2022 18:29
@NachoSoto NachoSoto force-pushed the customer-info-decodable branch from a243e5e to 093d620 Compare May 20, 2022 17:17
@NachoSoto NachoSoto force-pushed the raw-data-container-all-data branch from d9f8c16 to 49198cf Compare May 20, 2022 17:32
Comment thread Sources/Misc/Obsoletions.swift Outdated
@NachoSoto NachoSoto force-pushed the customer-info-decodable branch from 093d620 to 8e41b91 Compare May 24, 2022 17:58
@NachoSoto NachoSoto force-pushed the raw-data-container-all-data branch from 49198cf to b466521 Compare May 24, 2022 18:01
Comment thread Sources/Misc/RawDataContainer.swift Outdated

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This logs an error and simply returns empty data. We wouldn't want to make the entire structure fail to decode because of this.

Comment on lines 20 to 25

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This is the new way of making CustomerInfoResponse: Hashable only look at subscriber :)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

nice!! Perhaps we could add that as a comment here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Oh yeah, I removed the comment from the previous implementation but that would be good here.

Comment on lines 81 to 84

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I think this is pretty elegant 🤓

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

yeah, the syntax for this is :chefskiss:

Comment on lines 100 to 108

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This is the only downside, but it's not too bad. We must manually implement this method. But reusing decodeRawData() it's pretty clean.

@NachoSoto NachoSoto force-pushed the customer-info-decodable branch from 8e41b91 to 181958f Compare May 24, 2022 21:15
@NachoSoto NachoSoto force-pushed the raw-data-container-all-data branch from 7951bed to 09605aa Compare May 24, 2022 21:16
Comment on lines 20 to 25

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

nice!! Perhaps we could add that as a comment here?

Comment on lines 81 to 84

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

yeah, the syntax for this is :chefskiss:

Comment thread Sources/Networking/Responses/CustomerInfoResponse.swift Outdated
Base automatically changed from customer-info-decodable to main May 24, 2022 21:22
NachoSoto added a commit that referenced this pull request May 24, 2022
### Depends on:
- #1537
- #1540
- #1541
- #1543
- #1546
- #1547
- #1550
- #1551,
- #1565

### Changes

- Removed all custom deserialization code thanks to #1537, #1541, and #1543.
- The format of the customer info response is now very concise and only in one place (`CustomerInfoResponse`) instead of being passed through dictionaries and magic strings everywhere.
- `CustomerInfo` was combining the dictionaries inside of `"subscriptions"` and `"non_subscriptions"`, despite them having different formats. This is now made explicit through type conversions between the two, (see `CustomerInfoResponse.allTransactionsByProductId`).
- Removed all custom `CustomerInfo` errors since deserialization is automatic, and the underlying `Error` information will be provided thanks to `ErrorUtils.logDecodingError`.
- `CustomerInfo` deserialization always wraps errors into `ErrorCode.customerInfoError`, which is now also covered in a new test.
- Added a new snapshot test that covers `CustomerInfo` serialization by storing the result in a JSON file.

### Other changes:
- `CustomerInfo` equality is now automatic through the `CustomerInfoResponse` `Equatable` conformance.
- Simplified `schemaVersion` handling
- All tests still use `CustomerInfo(data: [String: Any])`, but that method is only visible for tests now, and it uses the underlying `Decodable` implementation (see `CustomerInfo+TestExtensions.swift`).
- Added extra tests for `CustomerInfoResponse` that use a fixture `CustomerInfo.json`, which is easier to maintain than having JSON in code.
- `RawDataContainer` implementation is changed in #1565.

### TODO:
- [x] Fix all tests
- [x] Update `RawValueContainer`
- [x] Consider recovering `CustomerInfoError`
- [x] Look at all previously logged errors
- [x] Finish `CustomerInfoResponseTests`
- [x] Handle all TODOs
- [x] Make `RawDataContainer` actually store all the original data: #1565
NachoSoto added 3 commits May 24, 2022 14:22
Follow up to #1496. Depends on #1567 and #1568.

The solution implemented in #1496 was very simple, but [as pointed out by @joshdholtz](#1496 (comment)), it was only encoding the data that was formally decoded by the `struct`s.
We had no test coverage for this, so it went unnoticed.

The new solution adds a new field:
```swift
@IgnoreEncodable
var rawData: [String: Any]
```

Which, together with a new helper method `decodeRawData()`, allows storing all the data to be used by `CustomerInfo`'s `RawDataContainer` implementation.

Unfortunately, despite many attempts, I realized that this can't be abstracted into a property wrapper, because a `KeyedDecodingContainer` can't access the parent `Decoder`.
That's why the `CustomerInfoResponse` and `CustomerInfoResponse.Entitlement` must manually call this method.
@NachoSoto NachoSoto force-pushed the raw-data-container-all-data branch from 09605aa to 7e630a3 Compare May 24, 2022 21:22
@NachoSoto NachoSoto merged commit 395322a into main May 24, 2022
@NachoSoto NachoSoto deleted the raw-data-container-all-data branch May 24, 2022 21:23
@joshdholtz joshdholtz mentioned this pull request May 26, 2022
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