Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support non-JSON object decodable values in getMetadataValue #4555

Merged
merged 5 commits into from
Dec 6, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 16 additions & 10 deletions Sources/Purchasing/Offering.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<T: Decodable>(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
}
codykerns marked this conversation as resolved.
Show resolved Hide resolved
} else if let value = value as? T {
return value
} else {
return nil
}
}
Expand Down