From 98732ccef99b2310594c1f07635e4721dd96778f Mon Sep 17 00:00:00 2001 From: Adam Young Date: Tue, 16 Jan 2024 14:02:10 +0000 Subject: [PATCH] FEATURE: Make all models conform to Codable (#140) * FEATURE: Make all models conform to Codable * Fix lint warnings --- Dockerfile | 2 +- Sources/TMDb/Models/Country.swift | 2 +- Sources/TMDb/Models/Department.swift | 2 +- Sources/TMDb/Models/Language.swift | 2 +- Sources/TMDb/Models/Media.swift | 2 +- .../TMDb/Models/PersonCombinedCredits.swift | 2 +- .../TMDb/Models/PersonImageCollection.swift | 2 +- Sources/TMDb/Models/PersonMovieCredits.swift | 2 +- .../TMDb/Models/PersonTVSeriesCredits.swift | 2 +- Sources/TMDb/Models/ShowCredits.swift | 2 +- Sources/TMDb/Models/ShowWatchProvider.swift | 88 +++++++++++++++++++ .../TMDb/Models/ShowWatchProviderResult.swift | 10 +-- .../Models/TVEpisodeImageCollection.swift | 2 +- .../TMDb/Models/TVSeasonImageCollection.swift | 2 +- Sources/TMDb/Models/WatchProvider.swift | 2 +- .../TMDb/Models/WatchProviderRegions.swift | 2 +- Sources/TMDb/Models/WatchProviderResult.swift | 2 +- .../Models/ShowWatchProvider+Mocks.swift | 4 +- 18 files changed, 107 insertions(+), 25 deletions(-) create mode 100644 Sources/TMDb/Models/ShowWatchProvider.swift diff --git a/Dockerfile b/Dockerfile index 985ab20b..ec419a13 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM swift:5.9-jammy +FROM swift:5.9.2-jammy WORKDIR /tmp diff --git a/Sources/TMDb/Models/Country.swift b/Sources/TMDb/Models/Country.swift index a1f6e4bb..2a4e2b3d 100644 --- a/Sources/TMDb/Models/Country.swift +++ b/Sources/TMDb/Models/Country.swift @@ -22,7 +22,7 @@ import Foundation /// /// A model representing a country. /// -public struct Country: Identifiable, Decodable, Equatable, Hashable { +public struct Country: Identifiable, Codable, Equatable, Hashable { /// /// Country's identifier (same as `countryCode`). diff --git a/Sources/TMDb/Models/Department.swift b/Sources/TMDb/Models/Department.swift index 347c2e3d..4f4614fb 100644 --- a/Sources/TMDb/Models/Department.swift +++ b/Sources/TMDb/Models/Department.swift @@ -22,7 +22,7 @@ import Foundation /// /// A model representing a department and jobs. /// -public struct Department: Identifiable, Decodable, Equatable, Hashable { +public struct Department: Identifiable, Codable, Equatable, Hashable { /// /// Departments's identifier (same as `name`). diff --git a/Sources/TMDb/Models/Language.swift b/Sources/TMDb/Models/Language.swift index 01ea3b5f..cc84927b 100644 --- a/Sources/TMDb/Models/Language.swift +++ b/Sources/TMDb/Models/Language.swift @@ -22,7 +22,7 @@ import Foundation /// /// A model representing a language. /// -public struct Language: Identifiable, Decodable, Equatable, Hashable { +public struct Language: Identifiable, Codable, Equatable, Hashable { /// /// Language code. diff --git a/Sources/TMDb/Models/Media.swift b/Sources/TMDb/Models/Media.swift index bd005fed..ba1b5ea0 100644 --- a/Sources/TMDb/Models/Media.swift +++ b/Sources/TMDb/Models/Media.swift @@ -63,7 +63,7 @@ extension Media { case mediaType } - private enum MediaType: String, Decodable, Equatable { + private enum MediaType: String, Codable, Equatable { case movie case tvSeries = "tv" case person diff --git a/Sources/TMDb/Models/PersonCombinedCredits.swift b/Sources/TMDb/Models/PersonCombinedCredits.swift index 6d75610c..c3e2d304 100644 --- a/Sources/TMDb/Models/PersonCombinedCredits.swift +++ b/Sources/TMDb/Models/PersonCombinedCredits.swift @@ -24,7 +24,7 @@ import Foundation /// /// A person can be both a cast member and crew member of the same show. /// -public struct PersonCombinedCredits: Identifiable, Decodable, Equatable, Hashable { +public struct PersonCombinedCredits: Identifiable, Codable, Equatable, Hashable { /// /// Person identifier. diff --git a/Sources/TMDb/Models/PersonImageCollection.swift b/Sources/TMDb/Models/PersonImageCollection.swift index 4d67aec9..451cea34 100644 --- a/Sources/TMDb/Models/PersonImageCollection.swift +++ b/Sources/TMDb/Models/PersonImageCollection.swift @@ -22,7 +22,7 @@ import Foundation /// /// A model representing a person image collection. /// -public struct PersonImageCollection: Identifiable, Decodable, Equatable, Hashable { +public struct PersonImageCollection: Identifiable, Codable, Equatable, Hashable { /// /// Person identifier. diff --git a/Sources/TMDb/Models/PersonMovieCredits.swift b/Sources/TMDb/Models/PersonMovieCredits.swift index 831e1c00..886c55c3 100644 --- a/Sources/TMDb/Models/PersonMovieCredits.swift +++ b/Sources/TMDb/Models/PersonMovieCredits.swift @@ -24,7 +24,7 @@ import Foundation /// /// A person can be both a cast member and crew member of the same movie. /// -public struct PersonMovieCredits: Identifiable, Decodable, Equatable, Hashable { +public struct PersonMovieCredits: Identifiable, Codable, Equatable, Hashable { /// /// Person identifier. diff --git a/Sources/TMDb/Models/PersonTVSeriesCredits.swift b/Sources/TMDb/Models/PersonTVSeriesCredits.swift index 4f811a0c..512e5912 100644 --- a/Sources/TMDb/Models/PersonTVSeriesCredits.swift +++ b/Sources/TMDb/Models/PersonTVSeriesCredits.swift @@ -24,7 +24,7 @@ import Foundation /// /// A person can be both a cast member and crew member of the same TV series. /// -public struct PersonTVSeriesCredits: Identifiable, Decodable, Equatable, Hashable { +public struct PersonTVSeriesCredits: Identifiable, Codable, Equatable, Hashable { /// /// Person identifier. diff --git a/Sources/TMDb/Models/ShowCredits.swift b/Sources/TMDb/Models/ShowCredits.swift index 719b4d66..80e4767b 100644 --- a/Sources/TMDb/Models/ShowCredits.swift +++ b/Sources/TMDb/Models/ShowCredits.swift @@ -24,7 +24,7 @@ import Foundation /// /// A person can be both a cast member and crew member of the same show. /// -public struct ShowCredits: Identifiable, Decodable, Equatable, Hashable { +public struct ShowCredits: Identifiable, Codable, Equatable, Hashable { /// /// Movie or TV series identifier. diff --git a/Sources/TMDb/Models/ShowWatchProvider.swift b/Sources/TMDb/Models/ShowWatchProvider.swift new file mode 100644 index 00000000..73b51dd1 --- /dev/null +++ b/Sources/TMDb/Models/ShowWatchProvider.swift @@ -0,0 +1,88 @@ +// +// ShowWatchProvider.swift +// TMDb +// +// Copyright © 2023 Adam Young. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an AS IS BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +import Foundation + +/// +/// A model representing a show's watch provider. +/// +public struct ShowWatchProvider: Equatable, Codable { + + /// + /// A link to the watch provider. + /// + public let link: String + + /// + /// A list of free watch providers. + /// + public let free: [WatchProvider]? + + /// + /// A list of flat rate watch providers. + /// + public let flatRate: [WatchProvider]? + + /// + /// A list of watch providers to buy from. + /// + public let buy: [WatchProvider]? + + /// + /// A list of watch providers to rent from. + /// + public let rent: [WatchProvider]? + + /// + /// Creates a show credits object. + /// + /// - Parameters: + /// - link: A link to the watch provider. + /// - free: A list of free watch providers. + /// - flatRate: A list of flat rate watch providers. + /// - buy: A list of watch providers to buy from. + /// - rent: A list of watch providers to rent from. + /// + public init( + link: String, + free: [WatchProvider]? = nil, + flatRate: [WatchProvider]? = nil, + buy: [WatchProvider]? = nil, + rent: [WatchProvider]? = nil + ) { + self.link = link + self.free = free + self.flatRate = flatRate + self.buy = buy + self.rent = rent + } + +} + +extension ShowWatchProvider { + + private enum CodingKeys: String, CodingKey { + case link + case free + case flatRate = "flatrate" + case buy + case rent + } + +} diff --git a/Sources/TMDb/Models/ShowWatchProviderResult.swift b/Sources/TMDb/Models/ShowWatchProviderResult.swift index cd948c10..4c0bbdb4 100644 --- a/Sources/TMDb/Models/ShowWatchProviderResult.swift +++ b/Sources/TMDb/Models/ShowWatchProviderResult.swift @@ -19,15 +19,9 @@ import Foundation -struct ShowWatchProviderResult: Equatable, Decodable { +struct ShowWatchProviderResult: Equatable, Codable { + let id: Int let results: [String: ShowWatchProvider] -} -public struct ShowWatchProvider: Equatable, Decodable { - public let link: String - public let free: [WatchProvider]? - public let flatrate: [WatchProvider]? - public let buy: [WatchProvider]? - public let rent: [WatchProvider]? } diff --git a/Sources/TMDb/Models/TVEpisodeImageCollection.swift b/Sources/TMDb/Models/TVEpisodeImageCollection.swift index 2addd4f0..71698791 100644 --- a/Sources/TMDb/Models/TVEpisodeImageCollection.swift +++ b/Sources/TMDb/Models/TVEpisodeImageCollection.swift @@ -22,7 +22,7 @@ import Foundation /// /// A model representing a TV episode image collection. /// -public struct TVEpisodeImageCollection: Identifiable, Decodable, Equatable, Hashable { +public struct TVEpisodeImageCollection: Identifiable, Codable, Equatable, Hashable { /// /// Collection identifier. diff --git a/Sources/TMDb/Models/TVSeasonImageCollection.swift b/Sources/TMDb/Models/TVSeasonImageCollection.swift index 5b976947..2434a2af 100644 --- a/Sources/TMDb/Models/TVSeasonImageCollection.swift +++ b/Sources/TMDb/Models/TVSeasonImageCollection.swift @@ -22,7 +22,7 @@ import Foundation /// /// A model representing a TV season image collection. /// -public struct TVSeasonImageCollection: Identifiable, Decodable, Equatable, Hashable { +public struct TVSeasonImageCollection: Identifiable, Codable, Equatable, Hashable { /// /// Collection identifier. diff --git a/Sources/TMDb/Models/WatchProvider.swift b/Sources/TMDb/Models/WatchProvider.swift index 268b1e23..f1009e3d 100644 --- a/Sources/TMDb/Models/WatchProvider.swift +++ b/Sources/TMDb/Models/WatchProvider.swift @@ -22,7 +22,7 @@ import Foundation /// /// A model representing a watch provider. /// -public struct WatchProvider: Identifiable, Decodable, Equatable, Hashable { +public struct WatchProvider: Identifiable, Codable, Equatable, Hashable { /// /// Watch Provider identifier. diff --git a/Sources/TMDb/Models/WatchProviderRegions.swift b/Sources/TMDb/Models/WatchProviderRegions.swift index 5744f337..ee035450 100644 --- a/Sources/TMDb/Models/WatchProviderRegions.swift +++ b/Sources/TMDb/Models/WatchProviderRegions.swift @@ -19,7 +19,7 @@ import Foundation -struct WatchProviderRegions: Decodable { +struct WatchProviderRegions: Codable { let results: [Country] diff --git a/Sources/TMDb/Models/WatchProviderResult.swift b/Sources/TMDb/Models/WatchProviderResult.swift index 441ccbdc..69258036 100644 --- a/Sources/TMDb/Models/WatchProviderResult.swift +++ b/Sources/TMDb/Models/WatchProviderResult.swift @@ -19,7 +19,7 @@ import Foundation -struct WatchProviderResult: Decodable, Equatable { +struct WatchProviderResult: Codable, Equatable { let results: [WatchProvider] diff --git a/Tests/TMDbTests/Mocks/Models/ShowWatchProvider+Mocks.swift b/Tests/TMDbTests/Mocks/Models/ShowWatchProvider+Mocks.swift index 6ef4eba4..104a80ba 100644 --- a/Tests/TMDbTests/Mocks/Models/ShowWatchProvider+Mocks.swift +++ b/Tests/TMDbTests/Mocks/Models/ShowWatchProvider+Mocks.swift @@ -25,14 +25,14 @@ extension ShowWatchProvider { static func mock( link: String = "", free: [WatchProvider]? = [.netflix], - flatrate: [WatchProvider]? = [.netflix], + flatRate: [WatchProvider]? = [.netflix], buy: [WatchProvider]? = [.netflix], rent: [WatchProvider]? = [.netflix] ) -> Self { .init( link: link, free: free, - flatrate: flatrate, + flatRate: flatRate, buy: buy, rent: rent )