Skip to content

Commit e9ba846

Browse files
authored
feat: adds equalTo QueryConstraint along which uses $eq (#310)
* feat: adds equalTo QueryConstraint along which uses $eq * nits * nit * Fix merge conflicts * Update change log * Update CHANGELOG.md * Update CHANGELOG.md * Update README.md * nits
1 parent 4c611ac commit e9ba846

File tree

8 files changed

+108
-18
lines changed

8 files changed

+108
-18
lines changed

CHANGELOG.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@
99
[Full Changelog](https://github.com/parse-community/Parse-Swift/compare/2.5.1...3.0.0)
1010

1111
__New features__
12+
- Adds equalTo QueryConstraint along with ability to change the SDK default behavior of using $eq QueryConstraint parameter or not ([#310](https://github.com/parse-community/Parse-Swift/pull/310)), thanks to [Corey Baker](https://github.com/cbaker6).
13+
- Adds isNull and isNotNull QueryConstraint along with the ability set/forceSet null using ParseOperation ([#308](https://github.com/parse-community/Parse-Swift/pull/308)), thanks to [Corey Baker](https://github.com/cbaker6).
14+
- Adds auth support for GitHub, Google, and LinkedIn ([#307](https://github.com/parse-community/Parse-Swift/pull/307)), thanks to [Corey Baker](https://github.com/cbaker6).
1215
- (Breaking Change) Adds options to matchesText QueryConstraint along with the ability to see matching score. The compiler should recommend the new score property to all ParseObjects ([#306](https://github.com/parse-community/Parse-Swift/pull/306)), thanks to [Corey Baker](https://github.com/cbaker6).
1316
- Adds withCount query ([#306](https://github.com/parse-community/Parse-Swift/pull/306)), thanks to [Corey Baker](https://github.com/cbaker6).
14-
- Adds auth support for GitHub, Google, and LinkedIn ([#307](https://github.com/parse-community/Parse-Swift/pull/307)), thanks to [Corey Baker](https://github.com/cbaker6).
15-
- Adds isNull QueryConstraint along with the ability set/forceSet null using ParseOperation ([#308](https://github.com/parse-community/Parse-Swift/pull/308)), thanks to [Corey Baker](https://github.com/cbaker6).
1617

1718
__Improvements__
1819
- (Breaking Change) Change boolean configuration parameters to match Swift conventions. The compilor should help with name changes ([#311](https://github.com/parse-community/Parse-Swift/pull/311)), thanks to [Corey Baker](https://github.com/cbaker6).

ParseSwift.playground/Pages/13 - Operations.xcplaygroundpage/Contents.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ do {
7272
print(error)
7373
}
7474

75-
//: Query all scores whose is null or undefined.
76-
let query1 = GameScore.query(notNull(key: "name"))
75+
//: Query all scores whose name is null or undefined.
76+
let query1 = GameScore.query(isNotNull(key: "name"))
7777
let results1 = try query1.find()
7878
print("Total found: \(results1.count)")
7979
results1.forEach { score in

ParseSwift.playground/Pages/7 - GeoPoint.xcplaygroundpage/Contents.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,8 @@ query4.find { results in
191191
}
192192
}
193193

194-
//: Get the same results as the previous query whose location is not null or undefined.
195-
var anotherQuery4 = GameScore.query("points" > 9, notNull(key: "location"))
194+
//: If you want to query for points > 9 and whose location is not null or undefined.
195+
var anotherQuery4 = GameScore.query("points" > 9, isNotNull(key: "location"))
196196
anotherQuery4.find { results in
197197
switch results {
198198
case .success(let scores):

ParseSwift.playground/Sources/Common.swift

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,17 @@ import ParseSwift
33

44
public func initializeParse() {
55
ParseSwift.initialize(applicationId: "applicationId",
6-
clientKey: "clientKey",
7-
masterKey: "masterKey",
8-
serverURL: URL(string: "http://localhost:1337/1")!,
9-
isUsingTransactions: false)
6+
clientKey: "clientKey",
7+
masterKey: "masterKey",
8+
serverURL: URL(string: "http://localhost:1337/1")!,
9+
isUsingTransactions: false,
10+
isUsingEqualQueryConstraint: false)
1011
}
1112

1213
public func initializeParseCustomObjectId() {
1314
ParseSwift.initialize(applicationId: "applicationId",
14-
clientKey: "clientKey",
15-
serverURL: URL(string: "http://localhost:1337/1")!,
16-
isAllowingCustomObjectIds: true)
15+
clientKey: "clientKey",
16+
serverURL: URL(string: "http://localhost:1337/1")!,
17+
isAllowingCustomObjectIds: true,
18+
isUsingEqualQueryConstraint: false)
1719
}

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ let package = Package(
6464
Then run `swift build`.
6565

6666
You can also install using SPM in your Xcode project by going to
67-
"Project->NameOfYourProject->Swift Packages" and placing "https://github.com/parse-community/Parse-Swift.git" in the
67+
"Project->NameOfYourProject->Swift Packages" and placing `https://github.com/parse-community/Parse-Swift.git` in the
6868
search field.
6969

7070
### [CocoaPods](https://cocoapods.org)

Sources/ParseSwift/Parse.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ public struct ParseConfiguration {
3030
/// - warning: This is experimental.
3131
public internal(set) var isUsingTransactions = false
3232

33+
/// Use the **$eq** query constraint when querying.
34+
/// - warning: This is known not to work for LiveQuery on Parse Servers <= 5.0.0.
35+
public internal(set) var isUsingEqualQueryConstraint = false
36+
3337
/// The default caching policy for all http requests that determines when to
3438
/// return a response from the cache. Defaults to `useProtocolCachePolicy`.
3539
/// See Apple's [documentation](https://developer.apple.com/documentation/foundation/url_loading_system/accessing_cached_data)
@@ -76,6 +80,7 @@ public struct ParseConfiguration {
7680
- parameter isAllowingCustomObjectIds: Allows objectIds to be created on the client.
7781
side for each object. Must be enabled on the server to work.
7882
- parameter isUsingTransactions: Use transactions when saving/updating multiple objects.
83+
- parameter isUsingEqualQueryConstraint: Use the **$eq** query constraint when querying.
7984
- parameter keyValueStore: A key/value store that conforms to the `ParseKeyValueStore`
8085
protocol. Defaults to `nil` in which one will be created an memory, but never persisted. For Linux, this
8186
this is the only store available since there is no Keychain. Linux users should replace this store with an
@@ -106,8 +111,10 @@ public struct ParseConfiguration {
106111
masterKey: String? = nil,
107112
serverURL: URL,
108113
liveQueryServerURL: URL? = nil,
114+
allowCustomObjectId: Bool = false,
109115
isAllowingCustomObjectIds: Bool = false,
110116
isUsingTransactions: Bool = false,
117+
isUsingEqualQueryConstraint: Bool = false,
111118
keyValueStore: ParseKeyValueStore? = nil,
112119
requestCachePolicy: URLRequest.CachePolicy = .useProtocolCachePolicy,
113120
cacheMemoryCapacity: Int = 512_000,
@@ -126,6 +133,7 @@ public struct ParseConfiguration {
126133
self.liveQuerysServerURL = liveQueryServerURL
127134
self.isAllowingCustomObjectIds = isAllowingCustomObjectIds
128135
self.isUsingTransactions = isUsingTransactions
136+
self.isUsingEqualQueryConstraint = isUsingEqualQueryConstraint
129137
self.mountPath = "/" + serverURL.pathComponents
130138
.filter { $0 != "/" }
131139
.joined(separator: "/")
@@ -237,6 +245,7 @@ public struct ParseSwift {
237245
- parameter isAllowingCustomObjectIds: Allows objectIds to be created on the client.
238246
side for each object. Must be enabled on the server to work.
239247
- parameter isUsingTransactions: Use transactions when saving/updating multiple objects.
248+
- parameter isUsingEqualQueryConstraint: Use the **$eq** query constraint when querying.
240249
- parameter keyValueStore: A key/value store that conforms to the `ParseKeyValueStore`
241250
protocol. Defaults to `nil` in which one will be created an memory, but never persisted. For Linux, this
242251
this is the only store available since there is no Keychain. Linux users should replace this store with an
@@ -268,6 +277,7 @@ public struct ParseSwift {
268277
liveQueryServerURL: URL? = nil,
269278
isAllowingCustomObjectIds: Bool = false,
270279
isUsingTransactions: Bool = false,
280+
isUsingEqualQueryConstraint: Bool = false,
271281
keyValueStore: ParseKeyValueStore? = nil,
272282
requestCachePolicy: URLRequest.CachePolicy = .useProtocolCachePolicy,
273283
cacheMemoryCapacity: Int = 512_000,
@@ -287,6 +297,7 @@ public struct ParseSwift {
287297
liveQueryServerURL: liveQueryServerURL,
288298
isAllowingCustomObjectIds: isAllowingCustomObjectIds,
289299
isUsingTransactions: isUsingTransactions,
300+
isUsingEqualQueryConstraint: isUsingEqualQueryConstraint,
290301
keyValueStore: keyValueStore,
291302
requestCachePolicy: requestCachePolicy,
292303
cacheMemoryCapacity: cacheMemoryCapacity,
@@ -305,6 +316,7 @@ public struct ParseSwift {
305316
liveQueryServerURL: URL? = nil,
306317
isAllowingCustomObjectIds: Bool = false,
307318
isUsingTransactions: Bool = false,
319+
isUsingEqualQueryConstraint: Bool = false,
308320
keyValueStore: ParseKeyValueStore? = nil,
309321
requestCachePolicy: URLRequest.CachePolicy = .useProtocolCachePolicy,
310322
cacheMemoryCapacity: Int = 512_000,
@@ -324,6 +336,7 @@ public struct ParseSwift {
324336
liveQueryServerURL: liveQueryServerURL,
325337
isAllowingCustomObjectIds: isAllowingCustomObjectIds,
326338
isUsingTransactions: isUsingTransactions,
339+
isUsingEqualQueryConstraint: isUsingEqualQueryConstraint,
327340
keyValueStore: keyValueStore,
328341
requestCachePolicy: requestCachePolicy,
329342
cacheMemoryCapacity: cacheMemoryCapacity,

Sources/ParseSwift/Types/QueryConstraint.swift

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public struct QueryConstraint: Encodable, Hashable {
1515
case lessThanOrEqualTo = "$lte"
1616
case greaterThan = "$gt"
1717
case greaterThanOrEqualTo = "$gte"
18+
case equalTo = "$eq"
1819
case notEqualTo = "$ne"
1920
case containedIn = "$in"
2021
case notContainedIn = "$nin"
@@ -136,9 +137,36 @@ public func <= <T>(key: String, value: T) -> QueryConstraint where T: Encodable
136137
- parameter key: The key that the value is stored in.
137138
- parameter value: The value to compare.
138139
- returns: The same instance of `QueryConstraint` as the receiver.
140+
- warning: See `equalTo` for more information.
141+
Behavior changes based on `ParseSwift.configuration.isUsingEqualQueryConstraint`
142+
where isUsingEqualQueryConstraint == true is known not to work for LiveQuery on
143+
Parse Servers <= 5.0.0.
139144
*/
140145
public func == <T>(key: String, value: T) -> QueryConstraint where T: Encodable {
141-
QueryConstraint(key: key, value: value)
146+
equalTo(key: key, value: value)
147+
}
148+
149+
/**
150+
Add a constraint that requires that a key is equal to a value.
151+
- parameter key: The key that the value is stored in.
152+
- parameter value: The value to compare.
153+
- parameter isUsingEQ: Set to **true** to use **$eq** comparater,
154+
allowing for multiple `QueryConstraint`'s to be used on a single **key**.
155+
Setting to *false* may override any `QueryConstraint`'s on the same **key**.
156+
Defaults to `ParseSwift.configuration.isUsingEqualQueryConstraint`.
157+
- returns: The same instance of `QueryConstraint` as the receiver.
158+
- warning: `isUsingEQ == true` is known not to work for LiveQueries
159+
on Parse Servers <= 5.0.0.
160+
*/
161+
public func equalTo <T>(key: String,
162+
value: T,
163+
//swiftlint:disable:next line_length
164+
isUsingEQ: Bool = ParseSwift.configuration.isUsingEqualQueryConstraint) -> QueryConstraint where T: Encodable {
165+
if !isUsingEQ {
166+
return QueryConstraint(key: key, value: value)
167+
} else {
168+
return QueryConstraint(key: key, value: value, comparator: .equalTo)
169+
}
142170
}
143171

144172
/**
@@ -147,9 +175,37 @@ public func == <T>(key: String, value: T) -> QueryConstraint where T: Encodable
147175
- parameter value: The `ParseObject` to compare.
148176
- returns: The same instance of `QueryConstraint` as the receiver.
149177
- throws: An error of type `ParseError`.
178+
- warning: See `equalTo` for more information.
179+
Behavior changes based on `ParseSwift.configuration.isUsingEqualQueryConstraint`
180+
where isUsingEqualQueryConstraint == true is known not to work for LiveQuery on
181+
Parse Servers <= 5.0.0.
150182
*/
151183
public func == <T>(key: String, value: T) throws -> QueryConstraint where T: ParseObject {
152-
try QueryConstraint(key: key, value: value.toPointer())
184+
try equalTo(key: key, value: value)
185+
}
186+
187+
/**
188+
Add a constraint that requires that a key is equal to a `ParseObject`.
189+
- parameter key: The key that the value is stored in.
190+
- parameter value: The `ParseObject` to compare.
191+
- parameter isUsingEQ: Set to **true** to use **$eq** comparater,
192+
allowing for multiple `QueryConstraint`'s to be used on a single **key**.
193+
Setting to *false* may override any `QueryConstraint`'s on the same **key**.
194+
Defaults to `ParseSwift.configuration.isUsingEqualQueryConstraint`.
195+
- returns: The same instance of `QueryConstraint` as the receiver.
196+
- throws: An error of type `ParseError`.
197+
- warning: `isUsingEQ == true` is known not to work for LiveQueries
198+
on Parse Servers <= 5.0.0.
199+
*/
200+
public func equalTo <T>(key: String,
201+
value: T,
202+
//swiftlint:disable:next line_length
203+
isUsingEQ: Bool = ParseSwift.configuration.isUsingEqualQueryConstraint) throws -> QueryConstraint where T: ParseObject {
204+
if !isUsingEQ {
205+
return try QueryConstraint(key: key, value: value.toPointer())
206+
} else {
207+
return try QueryConstraint(key: key, value: value.toPointer(), comparator: .equalTo)
208+
}
153209
}
154210

155211
/**
@@ -703,7 +759,7 @@ public func isNull (key: String) -> QueryConstraint {
703759
- parameter key: The key that the value is stored in.
704760
- returns: The same instance of `QueryConstraint` as the receiver.
705761
*/
706-
public func notNull (key: String) -> QueryConstraint {
762+
public func isNotNull (key: String) -> QueryConstraint {
707763
QueryConstraint(key: key, comparator: .notEqualTo, isNull: true)
708764
}
709765

Tests/ParseSwiftTests/ParseQueryTests.swift

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ class ParseQueryTests: XCTestCase { // swiftlint:disable:this type_body_length
6262
clientKey: "clientKey",
6363
masterKey: "masterKey",
6464
serverURL: url,
65+
isUsingEqualQueryConstraint: false,
6566
isTesting: true)
6667
}
6768

@@ -1186,6 +1187,14 @@ class ParseQueryTests: XCTestCase { // swiftlint:disable:this type_body_length
11861187
XCTAssertEqual(query.description, expected)
11871188
}
11881189

1190+
func testWhereKeyEqualToBoolEQ() throws {
1191+
let query = GameScore.query(equalTo(key: "isCounts", value: true, isUsingEQ: true))
1192+
// swiftlint:disable:next line_length
1193+
let expected = "GameScore ({\"limit\":100,\"skip\":0,\"_method\":\"GET\",\"where\":{\"isCounts\":{\"$eq\":true}}})"
1194+
XCTAssertEqual(query.debugDescription, expected)
1195+
XCTAssertEqual(query.description, expected)
1196+
}
1197+
11891198
func testWhereKeyEqualToParseObject() throws {
11901199
var compareObject = GameScore(points: 11)
11911200
compareObject.objectId = "hello"
@@ -1195,6 +1204,15 @@ class ParseQueryTests: XCTestCase { // swiftlint:disable:this type_body_length
11951204
XCTAssertEqual(query.debugDescription, expected)
11961205
}
11971206

1207+
func testWhereKeyEqualToParseObjectEQ() throws {
1208+
var compareObject = GameScore(points: 11)
1209+
compareObject.objectId = "hello"
1210+
let query = try GameScore.query(equalTo(key: "yolo", value: compareObject, isUsingEQ: true))
1211+
// swiftlint:disable:next line_length
1212+
let expected = "GameScore ({\"limit\":100,\"skip\":0,\"_method\":\"GET\",\"where\":{\"yolo\":{\"$eq\":{\"__type\":\"Pointer\",\"className\":\"GameScore\",\"objectId\":\"hello\"}}}})"
1213+
XCTAssertEqual(query.debugDescription, expected)
1214+
}
1215+
11981216
func testWhereKeyEqualToParseObjectDuplicateConstraint() throws {
11991217
var compareObject = GameScore(points: 11)
12001218
compareObject.objectId = "hello"
@@ -1235,7 +1253,7 @@ class ParseQueryTests: XCTestCase { // swiftlint:disable:this type_body_length
12351253
func testWhereKeyNotNull() throws {
12361254
var compareObject = GameScore(points: 11)
12371255
compareObject.objectId = "hello"
1238-
let query = GameScore.query(notNull(key: "yolo"))
1256+
let query = GameScore.query(isNotNull(key: "yolo"))
12391257
let expected = "GameScore ({\"limit\":100,\"skip\":0,\"_method\":\"GET\",\"where\":{\"yolo\":{\"$ne\":null}}})"
12401258
XCTAssertEqual(query.debugDescription, expected)
12411259
}

0 commit comments

Comments
 (0)