Skip to content

Commit 89bdbb8

Browse files
authored
Merge pull request #6 from RougeWare/feature/raw-Decodable
Adds raw value decoding
2 parents 6a80178 + 8fd17ab commit 89bdbb8

File tree

5 files changed

+80
-12
lines changed

5 files changed

+80
-12
lines changed

.github/workflows/swift.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ jobs:
1616

1717
steps:
1818
- uses: actions/checkout@v4
19-
- uses: k-arindam/setup-swift@v6.0.0
20-
with:
21-
swift-version: "6.0.0"
19+
# - uses: k-arindam/setup-swift@v6.0.0
20+
# with:
21+
# swift-version: "6.0.0"
2222

2323
- name: Get swift version
2424
run: swift --version

LICENSE.txt

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
Copyright © 2023 Ky Leggiero
22

3-
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4-
5-
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6-
7-
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
3+
Usage of the works is permitted provided that this instrument is retained with the works, so that any entity that uses the works is notified of this instrument.
84

5+
DISCLAIMER: THE WORKS ARE WITHOUT WARRANTY.

README.md

100644100755
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,21 @@ Currently, these are supported:
6464
}
6565
}
6666
```
67+
- **`Decodable` – Decoding can accept raw values.** Of course, serialized data like the above are still decoded exactly as described. As of version 2.0.0, this can also be used to decode just the raw value itself, without specifying `"left"`/`"right"`:
68+
```json
69+
{
70+
"either": {
71+
"name": "Dax",
72+
"favoriteColor": 6765239
73+
}
74+
}
75+
```
76+
or:
77+
```json
78+
{
79+
"right": 42
80+
}
81+
```
6782
- **`Sendable`** Simply declares `Sendable` conformance when `Left` and `Right` are also `Sendable`
6883

6984

Sources/Either/Either + autoconformance.swift

100644100755
Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -212,12 +212,29 @@ extension Either: Encodable where Left: Encodable, Right: Encodable {
212212

213213
extension Either: Decodable where Left: Decodable, Right: Decodable {
214214
public init(from decoder: Decoder) throws {
215-
let container = try decoder.container(keyedBy: CodingKey.self)
215+
if let container = try? decoder.container(keyedBy: CodingKey.self) {
216+
217+
if let left = try container.decodeIfPresent(Left.self, forKey: .left) {
218+
self = .left(left)
219+
return
220+
}
221+
else if let right = try container.decodeIfPresent(Right.self, forKey: .right) {
222+
self = .right(right)
223+
return
224+
}
225+
else {
226+
// try decoding a raw value
227+
}
228+
}
229+
230+
// Raw value decoding
231+
232+
let container = try decoder.singleValueContainer()
216233

217-
if let left = try container.decodeIfPresent(Left.self, forKey: .left) {
234+
if let left = try? container.decode(Left.self) {
218235
self = .left(left)
219236
}
220-
else if let right = try container.decodeIfPresent(Right.self, forKey: .right) {
237+
else if let right = try? container.decode(Right.self) {
221238
self = .right(right)
222239
}
223240
else {

Tests/EitherTests/Either + autoconformance Tests.swift

100644100755
Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ final class Either___autoconformance_Tests: XCTestCase {
336336

337337

338338

339-
for _ in 1 ... 20 {
339+
for _ in 1 ... 1000 {
340340
let either_TestCodable_Int_randomTest = CurrentEither.left(.init(
341341
name: "Dax",
342342
description: Bool.random() ? "A snepderg who helps others" : nil,
@@ -370,6 +370,45 @@ final class Either___autoconformance_Tests: XCTestCase {
370370
XCTAssertEqual(wrapped_randomInt, decoded_wrapped_randomInt)
371371
}
372372
}
373+
374+
375+
func testDecodeRawValue() throws {
376+
let payloadLeft = try XCTUnwrap("""
377+
{
378+
"value": "stringValue"
379+
}
380+
""".data(using: .utf8))
381+
382+
let payloadRight = try XCTUnwrap("""
383+
{
384+
"value": {
385+
"name": "Arc",
386+
"favoriteNumber": 1981
387+
}
388+
}
389+
""".data(using: .utf8))
390+
391+
struct ComplexStruct: Decodable, Equatable {
392+
var name: String
393+
var favoriteNumber: Int
394+
}
395+
396+
struct TestStruct: Decodable {
397+
var value: Either<String, ComplexStruct>
398+
}
399+
400+
401+
let decoder = JSONDecoder()
402+
403+
let eitherLeft = try decoder.decode(TestStruct.self, from: payloadLeft).value
404+
XCTAssertEqual(try XCTUnwrap(eitherLeft.left), "stringValue")
405+
406+
let eitherRight = try decoder.decode(TestStruct.self, from: payloadRight).value
407+
XCTAssertEqual(try XCTUnwrap(eitherRight.right), ComplexStruct(name: "Arc", favoriteNumber: 1981))
408+
409+
XCTAssertNil(try decoder.decode(TestStruct.self, from: payloadRight).value.left, "Expected right-only either to be decoded, but somehow left-only either had a value.")
410+
XCTAssertNil(try decoder.decode(TestStruct.self, from: payloadLeft).value.right, "Expected left-only either to be decoded, but somehow right-only either had a value.")
411+
}
373412
}
374413

375414

0 commit comments

Comments
 (0)