From 5e3b9e703f87ec2d17cea4666d2049b57df207c2 Mon Sep 17 00:00:00 2001 From: cdricms <36056008+cdricms@users.noreply.github.com> Date: Tue, 26 Dec 2023 15:17:49 +0100 Subject: [PATCH] product_quantity shouldn't be an issue anymore; and some tests --- Sources/OpenFoodFacts/types/Product.swift | 32 ++++++++++++++++++- Sources/exe/main.swift | 10 +----- .../OpenFoodFactsTests.swift | 17 ++++++---- 3 files changed, 43 insertions(+), 16 deletions(-) diff --git a/Sources/OpenFoodFacts/types/Product.swift b/Sources/OpenFoodFacts/types/Product.swift index aa944eb..19a65fb 100644 --- a/Sources/OpenFoodFacts/types/Product.swift +++ b/Sources/OpenFoodFacts/types/Product.swift @@ -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) + } } diff --git a/Sources/exe/main.swift b/Sources/exe/main.swift index 6a3b0f2..ed6d58e 100644 --- a/Sources/exe/main.swift +++ b/Sources/exe/main.swift @@ -2,12 +2,4 @@ import Foundation import OpenFoodFacts let off = OpenFoodFactsClient() - -for barcode in ["0737628064502", "0812133010036", "0849092103196", "22007377", "3033610048398", "3222473161867", "3242272260059", "3245412470929", "3502110000880", "3551100749018", "3560070805259", "3560070976867", "3596710352418", "3800020430781", "4000539770708", "4388858946739", "5010251168577", "5015821151720", "5050854517631", "5054070608074", "5201051001076", "5410228196693", "5449000179661"] { - do { - try await off.getProductByBarcode(barcode) - // print(res.product!) - } catch { - print("[BARCODE: \(barcode)]\(error)") - } -} +off.prod = true diff --git a/Tests/OpenFoodFactsTests/OpenFoodFactsTests.swift b/Tests/OpenFoodFactsTests/OpenFoodFactsTests.swift index ae751af..613de3d 100644 --- a/Tests/OpenFoodFactsTests/OpenFoodFactsTests.swift +++ b/Tests/OpenFoodFactsTests/OpenFoodFactsTests.swift @@ -1,12 +1,17 @@ -@testable import swift_openfoodfacts_sdk +@testable import OpenFoodFacts import XCTest final class swift_openfoodfacts_sdkTests: XCTestCase { - func testExample() throws { - // XCTest Documentation - // https://developer.apple.com/documentation/xctest + func testBarcodeProcessing() async throws { + let off = OpenFoodFactsClient() + off.prod = true - // Defining Test Cases and Test Methods - // https://developer.apple.com/documentation/xctest/defining_test_cases_and_test_methods + for barcode in ["0737628064502", "0812133010036", "0849092103196", "22007377", "3033610048398", "3222473161867", "3242272260059", "3245412470929", "3502110000880", "3551100749018", "3560070805259", "3560070976867", "3596710352418", "3800020430781", "4000539770708", "4388858946739", "5010251168577", "5015821151720", "5050854517631", "5054070608074", "5201051001076", "5410228196693", "5449000179661", "5601077161035", "6194002510064", "7311041026670", "7640101710236", "8424259826051", "8585002476821", "8712000031312", "8992696419766", "9300601768226", "9300650658615", "9310155100335"] { + do { + let _ = try await off.getProductByBarcode(barcode) + } catch { + XCTFail("[BARCODE: \(barcode)] \(error)") + } + } } }