Skip to content

Commit c1ce030

Browse files
committed
Add support to Encodable types, fix ClientTests
1 parent 9a2bf4c commit c1ce030

File tree

12 files changed

+143
-19
lines changed

12 files changed

+143
-19
lines changed

Sources/MeiliSearch/Client.swift

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,36 @@ public struct MeiliSearch {
135135
For a partial update of the document see `updateDocument`.
136136

137137
- parameter UID: The unique identifier for the Document's index to be found.
138-
- parameter documents: The documents data (JSON) to be processed.
138+
- parameter documents: The documents to be processed.
139+
- parameter completion: The completion closure used to notify when the server
140+
completes the update request, it returns a `Result` object that contains `Update`
141+
value. If the request was sucessful or `Error` if a failure occured.
142+
*/
143+
public func addDocuments<T>(
144+
UID: String,
145+
documents: [T],
146+
encoder: JSONEncoder? = nil,
147+
primaryKey: String?,
148+
_ completion: @escaping (Result<Update, Swift.Error>) -> Void) where T: Encodable {
149+
self.documents.add(
150+
UID,
151+
documents,
152+
encoder,
153+
primaryKey,
154+
completion)
155+
}
156+
157+
/**
158+
Add a list of documents as data or replace them if they already exist.
159+
160+
If you send an already existing document (same id) the whole existing document will
161+
be overwritten by the new document. Fields previously in the document not present in
162+
the new document are removed.
163+
164+
For a partial update of the document see `updateDocument`.
165+
166+
- parameter UID: The unique identifier for the Document's index to be found.
167+
- parameter documents: The data to be processed.
139168
- parameter completion: The completion closure used to notify when the server
140169
completes the update request, it returns a `Result` object that contains `Update`
141170
value. If the request was sucessful or `Error` if a failure occured.

Sources/MeiliSearch/Constants.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,13 @@ struct Constants {
55
static let customJSONDecoder: JSONDecoder = {
66
let decoder = JSONDecoder()
77
decoder.dateDecodingStrategy = .formatted(Formatter.iso8601)
8-
98
return decoder
109
}()
10+
11+
static let customJSONEecoder: JSONEncoder = {
12+
let encoder = JSONEncoder()
13+
encoder.dateEncodingStrategy = .formatted(Formatter.iso8601)
14+
return encoder
15+
}()
16+
1117
}

Sources/MeiliSearch/Documents.swift

Lines changed: 51 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ struct Documents {
7575
_ document: Data,
7676
_ primaryKey: String?,
7777
_ completion: @escaping (Result<Update, Swift.Error>) -> Void) {
78+
7879
var query: String = "/indexes/\(UID)/documents"
7980
if let primaryKey: String = primaryKey {
8081
query += "?primaryKey=\(primaryKey)"
@@ -94,6 +95,42 @@ struct Documents {
9495
}
9596
}
9697

98+
func add<T>(
99+
_ UID: String,
100+
_ documents: [T],
101+
_ encoder: JSONEncoder? = nil,
102+
_ primaryKey: String?,
103+
_ completion: @escaping (Result<Update, Swift.Error>) -> Void) where T: Encodable {
104+
105+
var query: String = "/indexes/\(UID)/documents"
106+
if let primaryKey: String = primaryKey {
107+
query += "?primaryKey=\(primaryKey)"
108+
}
109+
110+
let data: Data!
111+
112+
switch encodeJSON(documents, encoder) {
113+
case .success(let documentData):
114+
data = documentData
115+
case .failure(let error):
116+
completion(.failure(error))
117+
return
118+
}
119+
120+
request.post(api: query, data) { result in
121+
122+
switch result {
123+
case .success(let data):
124+
125+
Documents.decodeJSON(data, completion: completion)
126+
127+
case .failure(let error):
128+
completion(.failure(error))
129+
}
130+
131+
}
132+
}
133+
97134
func update(
98135
_ UID: String,
99136
_ document: Data,
@@ -209,19 +246,24 @@ struct Documents {
209246
_ customDecoder: JSONDecoder? = nil,
210247
completion: (Result<T, Swift.Error>) -> Void) {
211248
do {
212-
213-
let decoder: JSONDecoder
214-
if let customDecoder: JSONDecoder = customDecoder {
215-
decoder = customDecoder
216-
} else {
217-
decoder = Constants.customJSONDecoder
218-
}
219-
220-
let value: T = try decoder.decode(T.self, from: data)
249+
let value: T = try (customDecoder ?? Constants.customJSONDecoder)
250+
.decode(T.self, from: data)
221251
completion(.success(value))
222252
} catch {
223253
completion(.failure(error))
224254
}
225255
}
226256

257+
private func encodeJSON<T: Encodable>(
258+
_ documents: [T],
259+
_ encoder: JSONEncoder?) -> Result<Data, Swift.Error> {
260+
do {
261+
let data: Data = try (encoder ?? Constants.customJSONEecoder)
262+
.encode(documents)
263+
return .success(data)
264+
} catch {
265+
return .failure(error)
266+
}
267+
}
268+
227269
}

Sources/MeiliSearch/Stats.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ struct Stats {
5656

5757
do {
5858
let allStats = try Constants.customJSONDecoder.decode(AllStats.self, from: data)
59-
59+
6060
completion(.success(allStats))
6161
} catch {
6262
completion(.failure(error))

Sources/MeiliSearch/System.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ struct System {
5858

5959
do {
6060
let vesion = try Constants.customJSONDecoder.decode(Version.self, from: data)
61-
61+
6262
completion(.success(vesion))
6363
} catch {
6464
completion(.failure(error))

Sources/MeiliSearch/Updates.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ struct Updates {
6262

6363
do {
6464
let result = try Constants.customJSONDecoder.decode([Update.Result].self, from: data)
65-
65+
6666
completion(.success(result))
6767
} catch {
6868
completion(.failure(error))

Tests/MeiliSearchIntegrationTests/DocumentsTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class DocumentsTests: XCTestCase {
5454
self.client.deleteIndex(UID: uid) { _ in
5555
self.client.getOrCreateIndex(UID: uid) { result in
5656
switch result {
57-
case .success(_):
57+
case .success:
5858
break
5959
case .failure(let error):
6060
print(error)

Tests/MeiliSearchIntegrationTests/IndexesTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class IndexesTests: XCTestCase {
1616
pool(client)
1717

1818
let expectation = XCTestExpectation(description: "Try to delete index between tests")
19-
self.client.deleteIndex(UID: self.uid) { result in
19+
self.client.deleteIndex(UID: self.uid) { _ in
2020
expectation.fulfill()
2121
}
2222
self.wait(for: [expectation], timeout: 1.0)

Tests/MeiliSearchIntegrationTests/Pooling.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import Foundation
99
@testable import MeiliSearch
1010

11-
1211
public func pool(_ client: MeiliSearch) {
1312

1413
let semaphore = DispatchSemaphore(value: 1)

Tests/MeiliSearchIntegrationTests/XCTestManifests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import XCTest
1313
#if !os(macOS)
1414
public func allTests() -> [XCTestCaseEntry] {
1515
[
16-
testCase(DocumentsTests.allTests),
16+
testCase(DocumentsTests.allTests)
1717
]
1818
}
1919
#endif

0 commit comments

Comments
 (0)