From e8a42c1e19df6fc56648dfce5a62d73a01fc6eae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Steffen=20K=C3=B6tte?= Date: Sun, 11 Dec 2022 17:03:24 +0100 Subject: [PATCH] Fix swift lint errors --- Sources/Wealthsimple/Token.swift | 27 ++++++------ .../Wealthsimple/WealthsimpleAccount.swift | 41 ++++++++++--------- .../Wealthsimple/WealthsimplePosition.swift | 41 ++++++++++--------- .../WealthsimpleTransaction.swift | 41 ++++++++++--------- 4 files changed, 80 insertions(+), 70 deletions(-) diff --git a/Sources/Wealthsimple/Token.swift b/Sources/Wealthsimple/Token.swift index 3dd16a9..94feaca 100644 --- a/Sources/Wealthsimple/Token.swift +++ b/Sources/Wealthsimple/Token.swift @@ -147,21 +147,22 @@ struct Token { return } do { - guard let json = try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any] else { - completion(.failure(TokenError.invalidJsonType(json: try JSONSerialization.jsonObject(with: data, options: [])))) - return - } - do { - let token = try Token(json: json, credentialStorage: credentialStorage) - token.saveToken() - completion(.success(token)) - } catch { - - completion(.failure(error as! TokenError)) // swiftlint:disable:this force_cast - } + completion(try parse(data: data, credentialStorage: credentialStorage)) } catch { completion(.failure(TokenError.invalidJson(error: error.localizedDescription))) - return + } + } + + private static func parse(data: Data, credentialStorage: CredentialStorage) throws -> Result { + guard let json = try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any] else { + return .failure(TokenError.invalidJsonType(json: try JSONSerialization.jsonObject(with: data, options: []))) + } + do { + let token = try Self(json: json, credentialStorage: credentialStorage) + token.saveToken() + return .success(token) + } catch { + return .failure(error as! TokenError) // swiftlint:disable:this force_cast } } diff --git a/Sources/Wealthsimple/WealthsimpleAccount.swift b/Sources/Wealthsimple/WealthsimpleAccount.swift index b962be5..4339089 100644 --- a/Sources/Wealthsimple/WealthsimpleAccount.swift +++ b/Sources/Wealthsimple/WealthsimpleAccount.swift @@ -129,28 +129,31 @@ struct WealthsimpleAccount: Account { return } do { - guard let json = try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any] else { - completion(.failure(AccountError.invalidJsonType(json: try JSONSerialization.jsonObject(with: data, options: [])))) - return - } - do { - guard let results = json["results"] as? [[String: Any]], let object = json["object"] as? String else { - throw AccountError.missingResultParamenter(json: json) - } - guard object == "account" else { - throw AccountError.invalidResultParamenter(json: json) - } - var accounts = [Account]() - for result in results { - accounts.append(try WealthsimpleAccount(json: result)) - } - completion(.success(accounts)) - } catch { - completion(.failure(error as! AccountError)) // swiftlint:disable:this force_cast - } + completion(try parse(data: data)) } catch { completion(.failure(AccountError.invalidJson(error: error.localizedDescription))) return } } + + private static func parse(data: Data) throws -> Result<[Account], AccountError> { + guard let json = try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any] else { + return .failure(AccountError.invalidJsonType(json: try JSONSerialization.jsonObject(with: data, options: []))) + } + do { + guard let results = json["results"] as? [[String: Any]], let object = json["object"] as? String else { + throw AccountError.missingResultParamenter(json: json) + } + guard object == "account" else { + throw AccountError.invalidResultParamenter(json: json) + } + var accounts = [Account]() + for result in results { + accounts.append(try Self(json: result)) + } + return .success(accounts) + } catch { + return .failure(error as! AccountError) // swiftlint:disable:this force_cast + } + } } diff --git a/Sources/Wealthsimple/WealthsimplePosition.swift b/Sources/Wealthsimple/WealthsimplePosition.swift index 2536816..882abb6 100644 --- a/Sources/Wealthsimple/WealthsimplePosition.swift +++ b/Sources/Wealthsimple/WealthsimplePosition.swift @@ -134,29 +134,32 @@ struct WealthsimplePosition: Position { return } do { - guard let json = try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any] else { - completion(.failure(PositionError.invalidJsonType(json: try JSONSerialization.jsonObject(with: data, options: [])))) - return - } - do { - guard let results = json["results"] as? [[String: Any]], let object = json["object"] as? String else { - throw PositionError.missingResultParamenter(json: json) - } - guard object == "position" else { - throw PositionError.invalidResultParamenter(json: json) - } - var positions = [Position]() - for result in results { - positions.append(try Self(json: result)) - } - completion(.success(positions)) - } catch { - completion(.failure(error as! PositionError)) // swiftlint:disable:this force_cast - } + completion(try parse(data: data)) } catch { completion(.failure(PositionError.invalidJson(error: error.localizedDescription))) return } } + private static func parse(data: Data) throws -> Result<[Position], PositionError> { + guard let json = try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any] else { + return .failure(PositionError.invalidJsonType(json: try JSONSerialization.jsonObject(with: data, options: []))) + } + do { + guard let results = json["results"] as? [[String: Any]], let object = json["object"] as? String else { + throw PositionError.missingResultParamenter(json: json) + } + guard object == "position" else { + throw PositionError.invalidResultParamenter(json: json) + } + var positions = [Position]() + for result in results { + positions.append(try Self(json: result)) + } + return .success(positions) + } catch { + return .failure(error as! PositionError) // swiftlint:disable:this force_cast + } + } + } diff --git a/Sources/Wealthsimple/WealthsimpleTransaction.swift b/Sources/Wealthsimple/WealthsimpleTransaction.swift index 8288c74..921d14a 100644 --- a/Sources/Wealthsimple/WealthsimpleTransaction.swift +++ b/Sources/Wealthsimple/WealthsimpleTransaction.swift @@ -228,29 +228,32 @@ struct WealthsimpleTransaction: Transaction { return } do { - guard let json = try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any] else { - completion(.failure(TransactionError.invalidJsonType(json: try JSONSerialization.jsonObject(with: data, options: [])))) - return - } - do { - guard let results = json["results"] as? [[String: Any]], let object = json["object"] as? String else { - throw TransactionError.missingResultParamenter(json: json) - } - guard object == "transaction" else { - throw TransactionError.invalidResultParamenter(json: json) - } - var transactions = [Transaction]() - for result in results { - transactions.append(try WealthsimpleTransaction(json: result)) - } - completion(.success(transactions)) - } catch { - completion(.failure(error as! TransactionError)) // swiftlint:disable:this force_cast - } + completion(try parse(data: data)) } catch { completion(.failure(TransactionError.invalidJson(error: error.localizedDescription))) return } } + private static func parse(data: Data) throws -> Result<[Transaction], TransactionError> { + guard let json = try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any] else { + return .failure(TransactionError.invalidJsonType(json: try JSONSerialization.jsonObject(with: data, options: []))) + } + do { + guard let results = json["results"] as? [[String: Any]], let object = json["object"] as? String else { + throw TransactionError.missingResultParamenter(json: json) + } + guard object == "transaction" else { + throw TransactionError.invalidResultParamenter(json: json) + } + var transactions = [Transaction]() + for result in results { + transactions.append(try Self(json: result)) + } + return .success(transactions) + } catch { + return .failure(error as! TransactionError) // swiftlint:disable:this force_cast + } + } + }