Ahhh, OpenFoodFacts and their types...
This commit is contained in:
@@ -8,4 +8,15 @@ extension KeyedDecodingContainer {
|
|||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public func decodeStringOrInt(forKey key: Key) throws -> Int? {
|
||||||
|
if let intVal = try? decode(Int.self, forKey: key) {
|
||||||
|
return intVal
|
||||||
|
}
|
||||||
|
if let stringVal = try? decode(String.self, forKey: key) {
|
||||||
|
return Int(stringVal)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,4 +23,23 @@ public struct NutriscoreData: Sendable, Codable {
|
|||||||
case energy
|
case energy
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public init(from decoder: any Decoder) throws {
|
||||||
|
let container = try decoder.container(keyedBy: CodingKeys.self)
|
||||||
|
|
||||||
|
saturatedFatRatio = try container.decodeIfPresent(
|
||||||
|
Float.self, forKey: .saturatedFatRatio)
|
||||||
|
saturatedFatRatioPoints = try container.decodeIfPresent(
|
||||||
|
Int.self, forKey: .saturatedFatRatioPoints)
|
||||||
|
saturatedFatRatioValue = try container.decodeIfPresent(
|
||||||
|
Float.self, forKey: .saturatedFatRatioValue)
|
||||||
|
|
||||||
|
isBeverage = try container.decodeIfPresent(
|
||||||
|
Int.self, forKey: .isBeverage)
|
||||||
|
isCheese = try container.decodeIfPresent(Int.self, forKey: .isCheese)
|
||||||
|
isWater = try container.decodeStringOrInt(forKey: .isWater)
|
||||||
|
isFat = try container.decodeIfPresent(Int.self, forKey: .isFat)
|
||||||
|
energy = try container.decodeIfPresent(Int.self, forKey: .energy)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -43,7 +43,7 @@ final class OpenFoodFactsTests: XCTestCase {
|
|||||||
let results = response.products
|
let results = response.products
|
||||||
|
|
||||||
let jsonResults = try JSONEncoder().encode(results)
|
let jsonResults = try JSONEncoder().encode(results)
|
||||||
try jsonResults.write(to: .init(filePath: "./jsonResults.json"))
|
_ = jsonResults
|
||||||
|
|
||||||
XCTAssertFalse(results.isEmpty)
|
XCTAssertFalse(results.isEmpty)
|
||||||
XCTAssertEqual(results.count, 5)
|
XCTAssertEqual(results.count, 5)
|
||||||
|
|||||||
Reference in New Issue
Block a user