Skip to content

Commit b3536b1

Browse files
committed
Add temporary shims to Bug's Codable implementation to support proposed new layout.
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.
1 parent e1e6b75 commit b3536b1

File tree

1 file changed

+35
-1
lines changed

1 file changed

+35
-1
lines changed

Sources/Testing/Traits/Bug.swift

+35-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,41 @@ 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+
case id = "id"
51+
case url = "url"
52+
case identifier = "identifier"
53+
case comment = "comment"
54+
}
55+
56+
public func encode(to encoder: any Encoder) throws {
57+
var container = encoder.container(keyedBy: _CodingKeys.self)
58+
59+
try container.encode(identifier, forKey: .identifier)
60+
try container.encodeIfPresent(comment, forKey: .comment)
61+
62+
// Temporary compatibility shims to support the refactored form of Bug from
63+
// https://github.com/apple/swift-testing/pull/412 .
64+
if identifier.contains(":") {
65+
try container.encode(identifier, forKey: .url)
66+
} else {
67+
try container.encode(identifier, forKey: .id)
68+
}
69+
}
70+
71+
public init(from decoder: any Decoder) throws {
72+
let container = try decoder.container(keyedBy: _CodingKeys.self)
73+
identifier = try container.decodeIfPresent(String.self, forKey: .identifier)
74+
// Temporary compatibility shims to support the refactored form of Bug
75+
// from https://github.com/apple/swift-testing/pull/412 .
76+
?? container.decodeIfPresent(String.self, forKey: .id)
77+
?? container.decode(String.self, forKey: .url)
78+
comment = try container.decodeIfPresent(Comment.self, forKey: .comment)
79+
}
80+
}
4781

4882
// MARK: - Trait, TestTrait, SuiteTrait
4983

0 commit comments

Comments
 (0)