Conformed the Amiibo model with its child models to the Codable protocol.

This commit is contained in:
Javier Cicchelli 2023-07-23 16:29:13 +02:00
parent 4b648abb83
commit 9c73e0de16
4 changed files with 46 additions and 10 deletions

View File

@ -53,6 +53,7 @@ extension DTO {
// MARK: - Structs // MARK: - Structs
extension DTO.Amiibo { extension DTO.Amiibo {
/// This model represents the list of games related to a particular amiibo, grouped by system. /// This model represents the list of games related to a particular amiibo, grouped by system.
public struct Games: Decodable { public struct Games: Decodable {
@ -67,13 +68,14 @@ extension DTO.Amiibo {
/// /// A list of [Nintendo Switch system](https://en.wikipedia.org/wiki/Nintendo_Switch) games the amiibo can be used with. /// /// A list of [Nintendo Switch system](https://en.wikipedia.org/wiki/Nintendo_Switch) games the amiibo can be used with.
public let `switch`: [Game] public let `switch`: [Game]
} }
} }
// MARK: - Decodable // MARK: - Codable
extension DTO.Amiibo: Decodable { extension DTO.Amiibo: Codable {
// MARK: Enumerations // MARK: Keys
enum CodingKeys: String, CodingKey { enum CodingKeys: String, CodingKey {
case type case type
@ -90,7 +92,7 @@ extension DTO.Amiibo: Decodable {
case gamesSwitch case gamesSwitch
} }
// MARK: Initialisers // MARK: Decoding
/// Initialises this model by decoding from the given decoder. /// Initialises this model by decoding from the given decoder.
/// - Parameter decoder: The decoder to read data from. /// - Parameter decoder: The decoder to read data from.
@ -122,5 +124,27 @@ extension DTO.Amiibo: Decodable {
} }
}() }()
} }
// MARK: Encoding
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(type, forKey: .type)
try container.encode(head, forKey: .head)
try container.encode(tail, forKey: .tail)
try container.encode(name, forKey: .name)
try container.encode(character, forKey: .character)
try container.encode(series, forKey: .series)
try container.encode(gameSeries, forKey: .gameSeries)
try container.encode(image, forKey: .image)
try container.encode(release, forKey: .release)
if let games {
try container.encode(games.n3ds, forKey: .games3DS)
try container.encode(games.wiiu, forKey: .gamesWiiU)
try container.encode(games.switch, forKey: .gamesSwitch)
}
}
} }

View File

@ -30,12 +30,16 @@ extension DTO.Amiibo {
} }
// MARK: - Decodable // MARK: - Codable
extension DTO.Amiibo.Game: Decodable { extension DTO.Amiibo.Game: Codable {
// MARK: Keys
enum CodingKeys: String, CodingKey { enum CodingKeys: String, CodingKey {
case ids = "gameID" case ids = "gameID"
case name = "gameName" case name = "gameName"
case usage = "amiiboUsage" case usage = "amiiboUsage"
} }
} }

View File

@ -27,11 +27,15 @@ extension DTO.Amiibo.Game {
} }
// MARK: - Decodable // MARK: - Codable
extension DTO.Amiibo.Game.Usage: Decodable { extension DTO.Amiibo.Game.Usage: Codable {
// MARK: Keys
enum CodingKeys: String, CodingKey { enum CodingKeys: String, CodingKey {
case explanation = "Usage" case explanation = "Usage"
case isWritable = "write" case isWritable = "write"
} }
} }

View File

@ -35,13 +35,17 @@ extension DTO.Amiibo {
} }
// MARK: - Decodable // MARK: - Codable
extension DTO.Amiibo.Release: Decodable { extension DTO.Amiibo.Release: Codable {
// MARK: Keys
enum CodingKeys: String, CodingKey { enum CodingKeys: String, CodingKey {
case australia = "au" case australia = "au"
case europe = "eu" case europe = "eu"
case japan = "jp" case japan = "jp"
case america = "na" case america = "na"
} }
} }