From cff33b7d09090ee5f16cfd0be0a1edb468382b9a Mon Sep 17 00:00:00 2001 From: James Sherlock <15193942+Sherlouk@users.noreply.github.com> Date: Sat, 23 Sep 2023 22:21:54 +0100 Subject: [PATCH] Use Types instead of Strings for Task Queries --- .../MeiliSearch/QueryParameters/CancelTasksQuery.swift | 10 +++++----- .../MeiliSearch/QueryParameters/DeleteTasksQuery.swift | 10 +++++----- Sources/MeiliSearch/QueryParameters/TasksQuery.swift | 10 +++++----- Tests/MeiliSearchUnitTests/IndexesTests.swift | 2 +- .../QueryParameters/TasksQueryTests.swift | 2 +- Tests/MeiliSearchUnitTests/TasksTests.swift | 2 +- 6 files changed, 18 insertions(+), 18 deletions(-) diff --git a/Sources/MeiliSearch/QueryParameters/CancelTasksQuery.swift b/Sources/MeiliSearch/QueryParameters/CancelTasksQuery.swift index 11ad5f5a..4cef573c 100644 --- a/Sources/MeiliSearch/QueryParameters/CancelTasksQuery.swift +++ b/Sources/MeiliSearch/QueryParameters/CancelTasksQuery.swift @@ -5,9 +5,9 @@ import Foundation */ public class CancelTasksQuery: Queryable { /// List of strings with all the types the response should contain. - public let types: [String] + public let types: [TaskType] /// List of strings with all the statuses the response should contain. - public let statuses: [String] + public let statuses: [Task.Status] /// Filter tasks response by a particular list of index Uids strings public let indexUids: [String] /// Filter tasks based on a list of task's uids. @@ -22,7 +22,7 @@ public class CancelTasksQuery: Queryable { public let afterStartedAt: Date? init( - types: [String]? = nil, statuses: [String]? = nil, + types: [TaskType]? = nil, statuses: [Task.Status]? = nil, indexUids: [String]? = nil, uids: [Int]? = nil, beforeEnqueuedAt: Date? = nil, afterEnqueuedAt: Date? = nil, beforeStartedAt: Date? = nil, afterStartedAt: Date? = nil @@ -40,8 +40,8 @@ public class CancelTasksQuery: Queryable { internal func buildQuery() -> [String: Codable?] { [ "uids": uids.isEmpty ? nil : uids.map(String.init).joined(separator: ","), - "types": types.isEmpty ? nil : types.joined(separator: ","), - "statuses": statuses.isEmpty ? nil : statuses.joined(separator: ","), + "types": types.isEmpty ? nil : types.map({ $0.description }).joined(separator: ","), + "statuses": statuses.isEmpty ? nil : statuses.map({ $0.rawValue }).joined(separator: ","), "indexUids": indexUids.isEmpty ? nil : indexUids.joined(separator: ","), "beforeEnqueuedAt": Formatter.formatOptionalDate(date: beforeEnqueuedAt), "afterEnqueuedAt": Formatter.formatOptionalDate(date: afterEnqueuedAt), diff --git a/Sources/MeiliSearch/QueryParameters/DeleteTasksQuery.swift b/Sources/MeiliSearch/QueryParameters/DeleteTasksQuery.swift index 5b7ce8bd..b720bc06 100644 --- a/Sources/MeiliSearch/QueryParameters/DeleteTasksQuery.swift +++ b/Sources/MeiliSearch/QueryParameters/DeleteTasksQuery.swift @@ -5,9 +5,9 @@ import Foundation */ public class DeleteTasksQuery: Queryable { /// List of strings with all the types the response should contain. - public let types: [String] + public let types: [TaskType] /// List of strings with all the statuses the response should contain. - public let statuses: [String] + public let statuses: [Task.Status] /// Filter tasks response by a particular list of index Uids strings public let indexUids: [String] /// Filter tasks based on a list of task's uids. @@ -28,7 +28,7 @@ public class DeleteTasksQuery: Queryable { public let afterFinishedAt: Date? init( - statuses: [String]? = nil, types: [String]? = nil, + statuses: [Task.Status]? = nil, types: [TaskType]? = nil, indexUids: [String]? = nil, uids: [Int]? = nil, canceledBy: [Int]? = nil, beforeEnqueuedAt: Date? = nil, afterEnqueuedAt: Date? = nil, beforeStartedAt: Date? = nil, afterStartedAt: Date? = nil, @@ -50,8 +50,8 @@ public class DeleteTasksQuery: Queryable { internal func buildQuery() -> [String: Codable?] { [ "uids": uids.isEmpty ? nil : uids.map(String.init).joined(separator: ","), - "types": types.isEmpty ? nil : types.joined(separator: ","), - "statuses": statuses.isEmpty ? nil : statuses.joined(separator: ","), + "types": types.isEmpty ? nil : types.map({ $0.description }).joined(separator: ","), + "statuses": statuses.isEmpty ? nil : statuses.map({ $0.rawValue }).joined(separator: ","), "indexUids": indexUids.isEmpty ? nil : indexUids.joined(separator: ","), "canceledBy": canceledBy.isEmpty ? nil : canceledBy.map(String.init).joined(separator: ","), "beforeEnqueuedAt": Formatter.formatOptionalDate(date: beforeEnqueuedAt), diff --git a/Sources/MeiliSearch/QueryParameters/TasksQuery.swift b/Sources/MeiliSearch/QueryParameters/TasksQuery.swift index 7e5980de..d8eda862 100644 --- a/Sources/MeiliSearch/QueryParameters/TasksQuery.swift +++ b/Sources/MeiliSearch/QueryParameters/TasksQuery.swift @@ -13,9 +13,9 @@ public class TasksQuery: Queryable { /// Integer value used to retrieve the next batch of tasks. private var next: Int? /// List of strings with all the types the response should contain. - private var types: [String] + private var types: [TaskType] /// List of strings with all the statuses the response should contain. - private var statuses: [String] + private var statuses: [Task.Status] /// Filter tasks response by a particular list of index Uids strings var indexUids: [String] /// Filter tasks based on a list of task's uids. @@ -37,7 +37,7 @@ public class TasksQuery: Queryable { init( limit: Int? = nil, from: Int? = nil, next: Int? = nil, - statuses: [String]? = nil, types: [String]? = nil, + statuses: [Task.Status]? = nil, types: [TaskType]? = nil, indexUids: [String]? = nil, uids: [Int]? = nil, canceledBy: [Int]? = nil, beforeEnqueuedAt: Date? = nil, afterEnqueuedAt: Date? = nil, afterFinishedAt: Date? = nil, beforeStartedAt: Date? = nil, @@ -65,8 +65,8 @@ public class TasksQuery: Queryable { "from": from, "next": next, "uids": uids.isEmpty ? nil : uids.map(String.init).joined(separator: ","), - "types": types.isEmpty ? nil : types.joined(separator: ","), - "statuses": statuses.isEmpty ? nil : statuses.joined(separator: ","), + "types": types.isEmpty ? nil : types.map({ $0.description }).joined(separator: ","), + "statuses": statuses.isEmpty ? nil : statuses.map({ $0.rawValue }).joined(separator: ","), "indexUids": indexUids.isEmpty ? nil : indexUids.joined(separator: ","), "canceledBy": canceledBy.isEmpty ? nil : canceledBy.map(String.init).joined(separator: ","), "beforeEnqueuedAt": Formatter.formatOptionalDate(date: beforeEnqueuedAt), diff --git a/Tests/MeiliSearchUnitTests/IndexesTests.swift b/Tests/MeiliSearchUnitTests/IndexesTests.swift index 908f296d..a2ea1133 100755 --- a/Tests/MeiliSearchUnitTests/IndexesTests.swift +++ b/Tests/MeiliSearchUnitTests/IndexesTests.swift @@ -250,7 +250,7 @@ class IndexesTests: XCTestCase { // Start the test with the mocked server let expectation = XCTestExpectation(description: "Get keys with parameters") - self.index.getTasks(params: TasksQuery(limit: 20, from: 5, next: 98, types: ["indexCreation"])) { result in + self.index.getTasks(params: TasksQuery(limit: 20, from: 5, next: 98, types: [.indexCreation])) { result in switch result { case .success: let requestQuery = self.session.nextDataTask.request?.url?.query diff --git a/Tests/MeiliSearchUnitTests/QueryParameters/TasksQueryTests.swift b/Tests/MeiliSearchUnitTests/QueryParameters/TasksQueryTests.swift index 78a5ca4c..b2fe61d1 100644 --- a/Tests/MeiliSearchUnitTests/QueryParameters/TasksQueryTests.swift +++ b/Tests/MeiliSearchUnitTests/QueryParameters/TasksQueryTests.swift @@ -8,7 +8,7 @@ class TasksQueryTests: XCTestCase { func testRenderedQuery() { let data: [[String: TasksQuery]] = [ ["?limit=2": TasksQuery(limit: 2)], - ["?from=99&limit=2&types=name,title": TasksQuery(limit: 2, from: 99, types: ["name", "title"])], + ["?from=99&limit=2&types=indexSwap,dumpCreation": TasksQuery(limit: 2, from: 99, types: [.indexSwap, .dumpCreation])], ["?limit=2": TasksQuery(limit: 2, next: nil)], ["?from=2": TasksQuery(from: 2)], ["?indexUids=my-index,123": TasksQuery(indexUids: ["my-index", "123"])], diff --git a/Tests/MeiliSearchUnitTests/TasksTests.swift b/Tests/MeiliSearchUnitTests/TasksTests.swift index 27b741a1..a27a5f69 100644 --- a/Tests/MeiliSearchUnitTests/TasksTests.swift +++ b/Tests/MeiliSearchUnitTests/TasksTests.swift @@ -30,7 +30,7 @@ class TasksTests: XCTestCase { // Start the test with the mocked server let expectation = XCTestExpectation(description: "Get keys with parameters") - self.client.getTasks(params: TasksQuery(limit: 20, from: 5, next: 98, types: ["indexCreation"])) { result in + self.client.getTasks(params: TasksQuery(limit: 20, from: 5, next: 98, types: [.indexCreation])) { result in switch result { case .success: let requestQuery = self.session.nextDataTask.request?.url?.query