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,204 +197,204 @@ public class Product: Codable {
public var updateKey: String?
public var vitaminsPrevTags: [String]?
public var vitaminsTags: [String]?
}
private enum CodingKeys: String, CodingKey {
case images
case ingredients
case languagesCodes = "languages_codes"
case nutrientLevels = "nutrient_levels"
case nutriments
case selectedImages = "selected_images"
case sources
case additivesN = "additives_n"
case additivesOldN = "additives_old_n"
case additivesOriginalTags = "additives_original_tags"
case additivesOldTags = "additives_old_tags"
case additivesPrevOriginalTags = "additives_prev_original_tags"
case additivesDebugTags = "additives_debug_tags"
case additivesTags = "additives_tags"
case allergens
case allergensFromIngredients = "allergens_from_ingredients"
case allergensFromUser = "allergens_from_user"
case allergensHierarchy = "allergens_hierarchy"
case allergensLc = "allergens_lc"
case allergensTags = "allergens_tags"
case aminoAcidsPrevTags = "amino_acids_prev_tags"
case aminoAcidsTags = "amino_acids_tags"
case brands
case brandsDebugTags = "brands_debug_tags"
case brandsTags = "brands_tags"
case carbonFootprintPercentOfKnownIngredients = "carbon_footprint_percent_of_known_ingredients"
case carbonFootprintFromKnownIngredientsDebug = "carbon_footprint_from_known_ingredients_debug"
case categories
case categoriesHierarchy = "categories_hierarchy"
case categoriesLc = "categories_lc"
case categoriesPropertiesTags = "categories_properties_tags"
case categoriesTags = "categories_tags"
case checkersTags = "checkers_tags"
case citiesTags = "cities_tags"
case code
case codesTags = "codes_tags"
case comparedToCategory = "compared_to_category"
case complete
case completedT = "completed_t"
case completeness
case conservationConditions = "conservation_conditions"
case countries
case countriesHierarchy = "countries_hierarchy"
case countriesLc = "countries_lc"
case countriesDebugTags = "countries_debug_tags"
case countriesTags = "countries_tags"
case correctorsTags = "correctors_tags"
case createdT = "created_t"
case creator
case dataQualityBugsTags = "data_quality_bugs_tags"
case dataQualityErrorsTags = "data_quality_errors_tags"
case dataQualityInfoTags = "data_quality_info_tags"
case dataQualityTags = "data_quality_tags"
case dataQualityWarningsTags = "data_quality_warnings_tags"
case dataSources = "data_sources"
case dataSourcesTags = "data_sources_tags"
case debugParamSortedLangs = "debug_param_sorted_langs"
case editorsTags = "editors_tags"
case embCodes = "emb_codes"
case embCodesDebugTags = "emb_codes_debug_tags"
case embCodesOrig = "emb_codes_orig"
case embCodesTags = "emb_codes_tags"
case entryDatesTags = "entry_dates_tags"
case expirationDate = "expiration_date"
case expirationDateDebugTags = "expiration_date_debug_tags"
case fruitsVegetablesNuts100GEstimate = "fruits-vegetables-nuts_100g_estimate"
case genericName
case id
case imageFrontSmallUrl = "image_front_small_url"
case imageFrontThumbUrl = "image_front_thumb_url"
case imageFrontUrl = "image_front_url"
case imageIngredientsUrl = "image_ingredients_url"
case imageIngredientsSmallUrl = "image_ingredients_small_url"
case imageIngredientsThumbUrl = "image_ingredients_thumb_url"
case imageNutritionSmallUrl = "image_nutrition_small_url"
case imageNutritionThumbUrl = "image_nutrition_thumb_url"
case imageNutritionUrl = "image_nutrition_url"
case imageSmallUrl = "image_small_url"
case imageThumbUrl = "image_thumb_url"
case imageUrl = "image_url"
case informersTags = "informers_tags"
case ingredientsAnalysisTags = "ingredients_analysis_tags"
case ingredientsDebug = "ingredients_debug"
case ingredientsFromOrThatMayBeFromPalmOilN = "ingredients_from_or_that_may_be_from_palm_oil_n"
case ingredientsFromPalmOilTags = "ingredients_from_palm_oil_tags"
case ingredientsFromPalmOilN = "ingredients_from_palm_oil_n"
case ingredientsHierarchy = "ingredients_hierarchy"
case ingredientsIdsDebug = "ingredients_ids_debug"
case ingredientsN = "ingredients_n"
case ingredientsNTags = "ingredients_n_tags"
case ingredientsOriginalTags = "ingredients_original_tags"
case ingredientsTags = "ingredients_tags"
case ingredientsText = "ingredients_text"
case ingredientsTextDebug = "ingredients_text_debug"
case ingredientsTextWithAllergens = "ingredients_text_with_allergens"
case ingredientsThatMayBeFromPalmOilN = "ingredients_that_may_be_from_palm_oil_n"
case ingredientsThatMayBeFromPalmOilTags = "ingredients_that_may_be_from_palm_oil_tags"
case interfaceVersionCreated = "interface_version_created"
case interfaceVersionModified = "interface_version_modified"
case keywords
case knownIngredientsN = "known_ingredients_n"
case labels
case labelsHierarchy = "labels_hierarchy"
case labelsLc = "labels_lc"
case labelsPrevHierarchy = "labels_prev_hierarchy"
case labelsPrevTags = "labels_prev_tags"
case labelsTags = "labels_tags"
case labelsDebugTags = "labels_debug_tags"
case lang
case langDebugTags = "lang_debug_tags"
case languagesHierarchy = "languages_hierarchy"
case languagesTags = "languages_tags"
case lastEditDatesTags = "last_edit_dates_tags"
case lastEditor = "last_editor"
case lastImageDatesTags = "last_image_dates_tags"
case lastImageT = "last_image_t"
case lastModifiedBy = "last_modified_by"
case lastModifiedT = "last_modified_t"
case lc
case link
case linkDebugTags = "link_debug_tags"
case manufacturingPlaces = "manufacturing_places"
case manufacturingPlacesDebugTags = "manufacturing_places_debug_tags"
case manufacturingPlacesTags = "manufacturing_places_tags"
case maxImgid = "max_imgid"
case mineralsPrevTags = "minerals_prev_tags"
case mineralsTags = "minerals_tags"
case miscTags = "misc_tags"
case netWeightUnit = "net_weight_unit"
case netWeightValue = "net_weight_value"
case nutritionDataPer = "nutrition_data_per"
case nutritionScoreWarningNoFruitsVegetablesNuts = "nutrition_score_warning_no_fruits_vegetables_nuts"
case noNutritionData = "no_nutrition_data"
case novaGroup = "nova_group"
case novaGroups = "nova_groups"
case novaGroupDebug = "nova_group_debug"
case novaGroupTags = "nova_group_tags"
case novaGroupsTags = "nova_groups_tags"
case nucleotidesPrevTags = "nucleotides_prev_tags"
case nucleotidesTags = "nucleotides_tags"
case nutrientLevelsTags = "nutrient_levels_tags"
case nutritionData = "nutrition_data"
case nutritionDataPerDebugTags = "nutrition_data_per_debug_tags"
case nutritionDataPrepared = "nutrition_data_prepared"
case nutritionDataPreparedPer = "nutrition_data_prepared_per"
case nutritionGrades = "nutrition_grades"
case nutritionScoreBeverage = "nutrition_score_beverage"
case nutritionScoreDebug = "nutrition_score_debug"
case nutritionScoreWarningNoFiber = "nutrition_score_warning_no_fiber"
case nutritionGradesTags = "nutrition_grades_tags"
case origins
case originsDebugTags = "origins_debug_tags"
case originsTags = "origins_tags"
case otherInformation = "other_information"
case otherNutritionalSubstancesTags = "other_nutritional_substances_tags"
case packaging
case packagingDebugTags = "packaging_debug_tags"
case packagingTags = "packaging_tags"
case photographersTags = "photographers_tags"
case pnnsGroups1 = "pnns_groups_1"
case pnnsGroups2 = "pnns_groups_2"
case pnnsGroups1Tags = "pnns_groups_1_tags"
case pnnsGroups2Tags = "pnns_groups_2_tags"
case popularityKey = "popularity_key"
case producerVersionId = "producer_version_id"
case productName = "product_name"
case productQuantity = "product_quantity"
case purchasePlaces = "purchase_places"
case purchasePlacesDebugTags = "purchase_places_debug_tags"
case purchasePlacesTags = "purchase_places_tags"
case qualityTags = "quality_tags"
case quantity
case quantityDebugTags = "quantity_debug_tags"
case recyclingInstructionsToDiscard = "recycling_instructions_to_discard"
case rev
case servingQuantity = "serving_quantity"
case servingSize = "serving_size"
case servingSizeDebugTags = "serving_size_debug_tags"
case sortkey
case states
case statesHierarchy = "states_hierarchy"
case statesTags = "states_tags"
case stores
case storesDebugTags = "stores_debug_tags"
case storesTags = "stores_tags"
case traces
case tracesFromIngredients = "traces_from_ingredients"
case tracesHierarchy = "traces_hierarchy"
case tracesDebugTags = "traces_debug_tags"
case tracesFromUser = "traces_from_user"
case tracesLc = "traces_lc"
case tracesTags = "traces_tags"
case unknownIngredientsN = "unknown_ingredients_n"
case unknownNutrientsTags = "unknown_nutrients_tags"
case updateKey = "update_key"
case vitaminsPrevTags = "vitamins_prev_tags"
case vitaminsTags = "vitamins_tags"
private enum CodingKeys: String, CodingKey {
// case images
case ingredients
case languagesCodes = "languages_codes"
case nutrientLevels = "nutrient_levels"
case nutriments
case selectedImages = "selected_images"
case sources
case additivesN = "additives_n"
case additivesOldN = "additives_old_n"
case additivesOriginalTags = "additives_original_tags"
case additivesOldTags = "additives_old_tags"
case additivesPrevOriginalTags = "additives_prev_original_tags"
case additivesDebugTags = "additives_debug_tags"
case additivesTags = "additives_tags"
case allergens
case allergensFromIngredients = "allergens_from_ingredients"
case allergensFromUser = "allergens_from_user"
case allergensHierarchy = "allergens_hierarchy"
case allergensLc = "allergens_lc"
case allergensTags = "allergens_tags"
case aminoAcidsPrevTags = "amino_acids_prev_tags"
case aminoAcidsTags = "amino_acids_tags"
case brands
case brandsDebugTags = "brands_debug_tags"
case brandsTags = "brands_tags"
case carbonFootprintPercentOfKnownIngredients = "carbon_footprint_percent_of_known_ingredients"
case carbonFootprintFromKnownIngredientsDebug = "carbon_footprint_from_known_ingredients_debug"
case categories
case categoriesHierarchy = "categories_hierarchy"
case categoriesLc = "categories_lc"
case categoriesPropertiesTags = "categories_properties_tags"
case categoriesTags = "categories_tags"
case checkersTags = "checkers_tags"
case citiesTags = "cities_tags"
case code
case codesTags = "codes_tags"
case comparedToCategory = "compared_to_category"
case complete
case completedT = "completed_t"
case completeness
case conservationConditions = "conservation_conditions"
case countries
case countriesHierarchy = "countries_hierarchy"
case countriesLc = "countries_lc"
case countriesDebugTags = "countries_debug_tags"
case countriesTags = "countries_tags"
case correctorsTags = "correctors_tags"
case createdT = "created_t"
case creator
case dataQualityBugsTags = "data_quality_bugs_tags"
case dataQualityErrorsTags = "data_quality_errors_tags"
case dataQualityInfoTags = "data_quality_info_tags"
case dataQualityTags = "data_quality_tags"
case dataQualityWarningsTags = "data_quality_warnings_tags"
case dataSources = "data_sources"
case dataSourcesTags = "data_sources_tags"
case debugParamSortedLangs = "debug_param_sorted_langs"
case editorsTags = "editors_tags"
case embCodes = "emb_codes"
case embCodesDebugTags = "emb_codes_debug_tags"
case embCodesOrig = "emb_codes_orig"
case embCodesTags = "emb_codes_tags"
case entryDatesTags = "entry_dates_tags"
case expirationDate = "expiration_date"
case expirationDateDebugTags = "expiration_date_debug_tags"
case fruitsVegetablesNuts100GEstimate = "fruits-vegetables-nuts_100g_estimate"
case genericName
case id
case imageFrontSmallUrl = "image_front_small_url"
case imageFrontThumbUrl = "image_front_thumb_url"
case imageFrontUrl = "image_front_url"
case imageIngredientsUrl = "image_ingredients_url"
case imageIngredientsSmallUrl = "image_ingredients_small_url"
case imageIngredientsThumbUrl = "image_ingredients_thumb_url"
case imageNutritionSmallUrl = "image_nutrition_small_url"
case imageNutritionThumbUrl = "image_nutrition_thumb_url"
case imageNutritionUrl = "image_nutrition_url"
case imageSmallUrl = "image_small_url"
case imageThumbUrl = "image_thumb_url"
case imageUrl = "image_url"
case informersTags = "informers_tags"
case ingredientsAnalysisTags = "ingredients_analysis_tags"
case ingredientsDebug = "ingredients_debug"
case ingredientsFromOrThatMayBeFromPalmOilN = "ingredients_from_or_that_may_be_from_palm_oil_n"
case ingredientsFromPalmOilTags = "ingredients_from_palm_oil_tags"
case ingredientsFromPalmOilN = "ingredients_from_palm_oil_n"
case ingredientsHierarchy = "ingredients_hierarchy"
case ingredientsIdsDebug = "ingredients_ids_debug"
case ingredientsN = "ingredients_n"
case ingredientsNTags = "ingredients_n_tags"
case ingredientsOriginalTags = "ingredients_original_tags"
case ingredientsTags = "ingredients_tags"
case ingredientsText = "ingredients_text"
case ingredientsTextDebug = "ingredients_text_debug"
case ingredientsTextWithAllergens = "ingredients_text_with_allergens"
case ingredientsThatMayBeFromPalmOilN = "ingredients_that_may_be_from_palm_oil_n"
case ingredientsThatMayBeFromPalmOilTags = "ingredients_that_may_be_from_palm_oil_tags"
case interfaceVersionCreated = "interface_version_created"
case interfaceVersionModified = "interface_version_modified"
case keywords
case knownIngredientsN = "known_ingredients_n"
case labels
case labelsHierarchy = "labels_hierarchy"
case labelsLc = "labels_lc"
case labelsPrevHierarchy = "labels_prev_hierarchy"
case labelsPrevTags = "labels_prev_tags"
case labelsTags = "labels_tags"
case labelsDebugTags = "labels_debug_tags"
case lang
case langDebugTags = "lang_debug_tags"
case languagesHierarchy = "languages_hierarchy"
case languagesTags = "languages_tags"
case lastEditDatesTags = "last_edit_dates_tags"
case lastEditor = "last_editor"
case lastImageDatesTags = "last_image_dates_tags"
case lastImageT = "last_image_t"
case lastModifiedBy = "last_modified_by"
case lastModifiedT = "last_modified_t"
case lc
case link
case linkDebugTags = "link_debug_tags"
case manufacturingPlaces = "manufacturing_places"
case manufacturingPlacesDebugTags = "manufacturing_places_debug_tags"
case manufacturingPlacesTags = "manufacturing_places_tags"
case maxImgid = "max_imgid"
case mineralsPrevTags = "minerals_prev_tags"
case mineralsTags = "minerals_tags"
case miscTags = "misc_tags"
case netWeightUnit = "net_weight_unit"
case netWeightValue = "net_weight_value"
case nutritionDataPer = "nutrition_data_per"
case nutritionScoreWarningNoFruitsVegetablesNuts = "nutrition_score_warning_no_fruits_vegetables_nuts"
case noNutritionData = "no_nutrition_data"
case novaGroup = "nova_group"
case novaGroups = "nova_groups"
case novaGroupDebug = "nova_group_debug"
case novaGroupTags = "nova_group_tags"
case novaGroupsTags = "nova_groups_tags"
case nucleotidesPrevTags = "nucleotides_prev_tags"
case nucleotidesTags = "nucleotides_tags"
case nutrientLevelsTags = "nutrient_levels_tags"
case nutritionData = "nutrition_data"
case nutritionDataPerDebugTags = "nutrition_data_per_debug_tags"
case nutritionDataPrepared = "nutrition_data_prepared"
case nutritionDataPreparedPer = "nutrition_data_prepared_per"
case nutritionGrades = "nutrition_grades"
case nutritionScoreBeverage = "nutrition_score_beverage"
case nutritionScoreDebug = "nutrition_score_debug"
case nutritionScoreWarningNoFiber = "nutrition_score_warning_no_fiber"
case nutritionGradesTags = "nutrition_grades_tags"
case origins
case originsDebugTags = "origins_debug_tags"
case originsTags = "origins_tags"
case otherInformation = "other_information"
case otherNutritionalSubstancesTags = "other_nutritional_substances_tags"
case packaging
case packagingDebugTags = "packaging_debug_tags"
case packagingTags = "packaging_tags"
case photographersTags = "photographers_tags"
case pnnsGroups1 = "pnns_groups_1"
case pnnsGroups2 = "pnns_groups_2"
case pnnsGroups1Tags = "pnns_groups_1_tags"
case pnnsGroups2Tags = "pnns_groups_2_tags"
case popularityKey = "popularity_key"
case producerVersionId = "producer_version_id"
case productName = "product_name"
case productQuantity = "product_quantity"
case purchasePlaces = "purchase_places"
case purchasePlacesDebugTags = "purchase_places_debug_tags"
case purchasePlacesTags = "purchase_places_tags"
case qualityTags = "quality_tags"
case quantity
case quantityDebugTags = "quantity_debug_tags"
case recyclingInstructionsToDiscard = "recycling_instructions_to_discard"
case rev
case servingQuantity = "serving_quantity"
case servingSize = "serving_size"
case servingSizeDebugTags = "serving_size_debug_tags"
case sortkey
case states
case statesHierarchy = "states_hierarchy"
case statesTags = "states_tags"
case stores
case storesDebugTags = "stores_debug_tags"
case storesTags = "stores_tags"
case traces
case tracesFromIngredients = "traces_from_ingredients"
case tracesHierarchy = "traces_hierarchy"
case tracesDebugTags = "traces_debug_tags"
case tracesFromUser = "traces_from_user"
case tracesLc = "traces_lc"
case tracesTags = "traces_tags"
case unknownIngredientsN = "unknown_ingredients_n"
case unknownNutrientsTags = "unknown_nutrients_tags"
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] = []