Skip to content

feat: allow includeAll with additional includes #367

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 3 commits into from
May 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ __New features__
- Add toCLLocation and toCLLocationCoordinate2D computed properties to ParseGeoPoint, deprecate toCLLocation() and toCLLocationCoordinate2D() ([#366](https://github.com/parse-community/Parse-Swift/pull/366)), thanks to [Corey Baker](https://github.com/cbaker6).
- Add query computed property to ParseObject ([#365](https://github.com/parse-community/Parse-Swift/pull/365)), thanks to [Corey Baker](https://github.com/cbaker6).
- Add macCatalyst to SPM ([#363](https://github.com/parse-community/Parse-Swift/pull/363)), thanks to [Corey Baker](https://github.com/cbaker6).
- Add includeAll computed property to Query and deprecate includeAll(). Add an order() method to Query that excepts a variadic list as input ([#362](https://github.com/parse-community/Parse-Swift/pull/362)), thanks to [Corey Baker](https://github.com/cbaker6).
- Add an order() method to Query that excepts a variadic list as input ([#362](https://github.com/parse-community/Parse-Swift/pull/362)), thanks to [Corey Baker](https://github.com/cbaker6).

__Improvements__
- Allow includeAll key to be sent with additional include keys. When fetching, if the include argument is specified, convert it to a Set to prevent duplicate keys from being sent to the server ([#367](https://github.com/parse-community/Parse-Swift/pull/367)), thanks to [Corey Baker](https://github.com/cbaker6).
- Allow LiveQuery client to be set using ParseLiveQuery.defaultClient and deprecate ParseLiveQuery.setDefault(). Show usage of deprecated code as warnings during compile time and suggest changes ([#360](https://github.com/parse-community/Parse-Swift/pull/360)), thanks to [Corey Baker](https://github.com/cbaker6).

### 4.4.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ query2.first { results in
objects.
*/
let query3 = Author.query("name" == "Bruce")
.includeAll
.includeAll()

query3.first { results in
switch results {
Expand All @@ -192,7 +192,7 @@ query3.first { results in
//: You can also check if a field is equal to a ParseObject.
do {
let query4 = try Author.query("book" == newBook)
.includeAll
.includeAll()

query4.first { results in
switch results {
Expand Down
2 changes: 1 addition & 1 deletion Sources/ParseSwift/API/API+Command.swift
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,7 @@ internal extension API.Command {

var params: [String: String]?
if let includeParams = include {
params = ["include": "\(includeParams)"]
params = ["include": "\(Set(includeParams))"]
}

return API.Command<T, T>(
Expand Down
2 changes: 1 addition & 1 deletion Sources/ParseSwift/Objects/ParseInstallation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@ extension ParseInstallation {

var params: [String: String]?
if let includeParams = include {
params = ["include": "\(includeParams)"]
params = ["include": "\(Set(includeParams))"]
}

return API.Command(method: .GET,
Expand Down
2 changes: 1 addition & 1 deletion Sources/ParseSwift/Objects/ParseUser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -916,7 +916,7 @@ extension ParseUser {

var params: [String: String]?
if let includeParams = include {
params = ["include": "\(includeParams)"]
params = ["include": "\(Set(includeParams))"]
}

return API.Command(method: .GET,
Expand Down
1 change: 1 addition & 0 deletions Sources/ParseSwift/ParseConstants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ enum ParseConstants {
static let fileDownloadsDirectory = "Downloads"
static let bundlePrefix = "com.parse.ParseSwift"
static let batchLimit = 50
static let includeAllKey = "*"
#if os(iOS)
static let deviceType = "ios"
#elseif os(macOS)
Expand Down
22 changes: 8 additions & 14 deletions Sources/ParseSwift/Types/Query.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,6 @@ public struct Query<T>: Encodable, Equatable where T: ParseObject {
Self.className
}

/**
Includes all nested `ParseObject`s one level deep.
- warning: Requires Parse Server 3.0.0+.
*/
public var includeAll: Query<T> {
var mutableQuery = self
mutableQuery.include = ["*"]
return mutableQuery
}

struct AggregateBody<T>: Encodable where T: ParseObject {
let pipeline: [[String: AnyEncodable]]?
let hint: AnyEncodable?
Expand Down Expand Up @@ -265,12 +255,16 @@ public struct Query<T>: Encodable, Equatable where T: ParseObject {
/**
Includes all nested `ParseObject`s one level deep.
- warning: Requires Parse Server 3.0.0+.
- warning: This will be removed in ParseSwift 5.0.0 in favor of the `includeAll` computed property.
- returns: The mutated instance of query for easy chaining.
*/
@available(*, deprecated, message: "Use the computed property instead by removing \"()\"")
public func includeAll(_ keys: [String]? = nil) -> Query<T> {
self.includeAll
public func includeAll() -> Query<T> {
var mutableQuery = self
if mutableQuery.include != nil {
mutableQuery.include?.insert(ParseConstants.includeAllKey)
} else {
mutableQuery.include = [ParseConstants.includeAllKey]
}
return mutableQuery
}

/**
Expand Down
21 changes: 7 additions & 14 deletions Tests/ParseSwiftTests/ParseInstallationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -777,21 +777,14 @@ class ParseInstallationTests: XCTestCase { // swiftlint:disable:this type_body_l
XCTAssertNotNil(command)
XCTAssertEqual(command.path.urlComponent, "/installations/\(objectId)")
XCTAssertEqual(command.method, API.Method.GET)
XCTAssertEqual(command.params, includeExpected)
XCTAssertNil(command.body)

// swiftlint:disable:next line_length
guard let urlExpected = URL(string: "http://localhost:1337/1/installations/yarr?include=%5B%22yolo%22,%20%22test%22%5D") else {
XCTFail("Should have unwrapped")
return
}
let request = command.prepareURLRequest(options: [])
switch request {
case .success(let url):
XCTAssertEqual(url.url, urlExpected)
case .failure(let error):
XCTFail(error.localizedDescription)
XCTAssertEqual(command.params?.keys.first, includeExpected.keys.first)
if let value = command.params?.values.first,
let includeValue = value {
XCTAssertTrue(includeValue.contains("\"yolo\""))
} else {
XCTFail("Should have unwrapped value")
}
XCTAssertNil(command.body)
} catch {
XCTFail(error.localizedDescription)
}
Expand Down
21 changes: 7 additions & 14 deletions Tests/ParseSwiftTests/ParseObjectTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -456,21 +456,14 @@ class ParseObjectTests: XCTestCase { // swiftlint:disable:this type_body_length
XCTAssertNotNil(command)
XCTAssertEqual(command.path.urlComponent, "/classes/\(className)/\(objectId)")
XCTAssertEqual(command.method, API.Method.GET)
XCTAssertEqual(command.params, includeExpected)
XCTAssertNil(command.body)

// swiftlint:disable:next line_length
guard let urlExpected = URL(string: "http://localhost:1337/1/classes/GameScore/yarr?include=%5B%22yolo%22,%20%22test%22%5D") else {
XCTFail("Should have unwrapped")
return
}
let request = command.prepareURLRequest(options: [])
switch request {
case .success(let url):
XCTAssertEqual(url.url, urlExpected)
case .failure(let error):
XCTFail(error.localizedDescription)
XCTAssertEqual(command.params?.keys.first, includeExpected.keys.first)
if let value = command.params?.values.first,
let includeValue = value {
XCTAssertTrue(includeValue.contains("\"yolo\""))
} else {
XCTFail("Should have unwrapped value")
}
XCTAssertNil(command.body)
} catch {
XCTFail(error.localizedDescription)
}
Expand Down
5 changes: 5 additions & 0 deletions Tests/ParseSwiftTests/ParseQueryTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,11 @@ class ParseQueryTests: XCTestCase { // swiftlint:disable:this type_body_length
let query2 = GameScore.query.includeAll()
XCTAssertEqual(query2.include?.count, 1)
XCTAssertEqual(query2.include, ["*"])
let query3 = GameScore.query
.include("hello")
.includeAll()
XCTAssertEqual(query3.include?.count, 2)
XCTAssertEqual(query3.include, Set(["hello", "*"]))
}

func testExcludeKeys() throws {
Expand Down
21 changes: 7 additions & 14 deletions Tests/ParseSwiftTests/ParseUserTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -213,21 +213,14 @@ class ParseUserTests: XCTestCase { // swiftlint:disable:this type_body_length
XCTAssertNotNil(command)
XCTAssertEqual(command.path.urlComponent, "/users/\(objectId)")
XCTAssertEqual(command.method, API.Method.GET)
XCTAssertEqual(command.params, includeExpected)
XCTAssertNil(command.body)

// swiftlint:disable:next line_length
guard let urlExpected = URL(string: "http://localhost:1337/1/users/yarr?include=%5B%22yolo%22,%20%22test%22%5D") else {
XCTFail("Should have unwrapped")
return
}
let request = command.prepareURLRequest(options: [])
switch request {
case .success(let url):
XCTAssertEqual(url.url, urlExpected)
case .failure(let error):
XCTFail(error.localizedDescription)
XCTAssertEqual(command.params?.keys.first, includeExpected.keys.first)
if let value = command.params?.values.first,
let includeValue = value {
XCTAssertTrue(includeValue.contains("\"yolo\""))
} else {
XCTFail("Should have unwrapped value")
}
XCTAssertNil(command.body)
} catch {
XCTFail(error.localizedDescription)
}
Expand Down