Skip to content

Commit 7aeef06

Browse files
dblythycbaker6
andauthored
feat: Add Set operation to allow for single key updates (#248)
* add set operation * add force set * Use keyPath instead of creating JSON dictionaries. * Update ParseOperationTests.swift * Update ParseOperationTests.swift * fix failing tests * add save set testcases * Update CHANGELOG.md * Update CHANGELOG.md * prepare for release * don't run testSet test on linux Co-authored-by: Corey Baker <[email protected]>
1 parent 7932ba7 commit 7aeef06

File tree

4 files changed

+219
-13
lines changed

4 files changed

+219
-13
lines changed

CHANGELOG.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
# Parse-Swift Changelog
22

33
### main
4-
[Full Changelog](https://github.com/parse-community/Parse-Swift/compare/1.10.4...main)
4+
[Full Changelog](https://github.com/parse-community/Parse-Swift/compare/1.11.0...main)
55
* _Contributing to this repo? Add info about your change here to be included in the next release_
66

7+
### 1.11.0
8+
[Full Changelog](https://github.com/parse-community/Parse-Swift/compare/1.10.4...1.11.0)
9+
10+
__Improvements__
11+
- Added `operation` for `set` and `forceSet`, used for single key updates ([#248](https://github.com/parse-community/Parse-Swift/pull/248)), thanks to [Daniel Blyth](https://github.com/dblythy) and [Corey Baker](https://github.com/cbaker6).
12+
713
### 1.10.4
814
[Full Changelog](https://github.com/parse-community/Parse-Swift/compare/1.10.3...1.10.4)
915

Sources/ParseSwift/Operations/ParseOperation.swift

+70-11
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,53 @@ public struct ParseOperation<T>: Savable where T: ParseObject {
2525
self.target = target
2626
}
2727

28+
/**
29+
An operation that sets a field's value if it has changed from its previous value.
30+
- Parameters:
31+
- key: A tuple consisting of the key and the respective KeyPath of the object.
32+
- value: The value to set it to.
33+
- returns: The updated operations.
34+
*/
35+
public func set<W>(_ key: (String, WritableKeyPath<T, W>),
36+
value: W) throws -> Self where W: Encodable {
37+
var mutableOperation = self
38+
guard let target = self.target else {
39+
throw ParseError(code: .unknownError, message: "Target shouldn't be nil")
40+
}
41+
if let currentValue = target[keyPath: key.1] as? NSObject,
42+
let updatedValue = value as? NSObject {
43+
if currentValue != updatedValue {
44+
mutableOperation.operations[key.0] = value
45+
mutableOperation.target?[keyPath: key.1] = value
46+
}
47+
} else {
48+
mutableOperation.operations[key.0] = value
49+
mutableOperation.target?[keyPath: key.1] = value
50+
}
51+
return mutableOperation
52+
}
53+
54+
/**
55+
An operation that force sets a field's value.
56+
- Parameters:
57+
- key: A tuple consisting of the key and the respective KeyPath of the object.
58+
- value: The value to set it to.
59+
- returns: The updated operations.
60+
*/
61+
public func forceSet<W>(_ key: (String, WritableKeyPath<T, W>),
62+
value: W) throws -> Self where W: Encodable {
63+
var mutableOperation = self
64+
mutableOperation.operations[key.0] = value
65+
mutableOperation.target?[keyPath: key.1] = value
66+
return mutableOperation
67+
}
68+
2869
/**
2970
An operation that increases a numeric field's value by a given amount.
3071
- Parameters:
3172
- key: The key of the object.
3273
- amount: How much to increment by.
74+
- returns: The updated operations.
3375
*/
3476
public func increment(_ key: String, by amount: Int) -> Self {
3577
var mutableOperation = self
@@ -43,6 +85,7 @@ public struct ParseOperation<T>: Savable where T: ParseObject {
4385
- Parameters:
4486
- key: The key of the object.
4587
- objects: The field of objects.
88+
- returns: The updated operations.
4689
*/
4790
public func addUnique<W>(_ key: String, objects: [W]) -> Self where W: Encodable, W: Hashable {
4891
var mutableOperation = self
@@ -54,8 +97,9 @@ public struct ParseOperation<T>: Savable where T: ParseObject {
5497
An operation that adds a new element to an array field,
5598
only if it wasn't already present.
5699
- Parameters:
57-
- key: A tuple consisting of the key and KeyPath of the object.
100+
- key: A tuple consisting of the key and the respective KeyPath of the object.
58101
- objects: The field of objects.
102+
- returns: The updated operations.
59103
*/
60104
public func addUnique<V>(_ key: (String, WritableKeyPath<T, [V]>),
61105
objects: [V]) throws -> Self where V: Encodable, V: Hashable {
@@ -74,8 +118,9 @@ public struct ParseOperation<T>: Savable where T: ParseObject {
74118
An operation that adds a new element to an array field,
75119
only if it wasn't already present.
76120
- Parameters:
77-
- key: A tuple consisting of the key and KeyPath of the object.
121+
- key: A tuple consisting of the key and the respective KeyPath of the object.
78122
- objects: The field of objects.
123+
- returns: The updated operations.
79124
*/
80125
public func addUnique<V>(_ key: (String, WritableKeyPath<T, [V]?>),
81126
objects: [V]) throws -> Self where V: Encodable, V: Hashable {
@@ -95,6 +140,7 @@ public struct ParseOperation<T>: Savable where T: ParseObject {
95140
- Parameters:
96141
- key: The key of the object.
97142
- objects: The field of objects.
143+
- returns: The updated operations.
98144
*/
99145
public func add<W>(_ key: String, objects: [W]) -> Self where W: Encodable {
100146
var mutableOperation = self
@@ -105,8 +151,9 @@ public struct ParseOperation<T>: Savable where T: ParseObject {
105151
/**
106152
An operation that adds a new element to an array field.
107153
- Parameters:
108-
- key: A tuple consisting of the key and KeyPath of the object.
154+
- key: A tuple consisting of the key and the respective KeyPath of the object.
109155
- objects: The field of objects.
156+
- returns: The updated operations.
110157
*/
111158
public func add<V>(_ key: (String, WritableKeyPath<T, [V]>),
112159
objects: [V]) throws -> Self where V: Encodable {
@@ -124,8 +171,9 @@ public struct ParseOperation<T>: Savable where T: ParseObject {
124171
/**
125172
An operation that adds a new element to an array field.
126173
- Parameters:
127-
- key: A tuple consisting of the key and KeyPath of the object.
174+
- key: A tuple consisting of the key and the respective KeyPath of the object.
128175
- objects: The field of objects.
176+
- returns: The updated operations.
129177
*/
130178
public func add<V>(_ key: (String, WritableKeyPath<T, [V]?>),
131179
objects: [V]) throws -> Self where V: Encodable {
@@ -145,6 +193,7 @@ public struct ParseOperation<T>: Savable where T: ParseObject {
145193
- Parameters:
146194
- key: The key of the object.
147195
- objects: The field of objects.
196+
- returns: The updated operations.
148197
*/
149198
public func addRelation<W>(_ key: String, objects: [W]) throws -> Self where W: ParseObject {
150199
var mutableOperation = self
@@ -155,8 +204,9 @@ public struct ParseOperation<T>: Savable where T: ParseObject {
155204
/**
156205
An operation that adds a new relation to an array field.
157206
- Parameters:
158-
- key: A tuple consisting of the key and KeyPath of the object.
207+
- key: A tuple consisting of the key and the respective KeyPath of the object.
159208
- objects: The field of objects.
209+
- returns: The updated operations.
160210
*/
161211
public func addRelation<V>(_ key: (String, WritableKeyPath<T, [V]>),
162212
objects: [V]) throws -> Self where V: ParseObject {
@@ -174,8 +224,9 @@ public struct ParseOperation<T>: Savable where T: ParseObject {
174224
/**
175225
An operation that adds a new relation to an array field.
176226
- Parameters:
177-
- key: A tuple consisting of the key and KeyPath of the object.
227+
- key: A tuple consisting of the key and the respective KeyPath of the object.
178228
- objects: The field of objects.
229+
- returns: The updated operations.
179230
*/
180231
public func addRelation<V>(_ key: (String, WritableKeyPath<T, [V]?>),
181232
objects: [V]) throws -> Self where V: ParseObject {
@@ -196,6 +247,7 @@ public struct ParseOperation<T>: Savable where T: ParseObject {
196247
- Parameters:
197248
- key: The key of the object.
198249
- objects: The field of objects.
250+
- returns: The updated operations.
199251
*/
200252
public func remove<W>(_ key: String, objects: [W]) -> Self where W: Encodable {
201253
var mutableOperation = self
@@ -207,8 +259,9 @@ public struct ParseOperation<T>: Savable where T: ParseObject {
207259
An operation that removes every instance of an element from
208260
an array field.
209261
- Parameters:
210-
- key: A tuple consisting of the key and KeyPath of the object.
262+
- key: A tuple consisting of the key and the respective KeyPath of the object.
211263
- objects: The field of objects.
264+
- returns: The updated operations.
212265
*/
213266
public func remove<V>(_ key: (String, WritableKeyPath<T, [V]>),
214267
objects: [V]) throws -> Self where V: Encodable, V: Hashable {
@@ -230,8 +283,9 @@ public struct ParseOperation<T>: Savable where T: ParseObject {
230283
An operation that removes every instance of an element from
231284
an array field.
232285
- Parameters:
233-
- key: A tuple consisting of the key and KeyPath of the object.
286+
- key: A tuple consisting of the key and the respective KeyPath of the object.
234287
- objects: The field of objects.
288+
- returns: The updated operations.
235289
*/
236290
public func remove<V>(_ key: (String, WritableKeyPath<T, [V]?>),
237291
objects: [V]) throws -> Self where V: Encodable, V: Hashable {
@@ -255,6 +309,7 @@ public struct ParseOperation<T>: Savable where T: ParseObject {
255309
- Parameters:
256310
- key: The key of the object.
257311
- objects: The field of objects.
312+
- returns: The updated operations.
258313
*/
259314
public func removeRelation<W>(_ key: String, objects: [W]) throws -> Self where W: ParseObject {
260315
var mutableOperation = self
@@ -266,8 +321,9 @@ public struct ParseOperation<T>: Savable where T: ParseObject {
266321
An operation that removes every instance of a relation from
267322
an array field.
268323
- Parameters:
269-
- key: A tuple consisting of the key and KeyPath of the object.
324+
- key: A tuple consisting of the key and the respective KeyPath of the object.
270325
- objects: The field of objects.
326+
- returns: The updated operations.
271327
*/
272328
public func removeRelation<V>(_ key: (String, WritableKeyPath<T, [V]>),
273329
objects: [V]) throws -> Self where V: ParseObject {
@@ -289,8 +345,9 @@ public struct ParseOperation<T>: Savable where T: ParseObject {
289345
An operation that removes every instance of a relation from
290346
an array field.
291347
- Parameters:
292-
- key: A tuple consisting of the key and KeyPath of the object.
348+
- key: A tuple consisting of the key and the respective KeyPath of the object.
293349
- objects: The field of objects.
350+
- returns: The updated operations.
294351
*/
295352
public func removeRelation<V>(_ key: (String, WritableKeyPath<T, [V]?>),
296353
objects: [V]) throws -> Self where V: ParseObject {
@@ -311,6 +368,7 @@ public struct ParseOperation<T>: Savable where T: ParseObject {
311368
/**
312369
An operation where a field is deleted from the object.
313370
- parameter key: The key of the object.
371+
- returns: The updated operations.
314372
*/
315373
public func unset(_ key: String) -> Self {
316374
var mutableOperation = self
@@ -321,7 +379,8 @@ public struct ParseOperation<T>: Savable where T: ParseObject {
321379
/**
322380
An operation where a field is deleted from the object.
323381
- Parameters:
324-
- key: A tuple consisting of the key and KeyPath of the object.
382+
- key: A tuple consisting of the key and the respective KeyPath of the object.
383+
- returns: The updated operations.
325384
*/
326385
public func unset<V>(_ key: (String, WritableKeyPath<T, V?>)) -> Self where V: Encodable {
327386
var mutableOperation = self

Sources/ParseSwift/ParseConstants.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import Foundation
1010

1111
enum ParseConstants {
1212
static let sdk = "swift"
13-
static let version = "1.10.4"
13+
static let version = "1.11.0"
1414
static let fileManagementDirectory = "parse/"
1515
static let fileManagementPrivateDocumentsDirectory = "Private Documents/"
1616
static let fileManagementLibraryDirectory = "Library/"

0 commit comments

Comments
 (0)