Skip to content

Commit

Permalink
fix swiftlint non_optional_string_data_conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
cbaker6 committed Jun 24, 2024
1 parent f93f696 commit 8132c64
Show file tree
Hide file tree
Showing 36 changed files with 172 additions and 214 deletions.
1 change: 1 addition & 0 deletions .swiftlint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ disabled_rules:
- file_length
- identifier_name
- blanket_disable_command
- non_optional_string_data_conversion
excluded: # paths to ignore during linting. Takes precedence over `included`.
- Tests/ParseSwiftTests/ParseEncoderTests
- DerivedData
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,7 @@ Task {
assertionFailure("Data is not the same. Something went wrong.")
}

guard let parseFileString = String(data: dataFromParseFile, encoding: .utf8) else {
fatalError("Error: Could not create String from data.")
}
let parseFileString = String(decoding: dataFromParseFile, as: UTF8.self)
print("The data saved on parse is: \"\(parseFileString)\"")
} else {
assertionFailure("Error fetching: there should be a localURL")
Expand Down
8 changes: 4 additions & 4 deletions Sources/ParseSwift/API/API.swift
Original file line number Diff line number Diff line change
Expand Up @@ -257,17 +257,17 @@ public struct API {
case .removeMimeType:
headers.removeValue(forKey: "Content-Type")
case .metadata(let metadata):
metadata.forEach {(key, value) -> Void in
metadata.forEach {(key, value) in
headers[key] = value
}
case .tags(let tags):
tags.forEach {(key, value) -> Void in
tags.forEach {(key, value) in
headers[key] = value
}
case .context(let context):
let context = AnyEncodable(context)
if let encoded = try? ParseCoding.jsonEncoder().encode(context),
let encodedString = String(data: encoded, encoding: .utf8) {
if let encoded = try? ParseCoding.jsonEncoder().encode(context) {
let encodedString = String(decoding: encoded, as: UTF8.self)
headers["X-Parse-Cloud-Context"] = encodedString
}
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@ public struct ParseApple<AuthenticatedUser: ParseUser>: ParseAuthentication {
/// to a string.
func makeDictionary(user: String,
identityToken: Data) throws -> [String: String] {
guard let identityTokenString = String(data: identityToken, encoding: .utf8) else {
throw ParseError(code: .otherCause, message: "Could not convert identityToken to String")
}
let identityTokenString = String(decoding: identityToken, as: UTF8.self)
return [AuthenticationKeys.id.rawValue: user,
AuthenticationKeys.token.rawValue: identityTokenString]
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/ParseSwift/Coding/AnyCodable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ extension AnyCodable: Hashable {
public func hash(into hasher: inout Hasher) {
do {
let encodedData = try ParseCoding.jsonEncoder().encode(self)
let encodedString = String(data: encodedData, encoding: .utf8)
let encodedString = String(decoding: encodedData, as: UTF8.self)
hasher.combine(encodedString)
} catch {
hasher.combine(0)
Expand Down
2 changes: 1 addition & 1 deletion Sources/ParseSwift/Coding/AnyEncodable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ extension AnyEncodable: Hashable {
public func hash(into hasher: inout Hasher) {
do {
let encodedData = try ParseCoding.jsonEncoder().encode(self)
let encodedString = String(data: encodedData, encoding: .utf8)
let encodedString = String(decoding: encodedData, as: UTF8.self)
hasher.combine(encodedString)
} catch {
hasher.combine(0)
Expand Down
3 changes: 1 addition & 2 deletions Sources/ParseSwift/Coding/ParseCoding.swift
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,7 @@ extension ParseCoding {

if
let decoded = try container.decodeIfPresent(String.self, forKey: .iso),
let date = dateFormatter.date(from: decoded)
{
let date = dateFormatter.date(from: decoded) {
return date
} else {
throw ParseError(
Expand Down
9 changes: 3 additions & 6 deletions Sources/ParseSwift/Extensions/Encodable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,17 @@ internal extension Encodable {
self,
acl: nil
),
let lhsString = String(data: lhsData, encoding: .utf8),
let other = other,
let rhsData = try? ParseCoding
.parseEncoder()
.encode(
other,
acl: nil
),
let rhsString = String(
data: rhsData,
encoding: .utf8
) else {
) else {
return false
}
let lhsString = String(decoding: lhsData, as: UTF8.self)
let rhsString = String(decoding: rhsData, as: UTF8.self)
return lhsString == rhsString
}
}
3 changes: 2 additions & 1 deletion Sources/ParseSwift/Extensions/URLSession.swift
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,9 @@ internal extension URLSession {
return .failure(ParseError(message: "Error decoding parse-server response: \(response)",
swift: error))
}
let errorString = String(decoding: json, as: UTF8.self)

Check warning on line 93 in Sources/ParseSwift/Extensions/URLSession.swift

View check run for this annotation

Codecov / codecov/patch

Sources/ParseSwift/Extensions/URLSession.swift#L93

Added line #L93 was not covered by tests
// swiftlint:disable:next line_length
return .failure(ParseError(message: "Error decoding parse-server response: \(response) with error: \(String(describing: error)) Format: \(String(describing: String(data: json, encoding: .utf8)))",
return .failure(ParseError(message: "Error decoding parse-server response: \(response) with error: \(String(describing: error)) Format: \(errorString)",

Check warning on line 95 in Sources/ParseSwift/Extensions/URLSession.swift

View check run for this annotation

Codecov / codecov/patch

Sources/ParseSwift/Extensions/URLSession.swift#L95

Added line #L95 was not covered by tests
swift: error))
}
return .failure(parseError)
Expand Down
10 changes: 2 additions & 8 deletions Sources/ParseSwift/LiveQuery/LiveQuerySocket.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,7 @@ extension LiveQuerySocket {
.encode(await StandardMessage(operation: .connect,
// swiftlint:disable:next line_length
additionalProperties: Parse.configuration.liveQueryConnectionAdditionalProperties))
guard let encodedAsString = String(data: encoded, encoding: .utf8) else {
throw ParseError(code: .otherCause,
message: "Could not encode connect message: \(encoded)")
}
let encodedAsString = String(decoding: encoded, as: UTF8.self)
try await task.send(.string(encodedAsString))
await self.receive(task)
}
Expand All @@ -69,10 +66,7 @@ extension LiveQuerySocket {
// MARK: Send
extension LiveQuerySocket {
func send(_ data: Data, task: URLSessionWebSocketTask) async throws {
guard let encodedAsString = String(data: data, encoding: .utf8) else {
throw ParseError(code: .otherCause,
message: "Could not encode data as string: \(data)")
}
let encodedAsString = String(decoding: data, as: UTF8.self)
try await task.send(.string(encodedAsString))
}
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/ParseSwift/LiveQuery/ParseLiveQuery.swift
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ Not attempting to open ParseLiveQuery socket anymore
// Resubscribe to all subscriptions by moving them in front of pending
var tempPendingSubscriptions = [(RequestId, SubscriptionRecord)]()
let subscriptions = await self.subscriptions.getCurrent()
subscriptions.forEach { (key, value) -> Void in
subscriptions.forEach { (key, value) in
tempPendingSubscriptions.append((key, value))
}
await self.subscriptions.removeAll()
Expand Down
5 changes: 2 additions & 3 deletions Sources/ParseSwift/Objects/ParseObject.swift
Original file line number Diff line number Diff line change
Expand Up @@ -721,11 +721,10 @@ transactions for this call.
// MARK: CustomDebugStringConvertible
extension ParseObject {
public var debugDescription: String {
guard let descriptionData = try? ParseCoding.jsonEncoder().encode(self),
let descriptionString = String(data: descriptionData, encoding: .utf8) else {
guard let descriptionData = try? ParseCoding.jsonEncoder().encode(self) else {
return "\(className) ()"
}

let descriptionString = String(decoding: descriptionData, as: UTF8.self)
return "\(className) (\(descriptionString))"
}
}
Expand Down
4 changes: 1 addition & 3 deletions Sources/ParseSwift/Protocols/Objectable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,7 @@ extension Objectable {
let encoded = try ParseCoding.parseEncoder().encode(object,
acl: nil,
batching: false)
guard let hashString = String(data: encoded, encoding: .utf8) else {
throw ParseError(code: .otherCause, message: "Could not create hash")
}
let hashString = String(decoding: encoded, as: UTF8.self)
return hashString
}

Expand Down
5 changes: 2 additions & 3 deletions Sources/ParseSwift/Protocols/ParseEncodable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@ public protocol ParseEncodable: Encodable {}
// MARK: CustomDebugStringConvertible
extension ParseEncodable {
public var debugDescription: String {
guard let descriptionData = try? ParseCoding.jsonEncoder().encode(self),
let descriptionString = String(data: descriptionData, encoding: .utf8) else {
guard let descriptionData = try? ParseCoding.jsonEncoder().encode(self) else {
return "()"
}

let descriptionString = String(decoding: descriptionData, as: UTF8.self)
return "\(descriptionString)"
}
}
Expand Down
7 changes: 3 additions & 4 deletions Sources/ParseSwift/Protocols/ParseOperationable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,10 @@ public protocol ParseOperationable: Codable,
// MARK: CustomDebugStringConvertible
public extension ParseOperationable {
var debugDescription: String {
guard let descriptionData = try? ParseCoding.jsonEncoder().encode(self),
let descriptionString = String(data: descriptionData, encoding: .utf8) else {
return "()"
guard let descriptionData = try? ParseCoding.jsonEncoder().encode(self) else {
return "()"

Check warning on line 23 in Sources/ParseSwift/Protocols/ParseOperationable.swift

View check run for this annotation

Codecov / codecov/patch

Sources/ParseSwift/Protocols/ParseOperationable.swift#L23

Added line #L23 was not covered by tests
}

let descriptionString = String(decoding: descriptionData, as: UTF8.self)
return "\(descriptionString)"
}
}
Expand Down
7 changes: 3 additions & 4 deletions Sources/ParseSwift/Protocols/ParseTypeable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,10 @@ public protocol ParseTypeable: Codable,
// MARK: CustomDebugStringConvertible
extension ParseTypeable {
public var debugDescription: String {
guard let descriptionData = try? ParseCoding.jsonEncoder().encode(self),
let descriptionString = String(data: descriptionData, encoding: .utf8) else {
return "()"
guard let descriptionData = try? ParseCoding.jsonEncoder().encode(self) else {
return "()"

Check warning on line 23 in Sources/ParseSwift/Protocols/ParseTypeable.swift

View check run for this annotation

Codecov / codecov/patch

Sources/ParseSwift/Protocols/ParseTypeable.swift#L23

Added line #L23 was not covered by tests
}

let descriptionString = String(decoding: descriptionData, as: UTF8.self)
return "\(descriptionString)"
}
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/ParseSwift/Types/ParseError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,7 @@ extension ParseError: LocalizedError {
}
}

// MARK:
// MARK: Equatable
extension ParseError: Equatable {
public static func == (lhs: Self, rhs: Self) -> Bool {
lhs.code == rhs.code &&
Expand Down
7 changes: 3 additions & 4 deletions Sources/ParseSwift/Types/ParseFieldOptions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,10 @@ extension ParseFieldOptions where V: ParseObject {
// MARK: CustomDebugStringConvertible
extension ParseFieldOptions {
public var debugDescription: String {
guard let descriptionData = try? ParseCoding.jsonEncoder().encode(self),
let descriptionString = String(data: descriptionData, encoding: .utf8) else {
return "()"
guard let descriptionData = try? ParseCoding.jsonEncoder().encode(self) else {
return "()"

Check warning on line 47 in Sources/ParseSwift/Types/ParseFieldOptions.swift

View check run for this annotation

Codecov / codecov/patch

Sources/ParseSwift/Types/ParseFieldOptions.swift#L47

Added line #L47 was not covered by tests
}

let descriptionString = String(decoding: descriptionData, as: UTF8.self)
return "\(descriptionString)"
}
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/ParseSwift/Types/ParseFile.swift
Original file line number Diff line number Diff line change
Expand Up @@ -520,10 +520,10 @@ extension ParseFile {
// MARK: CustomDebugStringConvertible
extension ParseFile: CustomDebugStringConvertible {
public var debugDescription: String {
guard let descriptionData = try? ParseCoding.jsonEncoder().encode(self),
let descriptionString = String(data: descriptionData, encoding: .utf8) else {
guard let descriptionData = try? ParseCoding.jsonEncoder().encode(self) else {
return "()"
}
let descriptionString = String(decoding: descriptionData, as: UTF8.self)
return "\(descriptionString)"
}
}
Expand Down
7 changes: 3 additions & 4 deletions Sources/ParseSwift/Types/ParseOperation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -439,11 +439,10 @@ public extension ParseObject {
// MARK: CustomDebugStringConvertible
public extension ParseOperation {
var debugDescription: String {
guard let descriptionData = try? ParseCoding.jsonEncoder().encode(self),
let descriptionString = String(data: descriptionData, encoding: .utf8) else {
return "()"
guard let descriptionData = try? ParseCoding.jsonEncoder().encode(self) else {
return "()"

Check warning on line 443 in Sources/ParseSwift/Types/ParseOperation.swift

View check run for this annotation

Codecov / codecov/patch

Sources/ParseSwift/Types/ParseOperation.swift#L443

Added line #L443 was not covered by tests
}

let descriptionString = String(decoding: descriptionData, as: UTF8.self)
return "\(descriptionString)"
}
}
Expand Down
19 changes: 9 additions & 10 deletions Sources/ParseSwift/Types/Query.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public struct Query<T>: ParseTypeable where T: ParseObject {
return nil
}
let encoded = try ParseCoding.jsonEncoder().encode(value)
return String(data: encoded, encoding: .utf8)
return String(decoding: encoded, as: UTF8.self)
}
}

Expand Down Expand Up @@ -104,7 +104,7 @@ public struct Query<T>: ParseTypeable where T: ParseObject {
return nil
}
let encoded = try ParseCoding.jsonEncoder().encode(value)
return String(data: encoded, encoding: .utf8)
return String(decoding: encoded, as: UTF8.self)
}
}

Expand Down Expand Up @@ -903,16 +903,15 @@ extension Query: Queryable {
pipeline.forEach { updatedPipeline = $0.map { [$0.key: AnyCodable($0.value)] } }

guard let encoded = try? ParseCoding.jsonEncoder()
.encode(self.`where`),
let whereString = String(data: encoded, encoding: .utf8) else {
.encode(self.`where`) else {
let error = ParseError(code: .otherCause,
message: "Cannot decode \"where\" to String.")
callbackQueue.async {
completion(.failure(error))
}
return
}

let whereString = String(decoding: encoded, as: UTF8.self)
var query = self
query.`where` = QueryWhere()

Expand Down Expand Up @@ -976,8 +975,7 @@ extension Query: Queryable {
pipeline.forEach { updatedPipeline = $0.map { [$0.key: AnyCodable($0.value)] } }

guard let encoded = try? ParseCoding.jsonEncoder()
.encode(self.`where`),
let whereString = String(data: encoded, encoding: .utf8) else {
.encode(self.`where`) else {
let error = ParseError(code: .otherCause,
message: "Cannot decode \"where\" to String.")
callbackQueue.async {
Expand All @@ -986,6 +984,7 @@ extension Query: Queryable {
return
}

let whereString = String(decoding: encoded, as: UTF8.self)
var query = self
query.`where` = QueryWhere()

Expand Down Expand Up @@ -1710,20 +1709,20 @@ internal extension Query {
return nil
}
let encoded = try ParseCoding.jsonEncoder().encode(value)
return String(data: encoded, encoding: .utf8)
return String(decoding: encoded, as: UTF8.self)
}

func encodeAsString(_ key: KeyPath<Self, Set<String>?>) throws -> String? {
guard let value = self[keyPath: key]?.sorted() else {
return nil
}
let encoded = try ParseCoding.jsonEncoder().encode(value)
return String(data: encoded, encoding: .utf8)
return String(decoding: encoded, as: UTF8.self)
}

func encodeAsString<W>(_ key: KeyPath<Self, W>) throws -> String? where W: Encodable {
let encoded = try ParseCoding.jsonEncoder().encode(self[keyPath: key])
return String(data: encoded, encoding: .utf8)
return String(decoding: encoded, as: UTF8.self)
}
}
// swiftlint:disable:this file_length
2 changes: 1 addition & 1 deletion Tests/ParseSwiftTests/IOS13Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ class IOS13Tests: XCTestCase {
.encode(body, collectChildren: false,
objectsSavedBeforeThisOne: nil,
filesSavedBeforeThisOne: nil).encoded
let decoded = try XCTUnwrap(String(data: encoded, encoding: .utf8))
let decoded = try XCTUnwrap(String(decoding: encoded, as: UTF8.self))
XCTAssertEqual(decoded, expected)
}
}
2 changes: 1 addition & 1 deletion Tests/ParseSwiftTests/ParseAnalyticsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class ParseAnalyticsTests: XCTestCase {
let expected2 = "{\"dimensions\":{\"stop\":\"drop\"},\"name\":\"hello\"}"
XCTAssertEqual(decoded2, expected2)
let encoded3 = try ParseCoding.parseEncoder().encode(event2)
let decoded3 = String(data: encoded3, encoding: .utf8)
let decoded3 = String(decoding: encoded3, as: UTF8.self)
let expected3 = "{\"dimensions\":{\"stop\":\"drop\"}}"
XCTAssertEqual(decoded3, expected3)
}
Expand Down
Loading

0 comments on commit 8132c64

Please sign in to comment.