Skip to content

Commit

Permalink
update: make WordPressPost.role insensitive and add unknown
Browse files Browse the repository at this point in the history
  • Loading branch information
devahmedshendy committed Sep 22, 2023
1 parent fcdf0ba commit a0c23d0
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 3 deletions.
55 changes: 53 additions & 2 deletions Sources/SyndiKit/Formats/Media/Podcast/PodcastPerson.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,59 @@ import Foundation
/// img="http://example.com/images/alicebrown.jpg"
/// >Alice Brown</podcast:person>
public struct PodcastPerson: Codable {
public enum PersonRole: String, Codable {
public enum PersonRole: Codable, Equatable {
case guest
case host
case editor
case writer
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

Check warning on line 37 in Sources/SyndiKit/Formats/Media/Podcast/PodcastPerson.swift

View check run for this annotation

Codecov / codecov/patch

Sources/SyndiKit/Formats/Media/Podcast/PodcastPerson.swift#L36-L37

Added lines #L36 - L37 were not covered by tests
}

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

Check warning on line 50 in Sources/SyndiKit/Formats/Media/Podcast/PodcastPerson.swift

View check run for this annotation

Codecov / codecov/patch

Sources/SyndiKit/Formats/Media/Podcast/PodcastPerson.swift#L50

Added line #L50 was not covered by tests
case Self.writer.rawValue:
self = .writer

Check warning on line 52 in Sources/SyndiKit/Formats/Media/Podcast/PodcastPerson.swift

View check run for this annotation

Codecov / codecov/patch

Sources/SyndiKit/Formats/Media/Podcast/PodcastPerson.swift#L52

Added line #L52 was not covered by tests
case Self.designer.rawValue:
self = .designer

Check warning on line 54 in Sources/SyndiKit/Formats/Media/Podcast/PodcastPerson.swift

View check run for this annotation

Codecov / codecov/patch

Sources/SyndiKit/Formats/Media/Podcast/PodcastPerson.swift#L54

Added line #L54 was not covered by tests
case Self.composer.rawValue:
self = .composer

Check warning on line 56 in Sources/SyndiKit/Formats/Media/Podcast/PodcastPerson.swift

View check run for this annotation

Codecov / codecov/patch

Sources/SyndiKit/Formats/Media/Podcast/PodcastPerson.swift#L56

Added line #L56 was not covered by tests
case Self.producer.rawValue:
self = .producer

Check warning on line 58 in Sources/SyndiKit/Formats/Media/Podcast/PodcastPerson.swift

View check run for this annotation

Codecov / codecov/patch

Sources/SyndiKit/Formats/Media/Podcast/PodcastPerson.swift#L58

Added line #L58 was not covered by tests
default:
return nil

Check warning on line 60 in Sources/SyndiKit/Formats/Media/Podcast/PodcastPerson.swift

View check run for this annotation

Codecov / codecov/patch

Sources/SyndiKit/Formats/Media/Podcast/PodcastPerson.swift#L60

Added line #L60 was not covered by tests
}
}
}

public let role: PersonRole?
Expand All @@ -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

Check warning on line 88 in Sources/SyndiKit/Formats/Media/Podcast/PodcastPerson.swift

View check run for this annotation

Codecov / codecov/patch

Sources/SyndiKit/Formats/Media/Podcast/PodcastPerson.swift#L88

Added line #L88 was not covered by tests
}


let hrefUrl = try container.decodeIfPresent(String.self, forKey: .href) ?? ""
self.href = hrefUrl.isEmpty ? nil : URL(string: hrefUrl)

Expand Down
2 changes: 1 addition & 1 deletion Tests/SyndiKitTests/RSSCodedTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ public final class SyndiKitTests: XCTestCase {
return
}

XCTAssertNil(item.podcastPeople)
XCTAssertTrue(item.podcastPeople.isEmpty)
}

func testEpisodesWithHostAndGuestPersons() {
Expand Down

0 comments on commit a0c23d0

Please sign in to comment.