fix for multitype json for attached_image

This commit is contained in:
Wilhelm Oks 2024-12-17 16:55:00 +01:00
parent 3eebb24a4c
commit edd526d2aa
2 changed files with 42 additions and 2 deletions

View File

@ -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)
}
}
}

View File

@ -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
)