Skip to content

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

Merged

Conversation

itaiferber
Copy link
Contributor

What's in this pull request
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.

Resolves SR-4789 and SR-4790.

@itaiferber
Copy link
Contributor Author

@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()
Copy link
Contributor Author

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.

@itaiferber itaiferber force-pushed the foundation-codable-conformances branch from f6ece07 to 682b219 Compare May 16, 2017 18:05
@itaiferber
Copy link
Contributor Author

@swift-ci Please smoke test and merge

@itaiferber itaiferber force-pushed the foundation-codable-conformances branch from 682b219 to 353d159 Compare May 16, 2017 19:29
@itaiferber
Copy link
Contributor Author

@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)
Copy link
Member

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?

Copy link
Contributor Author

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 Encoders and Decoders 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?

Copy link
Contributor Author

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.

Copy link
Member

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 Encoders and Decoders need to natively handle Int already, but that's not true for CGFloat.

@itaiferber
Copy link
Contributor Author

itaiferber commented May 16, 2017

Let's try this again.

@itaiferber
Copy link
Contributor Author

@swift-ci Please smoke test

@itaiferber
Copy link
Contributor Author

itaiferber commented May 16, 2017

@jrose-apple Any idea what's causing test/APINotes/versioned-objc.swift to fail here? #9579 added the failing test.

@itaiferber
Copy link
Contributor Author

#9663 may fix this; waiting for merge on that.

@jrose-apple
Copy link
Contributor

Sorry for the trouble!

@itaiferber
Copy link
Contributor Author

@jrose-apple No worries 😄

@itaiferber itaiferber force-pushed the foundation-codable-conformances branch from 353d159 to 116c3f9 Compare May 16, 2017 23:23
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.
@itaiferber itaiferber force-pushed the foundation-codable-conformances branch from 116c3f9 to dee889f Compare May 16, 2017 23:57
@itaiferber
Copy link
Contributor Author

@swift-ci Please smoke test and merge

@swift-ci swift-ci merged commit d0549ac into swiftlang:master May 17, 2017
@itaiferber itaiferber deleted the foundation-codable-conformances branch May 17, 2017 02:29
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