Skip to content

Commit 0e40929

Browse files
authored
refactor: Make some ParseObject methods required (#96)
* refactor: Make some ParseObject methods and properties required * nit * nits * api nits * remove unused code
1 parent a746db0 commit 0e40929

File tree

9 files changed

+117
-130
lines changed

9 files changed

+117
-130
lines changed

CHANGELOG.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,15 @@
22
# Parse-Swift Changelog
33

44
### main
5-
[Full Changelog](https://github.com/netreconlab/Parse-Swift/compare/5.4.2...main), [Documentation](https://swiftpackageindex.com/netreconlab/Parse-Swift/main/documentation/parseswift)
5+
[Full Changelog](https://github.com/netreconlab/Parse-Swift/compare/5.4.3...main), [Documentation](https://swiftpackageindex.com/netreconlab/Parse-Swift/main/documentation/parseswift)
66
* _Contributing to this repo? Add info about your change here to be included in the next release_
77

8+
### 5.4.3
9+
[Full Changelog](https://github.com/netreconlab/Parse-Swift/compare/5.4.2...5.4.3), [Documentation](https://swiftpackageindex.com/netreconlab/Parse-Swift/5.4.3/documentation/parseswift)
10+
11+
__Fixes__
12+
* Move some ParseObject methods and properties to required to leverage developer implementations ([#96](https://github.com/netreconlab/Parse-Swift/pull/96)), thanks to [Corey Baker](https://github.com/cbaker6).
13+
814
### 5.4.2
915
[Full Changelog](https://github.com/netreconlab/Parse-Swift/compare/5.4.1...5.4.2), [Documentation](https://swiftpackageindex.com/netreconlab/Parse-Swift/5.4.2/documentation/parseswift)
1016

Sources/ParseSwift/API/API+NonParseBodyCommand.swift

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -33,31 +33,6 @@ internal extension API {
3333
self.mapper = mapper
3434
}
3535

36-
/*
37-
func execute(options: API.Options) throws -> U {
38-
var responseResult: Result<U, ParseError>?
39-
let synchronizationQueue = DispatchQueue(label: "com.parse.NonParseBodyCommand.sync.\(UUID().uuidString)",
40-
qos: .default,
41-
attributes: .concurrent,
42-
autoreleaseFrequency: .inherit,
43-
target: nil)
44-
let group = DispatchGroup()
45-
group.enter()
46-
self.executeAsync(options: options,
47-
callbackQueue: synchronizationQueue,
48-
allowIntermediateResponses: false) { result in
49-
responseResult = result
50-
group.leave()
51-
}
52-
group.wait()
53-
54-
guard let response = responseResult else {
55-
throw ParseError(code: .otherCause,
56-
message: "Could not unrwrap server response")
57-
}
58-
return try response.get()
59-
} */
60-
6136
// MARK: Asynchronous Execution
6237
func execute(options: API.Options,
6338
callbackQueue: DispatchQueue,

Sources/ParseSwift/API/API.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import FoundationNetworking
1616
/// The REST API for communicating with a Parse Server.
1717
public struct API {
1818

19-
internal enum Method: String, Encodable {
19+
public enum Method: String, Encodable {
2020
case GET, POST, PUT, PATCH, DELETE
2121
}
2222

Sources/ParseSwift/API/Responses.swift

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import Foundation
1010

11-
internal struct CreateResponse: Decodable {
11+
struct CreateResponse: Decodable {
1212
var objectId: String
1313
var createdAt: Date
1414
var updatedAt: Date {
@@ -24,7 +24,7 @@ internal struct CreateResponse: Decodable {
2424
}
2525
}
2626

27-
internal struct ReplaceResponse: Decodable {
27+
struct ReplaceResponse: Decodable {
2828
var createdAt: Date?
2929
var updatedAt: Date?
3030

@@ -45,7 +45,7 @@ internal struct ReplaceResponse: Decodable {
4545
}
4646
}
4747

48-
internal struct UpdateResponse: Decodable {
48+
struct UpdateResponse: Decodable {
4949
var updatedAt: Date
5050

5151
func apply<T>(to object: T) -> T where T: ParseObject {
@@ -55,18 +55,18 @@ internal struct UpdateResponse: Decodable {
5555
}
5656
}
5757

58-
internal struct UpdateSessionTokenResponse: Decodable {
58+
struct UpdateSessionTokenResponse: Decodable {
5959
var updatedAt: Date
6060
let sessionToken: String?
6161
}
6262

6363
// MARK: ParseObject Batch
64-
internal struct BatchResponseItem<T>: Codable where T: Codable {
64+
struct BatchResponseItem<T>: Codable where T: Codable {
6565
let success: T?
6666
let error: ParseError?
6767
}
6868

69-
internal struct BatchResponse: Codable {
69+
struct BatchResponse: Codable {
7070
var objectId: String?
7171
var createdAt: Date?
7272
var updatedAt: Date?
@@ -112,13 +112,13 @@ internal struct BatchResponse: Codable {
112112
}
113113

114114
// MARK: Query
115-
internal struct QueryResponse<T>: Codable where T: ParseObject {
115+
struct QueryResponse<T>: Codable where T: ParseObject {
116116
let results: [T]
117117
let count: Int?
118118
}
119119

120120
// MARK: ParseUser
121-
internal struct LoginSignupResponse: Codable {
121+
struct LoginSignupResponse: Codable {
122122
let createdAt: Date
123123
let objectId: String
124124
let sessionToken: String
@@ -137,7 +137,7 @@ internal struct LoginSignupResponse: Codable {
137137
}
138138

139139
// MARK: ParseFile
140-
internal struct FileUploadResponse: Codable {
140+
struct FileUploadResponse: Codable {
141141
let name: String
142142
let url: URL
143143

@@ -150,35 +150,35 @@ internal struct FileUploadResponse: Codable {
150150
}
151151

152152
// MARK: AnyResultResponse
153-
internal struct AnyResultResponse<U: Decodable>: Decodable {
153+
struct AnyResultResponse<U: Decodable>: Decodable {
154154
let result: U
155155
}
156156

157157
// MARK: AnyResultsResponse
158-
internal struct AnyResultsResponse<U: Decodable>: Decodable {
158+
struct AnyResultsResponse<U: Decodable>: Decodable {
159159
let results: [U]
160160
}
161161

162-
internal struct AnyResultsMongoResponse<U: Decodable>: Decodable {
162+
struct AnyResultsMongoResponse<U: Decodable>: Decodable {
163163
let results: U
164164
}
165165

166166
// MARK: ConfigResponse
167-
internal struct ConfigFetchResponse<T>: Codable where T: ParseConfig {
167+
struct ConfigFetchResponse<T>: Codable where T: ParseConfig {
168168
let params: T
169169
}
170170

171-
internal struct BooleanResponse: Codable {
171+
struct BooleanResponse: Codable {
172172
let result: Bool
173173
}
174174

175175
// MARK: HealthResponse
176-
internal struct HealthResponse: Codable {
176+
struct HealthResponse: Codable {
177177
let status: ParseHealth.Status
178178
}
179179

180180
// MARK: PushResponse
181-
internal struct PushResponse: Codable {
181+
struct PushResponse: Codable {
182182
let data: Data
183183
let statusId: String
184184
}

Sources/ParseSwift/Objects/ParseObject.swift

Lines changed: 40 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,30 @@ public protocol ParseObject: ParseTypeable,
8282
*/
8383
init()
8484

85+
/**
86+
Creates a `ParseObject` with a specified `objectId`. Can be used to create references or associations
87+
between `ParseObject`'s.
88+
- warning: It is required that all added properties be optional properties so they can eventually be used as
89+
Parse `Pointer`'s. If a developer really wants to have a required key, they should require it on the server-side or
90+
create methods to check the respective properties on the client-side before saving objects. See
91+
[here](https://github.com/parse-community/Parse-Swift/pull/315#issuecomment-1014701003)
92+
for more information.
93+
*/
94+
init(objectId: String)
95+
96+
/**
97+
Determines if two objects have the same objectId.
98+
- parameter as: Object to compare.
99+
- returns: Returns a **true** if the other object has the same `objectId` or **false** if unsuccessful.
100+
*/
101+
func hasSameObjectId<T: ParseObject>(as other: T) -> Bool
102+
103+
/**
104+
Converts this `ParseObject` to a Parse Pointer.
105+
- returns: The pointer version of the `ParseObject`, Pointer<Self>.
106+
*/
107+
func toPointer() throws -> Pointer<Self>
108+
85109
/**
86110
Determines if a `KeyPath` of the current `ParseObject` should be restored
87111
by comparing it to another `ParseObject`.
@@ -148,29 +172,11 @@ public protocol ParseObject: ParseTypeable,
148172
// MARK: Default Implementations
149173
public extension ParseObject {
150174

151-
/**
152-
Creates a `ParseObject` with a specified `objectId`. Can be used to create references or associations
153-
between `ParseObject`'s.
154-
- warning: It is required that all added properties be optional properties so they can eventually be used as
155-
Parse `Pointer`'s. If a developer really wants to have a required key, they should require it on the server-side or
156-
create methods to check the respective properties on the client-side before saving objects. See
157-
[here](https://github.com/parse-community/Parse-Swift/pull/315#issuecomment-1014701003)
158-
for more information.
159-
*/
160-
init(objectId: String) {
161-
self.init()
162-
self.objectId = objectId
163-
}
164-
165-
func hash(into hasher: inout Hasher) {
166-
hasher.combine(self.id)
167-
}
168-
169175
/**
170176
A computed property that is a unique identifier and makes it easy to use `ParseObject`'s
171177
as models in MVVM and SwiftUI.
172-
- note: `id` allows `ParseObject`'s to be used even when they are not saved and do not have an `objectId`.
173-
- important: `id` will have the same value as `objectId` when a `ParseObject` is saved.
178+
- note: `id` allows `ParseObject`'s to be used even if they have not been saved and/or missing an `objectId`.
179+
- important: `id` will have the same value as `objectId` when a `ParseObject` contains an `objectId`.
174180
*/
175181
var id: String {
176182
objectId ?? UUID().uuidString
@@ -188,21 +194,17 @@ public extension ParseObject {
188194
return object
189195
}
190196

191-
/**
192-
Determines if two objects have the same objectId.
193-
- parameter as: Object to compare.
194-
- returns: Returns a **true** if the other object has the same `objectId` or **false** if unsuccessful.
195-
*/
197+
init(objectId: String) {
198+
self.init()
199+
self.objectId = objectId
200+
}
201+
196202
func hasSameObjectId<T: ParseObject>(as other: T) -> Bool {
197-
return other.className == className && other.objectId == objectId && objectId != nil
203+
other.className == className && other.objectId == objectId && objectId != nil
198204
}
199205

200-
/**
201-
Converts this `ParseObject` to a Parse Pointer.
202-
- returns: The pointer version of the `ParseObject`, Pointer<Self>.
203-
*/
204206
func toPointer() throws -> Pointer<Self> {
205-
return try Pointer(self)
207+
try Pointer(self)
206208
}
207209

208210
func shouldRestoreKey<W>(_ keyPath: KeyPath<Self, W?>,
@@ -240,6 +242,13 @@ extension ParseObject {
240242
}
241243
}
242244

245+
// MARK: Hashable
246+
public extension ParseObject {
247+
func hash(into hasher: inout Hasher) {
248+
hasher.combine(self.id)
249+
}
250+
}
251+
243252
// MARK: Helper Methods
244253
public extension ParseObject {
245254
/**

Sources/ParseSwift/Objects/ParseUser.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1084,7 +1084,7 @@ extension ParseUser {
10841084
if Parse.configuration.isRequiringCustomObjectIds && objectId == nil && !ignoringCustomObjectIdConfig {
10851085
throw ParseError(code: .missingObjectId, message: "objectId must not be nil")
10861086
}
1087-
if isSaved {
1087+
if try await isSaved() {
10881088
return try await replaceCommand() // MARK: Should be switched to "updateCommand" when server supports PATCH.
10891089
}
10901090
return try await createCommand()

Sources/ParseSwift/ParseConstants.swift

Lines changed: 1 addition & 1 deletion
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 = "5.4.2"
13+
static let version = "5.4.3"
1414
static let fileManagementDirectory = "parse/"
1515
static let fileManagementPrivateDocumentsDirectory = "Private Documents/"
1616
static let fileManagementLibraryDirectory = "Library/"

0 commit comments

Comments
 (0)