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

Fix swift lint errors #156

Merged
merged 1 commit into from
Dec 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
27 changes: 14 additions & 13 deletions Sources/Wealthsimple/Token.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<Self, TokenError> {
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
}
}

Expand Down
41 changes: 22 additions & 19 deletions Sources/Wealthsimple/WealthsimpleAccount.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}
41 changes: 22 additions & 19 deletions Sources/Wealthsimple/WealthsimplePosition.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

}
41 changes: 22 additions & 19 deletions Sources/Wealthsimple/WealthsimpleTransaction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

}