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

Fix encoding with pattern matching #9

Merged
merged 13 commits into from
Jul 14, 2019
6 changes: 5 additions & 1 deletion Sources/XMLCoder/Auxiliaries/XMLCoderElement.swift
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,11 @@ struct XMLCoderElement: Equatable {
extension XMLCoderElement {
init(key: String, box: UnkeyedBox) {
self.init(key: key, elements: box.map {
XMLCoderElement(key: "", box: $0)
if $0 is KeyedBox {
return XMLCoderElement(key: "", box: $0)
} else {
return XMLCoderElement(key: key, box: $0)
}
})
}

Expand Down
5 changes: 2 additions & 3 deletions Tests/XMLCoderTests/EnumAssociatedValueTestSimple.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ private enum IntOrString {
}

extension IntOrString: Codable {

enum CodingKeys: String, CodingKey {
case int
case string
Expand All @@ -23,9 +22,9 @@ extension IntOrString: Codable {
func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
switch self {
case .int(let value):
case let .int(value):
try container.encode(value, forKey: .int)
case .string(let value):
case let .string(value):
try container.encode(value, forKey: .string)
}
}
Expand Down