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

[TSCUtility] Introduce PolymorphicCodable property wrapper #67

Merged
merged 2 commits into from
Apr 20, 2020

Conversation

aciidgh
Copy link
Contributor

@aciidgh aciidgh commented Apr 13, 2020

This allows encoding and decoding polymorphic types without writing
a bunch of boilerplate code.

This allows encoding and decoding polymorphic types without writing
a bunch of boilerplate code.
public init(from decoder: Decoder) throws {
var container = try decoder.unkeyedContainer()
let typeCode = try container.decode(String.self)
guard let klass = T.implementations.first(where: { String(reflecting: $0) == typeCode }) else {
Copy link

Choose a reason for hiding this comment

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

Should this error or at least assert if there are duplications, to detect programmer error?

Copy link

Choose a reason for hiding this comment

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

if implementations was a Set then we (eventually) could typeHint -> _typeByName to then T.allowedImplementations.contains(thatTypeByName) to avoid that scanning through. The T we need anyway in order to get the Any.Type as Codable to call decoding on it

Copy link

Choose a reason for hiding this comment

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

I'm exploring another direction for registering types on Joe's hints... I'll report back once I have something.

More based around "intent" or "permissions" i.e. "this type is intended for crossing network" vs. "this one isnt" etc.


/// Allows encoding and decoding known polymorphic types.
public protocol PolymorphicCodableProtocol: Codable {
static var implementations: [PolymorphicCodableProtocol.Type] { get }
Copy link

Choose a reason for hiding this comment

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

Could be a Set<> I suppose 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Can't use Set:

Type 'PolymorphicCodableProtocol.Type' does not conform to protocol 'Hashable' and Cannot extend a metatype 'PolymorphicCodableProtocol.Type'

Copy link
Contributor

Choose a reason for hiding this comment

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

Since String(reflecting: ) is needed every time implementations is accessed, it might make sense to do a struct representing the (String, Type), and implement a Hashable over it. Then it is going to be Set-compatible. I am sure you've thought of that, just 2c.

@aciidgh
Copy link
Contributor Author

aciidgh commented Apr 20, 2020

@swift-ci test

@aciidgh aciidgh merged commit 39fb181 into master Apr 20, 2020
@aciidgh aciidgh deleted the polycodable branch April 20, 2020 00:49
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.

4 participants