29
29
#include " Firestore/core/src/firebase/firestore/model/snapshot_version.h"
30
30
#include " Firestore/core/src/firebase/firestore/nanopb/tag.h"
31
31
#include " Firestore/core/src/firebase/firestore/util/hard_assert.h"
32
+ #include " Firestore/core/src/firebase/firestore/util/string_format.h"
32
33
33
34
namespace firebase {
34
35
namespace firestore {
@@ -44,6 +45,7 @@ using nanopb::Reader;
44
45
using nanopb::Tag;
45
46
using nanopb::Writer;
46
47
using util::Status;
48
+ using util::StringFormat;
47
49
48
50
void LocalSerializer::EncodeMaybeDocument (
49
51
Writer* writer, const MaybeDocument& maybe_doc) const {
@@ -73,37 +75,24 @@ void LocalSerializer::EncodeMaybeDocument(
73
75
}
74
76
75
77
std::unique_ptr<MaybeDocument> LocalSerializer::DecodeMaybeDocument (
76
- Reader* reader) const {
77
- std::unique_ptr<MaybeDocument> result;
78
-
79
- while (reader->good ()) {
80
- switch (reader->ReadTag ()) {
81
- case firestore_client_MaybeDocument_document_tag:
82
- // TODO(rsgowman): If multiple 'document' values are found, we should
83
- // merge them (rather than using the last one.)
84
- result = reader->ReadNestedMessage <Document>(
85
- rpc_serializer_, &remote::Serializer::DecodeDocument);
86
- break ;
87
-
88
- case firestore_client_MaybeDocument_no_document_tag:
89
- // TODO(rsgowman): If multiple 'no_document' values are found, we should
90
- // merge them (rather than using the last one.)
91
- result = reader->ReadNestedMessage <NoDocument>(
92
- *this , &LocalSerializer::DecodeNoDocument);
93
- break ;
94
-
95
- default :
96
- reader->SkipUnknown ();
97
- }
98
- }
78
+ Reader* reader, const firestore_client_MaybeDocument& proto) const {
79
+ if (!reader->status ().ok ()) return nullptr ;
80
+
81
+ switch (proto.which_document_type ) {
82
+ case firestore_client_MaybeDocument_document_tag:
83
+ return rpc_serializer_.DecodeDocument (reader, proto.document );
84
+
85
+ case firestore_client_MaybeDocument_no_document_tag:
86
+ return DecodeNoDocument (reader, proto.no_document );
99
87
100
- if (!result) {
101
- reader->Fail (
102
- " Invalid MaybeDocument message: Neither 'no_document' nor 'document' "
103
- " fields set." );
104
- return nullptr ;
88
+ default :
89
+ reader->Fail (
90
+ " Invalid MaybeDocument message: Neither 'no_document' nor 'document' "
91
+ " fields set." );
92
+ return nullptr ;
105
93
}
106
- return result;
94
+
95
+ UNREACHABLE ();
107
96
}
108
97
109
98
void LocalSerializer::EncodeDocument (Writer* writer,
@@ -146,30 +135,16 @@ void LocalSerializer::EncodeNoDocument(Writer* writer,
146
135
}
147
136
148
137
std::unique_ptr<NoDocument> LocalSerializer::DecodeNoDocument (
149
- Reader* reader) const {
150
- std::string name;
151
- absl::optional<SnapshotVersion> version = SnapshotVersion::None ();
152
-
153
- while (reader->good ()) {
154
- switch (reader->ReadTag ()) {
155
- case firestore_client_NoDocument_name_tag:
156
- name = reader->ReadString ();
157
- break ;
158
-
159
- case firestore_client_NoDocument_read_time_tag:
160
- version = reader->ReadNestedMessage <SnapshotVersion>(
161
- rpc_serializer_.DecodeSnapshotVersion );
162
- break ;
163
-
164
- default :
165
- reader->SkipUnknown ();
166
- break ;
167
- }
168
- }
138
+ Reader* reader, const firestore_client_NoDocument& proto) const {
139
+ if (!reader->status ().ok ()) return nullptr ;
140
+
141
+ absl::optional<SnapshotVersion> version =
142
+ rpc_serializer_.DecodeSnapshotVersion (reader, proto.read_time );
169
143
170
144
if (!reader->status ().ok ()) return nullptr ;
171
- return absl::make_unique<NoDocument>(rpc_serializer_.DecodeKey (name),
172
- *std::move (version));
145
+ return absl::make_unique<NoDocument>(
146
+ rpc_serializer_.DecodeKey (rpc_serializer_.DecodeString (proto.name )),
147
+ *std::move (version));
173
148
}
174
149
175
150
void LocalSerializer::EncodeQueryData (Writer* writer,
@@ -207,45 +182,28 @@ void LocalSerializer::EncodeQueryData(Writer* writer,
207
182
}
208
183
209
184
absl::optional<QueryData> LocalSerializer::DecodeQueryData (
210
- Reader* reader) const {
211
- model::TargetId target_id = 0 ;
212
- absl::optional<SnapshotVersion> version = SnapshotVersion::None ();
213
- std::vector<uint8_t > resume_token;
185
+ Reader* reader, const firestore_client_Target& proto) const {
186
+ if (!reader->status ().ok ()) return absl::nullopt;
187
+
188
+ model::TargetId target_id = proto.target_id ;
189
+ absl::optional<SnapshotVersion> version =
190
+ rpc_serializer_.DecodeSnapshotVersion (reader, proto.snapshot_version );
191
+ std::vector<uint8_t > resume_token =
192
+ rpc_serializer_.DecodeBytes (proto.resume_token );
214
193
absl::optional<Query> query = Query::Invalid ();
215
194
216
- while (reader->good ()) {
217
- switch (reader->ReadTag ()) {
218
- case firestore_client_Target_target_id_tag:
219
- // TODO(rsgowman): How to handle truncation of integer types?
220
- target_id = static_cast <model::TargetId>(reader->ReadInteger ());
221
- break ;
222
-
223
- case firestore_client_Target_snapshot_version_tag:
224
- version = reader->ReadNestedMessage <SnapshotVersion>(
225
- rpc_serializer_.DecodeSnapshotVersion );
226
- break ;
227
-
228
- case firestore_client_Target_resume_token_tag:
229
- resume_token = reader->ReadBytes ();
230
- break ;
231
-
232
- case firestore_client_Target_query_tag:
233
- // TODO(rsgowman): Clear 'documents' field (since query and documents
234
- // are part of a 'oneof').
235
- query =
236
- reader->ReadNestedMessage <Query>(rpc_serializer_.DecodeQueryTarget );
237
- break ;
238
-
239
- case firestore_client_Target_documents_tag:
240
- // Clear 'query' field (since query and documents are part of a 'oneof')
241
- query = Query::Invalid ();
242
- // TODO(rsgowman): Implement.
243
- abort ();
244
-
245
- default :
246
- reader->SkipUnknown ();
247
- break ;
248
- }
195
+ switch (proto.which_target_type ) {
196
+ case firestore_client_Target_query_tag:
197
+ query = rpc_serializer_.DecodeQueryTarget (reader, proto.query );
198
+ break ;
199
+
200
+ case firestore_client_Target_documents_tag:
201
+ // TODO(rsgowman): Implement.
202
+ abort ();
203
+
204
+ default :
205
+ reader->Fail (
206
+ StringFormat (" Unknown target_type: %s" , proto.which_target_type ));
249
207
}
250
208
251
209
if (!reader->status ().ok ()) return absl::nullopt;
0 commit comments