Skip to content

Commit

Permalink
fix: Linter check on data decoding
Browse files Browse the repository at this point in the history
  • Loading branch information
fabriziodemaria committed Nov 4, 2024
1 parent 3bc9a04 commit f831167
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 16 deletions.
2 changes: 1 addition & 1 deletion Sources/Confidence/ConfidenceScreenTracker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ extension UIViewController {
let encoder = JSONEncoder()
do {
let data = try encoder.encode(trackableWithMessage.trackMessage())
let messageString = String(decoding: data, as: UTF8.self)
let messageString = String(data: data, encoding: .utf8) ?? "internal_error"
message.updateValue(messageString, forKey: ConfidenceScreenTracker.messageKey)
} catch {
}
Expand Down
23 changes: 13 additions & 10 deletions Sources/Confidence/EventStorage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,17 +75,20 @@ internal class EventStorageImpl: EventStorage {
let decoder = JSONDecoder()
let fileUrl = folderURL.appendingPathComponent(id)
let data = try Data(contentsOf: fileUrl)
let dataString = String(decoding: data, as: UTF8.self)
return try dataString.components(separatedBy: "\n")
.filter { events in
!events.isEmpty
}
.compactMap { eventString in
guard let stringData = eventString.data(using: .utf8) else {
return nil
if let dataString = String(data: data, encoding: .utf8) {
return try dataString.components(separatedBy: "\n")
.filter { events in
!events.isEmpty
}
return try decoder.decode(ConfidenceEvent.self, from: stringData)
}
.compactMap { eventString in
guard let stringData = eventString.data(using: .utf8) else {
return nil
}
return try decoder.decode(ConfidenceEvent.self, from: stringData)
}
} else {
throw ConfidenceError.corruptedCache(message: "Error decoding events cache file")
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions Sources/Confidence/Http/NetworkClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,10 @@ extension NetworkClient {
do {
response.decodedError = try decoder.decode(HttpError.self, from: responseData)
} catch {
let message = String(decoding: responseData, as: UTF8.self)
let message = String(data: responseData, encoding: .utf8)
response.decodedError = HttpError(
code: httpURLResponse.statusCode,
message: message,
message: message ?? "{Error when decoding error message}",
details: []
)
}
Expand Down
2 changes: 1 addition & 1 deletion Tests/ConfidenceProviderTests/ConfidenceProviderTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ private class StorageMock: Storage {
func save(data: Encodable) throws {
try storageQueue.sync {
let dataB = try JSONEncoder().encode(data)
self.data = String(decoding: dataB, as: UTF8.self)
self.data = try XCTUnwrap(String(data: dataB, encoding: .utf8)!)

Check failure on line 142 in Tests/ConfidenceProviderTests/ConfidenceProviderTest.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Force Unwrapping Violation: Force unwrapping should be avoided (force_unwrapping)

saveExpectation?.fulfill()
}
Expand Down
2 changes: 1 addition & 1 deletion Tests/ConfidenceTests/ConfidenceValueTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ final class ConfidenceConfidenceValueTests: XCTestCase {
let encoder = JSONEncoder()
encoder.outputFormatting = .sortedKeys
let data = try encoder.encode(value)
let resultString = try XCTUnwrap(String(decoding: data, as: UTF8.self))
let resultString = try XCTUnwrap(String(data: data, encoding: .utf8)!)

Check failure on line 128 in Tests/ConfidenceTests/ConfidenceValueTests.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Force Unwrapping Violation: Force unwrapping should be avoided (force_unwrapping)
let resultData = try XCTUnwrap(resultString.data(using: .utf8))
let decodedValue = try JSONDecoder().decode(ConfidenceValue.self, from: resultData)

Expand Down
2 changes: 1 addition & 1 deletion Tests/ConfidenceTests/Helpers/StorageMock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class StorageMock: Storage {
func save(data: Encodable) throws {
try storageQueue.sync {
let dataB = try JSONEncoder().encode(data)
self.data = String(decoding: dataB, as: UTF8.self)
self.data = try XCTUnwrap(String(data: dataB, encoding: .utf8)!)

Check failure on line 20 in Tests/ConfidenceTests/Helpers/StorageMock.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Force Unwrapping Violation: Force unwrapping should be avoided (force_unwrapping)

saveExpectation?.fulfill()
}
Expand Down

0 comments on commit f831167

Please sign in to comment.