diff --git a/Sources/Argo/Operators/Argo.swift b/Sources/Argo/Operators/Argo.swift index 7dfb51f..d3f29e3 100644 --- a/Sources/Argo/Operators/Argo.swift +++ b/Sources/Argo/Operators/Argo.swift @@ -7,4 +7,3 @@ precedencegroup ArgoDecodePrecedence { } infix operator <| : ArgoDecodePrecedence -infix operator <|? : ArgoDecodePrecedence diff --git a/Sources/Argo/Operators/DecodeOperators.swift b/Sources/Argo/Operators/DecodeOperators.swift index 0e470c0..d35fcb0 100644 --- a/Sources/Argo/Operators/DecodeOperators.swift +++ b/Sources/Argo/Operators/DecodeOperators.swift @@ -17,25 +17,6 @@ public func <| (json: JSON, key: String) -> Decoded where A == return json <| [key] } -/** - Attempt to decode an optional value at the specified key into the requested - type. - - This operator is used to decode an optional value from the `JSON`. If the key - isn't present in the `JSON`, this will still return `.Success`. However, if - the key exists but the object assigned to that key is unable to be decoded - into the requested type, this will return `.Failure`. - - - parameter json: The `JSON` object containing the key - - parameter key: The key for the object to decode - - - returns: A `Decoded` optional value representing the success or failure of - the decode operation -*/ -public func <|? (json: JSON, key: String) -> Decoded where A == A.DecodedType { - return json <|? [key] -} - /** Attempt to decode a value at the specified key path into the requested type. @@ -53,26 +34,3 @@ public func <|? (json: JSON, key: String) -> Decoded where A = public func <| (json: JSON, keys: [String]) -> Decoded where A == A.DecodedType { return flatReduce(keys, initial: json, combine: decodedJSON) >>- A.decode } - -/** - Attempt to decode an optional value at the specified key path into the - requested type. - - This operator is used to decode an optional value from the `JSON`. If any of - the keys in the key path aren't present in the `JSON`, this will still return - `.Success`. However, if the key path exists but the object assigned to the - final key is unable to be decoded into the requested type, this will return - `.Failure`. - - - parameter json: The `JSON` object containing the key - - parameter keys: The key path for the object to decode, represented by an - array of strings - - returns: A `Decoded` optional value representing the success or failure of - the decode operation -*/ -public func <|? (json: JSON, keys: [String]) -> Decoded where A == A.DecodedType { - switch flatReduce(keys, initial: json, combine: decodedJSON) { - case .failure: return .success(.none) - case .success(let x): return A.decode(x) >>- { .success(.some($0)) } - } -} diff --git a/Sources/Argo/Types/StandardTypes.swift b/Sources/Argo/Types/StandardTypes.swift index db276f8..f43133c 100644 --- a/Sources/Argo/Types/StandardTypes.swift +++ b/Sources/Argo/Types/StandardTypes.swift @@ -161,7 +161,7 @@ extension Bool: Decodable { } } -public extension Optional where Wrapped: Decodable, Wrapped == Wrapped.DecodedType { +extension Optional: Decodable where Wrapped: Decodable, Wrapped == Wrapped.DecodedType { /** Decode `JSON` into an `Optional` value where `Wrapped` is `Decodable`. @@ -172,7 +172,7 @@ public extension Optional where Wrapped: Decodable, Wrapped == Wrapped.DecodedTy - returns: A decoded optional `Wrapped` value */ - static func decode(_ json: JSON) -> Decoded { + public static func decode(_ json: JSON) -> Decoded { return Wrapped.decode(json) >>- { .success(.some($0)) } } } diff --git a/Tests/ArgoTests/Models/Post.swift b/Tests/ArgoTests/Models/Post.swift index aacaa97..f165241 100644 --- a/Tests/ArgoTests/Models/Post.swift +++ b/Tests/ArgoTests/Models/Post.swift @@ -34,7 +34,7 @@ extension LocationPost: Argo.Decodable { <*> json <| "text" <*> json <| "author" <*> json <| "comments" - <*> json <|? "location" + <*> .optional(json <| "location") } } diff --git a/Tests/ArgoTests/Models/TestModel.swift b/Tests/ArgoTests/Models/TestModel.swift index 43da4dc..ee22451 100644 --- a/Tests/ArgoTests/Models/TestModel.swift +++ b/Tests/ArgoTests/Models/TestModel.swift @@ -22,10 +22,10 @@ extension TestModel: Argo.Decodable { <*> json <| ["user_opt", "name"] <*> json <| "bool" <*> json <| "string_array" - <*> json <|? "string_array_opt" + <*> .optional(json <| "string_array_opt") <*> json <| ["embedded", "string_array"] - <*> json <|? ["embedded", "string_array_opt"] - <*> json <|? "user_opt" + <*> .optional(json <| ["embedded", "string_array_opt"]) + <*> .optional(json <| "user_opt") <*> json <| "dict" } } @@ -50,7 +50,7 @@ extension TestModelNumerics: Argo.Decodable { <*> json <| "int64_string" <*> json <| "double" <*> json <| "float" - <*> json <|? "int_opt" + <*> .optional(json <| "int_opt") return f <*> json <| "uint" diff --git a/Tests/ArgoTests/Models/User.swift b/Tests/ArgoTests/Models/User.swift index 58bd964..cab6bb4 100644 --- a/Tests/ArgoTests/Models/User.swift +++ b/Tests/ArgoTests/Models/User.swift @@ -13,6 +13,6 @@ extension User: Argo.Decodable { return curry(self.init) <^> json <| "id" <*> (json <| ["userinfo", "name"] <|> json <| "name") - <*> json <|? "email" + <*> .optional(json <| "email") } } diff --git a/Tests/ArgoTests/Tests/DecodedTests.swift b/Tests/ArgoTests/Tests/DecodedTests.swift index 56be115..ddea493 100644 --- a/Tests/ArgoTests/Tests/DecodedTests.swift +++ b/Tests/ArgoTests/Tests/DecodedTests.swift @@ -78,19 +78,9 @@ class DecodedTests: XCTestCase { "email": 1 ]) - let expected: [DecodeError] = [ - .missingKey("name"), - .typeMismatch(expected: "String", actual: String(describing: JSON.number(1))) - ] - - switch user { - case let .failure(.multiple(errors)): - print("expected: \(expected)") - print("actual: \(errors)") + let expected: DecodeError = .missingKey("name") - XCTAssertEqual(errors, expected) - default: XCTFail("Unexpected Case Occurred") - } + XCTAssertEqual(user.error, expected) } func testDecodedMaterializeSuccess() { diff --git a/Tests/ArgoTests/Tests/EmbeddedDecodingTests.swift b/Tests/ArgoTests/Tests/EmbeddedDecodingTests.swift index 9c0e178..2924a40 100644 --- a/Tests/ArgoTests/Tests/EmbeddedDecodingTests.swift +++ b/Tests/ArgoTests/Tests/EmbeddedDecodingTests.swift @@ -11,12 +11,12 @@ final class EmbeddedDecodingTests: XCTestCase { } } - func testFailOnEmbeddedObject() { + func testDecodeEmbeddedOptionalObject() { let post: Decoded = decode(json(fromFile: "bad_location_post")!) - switch post.error { + switch post.value { case .some: XCTAssert(true) - case .none: XCTFail("Unexpected Success") + case .none: XCTFail("Unexpected Failure") } } }