Fixed some bugs, but ambiguous types are there

This commit is contained in:
cdricms
2023-12-15 23:38:36 +01:00
parent 2f919ffdef
commit e699d71597
13 changed files with 294 additions and 233 deletions

View File

@@ -1,13 +1,15 @@
// public struct Images: Codable {
// private var otherData: [String: Data] = [:]
import Foundation
// mutating func setDetail<T: Encodable>(key: String, value: T) throws {
// let encodedValue = try JSONEncoder().encode(value)
// otherData[key] = encodedValue
// }
public struct Images: Codable {
public var otherData: [String: Data] = [:]
// func getDetail<T: Decodable>(key: String, type: T.Type) throws -> T? {
// guard let data = otherData[key] else { return nil }
// return try JSONDecoder().decode(type, from: data)
// }
// }
mutating func setDetail<T: Encodable>(key: String, value: T) throws {
let encodedValue = try JSONEncoder().encode(value)
otherData[key] = encodedValue
}
func getDetail<T: Decodable>(key: String, type: T.Type) throws -> T? {
guard let data = otherData[key] else { return nil }
return try JSONDecoder().decode(type, from: data)
}
}

View File

@@ -1,4 +1,4 @@
public struct Ingredient: Codable {
public struct Ingredient: Codable, ObjectDebugger {
public var fromPalmOil: String? = nil
public var id: String? = nil
public var origin: String? = nil

View File

@@ -1,6 +1,6 @@
public struct LanguagesCodes: Codable {
public var en: String? = nil
public var fr: String? = nil
public var pl: String? = nil
public struct LanguagesCodes: Codable, ObjectDebugger {
public var en: Int? = nil
public var fr: Int? = nil
public var pl: Int? = nil
}

View File

@@ -1,4 +1,4 @@
public struct NutrientLevels: Codable {
public struct NutrientLevels: Codable, ObjectDebugger {
public var fat: String? = nil
public var salt: String? = nil
public var saturatedFat: String? = nil

View File

@@ -1,4 +1,4 @@
public struct Nutriments: Codable {
public struct Nutriments: Codable, ObjectDebugger {
public var calcium: Float? = 0.0
public var calciumValue: Float? = 0.0
public var calcium100G: Float? = 0.0
@@ -27,9 +27,9 @@ public struct Nutriments: Codable {
public var energyValue: Int? = 0
public var energyKcalValue: Int? = 0
public var energyKjValue: Int? = 0
public var energy100G: Int? = 0
public var energy100G: Float? = 0
public var energyKcal100G: Int? = 0
public var energyKj100G: Int? = 0
public var energyKj100G: Float? = 0
public var energyServing: Int? = 0
public var energyKcalServing: Double? = 0.0
public var energyKjServing: Int? = 0

View File

@@ -0,0 +1,57 @@
public protocol ObjectDebugger: CustomStringConvertible {
var description: String { get }
}
public extension ObjectDebugger {
var description: String {
var description = "\(type(of: self))("
let mirror = Mirror(reflecting: self)
for (label, value) in mirror.children {
if let label = label {
description += "\(label): \(value), "
}
}
// Remove the trailing comma and space
description = String(description.dropLast(2))
description += ")"
return description
}
private func prettyPrint(object: Any, indentation: String = "") -> String {
var description = "\(type(of: object)) {"
let mirror = Mirror(reflecting: object)
for (label, value) in mirror.children {
if let label = label {
let childDescription: String
switch value {
case let nestedObject as CustomStringConvertible:
childDescription = prettyPrint(object: nestedObject, indentation: "\(indentation) ")
case let stringValue as String:
childDescription = "\"\(stringValue)\""
case let floatValue as Float:
childDescription = "\(floatValue)"
case let intValue as Int:
childDescription = "\(intValue)"
case let optionalValue as CustomStringConvertible?:
if let unwrapped = optionalValue {
childDescription = "\(unwrapped)"
} else {
childDescription = "nil"
}
default:
childDescription = "\(value)"
}
description += "\n\(indentation) \(label): \(childDescription)"
}
}
description += "\n\(indentation)}"
return description
}
}

View File

@@ -1,5 +1,5 @@
public class Product: Codable {
// var images: Images? = Images()
public class Product: Codable, ObjectDebugger {
// public var images: Images? = Images()
public var ingredients: [Ingredient]? = []
public var languagesCodes: LanguagesCodes?
public var nutrientLevels: NutrientLevels?
@@ -24,7 +24,7 @@ public class Product: Codable {
public var brands: String?
public var brandsDebugTags: [String]?
public var brandsTags: [String]?
public var carbonFootprintPercentOfKnownIngredients: String?
public var carbonFootprintPercentOfKnownIngredients: Float?
public var carbonFootprintFromKnownIngredientsDebug: String?
public var categories: String?
public var categoriesHierarchy: [String]?
@@ -133,7 +133,7 @@ public class Product: Codable {
public var nutritionDataPer: String?
public var nutritionScoreWarningNoFruitsVegetablesNuts: Int?
public var noNutritionData: String?
public var novaGroup: String?
public var novaGroup: Int?
public var novaGroups: String?
public var novaGroupDebug: String?
public var novaGroupTags: [String]?
@@ -197,10 +197,9 @@ public class Product: Codable {
public var updateKey: String?
public var vitaminsPrevTags: [String]?
public var vitaminsTags: [String]?
}
private enum CodingKeys: String, CodingKey {
case images
private enum CodingKeys: String, CodingKey {
// case images
case ingredients
case languagesCodes = "languages_codes"
case nutrientLevels = "nutrient_levels"
@@ -397,4 +396,5 @@ private enum CodingKeys: String, CodingKey {
case updateKey = "update_key"
case vitaminsPrevTags = "vitamins_prev_tags"
case vitaminsTags = "vitamins_tags"
}
}

View File

@@ -1,5 +1,5 @@
public struct ProductResponse: Codable {
public struct ProductResponse: Codable, ObjectDebugger {
public var product: Product?
public var code: String?
public var status: Int? // or Bool, depending on your needs

View File

@@ -1,4 +1,4 @@
public class SelectedImage: Codable {
public class SelectedImage: Codable, ObjectDebugger {
public var display: SelectedImageItem?
public var small: SelectedImageItem?
public var thumb: SelectedImageItem?

View File

@@ -1,4 +1,4 @@
public struct SelectedImageItem: Codable {
public struct SelectedImageItem: Codable, ObjectDebugger {
public let en: String?
public let fr: String?
public let pl: String?

View File

@@ -1,4 +1,4 @@
public struct SelectedImages: Codable {
public struct SelectedImages: Codable, ObjectDebugger {
public let front: SelectedImage?
public let ingredients: SelectedImage?
public let nutrition: SelectedImage?

View File

@@ -1,4 +1,4 @@
public struct Source: Codable {
public struct Source: Codable, ObjectDebugger {
public let fields: [String] = []
public let id: String? = nil
public let images: [String] = []

View File

@@ -3,9 +3,11 @@ import OpenFoodFacts
let off = OpenFoodFactsClient()
do {
let res = try await off.getProductByBarcode("3017620422003")
print(res.product!.nutriments!.energy!)
} catch {
print("\(error)")
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)")
}
}