Fixed some bugs, but ambiguous types are there
This commit is contained in:
@@ -1,13 +1,15 @@
|
|||||||
// public struct Images: Codable {
|
import Foundation
|
||||||
// private var otherData: [String: Data] = [:]
|
|
||||||
|
|
||||||
// mutating func setDetail<T: Encodable>(key: String, value: T) throws {
|
public struct Images: Codable {
|
||||||
// let encodedValue = try JSONEncoder().encode(value)
|
public var otherData: [String: Data] = [:]
|
||||||
// otherData[key] = encodedValue
|
|
||||||
// }
|
|
||||||
|
|
||||||
// func getDetail<T: Decodable>(key: String, type: T.Type) throws -> T? {
|
mutating func setDetail<T: Encodable>(key: String, value: T) throws {
|
||||||
// guard let data = otherData[key] else { return nil }
|
let encodedValue = try JSONEncoder().encode(value)
|
||||||
// return try JSONDecoder().decode(type, from: data)
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
public struct Ingredient: Codable {
|
public struct Ingredient: Codable, ObjectDebugger {
|
||||||
public var fromPalmOil: String? = nil
|
public var fromPalmOil: String? = nil
|
||||||
public var id: String? = nil
|
public var id: String? = nil
|
||||||
public var origin: String? = nil
|
public var origin: String? = nil
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
|
|
||||||
public struct LanguagesCodes: Codable {
|
public struct LanguagesCodes: Codable, ObjectDebugger {
|
||||||
public var en: String? = nil
|
public var en: Int? = nil
|
||||||
public var fr: String? = nil
|
public var fr: Int? = nil
|
||||||
public var pl: String? = nil
|
public var pl: Int? = nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
public struct NutrientLevels: Codable {
|
public struct NutrientLevels: Codable, ObjectDebugger {
|
||||||
public var fat: String? = nil
|
public var fat: String? = nil
|
||||||
public var salt: String? = nil
|
public var salt: String? = nil
|
||||||
public var saturatedFat: String? = nil
|
public var saturatedFat: String? = nil
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
public struct Nutriments: Codable {
|
public struct Nutriments: Codable, ObjectDebugger {
|
||||||
public var calcium: Float? = 0.0
|
public var calcium: Float? = 0.0
|
||||||
public var calciumValue: Float? = 0.0
|
public var calciumValue: Float? = 0.0
|
||||||
public var calcium100G: Float? = 0.0
|
public var calcium100G: Float? = 0.0
|
||||||
@@ -27,9 +27,9 @@ public struct Nutriments: Codable {
|
|||||||
public var energyValue: Int? = 0
|
public var energyValue: Int? = 0
|
||||||
public var energyKcalValue: Int? = 0
|
public var energyKcalValue: Int? = 0
|
||||||
public var energyKjValue: Int? = 0
|
public var energyKjValue: Int? = 0
|
||||||
public var energy100G: Int? = 0
|
public var energy100G: Float? = 0
|
||||||
public var energyKcal100G: Int? = 0
|
public var energyKcal100G: Int? = 0
|
||||||
public var energyKj100G: Int? = 0
|
public var energyKj100G: Float? = 0
|
||||||
public var energyServing: Int? = 0
|
public var energyServing: Int? = 0
|
||||||
public var energyKcalServing: Double? = 0.0
|
public var energyKcalServing: Double? = 0.0
|
||||||
public var energyKjServing: Int? = 0
|
public var energyKjServing: Int? = 0
|
||||||
|
|||||||
57
Sources/OpenFoodFacts/types/ObjectDebugger.swift
Normal file
57
Sources/OpenFoodFacts/types/ObjectDebugger.swift
Normal 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
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
public class Product: Codable {
|
public class Product: Codable, ObjectDebugger {
|
||||||
// var images: Images? = Images()
|
// public var images: Images? = Images()
|
||||||
public var ingredients: [Ingredient]? = []
|
public var ingredients: [Ingredient]? = []
|
||||||
public var languagesCodes: LanguagesCodes?
|
public var languagesCodes: LanguagesCodes?
|
||||||
public var nutrientLevels: NutrientLevels?
|
public var nutrientLevels: NutrientLevels?
|
||||||
@@ -24,7 +24,7 @@ public class Product: Codable {
|
|||||||
public var brands: String?
|
public var brands: String?
|
||||||
public var brandsDebugTags: [String]?
|
public var brandsDebugTags: [String]?
|
||||||
public var brandsTags: [String]?
|
public var brandsTags: [String]?
|
||||||
public var carbonFootprintPercentOfKnownIngredients: String?
|
public var carbonFootprintPercentOfKnownIngredients: Float?
|
||||||
public var carbonFootprintFromKnownIngredientsDebug: String?
|
public var carbonFootprintFromKnownIngredientsDebug: String?
|
||||||
public var categories: String?
|
public var categories: String?
|
||||||
public var categoriesHierarchy: [String]?
|
public var categoriesHierarchy: [String]?
|
||||||
@@ -133,7 +133,7 @@ public class Product: Codable {
|
|||||||
public var nutritionDataPer: String?
|
public var nutritionDataPer: String?
|
||||||
public var nutritionScoreWarningNoFruitsVegetablesNuts: Int?
|
public var nutritionScoreWarningNoFruitsVegetablesNuts: Int?
|
||||||
public var noNutritionData: String?
|
public var noNutritionData: String?
|
||||||
public var novaGroup: String?
|
public var novaGroup: Int?
|
||||||
public var novaGroups: String?
|
public var novaGroups: String?
|
||||||
public var novaGroupDebug: String?
|
public var novaGroupDebug: String?
|
||||||
public var novaGroupTags: [String]?
|
public var novaGroupTags: [String]?
|
||||||
@@ -197,10 +197,9 @@ public class Product: Codable {
|
|||||||
public var updateKey: String?
|
public var updateKey: String?
|
||||||
public var vitaminsPrevTags: [String]?
|
public var vitaminsPrevTags: [String]?
|
||||||
public var vitaminsTags: [String]?
|
public var vitaminsTags: [String]?
|
||||||
}
|
|
||||||
|
|
||||||
private enum CodingKeys: String, CodingKey {
|
private enum CodingKeys: String, CodingKey {
|
||||||
case images
|
// case images
|
||||||
case ingredients
|
case ingredients
|
||||||
case languagesCodes = "languages_codes"
|
case languagesCodes = "languages_codes"
|
||||||
case nutrientLevels = "nutrient_levels"
|
case nutrientLevels = "nutrient_levels"
|
||||||
@@ -398,3 +397,4 @@ private enum CodingKeys: String, CodingKey {
|
|||||||
case vitaminsPrevTags = "vitamins_prev_tags"
|
case vitaminsPrevTags = "vitamins_prev_tags"
|
||||||
case vitaminsTags = "vitamins_tags"
|
case vitaminsTags = "vitamins_tags"
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
|
|
||||||
public struct ProductResponse: Codable {
|
public struct ProductResponse: Codable, ObjectDebugger {
|
||||||
public var product: Product?
|
public var product: Product?
|
||||||
public var code: String?
|
public var code: String?
|
||||||
public var status: Int? // or Bool, depending on your needs
|
public var status: Int? // or Bool, depending on your needs
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
public class SelectedImage: Codable {
|
public class SelectedImage: Codable, ObjectDebugger {
|
||||||
public var display: SelectedImageItem?
|
public var display: SelectedImageItem?
|
||||||
public var small: SelectedImageItem?
|
public var small: SelectedImageItem?
|
||||||
public var thumb: SelectedImageItem?
|
public var thumb: SelectedImageItem?
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
public struct SelectedImageItem: Codable {
|
public struct SelectedImageItem: Codable, ObjectDebugger {
|
||||||
public let en: String?
|
public let en: String?
|
||||||
public let fr: String?
|
public let fr: String?
|
||||||
public let pl: String?
|
public let pl: String?
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
public struct SelectedImages: Codable {
|
public struct SelectedImages: Codable, ObjectDebugger {
|
||||||
public let front: SelectedImage?
|
public let front: SelectedImage?
|
||||||
public let ingredients: SelectedImage?
|
public let ingredients: SelectedImage?
|
||||||
public let nutrition: SelectedImage?
|
public let nutrition: SelectedImage?
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
public struct Source: Codable {
|
public struct Source: Codable, ObjectDebugger {
|
||||||
public let fields: [String] = []
|
public let fields: [String] = []
|
||||||
public let id: String? = nil
|
public let id: String? = nil
|
||||||
public let images: [String] = []
|
public let images: [String] = []
|
||||||
|
|||||||
@@ -3,9 +3,11 @@ import OpenFoodFacts
|
|||||||
|
|
||||||
let off = OpenFoodFactsClient()
|
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 {
|
do {
|
||||||
let res = try await off.getProductByBarcode("3017620422003")
|
try await off.getProductByBarcode(barcode)
|
||||||
print(res.product!.nutriments!.energy!)
|
// print(res.product!)
|
||||||
} catch {
|
} catch {
|
||||||
print("\(error)")
|
print("[BARCODE: \(barcode)]\(error)")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user