Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
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
7 changes: 2 additions & 5 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
Copyright © 2023 Ky Leggiero

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:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

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.
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.

DISCLAIMER: THE WORKS ARE WITHOUT WARRANTY.
15 changes: 15 additions & 0 deletions README.md
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,21 @@ Currently, these are supported:
}
}
```
- **`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"`:
```json
{
"either": {
"name": "Dax",
"favoriteColor": 6765239
}
}
```
or:
```json
{
"right": 42
}
```
- **`Sendable`** Simply declares `Sendable` conformance when `Left` and `Right` are also `Sendable`


Expand Down
23 changes: 20 additions & 3 deletions Sources/Either/Either + autoconformance.swift
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -212,12 +212,29 @@ extension Either: Encodable where Left: Encodable, Right: Encodable {

extension Either: Decodable where Left: Decodable, Right: Decodable {
public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKey.self)
if let container = try? decoder.container(keyedBy: CodingKey.self) {

if let left = try container.decodeIfPresent(Left.self, forKey: .left) {
self = .left(left)
return
}
else if let right = try container.decodeIfPresent(Right.self, forKey: .right) {
self = .right(right)
return
}
else {
// try decoding a raw value
}
}

// Raw value decoding

let container = try decoder.singleValueContainer()

if let left = try container.decodeIfPresent(Left.self, forKey: .left) {
if let left = try? container.decode(Left.self) {
self = .left(left)
}
else if let right = try container.decodeIfPresent(Right.self, forKey: .right) {
else if let right = try? container.decode(Right.self) {
self = .right(right)
}
else {
Expand Down
41 changes: 40 additions & 1 deletion Tests/EitherTests/Either + autoconformance Tests.swift
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ final class Either___autoconformance_Tests: XCTestCase {



for _ in 1 ... 20 {
for _ in 1 ... 1000 {
let either_TestCodable_Int_randomTest = CurrentEither.left(.init(
name: "Dax",
description: Bool.random() ? "A snepderg who helps others" : nil,
Expand Down Expand Up @@ -370,6 +370,45 @@ final class Either___autoconformance_Tests: XCTestCase {
XCTAssertEqual(wrapped_randomInt, decoded_wrapped_randomInt)
}
}


func testDecodeRawValue() throws {
let payloadLeft = try XCTUnwrap("""
{
"value": "stringValue"
}
""".data(using: .utf8))

let payloadRight = try XCTUnwrap("""
{
"value": {
"name": "Arc",
"favoriteNumber": 1981
}
}
""".data(using: .utf8))

struct ComplexStruct: Decodable, Equatable {
var name: String
var favoriteNumber: Int
}

struct TestStruct: Decodable {
var value: Either<String, ComplexStruct>
}


let decoder = JSONDecoder()

let eitherLeft = try decoder.decode(TestStruct.self, from: payloadLeft).value
XCTAssertEqual(try XCTUnwrap(eitherLeft.left), "stringValue")

let eitherRight = try decoder.decode(TestStruct.self, from: payloadRight).value
XCTAssertEqual(try XCTUnwrap(eitherRight.right), ComplexStruct(name: "Arc", favoriteNumber: 1981))

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.")
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.")
}
}


Expand Down
Loading