Skip to content

Commit 42fc194

Browse files
authored
Add temporary shims to Bug's Codable implementation to support proposed new layout. (#419)
We're proposing a new set of stored properties for `Bug` in #412. This new layout is incompatible with the old one when using `Codable`. This change adds a (very!) temporary workaround to ensure that bugs encoded with the current layout can be decoded with the new one once it is accepted and merged in. This implementation can be removed when #412 is merged. ### Checklist: - [x] Code and documentation should follow the style of the [Style Guide](https://github.com/apple/swift-testing/blob/main/Documentation/StyleGuide.md). - [x] If public symbols are renamed or modified, DocC references should be updated.
1 parent e1e6b75 commit 42fc194

File tree

1 file changed

+41
-1
lines changed

1 file changed

+41
-1
lines changed

Sources/Testing/Traits/Bug.swift

+41-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,47 @@ extension Bug: Equatable, Hashable, Comparable {
4343

4444
// MARK: - Codable
4545

46-
extension Bug: Codable {}
46+
extension Bug: Codable {
47+
/// A temporary explicit implementation of this type's coding keys enumeration
48+
/// to support the refactored form of `Bug` from [#412](https://github.com/apple/swift-testing/pull/412).
49+
private enum _CodingKeys: String, CodingKey {
50+
// Existing properties.
51+
case identifier = "identifier"
52+
case comment = "comment"
53+
54+
// Proposed new properties.
55+
case id = "id"
56+
case url = "url"
57+
case title = "title"
58+
}
59+
60+
public func encode(to encoder: any Encoder) throws {
61+
var container = encoder.container(keyedBy: _CodingKeys.self)
62+
63+
try container.encode(identifier, forKey: .identifier)
64+
try container.encodeIfPresent(comment, forKey: .comment)
65+
66+
// Temporary compatibility shims to support the refactored form of Bug from
67+
// https://github.com/apple/swift-testing/pull/412 .
68+
if identifier.contains(":") {
69+
try container.encode(identifier, forKey: .url)
70+
} else {
71+
try container.encode(identifier, forKey: .id)
72+
}
73+
try container.encodeIfPresent(comment, forKey: .title)
74+
}
75+
76+
public init(from decoder: any Decoder) throws {
77+
let container = try decoder.container(keyedBy: _CodingKeys.self)
78+
identifier = try container.decodeIfPresent(String.self, forKey: .identifier)
79+
// Temporary compatibility shims to support the refactored form of Bug
80+
// from https://github.com/apple/swift-testing/pull/412 .
81+
?? container.decodeIfPresent(String.self, forKey: .id)
82+
?? container.decode(String.self, forKey: .url)
83+
comment = try container.decodeIfPresent(Comment.self, forKey: .comment)
84+
?? container.decodeIfPresent(Comment.self, forKey: .title)
85+
}
86+
}
4787

4888
// MARK: - Trait, TestTrait, SuiteTrait
4989

0 commit comments

Comments
 (0)