Skip to content

Commit 4542f89

Browse files
authored
Merge pull request #295 from zapcannon87/developer
fix(storage): sequence of array operation is not right
2 parents e5d9051 + 435f581 commit 4542f89

File tree

10 files changed

+36
-164
lines changed

10 files changed

+36
-164
lines changed

LeanCloudTests/LCFileTestCase.swift

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,11 +210,22 @@ class LCFileTestCase: BaseTestCase {
210210
XCTAssertEqual(shadowFile?.objectId, file.objectId)
211211
XCTAssertEqual(shadowFile?.createdAt, file.createdAt)
212212

213-
object.fileField = LCFile()
213+
object.fileField = LCFile(payload: .fileURL(fileURL: fileURL))
214214
XCTAssertNotNil(object.save().error)
215-
216215
object.fileField = LCFile(url: file.url!)
217216
XCTAssertTrue(object.save().isSuccess)
217+
218+
object.filesField = [
219+
LCFile(payload: .fileURL(fileURL: fileURL)),
220+
LCFile(payload: .fileURL(fileURL: fileURL))]
221+
XCTAssertNotNil(object.save().error)
222+
object.filesField = [LCFile(url: file.url!), LCFile(url: file.url!)]
223+
XCTAssertTrue(object.save().isSuccess)
224+
225+
object.fileMapField = ["file1": LCFile(payload: .fileURL(fileURL: fileURL))]
226+
XCTAssertNotNil(object.save().error)
227+
object.fileMapField = ["file1": LCFile(url: file.url!)]
228+
XCTAssertTrue(object.save().isSuccess)
218229
}
219230

220231
func testThumbnailURL() {

Sources/Foundation/Array.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public class LCArray: NSObject, LCValue, Collection, ExpressibleByArrayLiteral {
6363
}
6464

6565
public func copy(with zone: NSZone?) -> Any {
66-
return LCArray(self.value)
66+
return LCArray(self)
6767
}
6868

6969
public override func isEqual(_ object: Any?) -> Bool {

Sources/Foundation/Bool.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public final class LCBool: NSObject, LCValue, LCValueExtension, ExpressibleByBoo
4343
}
4444

4545
public func copy(with zone: NSZone?) -> Any {
46-
return LCBool(value)
46+
return LCBool(self)
4747
}
4848

4949
public override func isEqual(_ object: Any?) -> Bool {

Sources/Foundation/Data.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public final class LCData: NSObject, LCValue, LCValueExtension {
7575
}
7676

7777
public func copy(with zone: NSZone?) -> Any {
78-
return LCData((value as NSData).copy() as! Data)
78+
return LCData(self)
7979
}
8080

8181
public override func isEqual(_ object: Any?) -> Bool {

Sources/Foundation/Date.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public final class LCDate: NSObject, LCValue, LCValueExtension {
109109
}
110110

111111
public func copy(with zone: NSZone?) -> Any {
112-
return LCDate((value as NSDate).copy() as! Date)
112+
return LCDate(self)
113113
}
114114

115115
public override func isEqual(_ object: Any?) -> Bool {

Sources/Foundation/Dictionary.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public final class LCDictionary: NSObject, LCValue, LCValueExtension, Collection
8080
}
8181

8282
public func copy(with zone: NSZone?) -> Any {
83-
return LCDictionary(value)
83+
return LCDictionary(self)
8484
}
8585

8686
public override func isEqual(_ object: Any?) -> Bool {

Sources/Foundation/GeoPoint.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public final class LCGeoPoint: NSObject, LCValue, LCValueExtension {
8181
}
8282

8383
public func copy(with zone: NSZone?) -> Any {
84-
return LCGeoPoint(latitude: latitude, longitude: longitude)
84+
return LCGeoPoint(self)
8585
}
8686

8787
public override func isEqual(_ object: Any?) -> Bool {

Sources/Foundation/Number.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public final class LCNumber: NSObject, LCValue, LCValueExtension, ExpressibleByF
4747
}
4848

4949
public func copy(with zone: NSZone?) -> Any {
50-
return LCNumber(value)
50+
return LCNumber(self)
5151
}
5252

5353
public override func isEqual(_ object: Any?) -> Bool {

Sources/Foundation/Operation.swift

Lines changed: 15 additions & 154 deletions
Original file line numberDiff line numberDiff line change
@@ -464,152 +464,19 @@ class OperationReducer {
464464
- REMOVE
465465
*/
466466
class Array: OperationReducer {
467-
var operationTable: [Operation.Name:Operation] = [:]
468-
467+
var operationSequence: [Operation] = []
468+
469469
override class func validOperationNames() -> [Operation.Name] {
470470
return [.set, .delete, .add, .addUnique, .remove]
471471
}
472-
472+
473473
override func reduce(_ operation: Operation) throws {
474474
try super.validate(operation)
475-
476-
switch operation.name {
477-
case .set:
478-
reset()
479-
setOperation(operation)
480-
case .delete:
481-
reset()
482-
setOperation(operation)
483-
case .add:
484-
try removeObjects(operation, .remove)
485-
try removeObjects(operation, .addUnique)
486-
487-
if hasOperation(.set) || hasOperation(.delete) {
488-
try addObjects(operation, .set)
489-
} else {
490-
try addObjects(operation, .add)
491-
}
492-
case .addUnique:
493-
try removeObjects(operation, .add)
494-
try removeObjects(operation, .remove)
495-
496-
if hasOperation(.set) || hasOperation(.delete) {
497-
try addObjects(operation, .set, unique: true)
498-
} else {
499-
try addObjects(operation, .addUnique, unique: true)
500-
}
501-
case .remove:
502-
try removeObjects(operation, .set)
503-
try removeObjects(operation, .add)
504-
try removeObjects(operation, .addUnique)
505-
506-
try addObjects(operation, .remove, unique: true)
507-
default:
508-
break
509-
}
475+
self.operationSequence.append(operation)
510476
}
511-
477+
512478
override func operations() -> [Operation] {
513-
var operationTable = self.operationTable
514-
removeEmptyOperation(&operationTable, [.add, .addUnique, .remove])
515-
return Swift.Array(operationTable.values)
516-
}
517-
518-
/**
519-
Remove empty operations from operation table.
520-
521-
- parameter operationTable: The operation table.
522-
- parameter operationNames: A set of operation names that specify which operation should be removed from operation table if it is empty.
523-
*/
524-
func removeEmptyOperation(_ operationTable: inout [Operation.Name:Operation], _ operationNames:Set<Operation.Name>) {
525-
operationNames.forEach { (operationName) in
526-
if let operation = operationTable[operationName] {
527-
if !hasObjects(operation) {
528-
operationTable[operationName] = nil
529-
}
530-
}
531-
}
532-
}
533-
534-
/**
535-
Check whether an operation has objects.
536-
537-
- parameter operation: The operation.
538-
539-
- returns: true if operation has objects, false otherwise.
540-
*/
541-
func hasObjects(_ operation: Operation) -> Bool {
542-
if let array = operation.value as? LCArray {
543-
return !array.value.isEmpty
544-
} else {
545-
return false
546-
}
547-
}
548-
549-
/**
550-
Check whether an operation existed for given operation name.
551-
552-
- parameter name: The operation name.
553-
554-
- returns: true if operation existed for operation name, false otherwise.
555-
*/
556-
func hasOperation(_ name: Operation.Name) -> Bool {
557-
return operationTable[name] != nil
558-
}
559-
560-
/**
561-
Remove objects from operation specified by operation name.
562-
563-
- parameter operation: The operation that contains objects to be removed.
564-
- parameter operationName: The operation name that specifies operation from which the objects will be removed.
565-
*/
566-
func removeObjects(_ operation: Operation, _ operationName: Operation.Name) throws {
567-
guard let rhs = operation.value as? LCArray else {
568-
return
569-
}
570-
guard let lhs = operationTable[operationName]?.value as? LCArray else {
571-
return
572-
}
573-
574-
let operation = try Operation(name: operation.name, key: operation.key, value: try lhs.differ(rhs))
575-
576-
setOperation(operation)
577-
}
578-
579-
/**
580-
Add objects in an operation from operation specified by operation name.
581-
582-
- parameter operation: The operation that contains objects to be removed.
583-
- parameter operationName: The operation name that specifies operation from which the objects will be removed.
584-
*/
585-
func addObjects(_ operation: Operation, _ operationName: Operation.Name, unique: Bool = false) throws {
586-
guard var value = operation.value else {
587-
return
588-
}
589-
590-
if let baseValue = operationTable[operationName]?.value as? LCArray {
591-
value = try baseValue.concatenate(value, unique: unique)
592-
}
593-
594-
let operation = try Operation(name: operationName, key: operation.key, value: value)
595-
596-
setOperation(operation)
597-
}
598-
599-
/**
600-
Set operation to operation table.
601-
602-
- parameter operation: The operation to set.
603-
*/
604-
func setOperation(_ operation: Operation) {
605-
self.operationTable[operation.name] = operation
606-
}
607-
608-
/**
609-
Reset operation table.
610-
*/
611-
func reset() {
612-
self.operationTable = [:]
479+
return self.operationSequence
613480
}
614481
}
615482

@@ -625,21 +492,15 @@ class OperationReducer {
625492
override class func validOperationNames() -> [Operation.Name] {
626493
return [.addRelation, .removeRelation]
627494
}
495+
}
496+
}
628497

629-
override func reduce(_ operation: Operation) throws {
630-
try super.validate(operation)
631-
632-
switch operation.name {
633-
case .addRelation:
634-
try removeObjects(operation, .removeRelation)
635-
try addObjects(operation, .addRelation)
636-
case .removeRelation:
637-
try removeObjects(operation, .addRelation)
638-
try addObjects(operation, .removeRelation, unique: true)
639-
default:
640-
break
641-
}
642-
}
643-
/* Stub class. */
498+
private extension LCError {
499+
500+
static func malformedKey(_ key: String) -> LCError {
501+
return LCError(
502+
code: .malformedData,
503+
reason: "Malformed key.",
504+
userInfo: ["key": key])
644505
}
645506
}

Sources/Foundation/String.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public final class LCString: NSObject, LCValue, LCValueExtension, ExpressibleByS
5454
}
5555

5656
public func copy(with zone: NSZone?) -> Any {
57-
return LCString(value)
57+
return LCString(self)
5858
}
5959

6060
public override func isEqual(_ object: Any?) -> Bool {

0 commit comments

Comments
 (0)