Skip to content

Add temporary shims to Bug's Codable implementation to support proposed new layout. #419

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

Merged
merged 2 commits into from
May 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 41 additions & 1 deletion Sources/Testing/Traits/Bug.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,47 @@ extension Bug: Equatable, Hashable, Comparable {

// MARK: - Codable

extension Bug: Codable {}
extension Bug: Codable {
/// A temporary explicit implementation of this type's coding keys enumeration
/// to support the refactored form of `Bug` from [#412](https://github.com/apple/swift-testing/pull/412).
private enum _CodingKeys: String, CodingKey {
// Existing properties.
case identifier = "identifier"
case comment = "comment"

// Proposed new properties.
case id = "id"
case url = "url"
case title = "title"
}

public func encode(to encoder: any Encoder) throws {
var container = encoder.container(keyedBy: _CodingKeys.self)

try container.encode(identifier, forKey: .identifier)
try container.encodeIfPresent(comment, forKey: .comment)

// Temporary compatibility shims to support the refactored form of Bug from
// https://github.com/apple/swift-testing/pull/412 .
if identifier.contains(":") {
try container.encode(identifier, forKey: .url)
} else {
try container.encode(identifier, forKey: .id)
}
try container.encodeIfPresent(comment, forKey: .title)
}

public init(from decoder: any Decoder) throws {
let container = try decoder.container(keyedBy: _CodingKeys.self)
identifier = try container.decodeIfPresent(String.self, forKey: .identifier)
// Temporary compatibility shims to support the refactored form of Bug
// from https://github.com/apple/swift-testing/pull/412 .
?? container.decodeIfPresent(String.self, forKey: .id)
?? container.decode(String.self, forKey: .url)
comment = try container.decodeIfPresent(Comment.self, forKey: .comment)
?? container.decodeIfPresent(Comment.self, forKey: .title)
}
}

// MARK: - Trait, TestTrait, SuiteTrait

Expand Down