product_quantity shouldn't be an issue anymore; and some tests

This commit is contained in:
cdricms
2023-12-26 15:17:49 +01:00
parent e699d71597
commit 5e3b9e703f
3 changed files with 43 additions and 16 deletions

View File

@@ -166,7 +166,7 @@ public class Product: Codable, ObjectDebugger {
public var popularityKey: Int?
public var producerVersionId: String?
public var productName: String?
public var productQuantity: String?
public var productQuantity: Float?
public var purchasePlaces: String?
public var purchasePlacesDebugTags: [String]?
public var purchasePlacesTags: [String]?
@@ -397,4 +397,34 @@ public class Product: Codable, ObjectDebugger {
case vitaminsPrevTags = "vitamins_prev_tags"
case vitaminsTags = "vitamins_tags"
}
public required init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
// Check for null value
if container.contains(.productQuantity) {
if try container.decodeNil(forKey: .productQuantity) {
productQuantity = nil
} else {
// Try to decode as Int
if let intValue = try? container.decode(Float.self, forKey: .productQuantity) {
productQuantity = intValue
} else if let stringValue = try? container.decode(String.self, forKey: .productQuantity) {
// If decoding as Int fails, try to decode as String
productQuantity = Float(stringValue)
} else {
// If decoding as both Int and String fails, handle the error accordingly
throw DecodingError.dataCorruptedError(
forKey: .productQuantity,
in: container,
debugDescription: "Unable to decode productQuantity"
)
}
}
} else {
productQuantity = nil
}
// ... (initialize other properties)
}
}