Explorar o código

Merge pull request #415 from kingst/use-array-for-empty-pump-events

Change pump event JSON to empty array instead of object
Deniz Cengiz hai 1 ano
pai
achega
359119fe08

+ 16 - 1
Trio/Sources/APS/OpenAPSSwift/JSONBridge.swift

@@ -45,7 +45,22 @@ enum JSONBridge {
     }
 
     static func pumpHistory(from: JSON) throws -> [PumpHistoryEvent] {
-        try JSONBridge.from(string: from.rawJSON)
+        do {
+            return try JSONBridge.from(string: from.rawJSON)
+        } catch {
+            // see if we got an empty object "{}"
+            guard let data = from.rawJSON.data(using: .utf8) else {
+                throw error
+            }
+
+            if let parsedObject = try? JSONSerialization.jsonObject(with: data, options: []) as? [String: Any],
+               parsedObject.isEmpty
+            {
+                return []
+            }
+
+            throw error
+        }
     }
 
     static func profile(from: JSON) throws -> Profile {

+ 8 - 0
TrioTests/OpenAPSSwiftTests/JSONCompareTests.swift

@@ -195,6 +195,14 @@ import Testing
         #expect(jsArray.count == 3)
         #expect(swiftArray.count == 2)
     }
+
+    @Test("should be empty array for {} and [] pump history strings") func emptyPumpHistoryParsing() async throws {
+        let emptyArray = try JSONBridge.pumpHistory(from: "[]")
+        let emptyObject = try JSONBridge.pumpHistory(from: "{}")
+
+        #expect(emptyArray.isEmpty)
+        #expect(emptyObject.isEmpty)
+    }
 }
 
 struct TestFailure: Error {