From b1bd1ac75abd46c498eb0022c97bed6b1ebc7164 Mon Sep 17 00:00:00 2001 From: Brent Simmons Date: Sun, 10 Sep 2017 11:02:05 -0700 Subject: [PATCH] Use a Set instead of [ParsedAuthor] array. --- .../RSParser/Feeds/JSON/JSONFeedParser.swift | 4 ++-- .../RSParser/Feeds/JSON/RSSInJSONParser.swift | 4 ++-- Frameworks/RSParser/Feeds/ParsedAuthor.swift | 17 +++++++++++++++-- Frameworks/RSParser/Feeds/ParsedFeed.swift | 4 ++-- Frameworks/RSParser/Feeds/ParsedItem.swift | 4 ++-- .../Feeds/XML/RSParsedFeedTransformer.swift | 4 ++-- 6 files changed, 25 insertions(+), 12 deletions(-) diff --git a/Frameworks/RSParser/Feeds/JSON/JSONFeedParser.swift b/Frameworks/RSParser/Feeds/JSON/JSONFeedParser.swift index b94e2e08a..a31f1deb5 100644 --- a/Frameworks/RSParser/Feeds/JSON/JSONFeedParser.swift +++ b/Frameworks/RSParser/Feeds/JSON/JSONFeedParser.swift @@ -50,7 +50,7 @@ public struct JSONFeedParser { private extension JSONFeedParser { - static func parseAuthors(_ dictionary: JSONDictionary) -> [ParsedAuthor]? { + static func parseAuthors(_ dictionary: JSONDictionary) -> Set? { guard let authorDictionary = dictionary["author"] as? JSONDictionary else { return nil @@ -63,7 +63,7 @@ private extension JSONFeedParser { return nil } let parsedAuthor = ParsedAuthor(name: name, url: url, avatarURL: avatar, emailAddress: nil) - return [parsedAuthor] + return Set([parsedAuthor]) } static func parseHubs(_ dictionary: JSONDictionary) -> Set? { diff --git a/Frameworks/RSParser/Feeds/JSON/RSSInJSONParser.swift b/Frameworks/RSParser/Feeds/JSON/RSSInJSONParser.swift index baa6a7a47..a37bf1770 100644 --- a/Frameworks/RSParser/Feeds/JSON/RSSInJSONParser.swift +++ b/Frameworks/RSParser/Feeds/JSON/RSSInJSONParser.swift @@ -132,13 +132,13 @@ private extension RSSInJSONParser { return nil } - static func parseAuthors(_ itemDictionary: JSONDictionary) -> [ParsedAuthor]? { + static func parseAuthors(_ itemDictionary: JSONDictionary) -> Set? { guard let authorEmailAddress = itemDictionary["author"] as? String else { return nil } let parsedAuthor = ParsedAuthor(name: nil, url: nil, avatarURL: nil, emailAddress: authorEmailAddress) - return [parsedAuthor] + return Set([parsedAuthor]) } static func parseTags(_ itemDictionary: JSONDictionary) -> [String]? { diff --git a/Frameworks/RSParser/Feeds/ParsedAuthor.swift b/Frameworks/RSParser/Feeds/ParsedAuthor.swift index ad5041889..cc8013ace 100644 --- a/Frameworks/RSParser/Feeds/ParsedAuthor.swift +++ b/Frameworks/RSParser/Feeds/ParsedAuthor.swift @@ -8,18 +8,31 @@ import Foundation -public struct ParsedAuthor { +public struct ParsedAuthor: Hashable { public let name: String? public let url: String? public let avatarURL: String? public let emailAddress: String? - + public let hashValue: Int + init(name: String?, url: String?, avatarURL: String?, emailAddress: String?) { self.name = name self.url = url self.avatarURL = avatarURL self.emailAddress = emailAddress + + var stringToHash = "" + stringToHash += name ?? "" + stringToHash += url ?? "" + stringToHash += avatarURL ?? "" + stringToHash += emailAddress ?? "" + self.hashValue = stringToHash.hashValue + } + + public static func ==(lhs: ParsedAuthor, rhs: ParsedAuthor) -> Bool { + + return lhs.hashValue == rhs.hashValue && lhs.name == rhs.name && lhs.url == rhs.url && lhs.avatarURL == rhs.avatarURL && lhs.emailAddress == rhs.emailAddress } } diff --git a/Frameworks/RSParser/Feeds/ParsedFeed.swift b/Frameworks/RSParser/Feeds/ParsedFeed.swift index 1710fce64..b4c2253e6 100644 --- a/Frameworks/RSParser/Feeds/ParsedFeed.swift +++ b/Frameworks/RSParser/Feeds/ParsedFeed.swift @@ -18,12 +18,12 @@ public struct ParsedFeed { public let nextURL: String? public let iconURL: String? public let faviconURL: String? - public let authors: [ParsedAuthor]? + public let authors: Set? public let expired: Bool public let hubs: Set? public let items: Set - init(type: FeedType, title: String?, homePageURL: String?, feedURL: String?, feedDescription: String?, nextURL: String?, iconURL: String?, faviconURL: String?, authors: [ParsedAuthor]?, expired: Bool, hubs: Set?, items: Set) { + init(type: FeedType, title: String?, homePageURL: String?, feedURL: String?, feedDescription: String?, nextURL: String?, iconURL: String?, faviconURL: String?, authors: Set?, expired: Bool, hubs: Set?, items: Set) { self.type = type self.title = title diff --git a/Frameworks/RSParser/Feeds/ParsedItem.swift b/Frameworks/RSParser/Feeds/ParsedItem.swift index 15dea8caf..1d90f47a7 100644 --- a/Frameworks/RSParser/Feeds/ParsedItem.swift +++ b/Frameworks/RSParser/Feeds/ParsedItem.swift @@ -23,12 +23,12 @@ public struct ParsedItem: Hashable { public let bannerImageURL: String? public let datePublished: Date? public let dateModified: Date? - public let authors: [ParsedAuthor]? + public let authors: Set? public let tags: [String]? public let attachments: [ParsedAttachment]? public let hashValue: Int - init(syncServiceID: String?, uniqueID: String, feedURL: String, url: String?, externalURL: String?, title: String?, contentHTML: String?, contentText: String?, summary: String?, imageURL: String?, bannerImageURL: String?, datePublished: Date?, dateModified: Date?, authors: [ParsedAuthor]?, tags: [String]?, attachments: [ParsedAttachment]?) { + init(syncServiceID: String?, uniqueID: String, feedURL: String, url: String?, externalURL: String?, title: String?, contentHTML: String?, contentText: String?, summary: String?, imageURL: String?, bannerImageURL: String?, datePublished: Date?, dateModified: Date?, authors: Set?, tags: [String]?, attachments: [ParsedAttachment]?) { self.syncServiceID = syncServiceID self.uniqueID = uniqueID diff --git a/Frameworks/RSParser/Feeds/XML/RSParsedFeedTransformer.swift b/Frameworks/RSParser/Feeds/XML/RSParsedFeedTransformer.swift index ced74ec5f..d44a84875 100644 --- a/Frameworks/RSParser/Feeds/XML/RSParsedFeedTransformer.swift +++ b/Frameworks/RSParser/Feeds/XML/RSParsedFeedTransformer.swift @@ -45,12 +45,12 @@ private extension RSParsedFeedTransformer { return ParsedItem(syncServiceID: nil, uniqueID: uniqueID, feedURL: parsedArticle.feedURL, url: url, externalURL: externalURL, title: title, contentHTML: contentHTML, contentText: nil, summary: nil, imageURL: nil, bannerImageURL: nil, datePublished: datePublished, dateModified: dateModified, authors: authors, tags: nil, attachments: nil) } - static func parsedAuthors(_ authorEmailAddress: String?) -> [ParsedAuthor]? { + static func parsedAuthors(_ authorEmailAddress: String?) -> Set? { guard let authorEmailAddress = authorEmailAddress else { return nil } let author = ParsedAuthor(name: nil, url: nil, avatarURL: nil, emailAddress: authorEmailAddress) - return [author] + return Set([author]) } }