Skip to content
Draft
Show file tree
Hide file tree
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 Jan 27, 2021
c24636e
Add new files to Xcode project
JosephDuffy Jan 27, 2021
8a99ebb
Add support for decoding
JosephDuffy Jan 27, 2021
bb14943
Run all tests on Linux
JosephDuffy Jan 27, 2021
4908a8b
Remove `Codable` requirement from `PartialCodable`
JosephDuffy Jan 27, 2021
3eb8bbf
Add PartiallyBuilt.swift to project
JosephDuffy Jan 27, 2021
020ef68
Use function builder based API
JosephDuffy Feb 13, 2021
6129ec8
Remove unused extensions
JosephDuffy Feb 13, 2021
3d77a26
Fix code style
JosephDuffy Feb 13, 2021
378842b
Improve encoding/decoding performance
JosephDuffy Feb 13, 2021
05e93aa
Split Codable types in to own files
JosephDuffy Feb 13, 2021
66955f5
Replace `foo`/`bar`
JosephDuffy Feb 13, 2021
08b01a1
Fix test compilation
JosephDuffy Feb 13, 2021
4f8576e
Generate KeyPathCodingKeyCollectionBuilder
JosephDuffy Feb 13, 2021
23a1965
Generate tests
JosephDuffy Feb 13, 2021
a771e36
Check GYB has been run
JosephDuffy Feb 13, 2021
dda142b
Merge branch 'master' into codable-support
JosephDuffy Feb 13, 2021
5640c60
Add new files to Xcode project
JosephDuffy Feb 13, 2021
592c3c8
Fix tests on Swift < 5.1
JosephDuffy Feb 13, 2021
c8490f6
Merge branch 'master' into codable-support
JosephDuffy Feb 13, 2021
dad66da
Fix various linting issues
JosephDuffy Feb 13, 2021
3814dd7
Exclude gyb files
JosephDuffy Feb 13, 2021
31298cd
Update swiftlint to 0.42.0
JosephDuffy Feb 13, 2021
ee85728
Throw error for invalid type
JosephDuffy Feb 13, 2021
4e28258
Replace preconditionFailure with throw
JosephDuffy Feb 14, 2021
e665b15
Add Codable conformance to PartialBuilder
JosephDuffy Feb 14, 2021
f16d86c
Add `decoded` function
JosephDuffy Feb 14, 2021
4549047
Fix PartialBuilder on Swift < 5.3
JosephDuffy Feb 14, 2021
e1c4466
Fix compiler error
JosephDuffy Feb 14, 2021
b965769
Fix Swift < 5.3 compilation
JosephDuffy Feb 14, 2021
c281b13
Fix test names
JosephDuffy Feb 14, 2021
8abd95a
Run Codable tests on Swift 5.0
JosephDuffy Feb 14, 2021
31e72c4
Fix tests on Swift 5.0
JosephDuffy Feb 14, 2021
059d521
Swift 5.0 compilation fixes
JosephDuffy Feb 14, 2021
a9d3cd7
Swift 5.0 compilation fixes
JosephDuffy Feb 14, 2021
5c685b2
Tell GitHub how to highlight .swift.gyb files
JosephDuffy Feb 14, 2021
ad74791
Remove subscript use (fix Swift < 5.2)
JosephDuffy Feb 14, 2021
b7b9a53
Swift < 5.1 fixes
JosephDuffy Feb 14, 2021
c000dd4
Swift < 5.1 fixes
JosephDuffy Feb 14, 2021
045ce70
Swift < 5.1 fixes
JosephDuffy Feb 14, 2021
7a238c8
Swift < 5.1 fixes
JosephDuffy Feb 14, 2021
57f24cc
Test compilation fix
JosephDuffy Feb 14, 2021
e177e28
Remove trailing whitespace
JosephDuffy Feb 14, 2021
79f8225
Include all sources in Podspec
JosephDuffy Feb 14, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.swift.gyb linguist-language=Swift
14 changes: 14 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,17 @@ jobs:

- name: swift test
run: swift test

check-gyb-run:
name: Check GYB has been run
runs-on: macOS-latest
steps:
- uses: actions/checkout@v2

- name: Install GYB
run: brew install nshipster/formulae/gyb

- run: ./scripts/generate-KeyPathCodingKeyCollectionBuilder.sh

- name: Check GYB was run before commit
run: git diff --exit-code
12 changes: 6 additions & 6 deletions Package.resolved

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 9 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,15 @@ let package = Package(
.package(url: "https://github.com/Quick/Nimble.git", from: "8.0.0"),
],
targets: [
.target(name: "Partial"),
.testTarget(name: "PartialTests", dependencies: ["Partial", "Quick", "Nimble"]),
.target(name: "Partial", exclude: ["Codable/KeyPathCodingKeyCollectionBuilder.swift.gyb"]),
.testTarget(
name: "PartialTests",
dependencies: ["Partial", "Quick", "Nimble"],
exclude: [
"Tests/Partial+CodableTests.swift.gyb",
"Tests/PartialBuilder+CodableTests.swift.gyb",
]
),
],
swiftLanguageVersions: [.v5]
)
Expand Down
11 changes: 9 additions & 2 deletions Package@swift-5.2.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,15 @@ let package = Package(
.package(url: "https://github.com/Quick/Nimble.git", from: "8.0.0"),
],
targets: [
.target(name: "Partial"),
.testTarget(name: "PartialTests", dependencies: ["Partial", "Quick", "Nimble"]),
.target(name: "Partial", exclude: ["Codable/KeyPathCodingKeyCollectionBuilder.swift.gyb"]),
.testTarget(
name: "PartialTests",
dependencies: ["Partial", "Quick", "Nimble"],
exclude: [
"Tests/Partial+CodableTests.swift.gyb",
"Tests/PartialBuilder+CodableTests.swift.gyb",
]
),
],
swiftLanguageVersions: [.v5]
)
11 changes: 9 additions & 2 deletions Package@swift-5.3.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,15 @@ let package = Package(
.package(url: "https://github.com/Realm/SwiftLint", from: "0.32.0"),
],
targets: [
.target(name: "Partial"),
.testTarget(name: "PartialTests", dependencies: ["Partial", "Quick", "Nimble"]),
.target(name: "Partial", exclude: ["Codable/KeyPathCodingKeyCollectionBuilder.swift.gyb"]),
.testTarget(
name: "PartialTests",
dependencies: ["Partial", "Quick", "Nimble"],
exclude: [
"Tests/Partial+CodableTests.swift.gyb",
"Tests/PartialBuilder+CodableTests.swift.gyb",
]
),
.target(
name: "DangerDependencies",
dependencies: [
Expand Down
2 changes: 1 addition & 1 deletion Partial.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Pod::Spec.new do |spec|
:git => "https://github.com/JosephDuffy/Partial.git",
:tag => "v#{spec.version}"
}
spec.source_files = "Sources/Partial/*.swift"
spec.source_files = "Sources/Partial/**/*.swift"
spec.osx.deployment_target = "10.10"
spec.ios.deployment_target = "8.0"
spec.tvos.deployment_target = "9.0"
Expand Down
88 changes: 80 additions & 8 deletions Partial.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

37 changes: 37 additions & 0 deletions Sources/Partial/Codable/KeyPathCodingKeyCollection.swift
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)
Copy link
Contributor

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?

Copy link
Owner Author

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 CodingKey returned be nil, and if it's nil it'll never be encoded, but if it's missing it'll throw here?

}

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
}
}
}
}
Loading