Skip to content

Commit

Permalink
LF-104 Make UserInfo model codable
Browse files Browse the repository at this point in the history
  • Loading branch information
duhnnie committed Jan 16, 2024
1 parent 7d835f6 commit a8a1429
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 6 deletions.
37 changes: 35 additions & 2 deletions Sources/LastFM/Models/LastFMImages.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Foundation

public struct LastFMImages: Decodable {
public enum LastFMImageSize: String, Decodable {
public struct LastFMImages: Codable {
public enum LastFMImageSize: String, Decodable, CaseIterable {
case S = "small"
case M = "medium"
case L = "large"
Expand Down Expand Up @@ -60,4 +60,37 @@ public struct LastFMImages: Decodable {
}
}
}

private func encodeItem(
container: inout UnkeyedEncodingContainer,
url: URL?,
size: LastFMImageSize
) throws {
if let url = url {
var subcontainer = container.nestedContainer(keyedBy: CodingKeys.self)

try subcontainer.encode(size.rawValue, forKey: .size)
try subcontainer.encode(url, forKey: .url)
}
}

public func encode(to encoder: Encoder) throws {
var container = encoder.unkeyedContainer()

for size in LastFMImageSize.allCases {
switch size {
case .S:
try encodeItem(container: &container, url: small, size: size)
case .M:
try encodeItem(container: &container, url: medium, size: size)
case .L:
try encodeItem(container: &container, url: large, size: size)
case .XL:
try encodeItem(container: &container, url: extraLarge, size: size)
case .MG:
try encodeItem(container: &container, url: mega, size: size)
}
}

}
}
36 changes: 33 additions & 3 deletions Sources/LastFM/Models/UserInfo.swift
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import Foundation

public struct UserInfo: Decodable {
public struct UserInfo: Codable {

public let name: String
public let age: UInt
public let subscriber: Bool
public let realname: String
public let bootsrap: Bool
public let bootstrap: Bool
public let playcount: UInt
public let artistCount: UInt
public let playlists: UInt
Expand Down Expand Up @@ -64,7 +64,7 @@ public struct UserInfo: Decodable {

self.age = try container.decode(UInt.self, forKey: .age)
self.subscriber = try container.decode(Bool.self, forKey: .subscriber)
self.bootsrap = try container.decode(Bool.self, forKey: .bootstrap)
self.bootstrap = try container.decode(Bool.self, forKey: .bootstrap)
self.playcount = try container.decode(UInt.self, forKey: .playcount)
self.artistCount = try container.decode(UInt.self, forKey: .artistCount)
self.playlists = try container.decode(UInt.self, forKey: .playlists)
Expand All @@ -76,5 +76,35 @@ public struct UserInfo: Decodable {

self.registered = Date(timeIntervalSince1970: registeredDouble)
}

public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: OuterCodingKeys.self)
var subcontainer = container.nestedContainer(keyedBy: CodingKeys.self, forKey: .user)

var subsubcontainer = subcontainer.nestedContainer(
keyedBy: CodingKeys.RegisteredKeys.self,
forKey: .registered
)

try subcontainer.encode(name, forKey: .name)
try subcontainer.encode(age, forKey: .age)
try subcontainer.encode(subscriber, forKey: .subscriber)
try subcontainer.encode(name, forKey: .name)
try subcontainer.encode(age, forKey: .age)
try subcontainer.encode(subscriber, forKey: .subscriber)
try subcontainer.encode(realname, forKey: .realname)
try subcontainer.encode(bootstrap, forKey: .bootstrap)
try subcontainer.encode(playcount, forKey: .playcount)
try subcontainer.encode(artistCount, forKey: .artistCount)
try subcontainer.encode(playlists, forKey: .playlists)
try subcontainer.encode(trackCount, forKey: .trackCount)
try subcontainer.encode(albumCount, forKey: .albumCount)
try subcontainer.encode(image, forKey: .image)
try subcontainer.encode(country, forKey: .country)
try subcontainer.encode(gender, forKey: .gender)
try subcontainer.encode(url, forKey: .url)
try subcontainer.encode(type, forKey: .type)
try subsubcontainer.encode(registered, forKey: .text)
}

}
2 changes: 1 addition & 1 deletion Tests/LastFMTests/Modules/UserModuleTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1093,7 +1093,7 @@ class UserTests: XCTestCase {
XCTAssertEqual(userInfo.age, 0)
XCTAssertEqual(userInfo.subscriber, false)
XCTAssertEqual(userInfo.realname, "Jose")
XCTAssertEqual(userInfo.bootsrap, false)
XCTAssertEqual(userInfo.bootstrap, false)
XCTAssertEqual(userInfo.playcount, 347575)
XCTAssertEqual(userInfo.artistCount, 14264)
XCTAssertEqual(userInfo.playlists, 0)
Expand Down

0 comments on commit a8a1429

Please sign in to comment.