From f9ffb240efbb6d55d2bfc6ef812945f408a59025 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Mon, 20 Jan 2020 12:03:10 -0500 Subject: [PATCH] Fix decoding Hashtag.History on Mastodon --- Pachyderm/Model/Hashtag.swift | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/Pachyderm/Model/Hashtag.swift b/Pachyderm/Model/Hashtag.swift index ff990359..af868580 100644 --- a/Pachyderm/Model/Hashtag.swift +++ b/Pachyderm/Model/Hashtag.swift @@ -32,6 +32,39 @@ extension Hashtag { public let uses: Int public let accounts: Int + public required init(from decoder: Decoder) throws { + let container = try decoder.container(keyedBy: CodingKeys.self) + + if let day = try? container.decode(Date.self, forKey: .day) { + self.day = day + } else if let unixTimestamp = try? container.decode(Double.self, forKey: .day) { + self.day = Date(timeIntervalSince1970: unixTimestamp) + } else if let str = try? container.decode(String.self, forKey: .day), + let unixTimestamp = Double(str) { + self.day = Date(timeIntervalSince1970: unixTimestamp) + } else { + throw DecodingError.dataCorruptedError(forKey: .day, in: container, debugDescription: "day must be either date or UNIX timestamp") + } + + if let uses = try? container.decode(Int.self, forKey: .uses) { + self.uses = uses + } else if let str = try? container.decode(String.self, forKey: .uses), + let uses = Int(str) { + self.uses = uses + } else { + throw DecodingError.dataCorruptedError(forKey: .uses, in: container, debugDescription: "uses must either be int or string containing int") + } + + if let accounts = try? container.decode(Int.self, forKey: .accounts) { + self.accounts = accounts + } else if let str = try? container.decode(String.self, forKey: .accounts), + let accounts = Int(str) { + self.accounts = accounts + } else { + throw DecodingError.dataCorruptedError(forKey: .accounts, in: container, debugDescription: "accounts must either be int or string containing int") + } + } + private enum CodingKeys: String, CodingKey { case day case uses