From e7e7528e2b7ee0e0767ddb294f2b7816ea67d010 Mon Sep 17 00:00:00 2001 From: Adam Sharp Date: Mon, 23 Sep 2019 10:04:10 -0400 Subject: [PATCH 1/5] Conditionally conform to Combine.TopLevelDecoder --- Sources/XMLCoder/Decoder/XMLDecoder.swift | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/Sources/XMLCoder/Decoder/XMLDecoder.swift b/Sources/XMLCoder/Decoder/XMLDecoder.swift index 024b9935..11f1ead8 100644 --- a/Sources/XMLCoder/Decoder/XMLDecoder.swift +++ b/Sources/XMLCoder/Decoder/XMLDecoder.swift @@ -368,3 +368,11 @@ open class XMLDecoder { return try decoder.unbox(topLevel) } } + +// MARK: TopLevelDecoder + +#if canImport(Combine) +import protocol Combine.TopLevelDecoder + +extension XMLDecoder: TopLevelDecoder {} +#endif From 5a2be5314389dba1386aaa342c5354415810c7e8 Mon Sep 17 00:00:00 2001 From: Adam Sharp Date: Sun, 29 Sep 2019 21:10:08 -0400 Subject: [PATCH 2/5] Add test and documentation for Combine integration --- README.md | 19 +++++++++++++ Tests/XMLCoderTests/CombineTests.swift | 37 ++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 Tests/XMLCoderTests/CombineTests.swift diff --git a/README.md b/README.md index 8f2f24bc..dae64f16 100644 --- a/README.md +++ b/README.md @@ -269,6 +269,25 @@ extension IntOrString: Codable { This is described in more details in PR [\#119](https://github.com/MaxDesiatov/XMLCoder/pull/119) by [@jsbean](https://github.com/jsbean) and [@bwetherfield](https://github.com/bwetherfield). +## Integrating with [Combine](https://developer.apple.com/documentation/combine) + +When Apple's Combine framework is available, `XMLDecoder` conforms to the +`TopLevelDecoder` protocol, which allows it to be used with the +`decode(type:decoder:)` operator: + +```swift +import Combine +import Foundation +import XMLCoder + +func fetchBook(from url: URL) -> AnyPublisher { + return URLSession.shared.dataTaskPublisher(for: url) + .map(\.data) + .decode(type: Book.self, decoder: XMLDecoder()) + .eraseToAnyPublisher() +} +``` + ## Installation ### Requirements diff --git a/Tests/XMLCoderTests/CombineTests.swift b/Tests/XMLCoderTests/CombineTests.swift new file mode 100644 index 00000000..ed212b01 --- /dev/null +++ b/Tests/XMLCoderTests/CombineTests.swift @@ -0,0 +1,37 @@ +// +// CombineTests.swift +// XMLCoder +// +// Created by Adam Sharp on 9/29/19. +// + +#if canImport(Combine) +import Combine +import Foundation +import XCTest +import XMLCoder + +private let xml = """ + + + Foo + +""".data(using: .utf8)! + +private struct Foo: Decodable { + var name: String +} + +@available(iOS 13.0, macOS 10.15.0, macCatalyst 13.0, tvOS 13.0, watchOS 6.0, *) +class CombineTests: XCTestCase { + func testDecodeFromXMLDecoder() { + let data = Just(xml) + var foo: Foo? + _ = data.decode(type: Foo.self, decoder: XMLDecoder()).sink( + receiveCompletion: { _ in }, + receiveValue: { foo = $0 } + ) + XCTAssertEqual(foo?.name, "Foo") + } +} +#endif From eba54650e0559851b95f0eaa5ac5c852112ed6d7 Mon Sep 17 00:00:00 2001 From: Adam Sharp Date: Sun, 29 Sep 2019 21:23:09 -0400 Subject: [PATCH 3/5] Remove macCatalyst from CombineTests This shouldn't be necessary because all versions of Mac Catalyst should have a version of Combine available. --- Tests/XMLCoderTests/CombineTests.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tests/XMLCoderTests/CombineTests.swift b/Tests/XMLCoderTests/CombineTests.swift index ed212b01..b3125a24 100644 --- a/Tests/XMLCoderTests/CombineTests.swift +++ b/Tests/XMLCoderTests/CombineTests.swift @@ -22,7 +22,7 @@ private struct Foo: Decodable { var name: String } -@available(iOS 13.0, macOS 10.15.0, macCatalyst 13.0, tvOS 13.0, watchOS 6.0, *) +@available(iOS 13.0, macOS 10.15.0, tvOS 13.0, watchOS 6.0, *) class CombineTests: XCTestCase { func testDecodeFromXMLDecoder() { let data = Just(xml) From ae8e76a9c84f6e04bd6bb70a2dd5146fd0c0b8fe Mon Sep 17 00:00:00 2001 From: Adam Sharp Date: Sun, 29 Sep 2019 21:30:58 -0400 Subject: [PATCH 4/5] Add CombineTests to the Xcode project --- XMLCoder.xcodeproj/project.pbxproj | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/XMLCoder.xcodeproj/project.pbxproj b/XMLCoder.xcodeproj/project.pbxproj index 76e0aed0..cda5d30e 100644 --- a/XMLCoder.xcodeproj/project.pbxproj +++ b/XMLCoder.xcodeproj/project.pbxproj @@ -21,6 +21,7 @@ /* End PBXAggregateTarget section */ /* Begin PBXBuildFile section */ + 4A062D4F2341924E009BCAC1 /* CombineTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4A062D4E2341924E009BCAC1 /* CombineTests.swift */; }; OBJ_148 /* BoolBox.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_12 /* BoolBox.swift */; }; OBJ_149 /* Box.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_13 /* Box.swift */; }; OBJ_150 /* ChoiceBox.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_14 /* ChoiceBox.swift */; }; @@ -152,6 +153,7 @@ /* End PBXContainerItemProxy section */ /* Begin PBXFileReference section */ + 4A062D4E2341924E009BCAC1 /* CombineTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CombineTests.swift; sourceTree = ""; }; OBJ_100 /* DecimalTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DecimalTests.swift; sourceTree = ""; }; OBJ_101 /* EmptyTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EmptyTests.swift; sourceTree = ""; }; OBJ_102 /* FloatTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FloatTests.swift; sourceTree = ""; }; @@ -424,6 +426,7 @@ OBJ_86 /* CDCatalog.swift */, OBJ_87 /* CDTest.swift */, OBJ_88 /* ClassTests.swift */, + 4A062D4E2341924E009BCAC1 /* CombineTests.swift */, OBJ_89 /* CompositeChoiceTests.swift */, OBJ_90 /* DecodingContainerTests.swift */, OBJ_91 /* DynamicNodeDecodingTest.swift */, @@ -734,6 +737,7 @@ OBJ_269 /* RJITest.swift in Sources */, OBJ_270 /* RelationshipsTest.swift in Sources */, OBJ_271 /* SimpleChoiceTests.swift in Sources */, + 4A062D4F2341924E009BCAC1 /* CombineTests.swift in Sources */, OBJ_272 /* SingleChildTests.swift in Sources */, OBJ_273 /* SpacePreserveTest.swift in Sources */, ); From 5994ae1c2cce90c04b5f9b95731b0fbd6ba33991 Mon Sep 17 00:00:00 2001 From: Max Desiatov Date: Fri, 4 Oct 2019 23:39:15 +0100 Subject: [PATCH 5/5] Disable CombineTests on macOS --- Tests/XMLCoderTests/CombineTests.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tests/XMLCoderTests/CombineTests.swift b/Tests/XMLCoderTests/CombineTests.swift index b3125a24..cdc0d7ea 100644 --- a/Tests/XMLCoderTests/CombineTests.swift +++ b/Tests/XMLCoderTests/CombineTests.swift @@ -5,7 +5,7 @@ // Created by Adam Sharp on 9/29/19. // -#if canImport(Combine) +#if canImport(Combine) && !os(macOS) import Combine import Foundation import XCTest