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

Feature #1

Open
GIKICoder opened this issue Jan 17, 2025 · 0 comments
Open

Feature #1

GIKICoder opened this issue Jan 17, 2025 · 0 comments

Comments

@GIKICoder
Copy link

1,能否支持默认值的宏扩展。
2,支持Empty 和 快速扩展的Copy 方法。
@codable
@DefaultCodable
public struct Model: Codable, Sendable {
public let name: String
public let title: String
public let createdAt: Int
}

extension Model {
enum CodingKeys: String, CodingKey {
case name
case title
case createdAt
}
public init(from decoder: any Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
self.name = try container.decodeIfPresent(String.self, forKey: .name) ?? ""
self.title = try container.decodeIfPresent(String.self, forKey: .title) ?? ""
self.createdAt = try container.decodeIfPresent(Int.self, forKey: .createdAt) ?? 0
}
public func encode(to encoder: any Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(name, forKey: .name)
try container.encode(title, forKey: .title)
try container.encode(createdAt, forKey: .createdAt)
}
public static let empty = Model(
name: "",
title: "",
createdAt: 0
)
public func copy(
name: String? = nil,
title: String? = nil,
createdAt: Int? = nil
) -> Model {
Model(
name: name ?? self.name,
title: title ?? self.title,
createdAt: createdAt ?? self.createdAt
)
}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant