From 062f5261558f3e9d74e7828783a289a58a450cd1 Mon Sep 17 00:00:00 2001 From: Povilas Staskus <4062343+staskus@users.noreply.github.com> Date: Mon, 1 Apr 2024 12:32:17 +0300 Subject: [PATCH] Make values and value-types optional when parsing Insight entity counts --- .../Models/Stats/Insights/StatsAllTimesInsight.swift | 8 ++++---- .../Stats/Insights/StatsTagsAndCategoriesInsight.swift | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/Sources/WordPressKit/Models/Stats/Insights/StatsAllTimesInsight.swift b/Sources/WordPressKit/Models/Stats/Insights/StatsAllTimesInsight.swift index 10e1c279..13f2f85f 100644 --- a/Sources/WordPressKit/Models/Stats/Insights/StatsAllTimesInsight.swift +++ b/Sources/WordPressKit/Models/Stats/Insights/StatsAllTimesInsight.swift @@ -35,11 +35,11 @@ extension StatsAllTimesInsight: StatsInsightData { let rootContainer = try decoder.container(keyedBy: RootKeys.self) let container = try rootContainer.nestedContainer(keyedBy: CodingKeys.self, forKey: .stats) - self.postsCount = try container.decodeIfPresent(Int.self, forKey: .postsCount) ?? 0 - self.bestViewsPerDayCount = try container.decode(Int.self, forKey: .bestViewsPerDayCount) - self.visitorsCount = try container.decodeIfPresent(Int.self, forKey: .visitorsCount) ?? 0 + self.postsCount = (try? container.decodeIfPresent(Int.self, forKey: .postsCount)) ?? 0 + self.bestViewsPerDayCount = (try? container.decodeIfPresent(Int.self, forKey: .bestViewsPerDayCount)) ?? 0 + self.visitorsCount = (try? container.decodeIfPresent(Int.self, forKey: .visitorsCount)) ?? 0 - self.viewsCount = try container.decodeIfPresent(Int.self, forKey: .viewsCount) ?? 0 + self.viewsCount = (try? container.decodeIfPresent(Int.self, forKey: .viewsCount)) ?? 0 let bestViewsDayString = try container.decodeIfPresent(String.self, forKey: .bestViewsDay) ?? "" self.bestViewsDay = StatsAllTimesInsight.dateFormatter.date(from: bestViewsDayString) ?? Date() } diff --git a/Sources/WordPressKit/Models/Stats/Insights/StatsTagsAndCategoriesInsight.swift b/Sources/WordPressKit/Models/Stats/Insights/StatsTagsAndCategoriesInsight.swift index b83e7628..8daaad19 100644 --- a/Sources/WordPressKit/Models/Stats/Insights/StatsTagsAndCategoriesInsight.swift +++ b/Sources/WordPressKit/Models/Stats/Insights/StatsTagsAndCategoriesInsight.swift @@ -47,7 +47,7 @@ extension StatsTagAndCategory { let container = try decoder.container(keyedBy: CodingKeys.self) let innerTags = try container.decodeIfPresent([StatsTagAndCategory].self, forKey: .children) ?? [] - let viewsCount = try container.decodeIfPresent(Int.self, forKey: .viewsCount) + let viewsCount = (try? container.decodeIfPresent(Int.self, forKey: .viewsCount)) ?? 0 // This gets kinda complicated. The API collects some tags/categories // into groups, and we have to handle that.