Skip to content

feat: Add Set operation to allow for single key updates #248

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Oct 9, 2021
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
# Parse-Swift Changelog

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

### 1.11.0
[Full Changelog](https://github.com/parse-community/Parse-Swift/compare/1.10.4...1.11.0)

__Improvements__
- 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).

### 1.10.4
[Full Changelog](https://github.com/parse-community/Parse-Swift/compare/1.10.3...1.10.4)

Expand Down
81 changes: 70 additions & 11 deletions Sources/ParseSwift/Operations/ParseOperation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,53 @@ public struct ParseOperation<T>: Savable where T: ParseObject {
self.target = target
}

/**
An operation that sets a field's value if it has changed from its previous value.
- Parameters:
- key: A tuple consisting of the key and the respective KeyPath of the object.
- value: The value to set it to.
- returns: The updated operations.
*/
public func set<W>(_ key: (String, WritableKeyPath<T, W>),
value: W) throws -> Self where W: Encodable {
var mutableOperation = self
guard let target = self.target else {
throw ParseError(code: .unknownError, message: "Target shouldn't be nil")
}
if let currentValue = target[keyPath: key.1] as? NSObject,
let updatedValue = value as? NSObject {
if currentValue != updatedValue {
mutableOperation.operations[key.0] = value
mutableOperation.target?[keyPath: key.1] = value
}
} else {
mutableOperation.operations[key.0] = value
mutableOperation.target?[keyPath: key.1] = value
}
return mutableOperation
}

/**
An operation that force sets a field's value.
- Parameters:
- key: A tuple consisting of the key and the respective KeyPath of the object.
- value: The value to set it to.
- returns: The updated operations.
*/
public func forceSet<W>(_ key: (String, WritableKeyPath<T, W>),
value: W) throws -> Self where W: Encodable {
var mutableOperation = self
mutableOperation.operations[key.0] = value
mutableOperation.target?[keyPath: key.1] = value
return mutableOperation
}

/**
An operation that increases a numeric field's value by a given amount.
- Parameters:
- key: The key of the object.
- amount: How much to increment by.
- returns: The updated operations.
*/
public func increment(_ key: String, by amount: Int) -> Self {
var mutableOperation = self
Expand All @@ -43,6 +85,7 @@ public struct ParseOperation<T>: Savable where T: ParseObject {
- Parameters:
- key: The key of the object.
- objects: The field of objects.
- returns: The updated operations.
*/
public func addUnique<W>(_ key: String, objects: [W]) -> Self where W: Encodable, W: Hashable {
var mutableOperation = self
Expand All @@ -54,8 +97,9 @@ public struct ParseOperation<T>: Savable where T: ParseObject {
An operation that adds a new element to an array field,
only if it wasn't already present.
- Parameters:
- key: A tuple consisting of the key and KeyPath of the object.
- key: A tuple consisting of the key and the respective KeyPath of the object.
- objects: The field of objects.
- returns: The updated operations.
*/
public func addUnique<V>(_ key: (String, WritableKeyPath<T, [V]>),
objects: [V]) throws -> Self where V: Encodable, V: Hashable {
Expand All @@ -74,8 +118,9 @@ public struct ParseOperation<T>: Savable where T: ParseObject {
An operation that adds a new element to an array field,
only if it wasn't already present.
- Parameters:
- key: A tuple consisting of the key and KeyPath of the object.
- key: A tuple consisting of the key and the respective KeyPath of the object.
- objects: The field of objects.
- returns: The updated operations.
*/
public func addUnique<V>(_ key: (String, WritableKeyPath<T, [V]?>),
objects: [V]) throws -> Self where V: Encodable, V: Hashable {
Expand All @@ -95,6 +140,7 @@ public struct ParseOperation<T>: Savable where T: ParseObject {
- Parameters:
- key: The key of the object.
- objects: The field of objects.
- returns: The updated operations.
*/
public func add<W>(_ key: String, objects: [W]) -> Self where W: Encodable {
var mutableOperation = self
Expand All @@ -105,8 +151,9 @@ public struct ParseOperation<T>: Savable where T: ParseObject {
/**
An operation that adds a new element to an array field.
- Parameters:
- key: A tuple consisting of the key and KeyPath of the object.
- key: A tuple consisting of the key and the respective KeyPath of the object.
- objects: The field of objects.
- returns: The updated operations.
*/
public func add<V>(_ key: (String, WritableKeyPath<T, [V]>),
objects: [V]) throws -> Self where V: Encodable {
Expand All @@ -124,8 +171,9 @@ public struct ParseOperation<T>: Savable where T: ParseObject {
/**
An operation that adds a new element to an array field.
- Parameters:
- key: A tuple consisting of the key and KeyPath of the object.
- key: A tuple consisting of the key and the respective KeyPath of the object.
- objects: The field of objects.
- returns: The updated operations.
*/
public func add<V>(_ key: (String, WritableKeyPath<T, [V]?>),
objects: [V]) throws -> Self where V: Encodable {
Expand All @@ -145,6 +193,7 @@ public struct ParseOperation<T>: Savable where T: ParseObject {
- Parameters:
- key: The key of the object.
- objects: The field of objects.
- returns: The updated operations.
*/
public func addRelation<W>(_ key: String, objects: [W]) throws -> Self where W: ParseObject {
var mutableOperation = self
Expand All @@ -155,8 +204,9 @@ public struct ParseOperation<T>: Savable where T: ParseObject {
/**
An operation that adds a new relation to an array field.
- Parameters:
- key: A tuple consisting of the key and KeyPath of the object.
- key: A tuple consisting of the key and the respective KeyPath of the object.
- objects: The field of objects.
- returns: The updated operations.
*/
public func addRelation<V>(_ key: (String, WritableKeyPath<T, [V]>),
objects: [V]) throws -> Self where V: ParseObject {
Expand All @@ -174,8 +224,9 @@ public struct ParseOperation<T>: Savable where T: ParseObject {
/**
An operation that adds a new relation to an array field.
- Parameters:
- key: A tuple consisting of the key and KeyPath of the object.
- key: A tuple consisting of the key and the respective KeyPath of the object.
- objects: The field of objects.
- returns: The updated operations.
*/
public func addRelation<V>(_ key: (String, WritableKeyPath<T, [V]?>),
objects: [V]) throws -> Self where V: ParseObject {
Expand All @@ -196,6 +247,7 @@ public struct ParseOperation<T>: Savable where T: ParseObject {
- Parameters:
- key: The key of the object.
- objects: The field of objects.
- returns: The updated operations.
*/
public func remove<W>(_ key: String, objects: [W]) -> Self where W: Encodable {
var mutableOperation = self
Expand All @@ -207,8 +259,9 @@ public struct ParseOperation<T>: Savable where T: ParseObject {
An operation that removes every instance of an element from
an array field.
- Parameters:
- key: A tuple consisting of the key and KeyPath of the object.
- key: A tuple consisting of the key and the respective KeyPath of the object.
- objects: The field of objects.
- returns: The updated operations.
*/
public func remove<V>(_ key: (String, WritableKeyPath<T, [V]>),
objects: [V]) throws -> Self where V: Encodable, V: Hashable {
Expand All @@ -230,8 +283,9 @@ public struct ParseOperation<T>: Savable where T: ParseObject {
An operation that removes every instance of an element from
an array field.
- Parameters:
- key: A tuple consisting of the key and KeyPath of the object.
- key: A tuple consisting of the key and the respective KeyPath of the object.
- objects: The field of objects.
- returns: The updated operations.
*/
public func remove<V>(_ key: (String, WritableKeyPath<T, [V]?>),
objects: [V]) throws -> Self where V: Encodable, V: Hashable {
Expand All @@ -255,6 +309,7 @@ public struct ParseOperation<T>: Savable where T: ParseObject {
- Parameters:
- key: The key of the object.
- objects: The field of objects.
- returns: The updated operations.
*/
public func removeRelation<W>(_ key: String, objects: [W]) throws -> Self where W: ParseObject {
var mutableOperation = self
Expand All @@ -266,8 +321,9 @@ public struct ParseOperation<T>: Savable where T: ParseObject {
An operation that removes every instance of a relation from
an array field.
- Parameters:
- key: A tuple consisting of the key and KeyPath of the object.
- key: A tuple consisting of the key and the respective KeyPath of the object.
- objects: The field of objects.
- returns: The updated operations.
*/
public func removeRelation<V>(_ key: (String, WritableKeyPath<T, [V]>),
objects: [V]) throws -> Self where V: ParseObject {
Expand All @@ -289,8 +345,9 @@ public struct ParseOperation<T>: Savable where T: ParseObject {
An operation that removes every instance of a relation from
an array field.
- Parameters:
- key: A tuple consisting of the key and KeyPath of the object.
- key: A tuple consisting of the key and the respective KeyPath of the object.
- objects: The field of objects.
- returns: The updated operations.
*/
public func removeRelation<V>(_ key: (String, WritableKeyPath<T, [V]?>),
objects: [V]) throws -> Self where V: ParseObject {
Expand All @@ -311,6 +368,7 @@ public struct ParseOperation<T>: Savable where T: ParseObject {
/**
An operation where a field is deleted from the object.
- parameter key: The key of the object.
- returns: The updated operations.
*/
public func unset(_ key: String) -> Self {
var mutableOperation = self
Expand All @@ -321,7 +379,8 @@ public struct ParseOperation<T>: Savable where T: ParseObject {
/**
An operation where a field is deleted from the object.
- Parameters:
- key: A tuple consisting of the key and KeyPath of the object.
- key: A tuple consisting of the key and the respective KeyPath of the object.
- returns: The updated operations.
*/
public func unset<V>(_ key: (String, WritableKeyPath<T, V?>)) -> Self where V: Encodable {
var mutableOperation = self
Expand Down
2 changes: 1 addition & 1 deletion Sources/ParseSwift/ParseConstants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Foundation

enum ParseConstants {
static let sdk = "swift"
static let version = "1.10.4"
static let version = "1.11.0"
static let fileManagementDirectory = "parse/"
static let fileManagementPrivateDocumentsDirectory = "Private Documents/"
static let fileManagementLibraryDirectory = "Library/"
Expand Down
Loading