-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Add Codable conformance to common Foundation types #9632
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
Add Codable conformance to common Foundation types #9632
Conversation
@swift-ci Please smoke test |
@@ -276,7 +283,7 @@ fileprivate struct _PlistKeyedEncodingContainer<K : CodingKey> : KeyedEncodingCo | |||
} | |||
|
|||
mutating func nestedUnkeyedContainer(forKey key: Key) -> UnkeyedEncodingContainer { | |||
let array = self.encoder.storage.pushUnkeyedContainer() | |||
let array = NSMutableArray() |
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.
This was the cause of a bug where requesting a nested unkeyed container inside a top-level keyed container would return an empty array for the encoded object.
f6ece07
to
682b219
Compare
@swift-ci Please smoke test and merge |
682b219
to
353d159
Compare
@swift-ci Please smoke test and merge |
@_transparent | ||
public init(from decoder: Decoder) throws { | ||
let container = try decoder.singleValueContainer() | ||
self.native = try container.decode(NativeType.self) |
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.
Doesn't this mean that archives with CGFloat
in them are nonportable? Is that acceptable?
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.
Potentially, yes, though we face a similar portability issue with Int
... One thing we can do is always encode as a Double
and simply fail on 32-bit platforms if the decoded Double
does not fit in a Float
. This doesn't change anything about the failure, but may play more nicely with fixed-width format Encoder
s and Decoder
s which may prevent you from decoding a Double
if you encoded a Float
, or vice versa (i.e. we push the failure out from Decoder
code into CGFloat
code, which we control, and can provide an error message). @parkera Thoughts?
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.
Another thing we can do is attempt to decode NativeType.self
, and if that fails with DecodingError.typeMismatch
, attempt the other type. If that fails as well, then it's neither a Float
nor a Double
.
I think this is a reasonable middle ground.
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.
Yeah, that seems like a reasonable middle ground. I guess I was assuming that the Int
case would be okay because Encoder
s and Decoder
s need to natively handle Int
already, but that's not true for CGFloat
.
Let's try this again. |
@swift-ci Please smoke test |
@jrose-apple Any idea what's causing |
#9663 may fix this; waiting for merge on that. |
Sorry for the trouble! |
@jrose-apple No worries 😄 |
353d159
to
116c3f9
Compare
Add conformances + unit tests for * CGFloat * AffineTransform * Calendar * CharacterSet * DateComponents * DateInterval * Decimal * IndexPath * IndexSet * Locale * Measurement * NSRange * PersonNameComponents * TimeZone * URL * UUID along with some unit tests for each.
116c3f9
to
dee889f
Compare
@swift-ci Please smoke test and merge |
What's in this pull request
Add conformances + unit tests for
along with some unit tests for each.
Resolves SR-4789 and SR-4790.