@@ -18,7 +18,7 @@ import Foundation
18
18
*/
19
19
public struct ParseOperation < T> : Savable where T: ParseObject {
20
20
21
- var target : T ?
21
+ var target : T
22
22
var operations = [ String: Encodable] ( )
23
23
24
24
public init ( target: T ) {
@@ -34,13 +34,10 @@ public struct ParseOperation<T>: Savable where T: ParseObject {
34
34
*/
35
35
public func set< W> ( _ key: ( String , WritableKeyPath < T , W > ) ,
36
36
value: W ) throws -> Self where W: Encodable {
37
- guard let target = self . target else {
38
- throw ParseError ( code: . unknownError, message: " Target shouldn't be nil " )
39
- }
40
37
var mutableOperation = self
41
38
if !target[ keyPath: key. 1 ] . isEqual ( value) {
42
39
mutableOperation. operations [ key. 0 ] = value
43
- mutableOperation. target ? [ keyPath: key. 1 ] = value
40
+ mutableOperation. target [ keyPath: key. 1 ] = value
44
41
}
45
42
return mutableOperation
46
43
}
@@ -54,12 +51,9 @@ public struct ParseOperation<T>: Savable where T: ParseObject {
54
51
*/
55
52
public func forceSet< W> ( _ key: ( String , WritableKeyPath < T , W > ) ,
56
53
value: W ) throws -> Self where W: Encodable {
57
- guard self . target != nil else {
58
- throw ParseError ( code: . unknownError, message: " Target shouldn't be nil " )
59
- }
60
54
var mutableOperation = self
61
55
mutableOperation. operations [ key. 0 ] = value
62
- mutableOperation. target ? [ keyPath: key. 1 ] = value
56
+ mutableOperation. target [ keyPath: key. 1 ] = value
63
57
return mutableOperation
64
58
}
65
59
@@ -100,14 +94,11 @@ public struct ParseOperation<T>: Savable where T: ParseObject {
100
94
*/
101
95
public func addUnique< V> ( _ key: ( String , WritableKeyPath < T , [ V ] > ) ,
102
96
objects: [ V ] ) throws -> Self where V: Encodable , V: Hashable {
103
- guard let target = self . target else {
104
- throw ParseError ( code: . unknownError, message: " Target shouldn't be nil " )
105
- }
106
97
var mutableOperation = self
107
98
mutableOperation. operations [ key. 0 ] = AddUnique ( objects: objects)
108
99
var values = target [ keyPath: key. 1 ]
109
100
values. append ( contentsOf: objects)
110
- mutableOperation. target ? [ keyPath: key. 1 ] = Array ( Set < V > ( values) )
101
+ mutableOperation. target [ keyPath: key. 1 ] = Array ( Set < V > ( values) )
111
102
return mutableOperation
112
103
}
113
104
@@ -121,14 +112,11 @@ public struct ParseOperation<T>: Savable where T: ParseObject {
121
112
*/
122
113
public func addUnique< V> ( _ key: ( String , WritableKeyPath < T , [ V ] ? > ) ,
123
114
objects: [ V ] ) throws -> Self where V: Encodable , V: Hashable {
124
- guard let target = self . target else {
125
- throw ParseError ( code: . unknownError, message: " Target shouldn't be nil " )
126
- }
127
115
var mutableOperation = self
128
116
mutableOperation. operations [ key. 0 ] = AddUnique ( objects: objects)
129
117
var values = target [ keyPath: key. 1 ] ?? [ ]
130
118
values. append ( contentsOf: objects)
131
- mutableOperation. target ? [ keyPath: key. 1 ] = Array ( Set < V > ( values) )
119
+ mutableOperation. target [ keyPath: key. 1 ] = Array ( Set < V > ( values) )
132
120
return mutableOperation
133
121
}
134
122
@@ -154,14 +142,11 @@ public struct ParseOperation<T>: Savable where T: ParseObject {
154
142
*/
155
143
public func add< V> ( _ key: ( String , WritableKeyPath < T , [ V ] > ) ,
156
144
objects: [ V ] ) throws -> Self where V: Encodable {
157
- guard let target = self . target else {
158
- throw ParseError ( code: . unknownError, message: " Target shouldn't be nil " )
159
- }
160
145
var mutableOperation = self
161
146
mutableOperation. operations [ key. 0 ] = Add ( objects: objects)
162
147
var values = target [ keyPath: key. 1 ]
163
148
values. append ( contentsOf: objects)
164
- mutableOperation. target ? [ keyPath: key. 1 ] = values
149
+ mutableOperation. target [ keyPath: key. 1 ] = values
165
150
return mutableOperation
166
151
}
167
152
@@ -174,14 +159,11 @@ public struct ParseOperation<T>: Savable where T: ParseObject {
174
159
*/
175
160
public func add< V> ( _ key: ( String , WritableKeyPath < T , [ V ] ? > ) ,
176
161
objects: [ V ] ) throws -> Self where V: Encodable {
177
- guard let target = self . target else {
178
- throw ParseError ( code: . unknownError, message: " Target shouldn't be nil " )
179
- }
180
162
var mutableOperation = self
181
163
mutableOperation. operations [ key. 0 ] = Add ( objects: objects)
182
164
var values = target [ keyPath: key. 1 ] ?? [ ]
183
165
values. append ( contentsOf: objects)
184
- mutableOperation. target ? [ keyPath: key. 1 ] = values
166
+ mutableOperation. target [ keyPath: key. 1 ] = values
185
167
return mutableOperation
186
168
}
187
169
@@ -207,14 +189,11 @@ public struct ParseOperation<T>: Savable where T: ParseObject {
207
189
*/
208
190
public func addRelation< V> ( _ key: ( String , WritableKeyPath < T , [ V ] > ) ,
209
191
objects: [ V ] ) throws -> Self where V: ParseObject {
210
- guard let target = self . target else {
211
- throw ParseError ( code: . unknownError, message: " Target shouldn't be nil " )
212
- }
213
192
var mutableOperation = self
214
193
mutableOperation. operations [ key. 0 ] = try AddRelation ( objects: objects)
215
194
var values = target [ keyPath: key. 1 ]
216
195
values. append ( contentsOf: objects)
217
- mutableOperation. target ? [ keyPath: key. 1 ] = values
196
+ mutableOperation. target [ keyPath: key. 1 ] = values
218
197
return mutableOperation
219
198
}
220
199
@@ -227,14 +206,11 @@ public struct ParseOperation<T>: Savable where T: ParseObject {
227
206
*/
228
207
public func addRelation< V> ( _ key: ( String , WritableKeyPath < T , [ V ] ? > ) ,
229
208
objects: [ V ] ) throws -> Self where V: ParseObject {
230
- guard let target = self . target else {
231
- throw ParseError ( code: . unknownError, message: " Target shouldn't be nil " )
232
- }
233
209
var mutableOperation = self
234
210
mutableOperation. operations [ key. 0 ] = try AddRelation ( objects: objects)
235
211
var values = target [ keyPath: key. 1 ] ?? [ ]
236
212
values. append ( contentsOf: objects)
237
- mutableOperation. target ? [ keyPath: key. 1 ] = values
213
+ mutableOperation. target [ keyPath: key. 1 ] = values
238
214
return mutableOperation
239
215
}
240
216
@@ -262,17 +238,14 @@ public struct ParseOperation<T>: Savable where T: ParseObject {
262
238
*/
263
239
public func remove< V> ( _ key: ( String , WritableKeyPath < T , [ V ] > ) ,
264
240
objects: [ V ] ) throws -> Self where V: Encodable , V: Hashable {
265
- guard let target = self . target else {
266
- throw ParseError ( code: . unknownError, message: " Target shouldn't be nil " )
267
- }
268
241
var mutableOperation = self
269
242
mutableOperation. operations [ key. 0 ] = Remove ( objects: objects)
270
243
let values = target [ keyPath: key. 1 ]
271
244
var set = Set < V > ( values)
272
245
objects. forEach {
273
246
set. remove ( $0)
274
247
}
275
- mutableOperation. target ? [ keyPath: key. 1 ] = Array ( set)
248
+ mutableOperation. target [ keyPath: key. 1 ] = Array ( set)
276
249
return mutableOperation
277
250
}
278
251
@@ -286,17 +259,14 @@ public struct ParseOperation<T>: Savable where T: ParseObject {
286
259
*/
287
260
public func remove< V> ( _ key: ( String , WritableKeyPath < T , [ V ] ? > ) ,
288
261
objects: [ V ] ) throws -> Self where V: Encodable , V: Hashable {
289
- guard let target = self . target else {
290
- throw ParseError ( code: . unknownError, message: " Target shouldn't be nil " )
291
- }
292
262
var mutableOperation = self
293
263
mutableOperation. operations [ key. 0 ] = Remove ( objects: objects)
294
264
let values = target [ keyPath: key. 1 ]
295
265
var set = Set < V > ( values ?? [ ] )
296
266
objects. forEach {
297
267
set. remove ( $0)
298
268
}
299
- mutableOperation. target ? [ keyPath: key. 1 ] = Array ( set)
269
+ mutableOperation. target [ keyPath: key. 1 ] = Array ( set)
300
270
return mutableOperation
301
271
}
302
272
@@ -309,9 +279,6 @@ public struct ParseOperation<T>: Savable where T: ParseObject {
309
279
- returns: The updated operations.
310
280
*/
311
281
public func removeRelation< W> ( _ key: String , objects: [ W ] ) throws -> Self where W: ParseObject {
312
- guard self . target != nil else {
313
- throw ParseError ( code: . unknownError, message: " Target shouldn't be nil " )
314
- }
315
282
var mutableOperation = self
316
283
mutableOperation. operations [ key] = try RemoveRelation ( objects: objects)
317
284
return mutableOperation
@@ -327,17 +294,14 @@ public struct ParseOperation<T>: Savable where T: ParseObject {
327
294
*/
328
295
public func removeRelation< V> ( _ key: ( String , WritableKeyPath < T , [ V ] > ) ,
329
296
objects: [ V ] ) throws -> Self where V: ParseObject {
330
- guard let target = self . target else {
331
- throw ParseError ( code: . unknownError, message: " Target shouldn't be nil " )
332
- }
333
297
var mutableOperation = self
334
298
mutableOperation. operations [ key. 0 ] = try RemoveRelation ( objects: objects)
335
299
let values = target [ keyPath: key. 1 ]
336
300
var set = Set < V > ( values)
337
301
objects. forEach {
338
302
set. remove ( $0)
339
303
}
340
- mutableOperation. target ? [ keyPath: key. 1 ] = Array ( set)
304
+ mutableOperation. target [ keyPath: key. 1 ] = Array ( set)
341
305
return mutableOperation
342
306
}
343
307
@@ -351,17 +315,14 @@ public struct ParseOperation<T>: Savable where T: ParseObject {
351
315
*/
352
316
public func removeRelation< V> ( _ key: ( String , WritableKeyPath < T , [ V ] ? > ) ,
353
317
objects: [ V ] ) throws -> Self where V: ParseObject {
354
- guard let target = self . target else {
355
- throw ParseError ( code: . unknownError, message: " Target shouldn't be nil " )
356
- }
357
318
var mutableOperation = self
358
319
mutableOperation. operations [ key. 0 ] = try RemoveRelation ( objects: objects)
359
320
let values = target [ keyPath: key. 1 ]
360
321
var set = Set < V > ( values ?? [ ] )
361
322
objects. forEach {
362
323
set. remove ( $0)
363
324
}
364
- mutableOperation. target ? [ keyPath: key. 1 ] = Array ( set)
325
+ mutableOperation. target [ keyPath: key. 1 ] = Array ( set)
365
326
return mutableOperation
366
327
}
367
328
@@ -385,7 +346,7 @@ public struct ParseOperation<T>: Savable where T: ParseObject {
385
346
public func unset< V> ( _ key: ( String , WritableKeyPath < T , V ? > ) ) -> Self where V: Encodable {
386
347
var mutableOperation = self
387
348
mutableOperation. operations [ key. 0 ] = Delete ( )
388
- mutableOperation. target ? [ keyPath: key. 1 ] = nil
349
+ mutableOperation. target [ keyPath: key. 1 ] = nil
389
350
return mutableOperation
390
351
}
391
352
@@ -410,9 +371,6 @@ extension ParseOperation {
410
371
- returns: Returns saved `ParseObject`.
411
372
*/
412
373
public func save( options: API . Options = [ ] ) throws -> T {
413
- guard let target = self . target else {
414
- throw ParseError ( code: . unknownError, message: " Target shouldn't be nil. " )
415
- }
416
374
if !target. isSaved {
417
375
throw ParseError ( code: . missingObjectId, message: " ParseObject isn't saved. " )
418
376
}
@@ -433,13 +391,6 @@ extension ParseOperation {
433
391
callbackQueue: DispatchQueue = . main,
434
392
completion: @escaping ( Result < T , ParseError > ) -> Void
435
393
) {
436
- guard let target = self . target else {
437
- callbackQueue. async {
438
- let error = ParseError ( code: . unknownError, message: " Target shouldn't be nil. " )
439
- completion ( . failure( error) )
440
- }
441
- return
442
- }
443
394
if !target. isSaved {
444
395
callbackQueue. async {
445
396
let error = ParseError ( code: . missingObjectId, message: " ParseObject isn't saved. " )
@@ -461,11 +412,8 @@ extension ParseOperation {
461
412
}
462
413
463
414
func saveCommand( ) throws -> API . NonParseBodyCommand < ParseOperation < T > , T > {
464
- guard let target = self . target else {
465
- throw ParseError ( code: . unknownError, message: " Target shouldn't be nil " )
466
- }
467
- return API . NonParseBodyCommand ( method: . PUT, path: target. endpoint, body: self ) {
468
- try ParseCoding . jsonDecoder ( ) . decode ( UpdateResponse . self, from: $0) . apply ( to: target)
415
+ API . NonParseBodyCommand ( method: . PUT, path: target. endpoint, body: self ) {
416
+ try ParseCoding . jsonDecoder ( ) . decode ( UpdateResponse . self, from: $0) . apply ( to: self . target)
469
417
}
470
418
}
471
419
}
0 commit comments