-
-
Notifications
You must be signed in to change notification settings - Fork 341
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
299 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import Foundation | ||
|
||
func decodeFromJSONData<T: Decodable>(jsonData: Data) -> T? { | ||
if jsonData.isEmpty { | ||
return nil | ||
} | ||
|
||
do { | ||
let decoder = JSONDecoder() | ||
return try decoder.decode(T.self, from: jsonData) | ||
} catch { | ||
SentryLog.error("Could not decode object of type \(T.self) from JSON data due to error: \(error)") | ||
return nil | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
@_implementationOnly import _SentryPrivate | ||
import Foundation | ||
|
||
extension Geo: Decodable { | ||
|
||
private enum CodingKeys: String, CodingKey { | ||
case city | ||
case countryCode = "country_code" | ||
case region | ||
} | ||
|
||
required convenience public init(from decoder: any Decoder) throws { | ||
let container = try decoder.container(keyedBy: CodingKeys.self) | ||
self.init() | ||
self.city = try container.decodeIfPresent(String.self, forKey: .city) | ||
self.countryCode = try container.decodeIfPresent(String.self, forKey: .countryCode) | ||
self.region = try container.decodeIfPresent(String.self, forKey: .region) | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
Tests/SentryTests/Protocol/Codable/SentryCodableTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
@testable import Sentry | ||
import XCTest | ||
|
||
class SentryCodableTests: XCTestCase { | ||
|
||
func testDecodeWithEmptyData_ReturnsNil() { | ||
XCTAssertNil(decodeFromJSONData(jsonData: Data()) as Geo?) | ||
} | ||
|
||
func testDecodeWithGarbageData_ReturnsNil() { | ||
let data = "garbage".data(using: .utf8)! | ||
XCTAssertNil(decodeFromJSONData(jsonData: data) as Geo?) | ||
} | ||
|
||
func testDecodeWithWrongJSON_ReturnsEmptyObject() { | ||
let wrongJSON = "{\"wrong\": \"json\"}".data(using: .utf8)! | ||
let actual = decodeFromJSONData(jsonData: wrongJSON) as Geo? | ||
let expected = Geo() | ||
|
||
XCTAssertEqual(expected, actual) | ||
} | ||
|
||
func testDecodeWithBrokenJSON_ReturnsNil() { | ||
let brokenJSON = "{\"broken\": \"json\"".data(using: .utf8)! | ||
XCTAssertNil(decodeFromJSONData(jsonData: brokenJSON) as Geo?) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters