From a0c23d0d7cc610f29bf93743454e39bf96a7d36e Mon Sep 17 00:00:00 2001 From: Ahmed Shendy Date: Sat, 23 Sep 2023 02:54:58 +0300 Subject: [PATCH] update: make WordPressPost.role insensitive and add unknown --- .../Formats/Media/Podcast/PodcastPerson.swift | 55 ++++++++++++++++++- Tests/SyndiKitTests/RSSCodedTests.swift | 2 +- 2 files changed, 54 insertions(+), 3 deletions(-) diff --git a/Sources/SyndiKit/Formats/Media/Podcast/PodcastPerson.swift b/Sources/SyndiKit/Formats/Media/Podcast/PodcastPerson.swift index f883a88..fd2e6a3 100644 --- a/Sources/SyndiKit/Formats/Media/Podcast/PodcastPerson.swift +++ b/Sources/SyndiKit/Formats/Media/Podcast/PodcastPerson.swift @@ -7,7 +7,7 @@ import Foundation /// img="http://example.com/images/alicebrown.jpg" /// >Alice Brown public struct PodcastPerson: Codable { - public enum PersonRole: String, Codable { + public enum PersonRole: Codable, Equatable { case guest case host case editor @@ -15,6 +15,51 @@ public struct PodcastPerson: Codable { case designer case composer case producer + + case unknown(String) + + public var rawValue: String { + switch self { + case .guest: return "guest" + case .host: return "host" + case .editor: return "editor" + case .writer: return "writer" + case .designer: return "designer" + case .composer: return "composer" + case .producer: return "producer" + case .unknown(let string): return "\(string)" + } + } + + public init(value: String) { + guard let role = PersonRole(rawValue: value.lowercased()) else { + self = Self.unknown(value) + return + } + + self = role + } + + public init?(rawValue: String) { + switch rawValue { + case Self.guest.rawValue: + self = .guest + case Self.host.rawValue: + self = .host + case Self.editor.rawValue: + self = .editor + case Self.writer.rawValue: + self = .writer + case Self.designer.rawValue: + self = .designer + case Self.composer.rawValue: + self = .composer + case Self.producer.rawValue: + self = .producer + default: + return nil + } + } } public let role: PersonRole? @@ -34,10 +79,16 @@ public struct PodcastPerson: Codable { public init(from decoder: Decoder) throws { let container = try decoder.container(keyedBy: CodingKeys.self) - self.role = try container.decodeIfPresent(PersonRole.self, forKey: .role) self.group = try container.decodeIfPresent(String.self, forKey: .group) self.fullname = try container.decode(String.self, forKey: .fullname) + if let roleStr = try container.decodeIfPresent(String.self, forKey: .role) { + self.role = PersonRole(value: roleStr) + } else { + self.role = nil + } + + let hrefUrl = try container.decodeIfPresent(String.self, forKey: .href) ?? "" self.href = hrefUrl.isEmpty ? nil : URL(string: hrefUrl) diff --git a/Tests/SyndiKitTests/RSSCodedTests.swift b/Tests/SyndiKitTests/RSSCodedTests.swift index d714cc5..9a71a82 100644 --- a/Tests/SyndiKitTests/RSSCodedTests.swift +++ b/Tests/SyndiKitTests/RSSCodedTests.swift @@ -289,7 +289,7 @@ public final class SyndiKitTests: XCTestCase { return } - XCTAssertNil(item.podcastPeople) + XCTAssertTrue(item.podcastPeople.isEmpty) } func testEpisodesWithHostAndGuestPersons() {