Skip to content

Commit 63c3699

Browse files
committed
add test for list
1 parent 4a7ea8e commit 63c3699

File tree

7 files changed

+263
-94
lines changed

7 files changed

+263
-94
lines changed

Podfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@ target 'Sample' do
1818
pod 'Firebase'
1919
pod 'Firebase/Firestore'
2020
pod 'Firebase/Storage'
21-
pod 'Pring', :path => './'
21+
#pod 'Pring', :path => './'
2222

2323
end

Pring.xcodeproj/project.pbxproj

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,23 @@
7171
12BB91C121C6457900BD8166 /* OrderItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = 12BB91C021C6457900BD8166 /* OrderItem.swift */; };
7272
12BB91C321C647BA00BD8166 /* List.swift in Sources */ = {isa = PBXBuildFile; fileRef = 12BB91BC21C6267A00BD8166 /* List.swift */; };
7373
12BB91C421C6497700BD8166 /* GoogleService-Info.plist in Resources */ = {isa = PBXBuildFile; fileRef = 122988682109D4A900068424 /* GoogleService-Info.plist */; };
74+
12BB91C521C68D4000BD8166 /* DataSource.swift in Sources */ = {isa = PBXBuildFile; fileRef = 12AF830E1F8CAEEC00561159 /* DataSource.swift */; };
75+
12BB91C621C68D4000BD8166 /* Query.swift in Sources */ = {isa = PBXBuildFile; fileRef = 12C41E771FB31F41002B1526 /* Query.swift */; };
76+
12BB91C721C68D4000BD8166 /* DataType.swift in Sources */ = {isa = PBXBuildFile; fileRef = 12AF83101F8CAEEC00561159 /* DataType.swift */; };
77+
12BB91C821C68D4000BD8166 /* StorageLinkable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 12F84CBB1FB054CD00D889BE /* StorageLinkable.swift */; };
78+
12BB91C921C68D4000BD8166 /* Batchable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 120A178F1FC58FC3002767F6 /* Batchable.swift */; };
79+
12BB91CA21C68D4000BD8166 /* Reference.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1288E2211FCD203000E72594 /* Reference.swift */; };
80+
12BB91CB21C68D4000BD8166 /* Relation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 12AFC0212045912600E19B6D /* Relation.swift */; };
81+
12BB91CC21C68D4000BD8166 /* Document.swift in Sources */ = {isa = PBXBuildFile; fileRef = 12AF83111F8CAEEC00561159 /* Document.swift */; };
82+
12BB91CD21C68D4000BD8166 /* AnySubCollection.swift in Sources */ = {isa = PBXBuildFile; fileRef = 12CF86C1200C65300068BF70 /* AnySubCollection.swift */; };
83+
12BB91CE21C68D4000BD8166 /* File.swift in Sources */ = {isa = PBXBuildFile; fileRef = 12AF83121F8CAEEC00561159 /* File.swift */; };
84+
12BB91CF21C68D4000BD8166 /* Object.swift in Sources */ = {isa = PBXBuildFile; fileRef = 12AF83131F8CAEEC00561159 /* Object.swift */; };
85+
12BB91D021C68D4000BD8166 /* List.swift in Sources */ = {isa = PBXBuildFile; fileRef = 12BB91BC21C6267A00BD8166 /* List.swift */; };
86+
12BB91D121C68D4000BD8166 /* SubCollection.swift in Sources */ = {isa = PBXBuildFile; fileRef = 12AF83161F8CAEED00561159 /* SubCollection.swift */; };
87+
12BB91D221C68D4000BD8166 /* NestedCollection.swift in Sources */ = {isa = PBXBuildFile; fileRef = 12F4F5D3201845F5001D635E /* NestedCollection.swift */; };
88+
12BB91D321C68D4000BD8166 /* ReferenceCollection.swift in Sources */ = {isa = PBXBuildFile; fileRef = 12F4F5D5201846C3001D635E /* ReferenceCollection.swift */; };
89+
12BB91D421C68D4000BD8166 /* Disposer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 12AF830F1F8CAEEC00561159 /* Disposer.swift */; };
90+
12BB91D621C76DA900BD8166 /* ListTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 12BB91D521C76DA900BD8166 /* ListTests.swift */; };
7491
12BD88521FD165D80087A1C4 /* ReferenceItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = 12BD88511FD165D80087A1C4 /* ReferenceItem.swift */; };
7592
12C41E761FB30FEE002B1526 /* DataSourceItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = 12C41E751FB30FEE002B1526 /* DataSourceItem.swift */; };
7693
12C41E781FB31F41002B1526 /* Query.swift in Sources */ = {isa = PBXBuildFile; fileRef = 12C41E771FB31F41002B1526 /* Query.swift */; };
@@ -152,6 +169,7 @@
152169
12BB91BC21C6267A00BD8166 /* List.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = List.swift; sourceTree = "<group>"; };
153170
12BB91BE21C6457100BD8166 /* Order.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Order.swift; sourceTree = "<group>"; };
154171
12BB91C021C6457900BD8166 /* OrderItem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OrderItem.swift; sourceTree = "<group>"; };
172+
12BB91D521C76DA900BD8166 /* ListTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ListTests.swift; sourceTree = "<group>"; };
155173
12BD88511FD165D80087A1C4 /* ReferenceItem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ReferenceItem.swift; sourceTree = "<group>"; };
156174
12C41E751FB30FEE002B1526 /* DataSourceItem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DataSourceItem.swift; sourceTree = "<group>"; };
157175
12C41E771FB31F41002B1526 /* Query.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Query.swift; sourceTree = "<group>"; };
@@ -272,6 +290,7 @@
272290
12F31A6D21AAE28E00E4E63E /* TypeTests.swift */,
273291
124C323521B8043D001629C9 /* PackNestTests.swift */,
274292
124C323721B80450001629C9 /* PackReferenceTests.swift */,
293+
12BB91D521C76DA900BD8166 /* ListTests.swift */,
275294
1224CB8B201B9C61002DA245 /* SubCollection */,
276295
12AF83201F8CAF6E00561159 /* TestDocument.swift */,
277296
1266EB2020215664005F5F41 /* CustomDocument.swift */,
@@ -725,6 +744,7 @@
725744
123F0A1020A2D95400C0EAC1 /* Relation.swift in Sources */,
726745
123F0A1120A2D95400C0EAC1 /* Document.swift in Sources */,
727746
1224CB8A201B9B42002DA245 /* SubColletionTests.swift in Sources */,
747+
12BB91D621C76DA900BD8166 /* ListTests.swift in Sources */,
728748
123F0A0D20A2D95400C0EAC1 /* StorageLinkable.swift in Sources */,
729749
123F0A0C20A2D95400C0EAC1 /* DataType.swift in Sources */,
730750
12C41E761FB30FEE002B1526 /* DataSourceItem.swift in Sources */,
@@ -753,23 +773,39 @@
753773
isa = PBXSourcesBuildPhase;
754774
buildActionMask = 2147483647;
755775
files = (
776+
12BB91D221C68D4000BD8166 /* NestedCollection.swift in Sources */,
777+
12BB91C621C68D4000BD8166 /* Query.swift in Sources */,
778+
12BB91CB21C68D4000BD8166 /* Relation.swift in Sources */,
756779
1288E2291FCE9F2900E72594 /* Media.swift in Sources */,
757780
1288E2281FCE928400E72594 /* Photo.swift in Sources */,
758781
12027CB21FF4BDA800912630 /* FileControlViewController.swift in Sources */,
759782
1281D2AB204E955A00F5A89A /* MediaDataSourceViewController.swift in Sources */,
783+
12BB91C921C68D4000BD8166 /* Batchable.swift in Sources */,
760784
12AF83331F8CD2F400561159 /* ViewController.swift in Sources */,
761785
12E6F5A71FCED42D00347F4C /* Group.swift in Sources */,
786+
12BB91CE21C68D4000BD8166 /* File.swift in Sources */,
787+
12BB91CF21C68D4000BD8166 /* Object.swift in Sources */,
788+
12BB91C521C68D4000BD8166 /* DataSource.swift in Sources */,
789+
12BB91CA21C68D4000BD8166 /* Reference.swift in Sources */,
762790
12BB91BF21C6457100BD8166 /* Order.swift in Sources */,
763791
12AF83311F8CD2F400561159 /* AppDelegate.swift in Sources */,
764792
129E36921FA34F3200336C4D /* Item.swift in Sources */,
793+
12BB91C821C68D4000BD8166 /* StorageLinkable.swift in Sources */,
765794
1281B0F31FE11A6500B8FCEE /* Transcript.swift in Sources */,
766795
1249ABED1FB45DB400E78EA6 /* DataSourceViewController.swift in Sources */,
796+
12BB91C721C68D4000BD8166 /* DataType.swift in Sources */,
767797
12BB91C121C6457900BD8166 /* OrderItem.swift in Sources */,
798+
12BB91CD21C68D4000BD8166 /* AnySubCollection.swift in Sources */,
768799
1281D2AE204E9A9F00F5A89A /* MediaDataSourceViewCell.swift in Sources */,
800+
12BB91D021C68D4000BD8166 /* List.swift in Sources */,
769801
126080421FB991BD001B5DDA /* Collecor.swift in Sources */,
770802
1249ABEF1FB45ED800E78EA6 /* DataSourceViewCell.swift in Sources */,
803+
12BB91D321C68D4000BD8166 /* ReferenceCollection.swift in Sources */,
804+
12BB91D421C68D4000BD8166 /* Disposer.swift in Sources */,
805+
12BB91CC21C68D4000BD8166 /* Document.swift in Sources */,
771806
1288E22B1FCEA46A00E72594 /* Video.swift in Sources */,
772807
12FFEA172153A26E0084F414 /* MemoryLeakViewController.swift in Sources */,
808+
12BB91D121C68D4000BD8166 /* SubCollection.swift in Sources */,
773809
12AF83511F8CD4A400561159 /* User.swift in Sources */,
774810
);
775811
runOnlyForDeploymentPostprocessing = 0;

Pring/DataType.swift

Lines changed: 41 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,47 @@ public enum DataType {
191191

192192
DataType.verify(value: value)
193193

194+
if value is AnySubCollection {
195+
let collection: AnySubCollection = value as! AnySubCollection
196+
if let value: [String: Any] = data[key] as? [String: Any] {
197+
self = .collection(key, value, collection)
198+
return
199+
}
200+
} else if value is AnyList {
201+
if let object: [String: Any] = data[key] as? [String: Any] {
202+
let list: AnyList = value as! AnyList
203+
list.setValue(object)
204+
self = .list(key, object, list)
205+
return
206+
}
207+
} else if value is AnyReference {
208+
if let documentReference: DocumentReference = data[key] as? DocumentReference {
209+
var reference: AnyReference = value as! AnyReference
210+
reference.documentReference = documentReference
211+
self = .reference(key, documentReference, reference)
212+
return
213+
}
214+
} else if value is AnyRelation {
215+
if let id: String = data[key] as? String {
216+
var relation: AnyRelation = value as! AnyRelation
217+
relation.id = id
218+
self = .relation(key, id, relation)
219+
return
220+
}
221+
} else if value is Object {
222+
if let rawValue: [String: Any] = data[key] as? [String: Any] {
223+
self = .document(key, rawValue, nil)
224+
return
225+
}
226+
} else if value is [String: Any] {
227+
if let value: [String: Any] = data[key] as? [String: Any] {
228+
self = .dictionary(key, value, value)
229+
return
230+
}
231+
} else {
232+
self = .unknown
233+
}
234+
194235
if subjectType == Bool.self {
195236
if let value: Bool = data[key] as? Bool {
196237
self = .bool(key, Bool(value), Bool(value))
@@ -331,45 +372,6 @@ public enum DataType {
331372
}
332373
}
333374

334-
if value is AnySubCollection {
335-
let collection: AnySubCollection = value as! AnySubCollection
336-
if let value: [String: Any] = data[key] as? [String: Any] {
337-
self = .collection(key, value, collection)
338-
return
339-
}
340-
} else if value is AnyList {
341-
if let object: [String: Any] = data[key] as? [String: Any] {
342-
let list: AnyList = value as! AnyList
343-
self = .list(key, object, list)
344-
return
345-
}
346-
} else if value is AnyReference {
347-
if let documentReference: DocumentReference = data[key] as? DocumentReference {
348-
var reference: AnyReference = value as! AnyReference
349-
reference.documentReference = documentReference
350-
self = .reference(key, documentReference, reference)
351-
return
352-
}
353-
} else if value is AnyRelation {
354-
if let id: String = data[key] as? String {
355-
var relation: AnyRelation = value as! AnyRelation
356-
relation.id = id
357-
self = .relation(key, id, relation)
358-
return
359-
}
360-
} else if value is Object {
361-
if let rawValue: [String: Any] = data[key] as? [String: Any] {
362-
self = .document(key, rawValue, nil)
363-
return
364-
}
365-
} else if value is [String: Any] {
366-
if let value: [String: Any] = data[key] as? [String: Any] {
367-
self = .dictionary(key, value, value)
368-
return
369-
}
370-
} else {
371-
self = .unknown
372-
}
373375
self = .unknown
374376
}
375377

Pring/List.swift

Lines changed: 63 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ public protocol AnyList: class {
1717

1818
var value: [String: Any] { get }
1919

20-
func setValue(_ value: [String: Any], forKey key: String)
20+
var updateValue: [String: Any] { get }
21+
22+
func setValue(_ value: [String: Any])
2123

2224
func setParent(_ object: Object, forKey key: String)
2325
}
@@ -30,12 +32,16 @@ public extension AnyList {
3032
}
3133
}
3234

33-
public final class List<T: Document>: AnyList, ExpressibleByArrayLiteral {
35+
public final class List<T: Document>: AnyList, Collection, ExpressibleByArrayLiteral {
3436

3537
fileprivate var _storage: [String: T] = [:]
3638

3739
public typealias ArrayLiteralElement = T
3840

41+
public typealias Element = T
42+
43+
public typealias Index = Int
44+
3945
public init(arrayLiteral elements: T...) {
4046
var storage: [String: T] = [:]
4147
elements.forEach { (doc) in
@@ -56,7 +62,17 @@ public final class List<T: Document>: AnyList, ExpressibleByArrayLiteral {
5662
return value
5763
}
5864

59-
public func setValue(_ value: [String : Any], forKey key: String) {
65+
public var updateValue: [String: Any] {
66+
var updateValue: [String: Any] = [:]
67+
self.forEach { (document) in
68+
if !document.updateValue.isEmpty {
69+
updateValue[document.id] = document.updateValue
70+
}
71+
}
72+
return updateValue
73+
}
74+
75+
public func setValue(_ value: [String : Any]) {
6076
var storage: [String: T] = [:]
6177
value.forEach { (id, data) in
6278
if let data: [String: Any] = data as? [String: Any] {
@@ -71,46 +87,58 @@ public final class List<T: Document>: AnyList, ExpressibleByArrayLiteral {
7187
self.key = key
7288
}
7389

74-
public subscript(id: String) -> T? {
75-
return self._storage[id]
76-
}
77-
7890
public func append(_ object: T) {
7991
self._storage[object.id] = object
92+
if let parent: Object = self.parent, let key: String = self.key, parent.isSaved {
93+
parent.updateValue[key] = [object.id: object.rawValue]
94+
}
8095
}
8196

8297
public func remove(_ object: T) {
8398
self._storage.removeValue(forKey: object.id)
99+
if let parent: Object = self.parent, let key: String = self.key, parent.isSaved {
100+
parent.updateValue[key] = [object.id: FieldValue.delete()]
101+
}
102+
}
103+
104+
public var keys: [String] {
105+
return self._storage.keys.map { $0 }.sorted()
84106
}
85107
}
86108

87-
extension Array where Element == Document {
109+
extension List {
88110

89-
}
111+
public var count: Int {
112+
return self._storage.count
113+
}
90114

91-
//extension List: StorageLinkable {
92-
//
93-
// public func shouldUploadFiles(_ id: String) -> Bool {
94-
// for (_, object) in self._storage.enumerated() {
95-
// if object.value.shouldUploadFiles(id) {
96-
// return true
97-
// }
98-
// }
99-
// return false
100-
// }
101-
//
102-
// public func saveFiles(_ id: String, container: UploadContainer? = nil, block: ((Error?) -> Void)?) -> [String: StorageUploadTask] {
103-
// let uploadContainer: UploadContainer = container ?? UploadContainer()
104-
// for (_, object) in self._storage.enumerated() {
105-
// object.value.saveFiles(id, container: uploadContainer, block: nil)
106-
// }
107-
// return uploadContainer.tasks
108-
// }
109-
//
110-
// public func deleteFiles(_ id: String, container: DeleteContainer? = nil, block: ((Error?) -> Void)? = nil) {
111-
// let deleteContainer: DeleteContainer = container ?? DeleteContainer()
112-
// for (_, object) in self._storage.enumerated() {
113-
// object.value.deleteFiles(id, container: deleteContainer, block: nil)
114-
// }
115-
// }
116-
//}
115+
public var isEmpty: Bool {
116+
return self._storage.isEmpty
117+
}
118+
119+
public var first: T? {
120+
if self.isEmpty { return nil }
121+
return self._storage[self.keys[self.startIndex]]
122+
}
123+
124+
public var startIndex: Int {
125+
return 0
126+
}
127+
128+
public var endIndex: Int {
129+
return self.keys.endIndex
130+
}
131+
132+
public subscript(position: String) -> T {
133+
return self._storage[position]!
134+
}
135+
136+
public subscript(position: Int) -> T {
137+
let position: String = self.keys[position]
138+
return self._storage[position]!
139+
}
140+
141+
public func index(after i: Int) -> Int {
142+
return self.keys.index(after: i)
143+
}
144+
}

Pring/Object.swift

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ open class Object: NSObject, Document {
109109
switch DataType(key: child.label!, value: child.value) {
110110
case .file (let key, _, let file): file.setParent(self, forKey: key)
111111
case .collection (let key, _, let collection): collection.setParent(self, forKey: key)
112+
case .list (let key, _, let list): list.setParent(self, forKey: key)
112113
case .reference (let key, _, let reference): reference.setParent(self, forKey: key)
113114
case .relation (let key, _, let relation): relation.setParent(self, forKey: key)
114115
default: break
@@ -498,15 +499,17 @@ open class Object: NSObject, Document {
498499
}
499500
})
500501
case .update:
501-
if !updateValue.isEmpty {
502-
updateValue[(\Object.updatedAt)._kvcKeyPathString!] = FieldValue.serverTimestamp()
503-
batch.updateData(updateValue, forDocument: self.reference)
504-
}
502+
var updateValue: [String: Any] = self.updateValue
505503
self._properties.forEach({ (key, value) in
506504
if let value = value {
507505
switch DataType(key: key, value: value) {
508506
case .collection(_, _, let collection):
509507
collection.pack(.update, batch: batch)
508+
case .list(let key, _, let list):
509+
let listUpdateValue: [String: Any] = list.updateValue
510+
if !listUpdateValue.isEmpty {
511+
updateValue[key] = list.updateValue
512+
}
510513
case .reference(_, _, let reference):
511514
if reference is Batchable {
512515
(reference as! Batchable).pack(.update, batch: batch)
@@ -519,6 +522,11 @@ open class Object: NSObject, Document {
519522
}
520523
}
521524
})
525+
print("!!!!", updateValue)
526+
if !updateValue.isEmpty {
527+
updateValue[(\Object.updatedAt)._kvcKeyPathString!] = FieldValue.serverTimestamp()
528+
batch.setData(updateValue, forDocument: self.reference, merge: true)
529+
}
522530
case .delete:
523531
batch.deleteDocument(self.reference)
524532
}

0 commit comments

Comments
 (0)