Skip to content

Swift Archival & Serialization API #8124

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

Closed
wants to merge 10 commits into from

Conversation

itaiferber
Copy link
Contributor

@itaiferber itaiferber commented Mar 15, 2017

What's in this pull request?
This PR integrates preliminary work in introducing a new Swift-focused archival and serialization API as part of the Foundation framework.

This PR focuses on what is available as external public API. As such, it provides new types for consumers of the API and developers wishing to provide their own encoders and decoders with the API.

A swift-evolution proposal will be introduced momentarily and will link to this PR.

This work is not complete. Remaining top-level features which will be iterated upon:

  • Extensive unit tests for JSONEncoder and JSONDecoder
  • Integration of PropertyListEncoder and PropertyListDecoder
  • Unit tests for PropertyListEncoder and PropertyListDecoder

Itai Ferber added 2 commits March 15, 2017 14:46
Add the
* Codable protocol
* CodingKey protocol
* Encoder & Decoder protocols
* SingleValueEncodingContainer & SingleValueDecodingContainer protocols
* KeyedEncodingContainer & KeyedDecodingContainer classes
* Extensions on primitive Codable types
* Extensions on RawRepresentable types whose RawValue is a primitive Codable type
* New NSError codes for failure modes
Integrate preliminary implementations of the JSONEncoder and JSONDecoder classes
Copy link
Contributor

@slavapestov slavapestov left a comment

Choose a reason for hiding this comment

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

There's a lot of repetitive code here - please consider using gyb

///
/// - parameter type: The type to decode as.
/// - returns: A value of the requested type.

Copy link
Contributor

Choose a reason for hiding this comment

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

Can you use gyb to generate these extensions?

///
/// - parameter type: The type to decode as.
/// - returns: A value of the requested type.

Copy link
Contributor

Choose a reason for hiding this comment

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

Stray space before self

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think this comment refers to a different line than what's showing up for me here on Github? The referenced doc comment does not contain self

///
/// - parameter type: The type to decode as.
/// - returns: A value of the requested type.

Copy link
Contributor

Choose a reason for hiding this comment

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

Can you use gyb here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For readability, we'd like to avoid the introduction of an additional gyb file. We're willing to allow the repetition inline

Copy link
Contributor

Choose a reason for hiding this comment

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

I think it's less readable to have dozens of copy and pasted versions of the same piece of code. What if you have to change all of them, but forget to change one place?

case string(String)

/// An array container.
case array(NSMutableArray)
Copy link
Contributor

Choose a reason for hiding this comment

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

Why are these Foundation arrays and not Swift arrays?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We need the reference semantics of the Foundation collection types


// MARK: - SingleValueDecodingContainer Methods

func decode(_ type: Bool.Type) throws -> Bool {
Copy link
Contributor

Choose a reason for hiding this comment

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

You can overload on return value in Swift - no need to take the metatype parameter

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's true, but we'd like to avoid the potential ambiguity. It's also consistent with the keyed encoding and decoding methods.

@slavapestov
Copy link
Contributor

Actually instead of special casing everything on Int8/Int16/Int32 etc can we use the new integer protocols instead?

@dabrahams
Copy link
Contributor

dabrahams commented Mar 16, 2017 via email

@parkera
Copy link
Contributor

parkera commented Mar 16, 2017

Hey @dabrahams @slavapestov - thanks for your feedback. If it's ok with you I'd like to keep the API part of the discussion on the swift-evolution list. Thanks!

///
/// - parameter type: The type to decode as.
/// - returns: A value of the requested type.

Copy link
Contributor

Choose a reason for hiding this comment

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

Sorry, the stray space is here (and in the other 10000 copies of this method :-) )

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 is so weird. Github is showing this comment on the same exact line as before... 😕 Which line number are you referring to?

Copy link
Contributor

Choose a reason for hiding this comment

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

Search for " self)"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed, thanks!

Itai Ferber added 8 commits March 16, 2017 16:00
* Split Codable protocol into Encodable and Decodable
* Make CodingKey.stringValue non-optional
* Make KeyedEncodingContainer no longer a class
* Add UnkeyedEncodingContainers
* Rename codingKeyContext to codingPath
* Remove Data as an encoding primitive
* Move core Codable.swift file into stdlib
* Introduce EncodingError and DecodingError as Swift errors
  * Add extensions in Foundation overlay to bridge to NSErrors
@itaiferber
Copy link
Contributor Author

BTW, I'm currently working on shuffling PRs (this and #8125) so that all the Swift stdlib/derived conformance stuff will be part of one PR and can land together, and the Foundation bits can land separately. This should NOT be merged.

@itaiferber
Copy link
Contributor Author

The work put up here has been subsumed into #9004.

@itaiferber itaiferber closed this Apr 25, 2017
@itaiferber itaiferber mentioned this pull request Apr 25, 2017
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