Sync migrated iOS modules
This commit is contained in:
@ -32,6 +32,34 @@ struct ListPayload<T: Decodable>: Decodable {
|
||||
}
|
||||
}
|
||||
|
||||
/// 数据列表响应实体,表示后端常见的 total + data 分页结构,并兼容 list 字段。
|
||||
struct DataListPayload<T: Decodable>: Decodable {
|
||||
let total: Int
|
||||
let data: [T]
|
||||
|
||||
/// 数据列表响应的 JSON 字段映射。
|
||||
enum CodingKeys: String, CodingKey {
|
||||
case total
|
||||
case data
|
||||
case list
|
||||
}
|
||||
|
||||
/// 创建数据列表响应,主要用于测试和本地兜底数据。
|
||||
init(total: Int, data: [T]) {
|
||||
self.total = total
|
||||
self.data = data
|
||||
}
|
||||
|
||||
/// 自定义解码,兼容 data/list 字段和 total 字符串。
|
||||
init(from decoder: Decoder) throws {
|
||||
let container = try decoder.container(keyedBy: CodingKeys.self)
|
||||
total = try container.decodeLossyInt(forKey: .total) ?? 0
|
||||
data = try container.decodeIfPresent([T].self, forKey: .data)
|
||||
?? container.decodeIfPresent([T].self, forKey: .list)
|
||||
?? []
|
||||
}
|
||||
}
|
||||
|
||||
private extension KeyedDecodingContainer {
|
||||
/// 将 Int、Double 或数字字符串宽松解码为整数。
|
||||
func decodeLossyInt(forKey key: Key) throws -> Int? {
|
||||
|
||||
Reference in New Issue
Block a user