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

Conversation

kkebo
Copy link
Contributor

@kkebo kkebo commented May 20, 2020

TopLevelEncoder implementation was added by #175. However, its method cannot be overridden. If it can be, we can use custom root key, root attributes, or header even when we use Combine-style like this:

final class MyEncoder: XMLEncoder {
    override func encode<T>(_ value: T) throws -> Data where T : Encodable {
        try self.encode(value, withRootKey: "foo", rootAttributes: nil, header: XMLHeader(version: 1.0, encoding: "UTF-8"))
    }
}

So, I proposed the solution.

// MARK: - TopLevelEncoder

open func encode<T>(_ value: T) throws -> Data where T: Encodable {
try encode(value, withRootKey: nil, 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
try encode(value, withRootKey: nil, rootAttributes: nil, header: nil)
return try encode(value, withRootKey: nil, 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 84c33e7.

@@ -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 {
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
try self.encode(value, withRootKey: "bar", rootAttributes: nil, header: nil)
return try self.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 84c33e7.

Copy link
Collaborator

@MaxDesiatov MaxDesiatov left a comment

Choose a reason for hiding this comment

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

Thanks for the PR. I've added a few suggestions, as we should maintain backwards compatibility with Swift 4.2 syntax. Even though Combine is not available with Swift 4.2, it still seems like older compiler versions still attempt to parse and type check this code.

@codecov
Copy link

codecov bot commented May 20, 2020

Codecov Report

Merging #182 into master will not change coverage.
The diff coverage is 100.00%.

Impacted file tree graph

@@           Coverage Diff           @@
##           master     #182   +/-   ##
=======================================
  Coverage   73.32%   73.32%           
=======================================
  Files          43       43           
  Lines        2332     2332           
=======================================
  Hits         1710     1710           
  Misses        622      622           
Impacted Files Coverage Δ
Sources/XMLCoder/Decoder/XMLDecoder.swift 76.29% <ø> (-0.35%) ⬇️
Sources/XMLCoder/Encoder/XMLEncoder.swift 85.61% <100.00%> (+0.21%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update ac411bd...18282b5. Read the comment docs.

Copy link
Collaborator

@MaxDesiatov MaxDesiatov left a comment

Choose a reason for hiding this comment

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

Thanks! I have to request a few more changes to make the SwiftFormat check pass on CI.

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

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

@kkebo
Copy link
Contributor Author

kkebo commented May 20, 2020

@MaxDesiatov It seems that danger-lint has failed in XMLDecoder, but I didn't change XMLDecoder at all. Do you have any ideas?

@MaxDesiatov
Copy link
Collaborator

No worries, that seems like a SwiftLint bug 🙂

Copy link
Collaborator

@MaxDesiatov MaxDesiatov left a comment

Choose a reason for hiding this comment

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

Great stuff, thank you 👍

@MaxDesiatov MaxDesiatov merged commit 41e3d46 into CoreOffice:master May 20, 2020
arjungupta0107 pushed a commit to salido/XMLCoder that referenced this pull request Jun 26, 2020
`TopLevelEncoder` implementation was added by CoreOffice#175. However, its method cannot be overridden. If it can be, we can use custom root key, root attributes, or header even when we use Combine-style like this:

```swift
final class MyEncoder: XMLEncoder {
    override func encode<T>(_ value: T) throws -> Data where T : Encodable {
        try self.encode(value, withRootKey: "foo", rootAttributes: nil, header: XMLHeader(version: 1.0, encoding: "UTF-8"))
    }
}
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants