Skip to content
This repository has been archived by the owner on Oct 19, 2024. It is now read-only.

Add support for encoding any Encodable #63

Merged
merged 4 commits into from
Sep 13, 2021
Merged

Conversation

paulw11
Copy link
Contributor

@paulw11 paulw11 commented Sep 11, 2021

This PR adds minimal support for encoding any value that conforms to Encodable (#58).
Tests have been updated for this case.

What is missing:

Equatable and Hashable support for a wrapped Codable
Decodable support, however it does not make much sense to decode directly to AnyCodable anyway; You would decode the concrete type and then type erase to AnyCodable if required.

@NoahKamara
Copy link

Oh wow this is exactly what I needed right now :D good timing @paulw11

@minacle
Copy link
Collaborator

minacle commented Sep 13, 2021

How about decoding? Will it decoded correctly?

@paulw11
Copy link
Contributor Author

paulw11 commented Sep 13, 2021

As per my comment on the PR - What would be the semantics of decoding an AnyCodable - It is type erased. There are a (potentially) infinite number of concrete types that could be decoded when an object is encountered during the decoding process. The current implementation will decode an embedded object as a dictionary, which seems like a reasonable approach.

Hopefully when you are decoding something you know what you are decoding. If you don't know then you would need to implement your own decoding logic that determined the embedded type somehow.

Conceivably you could provide a list of potential types and when the decoder encountered an embedded object it could try and decode them, but there is a risk that if multiple types matched the object signature you would end up with the wrong thing.

@minacle
Copy link
Collaborator

minacle commented Sep 13, 2021

I know it is redundant but please add the same test for decoding encoding into AnyEncodableTests.swift.

Thank you for your contributions.

@paulw11
Copy link
Contributor Author

paulw11 commented Sep 13, 2021

Do you mean add code to the test to confirm that decoding the result of encoding gives the expected result?

This sounds like a reasonable test that should probably have been included in current test suite.

@minacle
Copy link
Collaborator

minacle commented Sep 13, 2021

Oh, I am sorry. I meant, encoding, not decoding, and add same test into AnyEncodableTests.swift.

@paulw11
Copy link
Contributor Author

paulw11 commented Sep 13, 2021

Oh ok. That makes more sense 😀

@paulw11
Copy link
Contributor Author

paulw11 commented Sep 13, 2021

I have updated the AnyEncodableTests

@minacle minacle merged commit e03f091 into Flight-School:master Sep 13, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants