-
Notifications
You must be signed in to change notification settings - Fork 126
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
Conversation
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 { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 } |
There was a problem hiding this comment.
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 🤔
There was a problem hiding this comment.
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'
There was a problem hiding this comment.
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.
@swift-ci test |
This allows encoding and decoding polymorphic types without writing
a bunch of boilerplate code.