Everything works well and good structure.
This commit is contained in:
28
Sources/MusicBrainz/SearchResponse.swift
Normal file
28
Sources/MusicBrainz/SearchResponse.swift
Normal file
@@ -0,0 +1,28 @@
|
||||
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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user