29 lines
876 B
Swift
29 lines
876 B
Swift
import Foundation
|
|
|
|
public struct SearchResponse<T: MusicBrainzSearchable>: Codable, Sendable {
|
|
public let count: Int
|
|
public let offset: Int
|
|
public let entities: [T]
|
|
|
|
private enum CodingKeys: String, CodingKey {
|
|
case count, offset
|
|
}
|
|
|
|
public init(from decoder: Decoder) throws {
|
|
let container = try decoder.container(keyedBy: CodingKeys.self)
|
|
count = try container.decode(Int.self, forKey: .count)
|
|
offset = try container.decode(Int.self, forKey: .offset)
|
|
|
|
let dynamicContainer = try decoder.container(keyedBy: DynamicCodingKey.self)
|
|
guard let key = DynamicCodingKey(stringValue: T.entityType.responseKey) else {
|
|
throw DecodingError.dataCorrupted(
|
|
DecodingError.Context(
|
|
codingPath: decoder.codingPath,
|
|
debugDescription: "Invalid response key for entity type"
|
|
)
|
|
)
|
|
}
|
|
entities = try dynamicContainer.decode([T].self, forKey: key)
|
|
}
|
|
}
|