-
-
Notifications
You must be signed in to change notification settings - Fork 7
Add Codable support #175
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
Draft
JosephDuffy
wants to merge
44
commits into
master
Choose a base branch
from
codable-support
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Add Codable support #175
Changes from all commits
Commits
Show all changes
44 commits
Select commit
Hold shift + click to select a range
f875523
Add basic Encodable support
JosephDuffy c24636e
Add new files to Xcode project
JosephDuffy 8a99ebb
Add support for decoding
JosephDuffy bb14943
Run all tests on Linux
JosephDuffy 4908a8b
Remove `Codable` requirement from `PartialCodable`
JosephDuffy 3eb8bbf
Add PartiallyBuilt.swift to project
JosephDuffy 020ef68
Use function builder based API
JosephDuffy 6129ec8
Remove unused extensions
JosephDuffy 3d77a26
Fix code style
JosephDuffy 378842b
Improve encoding/decoding performance
JosephDuffy 05e93aa
Split Codable types in to own files
JosephDuffy 66955f5
Replace `foo`/`bar`
JosephDuffy 08b01a1
Fix test compilation
JosephDuffy 4f8576e
Generate KeyPathCodingKeyCollectionBuilder
JosephDuffy 23a1965
Generate tests
JosephDuffy a771e36
Check GYB has been run
JosephDuffy dda142b
Merge branch 'master' into codable-support
JosephDuffy 5640c60
Add new files to Xcode project
JosephDuffy 592c3c8
Fix tests on Swift < 5.1
JosephDuffy c8490f6
Merge branch 'master' into codable-support
JosephDuffy dad66da
Fix various linting issues
JosephDuffy 3814dd7
Exclude gyb files
JosephDuffy 31298cd
Update swiftlint to 0.42.0
JosephDuffy ee85728
Throw error for invalid type
JosephDuffy 4e28258
Replace preconditionFailure with throw
JosephDuffy e665b15
Add Codable conformance to PartialBuilder
JosephDuffy f16d86c
Add `decoded` function
JosephDuffy 4549047
Fix PartialBuilder on Swift < 5.3
JosephDuffy e1c4466
Fix compiler error
JosephDuffy b965769
Fix Swift < 5.3 compilation
JosephDuffy c281b13
Fix test names
JosephDuffy 8abd95a
Run Codable tests on Swift 5.0
JosephDuffy 31e72c4
Fix tests on Swift 5.0
JosephDuffy 059d521
Swift 5.0 compilation fixes
JosephDuffy a9d3cd7
Swift 5.0 compilation fixes
JosephDuffy 5c685b2
Tell GitHub how to highlight .swift.gyb files
JosephDuffy ad74791
Remove subscript use (fix Swift < 5.2)
JosephDuffy b7b9a53
Swift < 5.1 fixes
JosephDuffy c000dd4
Swift < 5.1 fixes
JosephDuffy 045ce70
Swift < 5.1 fixes
JosephDuffy 7a238c8
Swift < 5.1 fixes
JosephDuffy 57f24cc
Test compilation fix
JosephDuffy e177e28
Remove trailing whitespace
JosephDuffy 79f8225
Include all sources in Podspec
JosephDuffy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| *.swift.gyb linguist-language=Swift |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| public struct KeyPathCodingKeyCollection<Root, CodingKey: Swift.CodingKey & Hashable> { | ||
| public enum EncodeError: Error { | ||
| case invalidType(value: Any, expectedType: Any.Type) | ||
| } | ||
|
|
||
| private typealias Encoder = (_ value: Any, _ container: inout KeyedEncodingContainer<CodingKey>) throws -> Void | ||
| private typealias Decoder = (_ codingKey: CodingKey, _ container: KeyedDecodingContainer<CodingKey>) throws -> (Any, PartialKeyPath<Root>)? | ||
|
|
||
| private var encoders: [PartialKeyPath<Root>: Encoder] = [:] | ||
| private var decoders: [CodingKey: Decoder] = [:] | ||
|
|
||
| func encode(_ value: Any, forKey keyPath: PartialKeyPath<Root>, to container: inout KeyedEncodingContainer<CodingKey>) throws { | ||
| try encoders[keyPath]?(value, &container) | ||
| } | ||
|
|
||
| func decode(_ codingKey: CodingKey, in container: KeyedDecodingContainer<CodingKey>) throws -> (Any, PartialKeyPath<Root>)? { | ||
| return try decoders[codingKey]?(codingKey, container) | ||
| } | ||
|
|
||
| public mutating func addPair<Value: Swift.Codable>(keyPath: KeyPath<Root, Value>, codingKey: CodingKey) { | ||
| encoders[keyPath] = { value, container in | ||
| guard let caseValue = value as? Value else { | ||
| throw EncodeError.invalidType(value: value, expectedType: Value.self) | ||
| } | ||
|
|
||
| try container.encode(caseValue, forKey: codingKey) | ||
| } | ||
|
|
||
| decoders[codingKey] = { codingKey, container in | ||
| if let decodedValue = try container.decodeIfPresent(Value.self, forKey: codingKey) { | ||
| return (decodedValue, keyPath) | ||
| } else { | ||
| return nil | ||
| } | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 throw an error if no encoder exists for a given
keyPath?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 hadn't considered that. It may be useful for consumers to be able to opt out certain values from being encoded, which making this throw would prevent.
But having this throw would also make debugging easier if a key path/coding key is missed.
Would it make sense to allow the
CodingKeyreturned benil, and if it'snilit'll never be encoded, but if it's missing it'll throw here?