18
18
19
19
#include < utility>
20
20
21
- #include " Firestore/core/src/firebase/firestore/util/autoid.h"
22
-
23
21
#import " Firestore/Source/API/FIRDocumentReference+Internal.h"
24
22
#import " Firestore/Source/API/FIRFirestore+Internal.h"
25
23
#import " Firestore/Source/API/FIRQuery+Internal.h"
24
+ #import " Firestore/Source/API/FSTUserDataConverter.h"
26
25
#import " Firestore/Source/Core/FSTQuery.h"
27
26
27
+ #include " Firestore/core/src/firebase/firestore/api/collection_reference.h"
28
28
#include " Firestore/core/src/firebase/firestore/api/input_validation.h"
29
- #include " Firestore/core/src/firebase/firestore/model/document_key.h"
30
29
#include " Firestore/core/src/firebase/firestore/model/resource_path.h"
31
- #include " Firestore/core/src/firebase/firestore/util/hashing .h"
30
+ #include " Firestore/core/src/firebase/firestore/util/error_apple .h"
32
31
#include " Firestore/core/src/firebase/firestore/util/string_apple.h"
33
32
34
33
namespace util = firebase::firestore::util;
34
+ using firebase::firestore::api::CollectionReference;
35
+ using firebase::firestore::api::DocumentReference;
35
36
using firebase::firestore::api::ThrowInvalidArgument;
36
- using firebase::firestore::model::DocumentKey ;
37
+ using firebase::firestore::core::ParsedSetData ;
37
38
using firebase::firestore::model::ResourcePath;
38
- using firebase::firestore::util::CreateAutoId;
39
39
40
40
NS_ASSUME_NONNULL_BEGIN
41
41
42
- @interface FIRCollectionReference ()
43
- - (instancetype )initWithPath : (const ResourcePath &)path
44
- firestore : (FIRFirestore *)firestore NS_DESIGNATED_INITIALIZER;
45
-
46
- // Mark the super class designated initializer unavailable.
47
- - (instancetype )initWithQuery : (api::Query &&)query NS_UNAVAILABLE;
48
- @end
49
-
50
- @implementation FIRCollectionReference (Internal)
51
- + (instancetype )referenceWithPath : (const ResourcePath &)path firestore : (FIRFirestore *)firestore {
52
- return [[FIRCollectionReference alloc ] initWithPath: path firestore: firestore];
53
- }
54
- @end
55
-
56
42
@implementation FIRCollectionReference
57
43
58
- - (instancetype )initWithPath : (const ResourcePath &)path firestore : (FIRFirestore *)firestore {
59
- if (path.size () % 2 != 1 ) {
60
- ThrowInvalidArgument (" Invalid collection reference. Collection references must have an odd "
61
- " number of segments, but %s has %s" ,
62
- path.CanonicalString (), path.size ());
63
- }
44
+ - (instancetype )initWithReference : (CollectionReference &&)reference {
45
+ return [super initWithQuery: std: :move (reference)];
46
+ }
64
47
65
- api::Query query ([FSTQuery queryWithPath: path], firestore.wrapped );
66
- self = [super initWithQuery: std: :move (query)];
67
- return self;
48
+ - (instancetype )initWithPath : (ResourcePath)path
49
+ firestore : (std::shared_ptr<api::Firestore>)firestore {
50
+ CollectionReference ref (std::move (path), std::move (firestore));
51
+ return [self initWithReference: std: :move (ref)];
68
52
}
69
53
70
54
// Override the designated initializer from the super class.
@@ -80,43 +64,47 @@ - (BOOL)isEqual:(nullable id)other {
80
64
return [self isEqualToReference: other];
81
65
}
82
66
83
- - (BOOL )isEqualToReference : (nullable FIRCollectionReference *)reference {
84
- if (self == reference ) return YES ;
85
- if (reference == nil ) return NO ;
86
- return [ self .firestore isEqual: reference.firestore] && [ self .query isEqual: reference.query] ;
67
+ - (BOOL )isEqualToReference : (nullable FIRCollectionReference *)otherReference {
68
+ if (self == otherReference ) return YES ;
69
+ if (otherReference == nil ) return NO ;
70
+ return self.reference == otherReference. reference ;
87
71
}
88
72
89
73
- (NSUInteger )hash {
90
- return util::Hash (self.firestore , self.query );
74
+ return self.reference .Hash ();
75
+ }
76
+
77
+ - (const CollectionReference &)reference {
78
+ // TODO(wilhuff): Use some alternate method for doing this.
79
+ //
80
+ // Casting from Query& to CollectionReference& when the value is actually a
81
+ // Query violates aliasing rules and is technically undefined behavior.
82
+ // Nevertheless this works on Clang so this is good enough for now.
83
+ return static_cast <const CollectionReference &>(self.apiQuery );
91
84
}
92
85
93
86
- (NSString *)collectionID {
94
- return util::WrapNSString (self.query . path . last_segment ());
87
+ return util::WrapNSString (self.reference . collection_id ());
95
88
}
96
89
97
90
- (FIRDocumentReference *_Nullable)parent {
98
- const ResourcePath parentPath = self.query . path . PopLast ();
99
- if (parentPath. empty () ) {
91
+ absl::optional<DocumentReference> parent = self.reference . parent ();
92
+ if (!parent ) {
100
93
return nil ;
101
- } else {
102
- DocumentKey key{parentPath};
103
- return [[FIRDocumentReference alloc ] initWithKey: std: :move (key)
104
- firestore: self .firestore.wrapped];
105
94
}
95
+ return [[FIRDocumentReference alloc ] initWithReference: std: :move (*parent)];
106
96
}
107
97
108
98
- (NSString *)path {
109
- return util::WrapNSString (self.query .path . CanonicalString ());
99
+ return util::WrapNSString (self.reference .path ());
110
100
}
111
101
112
102
- (FIRDocumentReference *)documentWithPath : (NSString *)documentPath {
113
103
if (!documentPath) {
114
104
ThrowInvalidArgument (" Document path cannot be nil." );
115
105
}
116
- const ResourcePath subPath = ResourcePath::FromString (util::MakeString (documentPath));
117
- ResourcePath path = self.query .path .Append (subPath);
118
- return [[FIRDocumentReference alloc ] initWithPath: std: :move (path)
119
- firestore: self .firestore.wrapped];
106
+ DocumentReference child = self.reference .Document (util::MakeString (documentPath));
107
+ return [[FIRDocumentReference alloc ] initWithReference: std: :move (child)];
120
108
}
121
109
122
110
- (FIRDocumentReference *)addDocumentWithData : (NSDictionary <NSString *, id> *)data {
@@ -126,14 +114,14 @@ - (FIRDocumentReference *)addDocumentWithData:(NSDictionary<NSString *, id> *)da
126
114
- (FIRDocumentReference *)addDocumentWithData : (NSDictionary <NSString *, id> *)data
127
115
completion :
128
116
(nullable void (^)(NSError *_Nullable error))completion {
129
- FIRDocumentReference *docRef = [self documentWithAutoID ];
130
- [docRef setData: data completion: completion];
131
- return docRef;
117
+ ParsedSetData parsed = [self .firestore.dataConverter parsedSetData: data];
118
+ DocumentReference docRef =
119
+ self.reference .AddDocument (std::move (parsed), util::MakeCallback (completion));
120
+ return [[FIRDocumentReference alloc ] initWithReference: std: :move (docRef)];
132
121
}
133
122
134
123
- (FIRDocumentReference *)documentWithAutoID {
135
- DocumentKey key{self.query .path .Append (CreateAutoId ())};
136
- return [[FIRDocumentReference alloc ] initWithKey: std: :move (key) firestore: self .firestore.wrapped];
124
+ return [[FIRDocumentReference alloc ] initWithReference: self .reference.Document ()];
137
125
}
138
126
139
127
@end
0 commit comments