Files
Rintaro Ishizaki 39606e6269 [refactoring] Rework "add codable implementation" refactoring
* Support extensions including conditional conformance
* Correct access modifiers
* More correct lookup for the synthesized declarations
* Avoid printing decls in nested types (rdar://98025945)
2024-03-13 13:34:32 +09:00

52 lines
1.0 KiB
Plaintext

struct User {
let firstName: String
let lastName: String?
}
extension User: Codable {
private enum CodingKeys: CodingKey {
case firstName
case lastName
}
init(from decoder: any Decoder) throws {
let container: KeyedDecodingContainer<User.CodingKeys> = try decoder.container(keyedBy: User.CodingKeys.self)
self.firstName = try container.decode(String.self, forKey: User.CodingKeys.firstName)
self.lastName = try container.decodeIfPresent(String.self, forKey: User.CodingKeys.lastName)
}
func encode(to encoder: any Encoder) throws {
var container = encoder.container(keyedBy: User.CodingKeys.self)
try container.encode(self.firstName, forKey: User.CodingKeys.firstName)
try container.encodeIfPresent(self.lastName, forKey: User.CodingKeys.lastName)
}
}
struct Generic<Value> {
var value: Value
}
extension Generic {
}
extension Generic: Codable where Value: Codable {
}
struct Outer {
struct Inner {
let value: Int
}
}
extension Outer.Inner: Codable {
}