From a09fdddda79d0d165bbec1c22452f518f2990102 Mon Sep 17 00:00:00 2001 From: Cody Kerns Date: Thu, 5 Dec 2024 16:49:31 -0500 Subject: [PATCH 1/5] support non-JSON decodable values in getMetadataValue --- Sources/Purchasing/Offering.swift | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/Sources/Purchasing/Offering.swift b/Sources/Purchasing/Offering.swift index 31991f3521..2cc87f62a0 100644 --- a/Sources/Purchasing/Offering.swift +++ b/Sources/Purchasing/Offering.swift @@ -314,16 +314,22 @@ extension Offering { /// - Throws: Error if the content couldn't be deserialized to the expected type. /// - Note: This decodes JSON using `JSONDecoder.KeyDecodingStrategy.convertFromSnakeCase`. public func getMetadataValue(for key: String) -> T? { - do { - guard let value = self.metadata[key] else { return nil } - let data = try JSONSerialization.data(withJSONObject: value) - - return try JSONDecoder.default.decode( - T.self, - jsonData: data, - logErrors: true - ) - } catch { + guard let value = self.metadata[key] else { return nil } + + if JSONSerialization.isValidJSONObject(value) { + do { + let data = try JSONSerialization.data(withJSONObject: value) + return try JSONDecoder.default.decode( + T.self, + jsonData: data, + logErrors: true + ) + } catch { + return nil + } + } else if let value = value as? T { + return value + } else { return nil } } From a5e564792495f90e6b5880775849ea14ff832139 Mon Sep 17 00:00:00 2001 From: Cody Kerns <44073103+codykerns@users.noreply.github.com> Date: Thu, 5 Dec 2024 17:16:23 -0500 Subject: [PATCH 2/5] remove method definition claiming to throw --- Sources/Purchasing/Offering.swift | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Sources/Purchasing/Offering.swift b/Sources/Purchasing/Offering.swift index 2cc87f62a0..9c97b77abc 100644 --- a/Sources/Purchasing/Offering.swift +++ b/Sources/Purchasing/Offering.swift @@ -310,8 +310,7 @@ extension Offering { } /// - Returns: The `metadata` value associated to `key` for the expected `Decodable` type, - /// or `nil` if not found. - /// - Throws: Error if the content couldn't be deserialized to the expected type. + /// or `nil` if not found or if the content couldn't be deserialized to the expected type.. /// - Note: This decodes JSON using `JSONDecoder.KeyDecodingStrategy.convertFromSnakeCase`. public func getMetadataValue(for key: String) -> T? { guard let value = self.metadata[key] else { return nil } From dfdc099c425526024be25e050d4c7e370a068da3 Mon Sep 17 00:00:00 2001 From: Cody Kerns <44073103+codykerns@users.noreply.github.com> Date: Thu, 5 Dec 2024 17:17:05 -0500 Subject: [PATCH 3/5] Update Offering.swift --- Sources/Purchasing/Offering.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/Purchasing/Offering.swift b/Sources/Purchasing/Offering.swift index 9c97b77abc..328517f4ae 100644 --- a/Sources/Purchasing/Offering.swift +++ b/Sources/Purchasing/Offering.swift @@ -310,7 +310,7 @@ extension Offering { } /// - Returns: The `metadata` value associated to `key` for the expected `Decodable` type, - /// or `nil` if not found or if the content couldn't be deserialized to the expected type.. + /// or `nil` if not found or if the content couldn't be deserialized to the expected type. /// - Note: This decodes JSON using `JSONDecoder.KeyDecodingStrategy.convertFromSnakeCase`. public func getMetadataValue(for key: String) -> T? { guard let value = self.metadata[key] else { return nil } From a64404460c312e16af59ab6eefda50f04b5588df Mon Sep 17 00:00:00 2001 From: Cody Kerns Date: Fri, 6 Dec 2024 09:56:54 -0500 Subject: [PATCH 4/5] test for primitive decodable metadata values without a default --- Tests/UnitTests/Purchasing/OfferingsTests.swift | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/Tests/UnitTests/Purchasing/OfferingsTests.swift b/Tests/UnitTests/Purchasing/OfferingsTests.swift index 2b011e74a0..50f093b72a 100644 --- a/Tests/UnitTests/Purchasing/OfferingsTests.swift +++ b/Tests/UnitTests/Purchasing/OfferingsTests.swift @@ -410,6 +410,18 @@ class OfferingsTests: TestCase { let wrongMetadataType = offeringA.getMetadataValue(for: "string", default: 5.5) expect(wrongMetadataType) == 5.5 + let intWithoutDefault: Int? = offeringA.getMetadataValue(for: "int") + expect(intWithoutDefault) == 5 + + let doubleWithoutDefault: Double? = offeringA.getMetadataValue(for: "double") + expect(doubleWithoutDefault) == 5.5 + + let boolWithoutDefault: Bool? = offeringA.getMetadataValue(for: "boolean") + expect(boolWithoutDefault) == true + + let stringWithoutDefault: String? = offeringA.getMetadataValue(for: "string") + expect(stringWithoutDefault) == "five" + struct Data: Decodable, Equatable { var number: Int } From accd3dc2e3c09ddf5f687d5c958fffdd75681153 Mon Sep 17 00:00:00 2001 From: Cody Kerns Date: Fri, 6 Dec 2024 10:25:15 -0500 Subject: [PATCH 5/5] simplify decoding --- Sources/Purchasing/Offering.swift | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/Sources/Purchasing/Offering.swift b/Sources/Purchasing/Offering.swift index 328517f4ae..e7ebb84ef6 100644 --- a/Sources/Purchasing/Offering.swift +++ b/Sources/Purchasing/Offering.swift @@ -315,17 +315,13 @@ extension Offering { public func getMetadataValue(for key: String) -> T? { guard let value = self.metadata[key] else { return nil } - if JSONSerialization.isValidJSONObject(value) { - do { - let data = try JSONSerialization.data(withJSONObject: value) - return try JSONDecoder.default.decode( - T.self, - jsonData: data, - logErrors: true - ) - } catch { - return nil - } + if JSONSerialization.isValidJSONObject(value), + let data = try? JSONSerialization.data(withJSONObject: value) { + return try? JSONDecoder.default.decode( + T.self, + jsonData: data, + logErrors: true + ) } else if let value = value as? T { return value } else {