Skip to content

Commit ecc7c53

Browse files
committed
Add test and documentation for Combine integration
1 parent 53955d2 commit ecc7c53

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,25 @@ extension IntOrString: Codable {
269269
This is described in more details in PR [\#119](https://github.com/MaxDesiatov/XMLCoder/pull/119)
270270
by [@jsbean](https://github.com/jsbean) and [@bwetherfield](https://github.com/bwetherfield).
271271

272+
## Integrating with [Combine](https://developer.apple.com/documentation/combine)
273+
274+
When Apple's Combine framework is available, `XMLDecoder` conforms to the
275+
`TopLevelDecoder` protocol, which allows it to be used with the
276+
`decode(type:decoder:)` operator:
277+
278+
```swift
279+
import Combine
280+
import Foundation
281+
import XMLCoder
282+
283+
func fetchBook(from url: URL) -> AnyPublisher<Book, Error> {
284+
return URLSession.shared.dataTaskPublisher(for: url)
285+
.map(\.data)
286+
.decode(type: Book.self, decoder: XMLDecoder())
287+
.eraseToAnyPublisher()
288+
}
289+
```
290+
272291
## Installation
273292

274293
### Requirements
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//
2+
// CombineTests.swift
3+
// XMLCoder
4+
//
5+
// Created by Adam Sharp on 9/29/19.
6+
//
7+
8+
#if canImport(Combine)
9+
import Combine
10+
import Foundation
11+
import XCTest
12+
import XMLCoder
13+
14+
private let xml = """
15+
<?xml version="1.0" encoding="UTF-8"?>
16+
<foo>
17+
<name>Foo</name>
18+
</foo>
19+
""".data(using: .utf8)!
20+
21+
private struct Foo: Decodable {
22+
var name: String
23+
}
24+
25+
@available(iOS 13.0, macOS 10.15.0, macCatalyst 13.0, tvOS 13.0, watchOS 6.0, *)
26+
class CombineTests: XCTestCase {
27+
func testDecodeFromXMLDecoder() {
28+
let data = Just(xml)
29+
var foo: Foo?
30+
_ = data.decode(type: Foo.self, decoder: XMLDecoder()).sink(receiveCompletion: { _ in }) {
31+
foo = $0
32+
}
33+
XCTAssertEqual(foo?.name, "Foo")
34+
}
35+
}
36+
#endif

0 commit comments

Comments
 (0)