Skip to content

Commit e4cb549

Browse files
meili-bors[bot]MiaKoringSherlouk
authored
Merge #455
455: Add KeyActions enum r=curquiza a=MiaKoring # Pull Request ## Related issue Fixes #418 ## What does this PR do? - add KeyAction enum with all supported cases and .unknown(String) - extends KeyAction with an initializer with String - extends KeyAction with a String representation of the enum value - adds initializer with [KeyAction] instead of String for actions to KeyParams - extends Key&KeyParams with value “enumActions” containing the actions as KeyAction representation - ## PR checklist Please check if your PR fulfills the following requirements: - [x] Does this PR fix an existing issue, or have you listed the changes applied in the PR description (and why they are needed)? - [X] Have you read the contributing guidelines? - [X] Have you made sure that the title is accurate and descriptive of the changes? Co-authored-by: MiaKoring <[email protected]> Co-authored-by: James Sherlock <[email protected]>
2 parents 1e82898 + e77d19b commit e4cb549

26 files changed

+370
-99
lines changed

Sources/MeiliSearch/Async/Client+async.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ extension MeiliSearch {
7171
/**
7272
See `waitForTask(taskUid:options:_:)`
7373
*/
74-
public func waitForTask(taskUid: Int, options: WaitOptions? = nil) async throws -> Task {
74+
public func waitForTask(taskUid: Int, options: WaitOptions? = nil) async throws -> MTask {
7575
try await withCheckedThrowingContinuation { continuation in
7676
self.waitForTask(taskUid: taskUid, options: options) { result in
7777
continuation.resume(with: result)
@@ -82,7 +82,7 @@ extension MeiliSearch {
8282
/**
8383
See `waitForTask(task:options:_:)`
8484
*/
85-
public func waitForTask(task: TaskInfo, options: WaitOptions? = nil) async throws -> Task {
85+
public func waitForTask(task: TaskInfo, options: WaitOptions? = nil) async throws -> MTask {
8686
try await withCheckedThrowingContinuation { continuation in
8787
self.waitForTask(task: task, options: options) { result in
8888
continuation.resume(with: result)
@@ -93,7 +93,7 @@ extension MeiliSearch {
9393
/**
9494
See `getTask(taskUid:_:)`
9595
*/
96-
public func getTask(taskUid: Int) async throws -> Task {
96+
public func getTask(taskUid: Int) async throws -> MTask {
9797
try await withCheckedThrowingContinuation { continuation in
9898
self.getTask(taskUid: taskUid) { result in
9999
continuation.resume(with: result)

Sources/MeiliSearch/Async/Indexes+async.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ extension Indexes {
159159
/**
160160
See `getTask(taskUid:_:)`
161161
*/
162-
public func getTask(taskUid: Int) async throws -> Task {
162+
public func getTask(taskUid: Int) async throws -> MTask {
163163
try await withCheckedThrowingContinuation { continuation in
164164
self.getTask(taskUid: taskUid) { result in
165165
continuation.resume(with: result)

Sources/MeiliSearch/Client.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ public struct MeiliSearch {
153153
public func waitForTask(
154154
taskUid: Int,
155155
options: WaitOptions? = nil,
156-
_ completion: @escaping (Result<Task, Swift.Error>
156+
_ completion: @escaping (Result<MTask, Swift.Error>
157157
) -> Void) {
158158
self.tasks.waitForTask(taskUid: taskUid, options: options, completion)
159159
}
@@ -171,7 +171,7 @@ public struct MeiliSearch {
171171
public func waitForTask(
172172
task: TaskInfo,
173173
options: WaitOptions? = nil,
174-
_ completion: @escaping (Result<Task, Swift.Error>
174+
_ completion: @escaping (Result<MTask, Swift.Error>
175175
) -> Void) {
176176
self.tasks.waitForTask(taskUid: task.taskUid, options: options, completion)
177177
}
@@ -188,7 +188,7 @@ public struct MeiliSearch {
188188
*/
189189
public func getTask(
190190
taskUid: Int,
191-
_ completion: @escaping (Result<Task, Swift.Error>) -> Void) {
191+
_ completion: @escaping (Result<MTask, Swift.Error>) -> Void) {
192192
self.tasks.get(taskUid: taskUid, completion)
193193
}
194194

Sources/MeiliSearch/Indexes.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -515,7 +515,7 @@ public struct Indexes {
515515
*/
516516
public func getTask(
517517
taskUid: Int,
518-
_ completion: @escaping (Result<Task, Swift.Error>) -> Void) {
518+
_ completion: @escaping (Result<MTask, Swift.Error>) -> Void) {
519519
self.tasks.get(taskUid: taskUid, completion)
520520
}
521521

Sources/MeiliSearch/Model/Key.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public struct Key: Codable, Equatable {
1919
/// An alphanumeric key value generated by Meilisearch by hashing the uid and the master key on API key creation.
2020
public let key: String
2121
/// An array of API actions permitted for the key.
22-
public let actions: [String]
22+
public let actions: [KeyAction]
2323
/// An array of strings (indexes names) the key is authorized to act on.
2424
public let indexes: [String]
2525
/// Date and time when the key will expire, represented in RFC 3339 format.
@@ -29,3 +29,4 @@ public struct Key: Codable, Equatable {
2929
/// Date and time when the key was last updated, represented in RFC 3339 format.
3030
public let updatedAt: String
3131
}
32+
Lines changed: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
1+
///Documentation: https://www.meilisearch.com/docs/reference/api/keys#actions
2+
public enum KeyAction: Equatable, Codable {
3+
case wildcard
4+
case search
5+
case documentsWildcard
6+
case documentsAdd
7+
case documentsGet
8+
case documentsDelete
9+
case indexesWildcard
10+
case indexesCreate
11+
case indexesGet
12+
case indexesUpdate
13+
case indexesDelete
14+
case indexesSwap
15+
case tasksWildcard
16+
case tasksGet
17+
case tasksCancel
18+
case tasksDelete
19+
case settingsWildcard
20+
case settingsGet
21+
case settingsUpdate
22+
case statsGet
23+
case dumpsCreate
24+
case snapshotsCreate
25+
case version
26+
case keysGet
27+
case keysCreate
28+
case keysUpdate
29+
case keysDelete
30+
case unknown(String)
31+
32+
public init(from decoder: Decoder) throws {
33+
let container = try decoder.singleValueContainer()
34+
let rawValue = try container.decode(String.self)
35+
36+
self = KeyAction(rawValue)
37+
}
38+
39+
public func encode(to encoder: Encoder) throws {
40+
var container = encoder.singleValueContainer()
41+
42+
try container.encode(self.value)
43+
}
44+
}
45+
46+
public extension KeyAction {
47+
init(_ value: String) {
48+
self = switch value {
49+
case "*":
50+
.wildcard
51+
case "search":
52+
.search
53+
case "documents.*":
54+
.documentsWildcard
55+
case "documents.add":
56+
.documentsAdd
57+
case "documents.get":
58+
.documentsGet
59+
case "documents.delete":
60+
.documentsDelete
61+
case "indexes.*":
62+
.indexesWildcard
63+
case "indexes.create":
64+
.indexesCreate
65+
case "indexes.get":
66+
.indexesGet
67+
case "indexes.update":
68+
.indexesUpdate
69+
case "indexes.delete":
70+
.indexesDelete
71+
case "indexes.swap":
72+
.indexesSwap
73+
case "tasks.*":
74+
.tasksWildcard
75+
case "tasks.get":
76+
.tasksGet
77+
case "tasks.cancel":
78+
.tasksCancel
79+
case "tasks.delete":
80+
.tasksDelete
81+
case "settings.*":
82+
.settingsWildcard
83+
case "settings.get":
84+
.settingsGet
85+
case "settings.update":
86+
.settingsUpdate
87+
case "stats.get":
88+
.statsGet
89+
case "dumps.create":
90+
.dumpsCreate
91+
case "snapshots.create":
92+
.snapshotsCreate
93+
case "version":
94+
.version
95+
case "keys.get":
96+
.keysGet
97+
case "keys.create":
98+
.keysCreate
99+
case "keys.update":
100+
.keysUpdate
101+
case "keys.delete":
102+
.keysDelete
103+
default:
104+
.unknown(value)
105+
}
106+
}
107+
108+
var value: String {
109+
switch self {
110+
case .wildcard:
111+
"*"
112+
case .search:
113+
"search"
114+
case .documentsWildcard:
115+
"documents.*"
116+
case .documentsAdd:
117+
"documents.add"
118+
case .documentsGet:
119+
"documents.get"
120+
case .documentsDelete:
121+
"documents.delete"
122+
case .indexesWildcard:
123+
"indexes.*"
124+
case .indexesCreate:
125+
"indexes.create"
126+
case .indexesGet:
127+
"indexes.get"
128+
case .indexesUpdate:
129+
"indexes.update"
130+
case .indexesDelete:
131+
"indexes.delete"
132+
case .indexesSwap:
133+
"indexes.swap"
134+
case .tasksWildcard:
135+
"tasks.*"
136+
case .tasksGet:
137+
"tasks.get"
138+
case .tasksCancel:
139+
"tasks.cancel"
140+
case .tasksDelete:
141+
"tasks.delete"
142+
case .settingsWildcard:
143+
"settings.*"
144+
case .settingsGet:
145+
"settings.get"
146+
case .settingsUpdate:
147+
"settings.update"
148+
case .statsGet:
149+
"stats.get"
150+
case .dumpsCreate:
151+
"dumps.create"
152+
case .snapshotsCreate:
153+
"snapshots.create"
154+
case .version:
155+
"version"
156+
case .keysGet:
157+
"keys.get"
158+
case .keysCreate:
159+
"keys.create"
160+
case .keysUpdate:
161+
"keys.update"
162+
case .keysDelete:
163+
"keys.delete"
164+
case .unknown(let string):
165+
string
166+
}
167+
}
168+
}
169+
170+
extension KeyAction: CaseIterable {
171+
public static var allCases: [KeyAction] {
172+
[
173+
.wildcard,
174+
.search,
175+
.documentsWildcard,
176+
.documentsAdd,
177+
.documentsGet,
178+
.documentsDelete,
179+
.indexesWildcard,
180+
.indexesCreate,
181+
.indexesGet,
182+
.indexesUpdate,
183+
.indexesDelete,
184+
.indexesSwap,
185+
.tasksWildcard,
186+
.tasksGet,
187+
.tasksCancel,
188+
.tasksDelete,
189+
.settingsWildcard,
190+
.settingsGet,
191+
.settingsUpdate,
192+
.statsGet,
193+
.dumpsCreate,
194+
.snapshotsCreate,
195+
.version,
196+
.keysGet,
197+
.keysCreate,
198+
.keysUpdate,
199+
.keysDelete,
200+
]
201+
}
202+
}

Sources/MeiliSearch/Model/KeyParams.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public struct KeyParams: Codable, Equatable {
1414
/// A uuid v4 to identify the API key. If not specified, it is automatically generated by Meilisearch.
1515
public var uid: String?
1616
/// An array of API actions permitted for the key.
17-
public let actions: [String]
17+
public let actions: [KeyAction]
1818
/// An array of strings (indexes names) the key is authorized to act on.
1919
public let indexes: [String]
2020
/// Date and time when the key will expire, represented in RFC 3339 format.
@@ -24,7 +24,7 @@ public struct KeyParams: Codable, Equatable {
2424
description: String? = nil,
2525
name: String? = nil,
2626
uid: String? = nil,
27-
actions: [String],
27+
actions: [KeyAction],
2828
indexes: [String],
2929
expiresAt: String?
3030
) {

Sources/MeiliSearch/Model/Task/Task.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import Foundation
44
`Task` instances represent the current transaction status, use the `uid` value to
55
verify the status of your transaction.
66
*/
7-
public struct Task: Decodable, Equatable {
7+
public struct MTask: Decodable, Equatable {
88
/// Unique ID for the current `Task`.
99
public let uid: Int
1010

Sources/MeiliSearch/Model/Task/TaskDetails.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import Foundation
22

3-
public extension Task {
3+
public extension MTask {
44
enum Details: Equatable {
55
case indexCreation(TaskIndexCreationDetails)
66
case indexUpdate(TaskIndexUpdateDetails)

Sources/MeiliSearch/Model/Task/TaskInfo.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public struct TaskInfo: Codable, Equatable {
1212
public let indexUid: String?
1313

1414
/// Returns if the task has been successful or not.
15-
public let status: Task.Status
15+
public let status: MTask.Status
1616

1717
/// Type of the task.
1818
public let type: TaskType
@@ -26,7 +26,7 @@ public struct TaskInfo: Codable, Equatable {
2626

2727
@discardableResult
2828
@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
29-
public func wait(on client: MeiliSearch, options: WaitOptions? = nil) async throws -> Task {
29+
public func wait(on client: MeiliSearch, options: WaitOptions? = nil) async throws -> MTask {
3030
try await client.waitForTask(task: self, options: options)
3131
}
3232
}

0 commit comments

Comments
 (0)