Skip to content

Commit b7910e8

Browse files
Merge pull request #644 from algolia/feat/new-partial-update-operations
Add IncrementSet and IncrementFrom as partial update operations.
2 parents 1968981 + 5285728 commit b7910e8

File tree

2 files changed

+117
-2
lines changed

2 files changed

+117
-2
lines changed

Sources/AlgoliaSearchClient/Models/Search/Indexing/PartialUpdate.swift

Lines changed: 115 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,34 @@ public struct PartialUpdate: Equatable {
1616

1717
public extension PartialUpdate {
1818

19+
/// Partially update an object field.
20+
/// - Parameter attribute: Attribute name to update
21+
/// - Parameter value: Updated value
1922
static func update(attribute: Attribute, value: JSON) -> Self {
2023
.init(attribute: attribute, content: .init(value: value, operation: nil))
2124
}
2225

2326
}
2427

2528
public extension PartialUpdate {
26-
29+
30+
/// Increment a numeric attribute
31+
/// - Parameter attribute: Attribute name to update
32+
/// - Parameter value: Value to increment by
2733
static func increment(attribute: Attribute, value: Int) -> Self {
2834
.operation(attribute: attribute, operation: .increment, value: .init(value))
2935
}
3036

37+
/// Increment a numeric attribute
38+
/// - Parameter attribute: Attribute name to update
39+
/// - Parameter value: Value to increment by
3140
static func increment(attribute: Attribute, value: Float) -> Self {
3241
.operation(attribute: attribute, operation: .increment, value: .init(value))
3342
}
3443

44+
/// Increment a numeric attribute
45+
/// - Parameter attribute: Attribute name to update
46+
/// - Parameter value: Value to increment by
3547
static func increment(attribute: Attribute, value: Double) -> Self {
3648
.operation(attribute: attribute, operation: .increment, value: .init(value))
3749
}
@@ -40,14 +52,58 @@ public extension PartialUpdate {
4052

4153
public extension PartialUpdate {
4254

55+
/**
56+
Increment a numeric integer attribute only if the provided value matches the current value,
57+
and otherwise ignore the whole object update.
58+
59+
For example, if you pass an IncrementFrom value of 2 for the version attribute,
60+
but the current value of the attribute is 1, the engine ignores the update.
61+
If the object doesn’t exist, the engine only creates it if you pass an IncrementFrom value of 0.
62+
63+
- Parameter attribute: Attribute name to update
64+
- Parameter value: Current value to increment
65+
*/
66+
static func incrementFrom(attribute: Attribute, value: Int) -> Self {
67+
.operation(attribute: attribute, operation: .incrementFrom, value: .init(value))
68+
}
69+
70+
/**
71+
Increment a numeric integer attribute only if the provided value is greater than the current value,
72+
and otherwise ignore the whole object update.
73+
74+
For example, if you pass an IncrementSet value of 2 for the version attribute,
75+
and the current value of the attribute is 1, the engine updates the object.
76+
If the object doesn’t exist yet, the engine only creates it if you pass an IncrementSet value that’s greater than 0.
77+
78+
- Parameter attribute: Attribute name to update
79+
- Parameter value: Value to set
80+
*/
81+
static func incrementSet(attribute: Attribute, value: Int) -> Self {
82+
.operation(attribute: attribute, operation: .incrementSet, value: .init(value))
83+
}
84+
85+
}
86+
87+
88+
public extension PartialUpdate {
89+
90+
/// Decrement a numeric attribute
91+
/// - Parameter attribute: Attribute name to update
92+
/// - Parameter value: Value to decrement by
4393
static func decrement(attribute: Attribute, value: Int) -> Self {
4494
.operation(attribute: attribute, operation: .decrement, value: .init(value))
4595
}
46-
96+
97+
/// Decrement a numeric attribute
98+
/// - Parameter attribute: Attribute name to update
99+
/// - Parameter value: Value to decrement by
47100
static func decrement(attribute: Attribute, value: Float) -> Self {
48101
.operation(attribute: attribute, operation: .decrement, value: .init(value))
49102
}
50103

104+
/// Decrement a numeric attribute
105+
/// - Parameter attribute: Attribute name to update
106+
/// - Parameter value: Value to decrement by
51107
static func decrement(attribute: Attribute, value: Double) -> Self {
52108
.operation(attribute: attribute, operation: .decrement, value: .init(value))
53109
}
@@ -56,18 +112,34 @@ public extension PartialUpdate {
56112

57113
public extension PartialUpdate {
58114

115+
/// Append a string element to an array attribute
116+
/// - Parameter attribute: Attribute name of an array to update
117+
/// - Parameter value: Value to append
118+
/// - Parameter unique: If true, append an element only if it’s not already present
59119
static func add(attribute: Attribute, value: String, unique: Bool) -> Self {
60120
.operation(attribute: attribute, operation: unique ? .addUnique : .add, value: .init(value))
61121
}
62122

123+
/// Append a number element to an array attribute
124+
/// - Parameter attribute: Attribute name of an array to update
125+
/// - Parameter value: Value to append
126+
/// - Parameter unique: If true, append an element only if it’s not already present
63127
static func add(attribute: Attribute, value: Int, unique: Bool) -> Self {
64128
.operation(attribute: attribute, operation: unique ? .addUnique : .add, value: .init(value))
65129
}
66130

131+
/// Append a number element to an array attribute
132+
/// - Parameter attribute: Attribute name of an array to update
133+
/// - Parameter value: Value to append
134+
/// - Parameter unique: If true, append an element only if it’s not already present
67135
static func add(attribute: Attribute, value: Float, unique: Bool) -> Self {
68136
.operation(attribute: attribute, operation: unique ? .addUnique : .add, value: .init(value))
69137
}
70138

139+
/// Append a number element to an array attribute
140+
/// - Parameter attribute: Attribute name of an array to update
141+
/// - Parameter value: Value to append
142+
/// - Parameter unique: If true, append an element only if it’s not already present
71143
static func add(attribute: Attribute, value: Double, unique: Bool) -> Self {
72144
.operation(attribute: attribute, operation: unique ? .addUnique : .add, value: .init(value))
73145
}
@@ -76,18 +148,30 @@ public extension PartialUpdate {
76148

77149
public extension PartialUpdate {
78150

151+
/// Remove all matching string elements from an array attribute
152+
/// - Parameter attribute: Attribute name of an array to update
153+
/// - Parameter value: Value to remove
79154
static func remove(attribute: Attribute, value: String) -> Self {
80155
.operation(attribute: attribute, operation: .remove, value: .init(value))
81156
}
82157

158+
/// Remove all matching number elements from an array attribute
159+
/// - Parameter attribute: Attribute name of an array to update
160+
/// - Parameter value: Value to remove
83161
static func remove(attribute: Attribute, value: Int) -> Self {
84162
.operation(attribute: attribute, operation: .remove, value: .init(value))
85163
}
86164

165+
/// Remove all matching number elements from an array attribute
166+
/// - Parameter attribute: Attribute name of an array to update
167+
/// - Parameter value: Value to remove
87168
static func remove(attribute: Attribute, value: Float) -> Self {
88169
.operation(attribute: attribute, operation: .remove, value: .init(value))
89170
}
90171

172+
/// Remove all matching number elements from an array attribute
173+
/// - Parameter attribute: Attribute name of an array to update
174+
/// - Parameter value: Value to remove
91175
static func remove(attribute: Attribute, value: Double) -> Self {
92176
.operation(attribute: attribute, operation: .remove, value: .init(value))
93177
}
@@ -122,10 +206,39 @@ extension PartialUpdate: Codable {
122206
extension PartialUpdate {
123207

124208
enum Operation: String, Codable {
209+
/// Increment a numeric attribute
125210
case increment = "Increment"
211+
212+
/**
213+
Increment a numeric integer attribute only if the provided value matches the current value,
214+
and otherwise ignore the whole object update.
215+
216+
For example, if you pass an IncrementFrom value of 2 for the version attribute,
217+
but the current value of the attribute is 1, the engine ignores the update.
218+
If the object doesn’t exist, the engine only creates it if you pass an IncrementFrom value of 0.
219+
*/
220+
case incrementFrom = "IncrementFrom"
221+
222+
/**
223+
Increment a numeric integer attribute only if the provided value is greater than the current value,
224+
and otherwise ignore the whole object update.
225+
226+
For example, if you pass an IncrementSet value of 2 for the version attribute,
227+
and the current value of the attribute is 1, the engine updates the object.
228+
If the object doesn’t exist yet, the engine only creates it if you pass an IncrementSet value that’s greater than 0.
229+
*/
230+
case incrementSet = "IncrementSet"
231+
232+
/// Decrement a numeric attribute
126233
case decrement = "Decrement"
234+
235+
/// Append a number or string element to an array attribute
127236
case add = "Add"
237+
238+
/// Remove all matching number or string elements from an array attribute
128239
case remove = "Remove"
240+
241+
/// Add a number or string element to an array attribute only if it’s not already present
129242
case addUnique = "AddUnique"
130243
}
131244

Tests/AlgoliaSearchClientTests/Unit/PartialUpdateTests.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ class PartialUpdateTests: XCTestCase {
1212
try AssertEncodeDecode(PartialUpdate.remove(attribute: "attr", value: 2.5), ["attr": ["value": 2.5, "_operation": "Remove"]])
1313
try AssertEncodeDecode(PartialUpdate.remove(attribute: "attr", value: "stringValue"), ["attr": ["value": "stringValue", "_operation": "Remove"]])
1414
try AssertEncodeDecode(PartialUpdate.increment(attribute: "attr", value: 2.5), ["attr": ["value": 2.5, "_operation": "Increment"]])
15+
try AssertEncodeDecode(PartialUpdate.incrementFrom(attribute: "attr", value: 2), ["attr": ["value": 2, "_operation": "IncrementFrom"]])
16+
try AssertEncodeDecode(PartialUpdate.incrementSet(attribute: "attr", value: 2), ["attr": ["value": 2, "_operation": "IncrementSet"]])
1517
try AssertEncodeDecode(PartialUpdate.decrement(attribute: "attr", value: 2.5), ["attr": ["value": 2.5, "_operation": "Decrement"]])
1618
try AssertEncodeDecode(PartialUpdate.update(attribute: "attr", value: "string"), ["attr": "string"])
1719
try AssertEncodeDecode(PartialUpdate.update(attribute: "attr", value: 2.5), ["attr": 2.5])

0 commit comments

Comments
 (0)