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 all swiftlint errors and remove Tests from the linter #134

Merged
merged 3 commits into from
Jun 8, 2021
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
4 changes: 2 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
name: integration-tests
runs-on: macos-latest
env:
DEVELOPER_DIR: /Applications/Xcode_11.5.app/Contents/Developer
DEVELOPER_DIR: /Applications/Xcode_12.4.app/Contents/Developer
steps:
- uses: actions/checkout@v2
- name: Download and run MeiliSearch
Expand All @@ -30,7 +30,7 @@ jobs:
name: linter-check
runs-on: macos-latest
env:
DEVELOPER_DIR: /Applications/Xcode_11.5.app/Contents/Developer
DEVELOPER_DIR: /Applications/Xcode_12.4.app/Contents/Developer
steps:
- uses: actions/checkout@v2
- name: Run SwiftLint
Expand Down
5 changes: 3 additions & 2 deletions .swiftlint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ opt_in_rules: # some rules are only opt-in
excluded:
- Carthage
- Pods
- VaporDemo/.build
- PerfectDemo/.build
- Demos/VaporDemo
- Demos/PerfectDemo
- Tests
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we exclude these folders?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need swiftlint in the demos and tests folder at the moment. I can open a new PR to sort them all later and reintroduce them back to the swiftlint.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey, really sorry @ppamorim but linting is bound to a project as a whole not only the source code. It creates consistency and makes contributing easier.
It would be weird if I don't have to follow the same rules in the tests folder than in the source folder.

- .build

force_cast: warning
Expand Down
4 changes: 2 additions & 2 deletions Demos/PerfectDemo/Sources/PerfectTemplate/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// Created by Kyle Jessup on 2015-11-05.
// Copyright (C) 2015 PerfectlySoft, Inc.
//
//===----------------------------------------------------------------------===//
// ===----------------------------------------------------------------------===//
//
// This source file is part of the Perfect.org open source project
//
Expand All @@ -14,7 +14,7 @@
//
// See http://perfect.org/licensing.html for license information
//
//===----------------------------------------------------------------------===//
// ===----------------------------------------------------------------------===//
//

import PerfectHTTP
Expand Down
4 changes: 2 additions & 2 deletions Sources/MeiliSearch/Client.swift
Original file line number Diff line number Diff line change
Expand Up @@ -811,7 +811,7 @@ public struct MeiliSearch {
If the request was sucessful or `false` if a failure occured.
*/
public func isHealthy(_ completion: @escaping (Bool) -> Void) {
self.health{ result in
self.health { result in
switch result {
case .success:
completion(true)
Expand Down Expand Up @@ -862,7 +862,7 @@ public struct MeiliSearch {
public func getDumpStatus(
UID: String,
_ completion: @escaping (Result<Dump, Swift.Error>) -> Void) {
self.dumps.status(UID,completion)
self.dumps.status(UID, completion)
}

}
8 changes: 4 additions & 4 deletions Sources/MeiliSearch/Config.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ public class Config {

// MARK: Static

///Deafault configuration for the default MeiliSearch host, do not use this in
///production since it does not contains the apiKey.
/// Default configuration for the default MeiliSearch host, do not use this in
/// production since it does not contains the apiKey.
public static let `default`: Config = Config(hostURL: localhost)

/// Default config instance set up to use localhost and port 7700.
Expand Down Expand Up @@ -84,12 +84,12 @@ public class Config {
return self
}

guard let _ = URL(string: hostURL) else {
guard URL(string: hostURL) != nil else {
throw MeiliSearch.Error.hostNotValid
}

let success: Bool = autoreleasepool {
let semaphore = DispatchSemaphore(value: 0)
let semaphore: DispatchSemaphore = DispatchSemaphore(value: 0)
var success: Bool = false
request.get(api: "") { result in
switch result {
Expand Down
8 changes: 4 additions & 4 deletions Sources/MeiliSearch/Constants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ import Foundation
struct Constants {

static let customJSONDecoder: JSONDecoder = {
let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .formatted(Formatter.iso8601)
let decoder: JSONDecoder = JSONDecoder()
decoder.dateDecodingStrategy = JSONDecoder.DateDecodingStrategy.formatted(Formatter.iso8601)
return decoder
}()

static let customJSONEecoder: JSONEncoder = {
let encoder = JSONEncoder()
encoder.dateEncodingStrategy = .formatted(Formatter.iso8601)
let encoder: JSONEncoder = JSONEncoder()
encoder.dateEncodingStrategy = JSONEncoder.DateEncodingStrategy.formatted(Formatter.iso8601)
return encoder
}()

Expand Down
4 changes: 2 additions & 2 deletions Sources/MeiliSearch/Error.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public extension MeiliSearch {

// MARK: Error

///Generic Error types for MeiliSearch,
/// Generic Error types for MeiliSearch,
enum Error: Swift.Error {

/// The client tried to contact the server but it was not found.
Expand All @@ -25,5 +25,5 @@ public extension MeiliSearch {

}

///Allow use of error comparasion for the MeiliSearch.Error type.
/// Allow use of error comparasion for the MeiliSearch.Error type.
extension MeiliSearch.Error: Equatable {}
15 changes: 10 additions & 5 deletions Sources/MeiliSearch/Indexes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,8 @@ struct Indexes {
_ UID: String,
_ completion: @escaping (Result<Index, Swift.Error>) -> Void) {

let payload = CreateIndexPayload(uid: UID)
let payload: CreateIndexPayload = CreateIndexPayload(uid: UID)
let data: Data

do {
data = try JSONEncoder().encode(payload)
} catch {
Expand Down Expand Up @@ -124,8 +123,14 @@ struct Indexes {
_ primaryKey: String,
_ completion: @escaping (Result<Index, Swift.Error>) -> Void) {

let payload = UpdateIndexPayload(primaryKey: primaryKey)
let data: Data = try! JSONEncoder().encode(payload)
let payload: UpdateIndexPayload = UpdateIndexPayload(primaryKey: primaryKey)
let data: Data
do {
data = try JSONEncoder().encode(payload)
} catch {
completion(.failure(MeiliSearch.Error.invalidJSON))
return
}

self.request.put(api: "/indexes/\(UID)", data) { result in

Expand Down Expand Up @@ -216,7 +221,7 @@ public enum CreateError: Swift.Error, Equatable {

let underlyingError: NSError = error.underlying as NSError

if let data = error.data {
if let data: Data = error.data {

let msErrorResponse: MSErrorResponse?
do {
Expand Down
10 changes: 5 additions & 5 deletions Sources/MeiliSearch/Model/Dump.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ public struct Dump: Codable, Equatable {
}

public init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
let rawStatus = try container.decode(String.self)
let container: SingleValueDecodingContainer = try decoder.singleValueContainer()
let rawStatus: String = try container.decode(String.self)
switch rawStatus {
case "in_progress":
self = .inProgress
self = Status.inProgress
case "dump_process_failed":
self = .failed
self = Status.failed
case "done":
self = .done
self = Status.done
default:
throw CodingError.unknownStatus
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/MeiliSearch/Model/Key.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ public struct Key: Codable, Equatable {

// MARK: Properties

///Private key used to access a determined set of API routes.
/// Private key used to access a determined set of API routes.
public let `private`: String

///Public key used to access a determined set of API routes.
/// Public key used to access a determined set of API routes.
public let `public`: String

}
2 changes: 1 addition & 1 deletion Sources/MeiliSearch/Model/Setting.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ extension Setting {

/// Tries to decode the JSON object to Setting object.
public init(from decoder: Decoder) throws {
let values = try? decoder.container(keyedBy: CodingKeys.self)
let values: KeyedDecodingContainer<CodingKeys>? = try? decoder.container(keyedBy: CodingKeys.self)
rankingRules = (try? values?.decodeIfPresent([String].self, forKey: .rankingRules)) ?? []
searchableAttributes = (try? values?.decodeIfPresent([String].self, forKey: .searchableAttributes)) ?? ["*"]
displayedAttributes = (try? values?.decodeIfPresent([String].self, forKey: .displayedAttributes)) ?? ["*"]
Expand Down
26 changes: 13 additions & 13 deletions Sources/MeiliSearch/Model/Update.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,30 @@ public struct Update: Codable, Equatable {
/// The UID of the update.
public let updateId: Int

///Result type for the Update.
/// Result type for the Update.
public struct Result: Codable, Equatable {

// MARK: Properties

///Returns if the update has been sucessful or not.
/// Returns if the update has been sucessful or not.
public let status: Status

///Unique ID for the current `Update`.
/// Unique ID for the current `Update`.
public let updateId: Int

///Type of update.
/// Type of update.
public let type: UpdateType

///Duration of the update process.
/// Duration of the update process.
public let duration: TimeInterval?

///Date when the update has been enqueued.
/// Date when the update has been enqueued.
public let enqueuedAt: Date

///Date when the update has been processed.
/// Date when the update has been processed.
public let processedAt: Date?

///Type of `Update`.
/// Type of `Update`.
public struct UpdateType: Codable, Equatable {

// MARK: Properties
Expand All @@ -60,15 +60,15 @@ public struct Update: Codable, Equatable {
}

public init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
let rawStatus = try container.decode(String.self)
let container: SingleValueDecodingContainer = try decoder.singleValueContainer()
let rawStatus: String = try container.decode(String.self)
switch rawStatus {
case "enqueued":
self = .enqueued
self = Status.enqueued
case "processed":
self = .processed
self = Status.processed
case "failed":
self = .failed
self = Status.failed
default:
throw StatusError.unknown
}
Expand Down
41 changes: 31 additions & 10 deletions Sources/MeiliSearch/Request.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public protocol URLSessionProtocol {
/// Result for the `execute` function.
typealias DataTaskResult = (Data?, URLResponse?, Error?) -> Void

///Function that will trigger the HTTP request.
/// Function that will trigger the HTTP request.
func execute(
with request: URLRequest,
completionHandler: @escaping DataTaskResult) -> URLSessionDataTaskProtocol
Expand All @@ -17,10 +17,14 @@ public protocol URLSessionProtocol {

/// URLSessionDataTaskProtocol handler.
public protocol URLSessionDataTaskProtocol {
///Trigger HTTP request.
/// Trigger HTTP request.
func resume()
}

public enum MSHTTPError: Swift.Error {
case invalidURL
}

struct MSError: Swift.Error {
let data: Data?
let underlying: Swift.Error
Expand Down Expand Up @@ -56,7 +60,12 @@ final class Request {
urlString += param
}

var request: URLRequest = URLRequest(url: URL(string: urlString)!)
guard let url: URL = URL(string: urlString) else {
completion(.failure(MSHTTPError.invalidURL))
return
}

var request: URLRequest = URLRequest(url: url)
request.httpMethod = "GET"
headers.forEach { (key, value) in
request.addValue(value, forHTTPHeaderField: key)
Expand Down Expand Up @@ -97,8 +106,12 @@ final class Request {
_ data: Data,
_ completion: @escaping (Result<Data, Swift.Error>) -> Void) {

let urlString: String = config.url(api: api)
var request: URLRequest = URLRequest(url: URL(string: urlString)!)
guard let url: URL = URL(string: config.url(api: api)) else {
completion(.failure(MSHTTPError.invalidURL))
return
}

var request: URLRequest = URLRequest(url: url)
request.httpMethod = "POST"
request.httpBody = data
request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
Expand All @@ -111,7 +124,7 @@ final class Request {
let task: URLSessionDataTaskProtocol = session.execute(with: request) { (data, response, error) in

if let error: Swift.Error = error {
let msError = MSError(data: data, underlying: error)
let msError: MSError = MSError(data: data, underlying: error)
completion(.failure(msError))
return
}
Expand Down Expand Up @@ -144,8 +157,12 @@ final class Request {
_ data: Data,
_ completion: @escaping (Result<Data?, Swift.Error>) -> Void) {

let urlString: String = config.url(api: api)
var request: URLRequest = URLRequest(url: URL(string: urlString)!)
guard let url: URL = URL(string: config.url(api: api)) else {
completion(.failure(MSHTTPError.invalidURL))
return
}

var request: URLRequest = URLRequest(url: url)
request.httpMethod = "PUT"
request.httpBody = data
request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
Expand Down Expand Up @@ -184,8 +201,12 @@ final class Request {
api: String,
_ completion: @escaping (Result<Data?, Swift.Error>) -> Void) {

let urlString: String = config.url(api: api)
var request: URLRequest = URLRequest(url: URL(string: urlString)!)
guard let url: URL = URL(string: config.url(api: api)) else {
completion(.failure(MSHTTPError.invalidURL))
return
}

var request: URLRequest = URLRequest(url: url)
request.httpMethod = "DELETE"
request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Accept")
Expand Down
Loading