From 10aa70cc2a3a5b70e410660520f068889757ece3 Mon Sep 17 00:00:00 2001 From: cdricms <36056008+cdricms@users.noreply.github.com> Date: Wed, 17 Jan 2024 13:45:17 +0100 Subject: [PATCH] Again with the types --- Sources/OpenFoodFacts/types/Product.swift | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/Sources/OpenFoodFacts/types/Product.swift b/Sources/OpenFoodFacts/types/Product.swift index 93d2c3d..99a713f 100644 --- a/Sources/OpenFoodFacts/types/Product.swift +++ b/Sources/OpenFoodFacts/types/Product.swift @@ -535,7 +535,27 @@ public class Product: Codable, ObjectDebugger { lastImageDatesTags = try container.decodeIfPresent([String].self, forKey: .lastImageDatesTags) lastImageT = try container.decodeIfPresent(Float.self, forKey: .lastImageT) lastModifiedBy = try container.decodeIfPresent(String.self, forKey: .lastModifiedBy) - lastModifiedT = try container.decodeIfPresent(Float.self, forKey: .lastModifiedT) + if container.contains(.lastModifiedT) { + if try container.decodeNil(forKey: .maxImgid) { + lastModifiedT = nil + } else { + if let floatValue = try? container.decode(Float.self, forKey: .lastModifiedT) { + lastModifiedT = floatValue + } else if let stringValue = try? container.decode(String.self, forKey: .lastModifiedT) { + lastModifiedT = Float(stringValue) + }else { + // If decoding as both Float and String fails, handle the error accordingly + throw DecodingError.dataCorruptedError( + forKey: .lastModifiedT, + in: container, + debugDescription: "Unable to decode lastModifiedT" + ) + } + + } + } else { + lastModifiedT = nil + } lc = try container.decodeIfPresent(String.self, forKey: .lc) link = try container.decodeIfPresent(String.self, forKey: .link) linkDebugTags = try container.decodeIfPresent([String].self, forKey: .linkDebugTags)