Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make TopLevelEncoder implementation overridable #182

Merged
merged 4 commits into from May 20, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 1 addition & 6 deletions Sources/XMLCoder/Decoder/XMLDecoder.swift
Expand Up @@ -382,10 +382,5 @@ import protocol OpenCombine.TopLevelEncoder

#if canImport(Combine) || canImport(OpenCombine)
extension XMLDecoder: TopLevelDecoder {}

extension XMLEncoder: TopLevelEncoder {
public func encode<T>(_ value: T) throws -> Data where T: Encodable {
try encode(value, withRootKey: nil, rootAttributes: nil, header: nil)
}
}
extension XMLEncoder: TopLevelEncoder {}
#endif
6 changes: 6 additions & 0 deletions Sources/XMLCoder/Encoder/XMLEncoder.swift
Expand Up @@ -376,6 +376,12 @@ open class XMLEncoder {
return element.toXMLString(with: header, formatting: outputFormatting)
.data(using: .utf8, allowLossyConversion: true)!
}

// MARK: - TopLevelEncoder

open func encode<T>(_ value: T) throws -> Data where T: Encodable {
return try encode(value, withRootKey: nil, rootAttributes: nil, header: nil)
}
}

private extension String {
Expand Down
22 changes: 22 additions & 0 deletions Tests/XMLCoderTests/CombineTests.swift
Expand Up @@ -27,6 +27,12 @@ private struct Foo: Codable {
var name: String
}

final class CustomEncoder: XMLEncoder {
override func encode<T>(_ value: T) throws -> Data where T : Encodable {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
override func encode<T>(_ value: T) throws -> Data where T : Encodable {
override func encode<T>(_ value: T) throws -> Data where T: Encodable {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm sorry I didn't check SwiftFormat. I fixed in 18282b5.

return try self.encode(value, withRootKey: "bar", rootAttributes: nil, header: nil)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return try self.encode(value, withRootKey: "bar", rootAttributes: nil, header: nil)
return try encode(value, withRootKey: "bar", rootAttributes: nil, header: nil)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I fixed in 18282b5.

}
}

@available(iOS 13.0, macOS 10.15.0, tvOS 13.0, watchOS 6.0, *)
final class CombineTests: XCTestCase {
func testDecode() {
Expand All @@ -51,5 +57,21 @@ final class CombineTests: XCTestCase {
)
XCTAssertEqual(foo?.name, "Foo")
}

func testCustomEncode() {
var foo: Data?
_ = Just(Foo(name: "Foo"))
.encode(encoder: CustomEncoder())
.sink(
receiveCompletion: { _ in },
receiveValue: {
foo = $0
}
)
XCTAssertEqual(
String(data: foo!, encoding: .utf8)!,
"<bar><name>Foo</name></bar>"
)
}
}
#endif