Skip to content

Commit 0d9cf1e

Browse files
committed
Update commented code on child batching
1 parent 04bc020 commit 0d9cf1e

File tree

4 files changed

+99
-62
lines changed

4 files changed

+99
-62
lines changed

Sources/ParseSwift/API/API+Commands.swift

Lines changed: 83 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -341,49 +341,6 @@ internal extension API.Command {
341341
mapper: mapper)
342342
}
343343

344-
// MARK: Saving ParseObjects - Encodable
345-
static func saveCommand(object: T) throws -> API.Command<T, PointerType> where T: Encodable {
346-
guard let objectable = object as? Objectable else {
347-
throw ParseError(code: .unknownError, message: "Not able to cast to objectable. Not saving")
348-
}
349-
if objectable.isSaved {
350-
return try updateCommand(object: object)
351-
} else {
352-
return try createCommand(object: object)
353-
}
354-
}
355-
356-
// MARK: Saving ParseObjects - [Encodable] - private
357-
private static func createCommand(object: T) throws -> API.Command<T, PointerType> where T: Encodable {
358-
guard var objectable = object as? Objectable else {
359-
throw ParseError(code: .unknownError, message: "Not able to cast to objectable. Not saving")
360-
}
361-
let mapper = { (data: Data) -> PointerType in
362-
let baseObjectable = try ParseCoding.jsonDecoder().decode(BaseObjectable.self, from: data)
363-
objectable.objectId = baseObjectable.objectId
364-
return try objectable.toPointer()
365-
}
366-
return API.Command<T, PointerType>(method: .POST,
367-
path: objectable.endpoint,
368-
body: object,
369-
mapper: mapper)
370-
}
371-
372-
private static func updateCommand(object: T) throws -> API.Command<T, PointerType> where T: Encodable {
373-
guard var objectable = object as? Objectable else {
374-
throw ParseError(code: .unknownError, message: "Not able to cast to objectable. Not saving")
375-
}
376-
let mapper = { (data: Data) -> PointerType in
377-
let baseObjectable = try ParseCoding.jsonDecoder().decode(BaseObjectable.self, from: data)
378-
objectable.objectId = baseObjectable.objectId
379-
return try objectable.toPointer()
380-
}
381-
return API.Command<T, PointerType>(method: .PUT,
382-
path: objectable.endpoint,
383-
body: object,
384-
mapper: mapper)
385-
}
386-
387344
// MARK: Saving ParseObjects - Encodable
388345
static func saveCommand<T>(_ object: T) throws -> API.Command<T, PointerType> where T: Encodable {
389346
guard let objectable = object as? Objectable else {
@@ -544,26 +501,26 @@ extension API.Command where T: ParseObject {
544501

545502
//This has been disabled, looking into getting it working in the future.
546503
//It's only needed for sending batches of childObjects which currently isn't being used.
547-
504+
/*
548505
// MARK: Batch - Child Objects
549-
extension API.Command where T: ParseType {
506+
extension API.ChildCommand {
550507

551508
internal var data: Data? {
552509
guard let body = body else { return nil }
553510
return try? ParseCoding.jsonEncoder().encode(body)
554511
}
555512

556-
static func batch(commands: [API.Command<T, PointerType>],
557-
transaction: Bool) -> RESTBatchCommandTypeEncodable<T> {
558-
let commands = commands.compactMap { (command) -> API.Command<T, PointerType>? in
513+
static func batch(commands: [API.ChildCommand<PointerType>],
514+
transaction: Bool) -> RESTBatchCommandTypeEncodable<ParseType> {
515+
let commands = commands.compactMap { (command) -> API.ChildCommand<PointerType>? in
559516
let path = ParseConfiguration.mountPath + command.path.urlComponent
560517
guard let body = command.body else {
561518
return nil
562519
}
563-
return API.Command<T, PointerType>(method: command.method, path: .any(path),
520+
return API.ChildCommand<PointerType>(method: command.method, path: .any(path),
564521
body: body, mapper: command.mapper)
565522
}
566-
let bodies = commands.compactMap { (command) -> (body: T, command: API.Method)? in
523+
let bodies = commands.compactMap { (command) -> (body: ParseType, command: API.Method)? in
567524
guard let body = command.body else {
568525
return nil
569526
}
@@ -599,7 +556,7 @@ extension API.Command where T: ParseType {
599556
return RESTBatchCommandTypeEncodable<T>(method: .POST, path: .batch, body: batchCommand, mapper: mapper)
600557
}
601558
}
602-
559+
*/
603560
// MARK: API.NonParseBodyCommand
604561
internal extension API {
605562
struct NonParseBodyCommand<T, U>: Encodable where T: Encodable {
@@ -718,4 +675,78 @@ internal extension API.NonParseBodyCommand {
718675
}
719676
}
720677
}
721-
} // swiftlint:disable:this file_length
678+
}
679+
/*
680+
// MARK: API.Command
681+
internal extension API {
682+
struct ChildCommand<U>: ParseType {
683+
typealias ReturnType = U
684+
let method: API.Method
685+
let path: API.Endpoint
686+
let body: ParseType?
687+
let mapper: ((Data) throws -> U)
688+
let params: [String: String?]?
689+
690+
init(method: API.Method,
691+
path: API.Endpoint,
692+
params: [String: String]? = nil,
693+
body: ParseType? = nil,
694+
mapper: @escaping ((Data) throws -> U)) {
695+
self.method = method
696+
self.path = path
697+
self.body = body
698+
self.mapper = mapper
699+
self.params = params
700+
}
701+
}
702+
703+
enum CodingKeys: String, CodingKey {
704+
case method, body, path
705+
}
706+
}
707+
708+
internal extension API.ChildCommand {
709+
// MARK: Saving ParseObjects - Encodable
710+
static func saveCommand(_ object: ParseType) throws -> API.ChildCommand<PointerType> {
711+
guard let objectable = object as? Objectable else {
712+
throw ParseError(code: .unknownError, message: "Not able to cast to objectable. Not saving")
713+
}
714+
if objectable.isSaved {
715+
return try updateCommand(object)
716+
} else {
717+
return try createCommand(object)
718+
}
719+
}
720+
721+
// MARK: Saving ParseObjects - Encodable - private
722+
private static func createCommand(_ object: ParseType) throws -> API.ChildCommand<PointerType> {
723+
guard var objectable = object as? Objectable else {
724+
throw ParseError(code: .unknownError, message: "Not able to cast to objectable. Not saving")
725+
}
726+
let mapper = { (data: Data) -> PointerType in
727+
let baseObjectable = try ParseCoding.jsonDecoder().decode(BaseObjectable.self, from: data)
728+
objectable.objectId = baseObjectable.objectId
729+
return try objectable.toPointer()
730+
}
731+
return API.ChildCommand<PointerType>(method: .POST,
732+
path: objectable.endpoint,
733+
body: object,
734+
mapper: mapper)
735+
}
736+
737+
private static func updateCommand(_ object: ParseType) throws -> API.ChildCommand<PointerType> {
738+
guard var objectable = object as? Objectable else {
739+
throw ParseError(code: .unknownError, message: "Not able to cast to objectable. Not saving")
740+
}
741+
let mapper = { (data: Data) -> PointerType in
742+
let baseObjectable = try ParseCoding.jsonDecoder().decode(BaseObjectable.self, from: data)
743+
objectable.objectId = baseObjectable.objectId
744+
return try objectable.toPointer()
745+
}
746+
return API.ChildCommand<PointerType>(method: .PUT,
747+
path: objectable.endpoint,
748+
body: object,
749+
mapper: mapper)
750+
}
751+
}// swiftlint:disable:this file_length
752+
*/

Sources/ParseSwift/API/BatchUtils.swift

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ typealias RESTBatchCommandType<T> = API.Command<ParseObjectBatchCommand<T>, Pars
1616
typealias ParseObjectBatchCommandNoBody<T> = BatchCommandNoBody<NoBody, NoBody>
1717
typealias ParseObjectBatchResponseNoBody<NoBody> = [(Result<Void, ParseError>)]
1818
typealias RESTBatchCommandNoBodyType<T> = API.NonParseBodyCommand<ParseObjectBatchCommandNoBody<T>, ParseObjectBatchResponseNoBody<T>> where T: Encodable
19-
19+
/*
2020
typealias ParseObjectBatchCommandEncodable<T> = BatchCommand<T, PointerType> where T: ParseType
2121
typealias ParseObjectBatchResponseEncodable<U> = [(Result<PointerType, ParseError>)]
2222
// swiftlint:disable line_length
2323
typealias RESTBatchCommandTypeEncodable<T> = API.Command<ParseObjectBatchCommandEncodable<T>, ParseObjectBatchResponseEncodable<PointerType>> where T: ParseType
2424
// swiftlint:enable line_length
25-
25+
*/
2626
internal struct BatchCommand<T, U>: ParseType where T: ParseType {
2727
let requests: [API.Command<T, U>]
2828
var transaction: Bool
@@ -32,7 +32,12 @@ internal struct BatchCommandNoBody<T, U>: Encodable where T: Encodable {
3232
let requests: [API.NonParseBodyCommand<T, U>]
3333
var transaction: Bool
3434
}
35-
35+
/*
36+
internal struct BatchChildCommand<U>: ParseType {
37+
let requests: [API.ChildCommand<U>]
38+
var transaction: Bool
39+
}
40+
*/
3641
struct BatchUtils {
3742
static func splitArray<U>(_ array: [U], valuesPerSegment: Int) -> [[U]] {
3843
if array.count < valuesPerSegment {

Sources/ParseSwift/Objects/ParseObject.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,7 @@ public extension Sequence where Element: ParseObject {
477477
- returns: Returns a Result enum with the object if a save was successful or a `ParseError` if it failed.
478478
- throws: `ParseError`
479479
*/
480-
func saveAll(transaction: Bool = true,
480+
func saveAllParseTypes(transaction: Bool = true,
481481
options: API.Options = []) throws -> [(Result<PointerType, ParseError>)] {
482482
let commands = try map { try $0.saveCommand() }
483483
return try API.Command<Self.Element, PointerType>
@@ -754,14 +754,14 @@ internal extension ParseType {
754754
func saveCommand() throws -> API.Command<Self, PointerType> {
755755
try API.Command<Self, PointerType>.saveCommand(self)
756756
}
757-
/*
758-
static func saveAll(objects: [ParseType],
759-
transaction: Bool = true,
760-
options: API.Options = []) throws -> [(Result<PointerType, ParseError>)] {
757+
/*
758+
func saveAll(objects: [ParseType],
759+
transaction: Bool = true,
760+
options: API.Options = []) throws -> [(Result<PointerType, ParseError>)] {
761761
let commands = try objects.map {
762-
try API.Command<Self, PointerType>.saveCommand(object: $0)
762+
try API.ChildCommand<PointerType>.saveCommand($0)
763763
}
764-
return try API.Command<Self, PointerType>
764+
return try API.ChildCommand<PointerType>
765765
.batch(commands: commands,
766766
transaction: transaction)
767767
.execute(options: options,

Sources/ParseSwift/Types/Query.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@ public func containedBy <T>(key: String, array: [T]) -> QueryConstraint where T:
267267
$lt, $lte, $gt, and $gte operators.
268268
- parameter time: The reference time, e.g. "12 days ago".
269269
- returns: The same instance of `QueryConstraint` as the receiver.
270+
- warning: This only works with Parse Servers using mongoDB.
270271
*/
271272
public func relative(key: String, comparator: QueryConstraint.Comparator, time: String) -> QueryConstraint {
272273
QueryConstraint(key: key, value: [QueryConstraint.Comparator.relativeTime.rawValue: time], comparator: comparator)

0 commit comments

Comments
 (0)