From edd526d2aade2dc7acfa6e32742ceeba00b393e9 Mon Sep 17 00:00:00 2001 From: Wilhelm Oks Date: Tue, 17 Dec 2024 16:55:00 +0100 Subject: [PATCH] fix for multitype json for attached_image --- Sources/SwiftDevRant/Models/Rant.swift | 40 ++++++++++++++++++++++ Sources/SwiftDevRant/Models/RantFeed.swift | 4 +-- 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/Sources/SwiftDevRant/Models/Rant.swift b/Sources/SwiftDevRant/Models/Rant.swift index a8ec118..14c43f2 100644 --- a/Sources/SwiftDevRant/Models/Rant.swift +++ b/Sources/SwiftDevRant/Models/Rant.swift @@ -92,6 +92,46 @@ extension Rant { let user_avatar: User.Avatar.CodingData let user_avatar_lg: User.Avatar.CodingData let user_dpp: Int? + + init(from decoder: Decoder) throws { + // We need custom decoding code here because the attached_image can be a dictionary OR a string. + + let values = try decoder.container(keyedBy: CodingKeys.self) + + id = try values.decode(Int.self, forKey: .id) + text = try values.decode(String.self, forKey: .text) + score = try values.decode(Int.self, forKey: .score) + created_time = try values.decode(Int.self, forKey: .created_time) + + do { + attached_image = try values.decode(AttachedImage.CodingData.self, forKey: .attached_image) + } catch { + attached_image = nil + } + + num_comments = try values.decode(Int.self, forKey: .num_comments) + tags = try values.decode([String].self, forKey: .tags) + vote_state = try values.decode(Int.self, forKey: .vote_state) + weekly = try? values.decode(Weekly.CodingData.self, forKey: .weekly) + edited = try values.decode(Bool.self, forKey: .edited) + favorited = try? values.decode(Int.self, forKey: .favorited) + link = try? values.decode(String.self, forKey: .link) + links = try? values.decode([Link.CodingData].self, forKey: .links) + + c_type = try? values.decode(Int.self, forKey: .c_type) + c_type_long = try? values.decode(String.self, forKey: .c_type_long) + c_description = try? values.decode(String.self, forKey: .c_description) + c_tech_stack = try? values.decode(String.self, forKey: .c_tech_stack) + c_team_size = try? values.decode(String.self, forKey: .c_team_size) + c_url = try? values.decode(String.self, forKey: .c_url) + + user_id = try values.decode(Int.self, forKey: .user_id) + user_username = try values.decode(String.self, forKey: .user_username) + user_score = try values.decode(Int.self, forKey: .user_score) + user_avatar = try values.decode(User.Avatar.CodingData.self, forKey: .user_avatar) + user_avatar_lg = try values.decode(User.Avatar.CodingData.self, forKey: .user_avatar_lg) + user_dpp = try? values.decode(Int.self, forKey: .user_dpp) + } } } diff --git a/Sources/SwiftDevRant/Models/RantFeed.swift b/Sources/SwiftDevRant/Models/RantFeed.swift index 0688609..2376cd2 100644 --- a/Sources/SwiftDevRant/Models/RantFeed.swift +++ b/Sources/SwiftDevRant/Models/RantFeed.swift @@ -57,7 +57,7 @@ extension RantFeed { //let settings //not sure what the purpose is. probably not needed. let set: String? let wrw: Int? - let dpp: Int + let dpp: Int? let num_notifs: Int? //let unread //probably the same info as already provided by num_notifs, so not needed. let news: News.CodingData? @@ -70,7 +70,7 @@ extension RantFeed.CodingData { rants: rants.map(\.decoded), sessionHash: `set`, weeklyRantWeek: wrw, - devRantSupporter: dpp != 0, + devRantSupporter: (dpp ?? 0) != 0, numberOfUnreadNotifications: num_notifs ?? 0, news: news?.decoded )