@@ -103,45 +103,35 @@ std::vector<uint8_t> Serializer::DecodeBytes(const pb_bytes_array_t* bytes) {
103
103
104
104
namespace {
105
105
106
- absl::optional< ObjectValue::Map> DecodeMapValue (
106
+ ObjectValue::Map DecodeMapValue (
107
107
Reader* reader, const google_firestore_v1beta1_MapValue& map_value);
108
108
109
- absl::optional< ObjectValue::Map::value_type> DecodeFieldsEntry (
109
+ ObjectValue::Map::value_type DecodeFieldsEntry (
110
110
Reader* reader,
111
111
const google_firestore_v1beta1_Document_FieldsEntry& fields) {
112
- if (!reader->status ().ok ()) return absl::nullopt ;
112
+ if (!reader->status ().ok ()) return {} ;
113
113
114
114
std::string key = Serializer::DecodeString (fields.key );
115
- absl::optional<FieldValue> value =
116
- Serializer::DecodeFieldValue (reader, fields.value );
115
+ FieldValue value = Serializer::DecodeFieldValue (reader, fields.value );
117
116
118
117
if (key.empty ()) {
119
118
reader->Fail (
120
119
" Invalid message: Empty key while decoding a Map field value." );
121
- return absl::nullopt ;
120
+ return {} ;
122
121
}
123
122
124
- if (!value.has_value ()) {
125
- reader->Fail (
126
- " Invalid message: Empty value while decoding a Map field value." );
127
- return absl::nullopt;
128
- }
129
-
130
- return ObjectValue::Map::value_type{std::move (key), *std::move (value)};
123
+ return ObjectValue::Map::value_type{std::move (key), std::move (value)};
131
124
}
132
125
133
- absl::optional< ObjectValue::Map> DecodeFields (
126
+ ObjectValue::Map DecodeFields (
134
127
Reader* reader,
135
128
size_t count,
136
129
const google_firestore_v1beta1_Document_FieldsEntry* fields) {
137
- if (!reader->status ().ok ()) return absl::nullopt ;
130
+ if (!reader->status ().ok ()) return {} ;
138
131
139
132
ObjectValue::Map result;
140
133
for (size_t i = 0 ; i < count; i++) {
141
- absl::optional<ObjectValue::Map::value_type> kv =
142
- DecodeFieldsEntry (reader, fields[i]);
143
- if (!reader->status ().ok ()) return absl::nullopt;
144
- result.emplace (*kv);
134
+ result.emplace (DecodeFieldsEntry (reader, fields[i]));
145
135
}
146
136
147
137
return result;
@@ -170,18 +160,17 @@ google_firestore_v1beta1_MapValue EncodeMapValue(
170
160
return result;
171
161
}
172
162
173
- absl::optional< ObjectValue::Map> DecodeMapValue (
163
+ ObjectValue::Map DecodeMapValue (
174
164
Reader* reader, const google_firestore_v1beta1_MapValue& map_value) {
175
- if (!reader->status ().ok ()) return absl::nullopt ;
165
+ if (!reader->status ().ok ()) return {} ;
176
166
ObjectValue::Map result;
177
167
178
168
for (size_t i = 0 ; i < map_value.fields_count ; i++) {
179
169
std::string key = Serializer::DecodeString (map_value.fields [i].key );
180
- absl::optional< FieldValue> value =
170
+ FieldValue value =
181
171
Serializer::DecodeFieldValue (reader, map_value.fields [i].value );
182
- if (!reader->status ().ok ()) return absl::nullopt;
183
172
184
- result[key] = * value;
173
+ result[key] = value;
185
174
}
186
175
187
176
return result;
@@ -305,9 +294,9 @@ google_firestore_v1beta1_Value Serializer::EncodeFieldValue(
305
294
return result;
306
295
}
307
296
308
- absl::optional< FieldValue> Serializer::DecodeFieldValue (
297
+ FieldValue Serializer::DecodeFieldValue (
309
298
Reader* reader, const google_firestore_v1beta1_Value& msg) {
310
- if (!reader->status ().ok ()) return absl::nullopt ;
299
+ if (!reader->status ().ok ()) return FieldValue::Null () ;
311
300
312
301
switch (msg.which_value_type ) {
313
302
case google_firestore_v1beta1_Value_null_value_tag:
@@ -317,6 +306,11 @@ absl::optional<FieldValue> Serializer::DecodeFieldValue(
317
306
return FieldValue::Null ();
318
307
319
308
case google_firestore_v1beta1_Value_boolean_value_tag:
309
+ // TODO(rsgowman): Due to the nanopb implementation, msg.boolean_value
310
+ // could be an integer other than 0 or 1, (such as 2). This leads to
311
+ // undefined behaviour when it's read as a boolean. eg. on at least gcc,
312
+ // the value is treated as both true *and* false. We need to decide if we
313
+ // care enough to do anything about this.
320
314
return FieldValue::FromBoolean (msg.boolean_value );
321
315
322
316
case google_firestore_v1beta1_Value_integer_value_tag:
@@ -326,17 +320,12 @@ absl::optional<FieldValue> Serializer::DecodeFieldValue(
326
320
return FieldValue::FromString (DecodeString (msg.string_value ));
327
321
328
322
case google_firestore_v1beta1_Value_timestamp_value_tag: {
329
- absl::optional<Timestamp> timestamp =
330
- DecodeTimestamp (reader, msg.timestamp_value );
331
- if (!reader->status ().ok ()) return absl::nullopt;
332
- return FieldValue::FromTimestamp (*timestamp);
323
+ return FieldValue::FromTimestamp (
324
+ DecodeTimestamp (reader, msg.timestamp_value ));
333
325
}
334
326
335
327
case google_firestore_v1beta1_Value_map_value_tag: {
336
- absl::optional<ObjectValue::Map> optional_map =
337
- DecodeMapValue (reader, msg.map_value );
338
- if (!reader->status ().ok ()) return absl::nullopt;
339
- return FieldValue::FromMap (*optional_map);
328
+ return FieldValue::FromMap (DecodeMapValue (reader, msg.map_value ));
340
329
}
341
330
342
331
case google_firestore_v1beta1_Value_double_value_tag:
@@ -352,7 +341,7 @@ absl::optional<FieldValue> Serializer::DecodeFieldValue(
352
341
// Unspecified type.
353
342
reader->Fail (StringFormat (" Invalid type while decoding FieldValue: %s" ,
354
343
msg.which_value_type ));
355
- return absl::nullopt ;
344
+ return FieldValue::Null () ;
356
345
}
357
346
358
347
UNREACHABLE ();
@@ -428,17 +417,17 @@ std::unique_ptr<model::Document> Serializer::DecodeFoundDocument(
428
417
" Tried to deserialize a found document from a missing document." );
429
418
430
419
DocumentKey key = DecodeKey (DecodeString (response.found .name ));
431
- absl::optional< ObjectValue::Map> value =
420
+ ObjectValue::Map value =
432
421
DecodeFields (reader, response.found .fields_count , response.found .fields );
433
- absl::optional< SnapshotVersion> version =
422
+ SnapshotVersion version =
434
423
DecodeSnapshotVersion (reader, response.found .update_time );
435
424
if (!reader->status ().ok ()) return nullptr ;
436
425
437
- HARD_ASSERT (* version != SnapshotVersion::None (),
426
+ HARD_ASSERT (version != SnapshotVersion::None (),
438
427
" Got a document response with no snapshot version" );
439
428
440
- return absl::make_unique<Document>(FieldValue::FromMap (* std::move (value)),
441
- std::move (key), * std::move (version),
429
+ return absl::make_unique<Document>(FieldValue::FromMap (std::move (value)),
430
+ std::move (key), std::move (version),
442
431
/* has_local_modifications=*/ false );
443
432
}
444
433
@@ -452,32 +441,30 @@ std::unique_ptr<model::NoDocument> Serializer::DecodeMissingDocument(
452
441
" Tried to deserialize a missing document from a found document." );
453
442
454
443
DocumentKey key = DecodeKey (DecodeString (response.missing ));
455
- absl::optional<SnapshotVersion> version =
456
- DecodeSnapshotVersion (reader, response.read_time );
444
+ SnapshotVersion version = DecodeSnapshotVersion (reader, response.read_time );
457
445
if (!reader->status ().ok ()) return nullptr ;
458
446
459
- if (* version == SnapshotVersion::None ()) {
447
+ if (version == SnapshotVersion::None ()) {
460
448
reader->Fail (" Got a no document response with no snapshot version" );
461
449
return nullptr ;
462
450
}
463
451
464
- return absl::make_unique<NoDocument>(std::move (key), * std::move (version));
452
+ return absl::make_unique<NoDocument>(std::move (key), std::move (version));
465
453
}
466
454
467
455
std::unique_ptr<Document> Serializer::DecodeDocument (
468
456
Reader* reader, const google_firestore_v1beta1_Document& proto) const {
469
457
if (!reader->status ().ok ()) return nullptr ;
470
458
471
- absl::optional< ObjectValue::Map> fields_internal =
459
+ ObjectValue::Map fields_internal =
472
460
DecodeFields (reader, proto.fields_count , proto.fields );
473
- absl::optional<SnapshotVersion> version =
474
- DecodeSnapshotVersion (reader, proto.update_time );
461
+ SnapshotVersion version = DecodeSnapshotVersion (reader, proto.update_time );
475
462
476
463
if (!reader->status ().ok ()) return nullptr ;
477
- return absl::make_unique<Document>(FieldValue::FromMap (*fields_internal),
478
- DecodeKey ( DecodeString (proto. name )),
479
- * version,
480
- /* has_local_modifications=*/ false );
464
+ return absl::make_unique<Document>(
465
+ FieldValue::FromMap ( std::move (fields_internal )),
466
+ DecodeKey ( DecodeString (proto. name )), std::move ( version) ,
467
+ /* has_local_modifications=*/ false );
481
468
}
482
469
483
470
google_firestore_v1beta1_Target_QueryTarget Serializer::EncodeQueryTarget (
@@ -541,7 +528,7 @@ ResourcePath DecodeQueryPath(absl::string_view name) {
541
528
}
542
529
}
543
530
544
- absl::optional< Query> Serializer::DecodeQueryTarget (
531
+ Query Serializer::DecodeQueryTarget (
545
532
nanopb::Reader* reader,
546
533
const google_firestore_v1beta1_Target_QueryTarget& proto) {
547
534
if (!reader->status ().ok ()) return Query::Invalid ();
@@ -551,7 +538,7 @@ absl::optional<Query> Serializer::DecodeQueryTarget(
551
538
google_firestore_v1beta1_Target_QueryTarget_structured_query_tag) {
552
539
reader->Fail (
553
540
StringFormat (" Unknown query_type: %s" , proto.which_query_type ));
554
- return absl::nullopt ;
541
+ return Query::Invalid () ;
555
542
}
556
543
557
544
ResourcePath path = DecodeQueryPath (DecodeString (proto.parent ));
@@ -596,14 +583,12 @@ google_protobuf_Timestamp Serializer::EncodeTimestamp(
596
583
return result;
597
584
}
598
585
599
- absl::optional< SnapshotVersion> Serializer::DecodeSnapshotVersion (
586
+ SnapshotVersion Serializer::DecodeSnapshotVersion (
600
587
nanopb::Reader* reader, const google_protobuf_Timestamp& proto) {
601
- absl::optional<Timestamp> version = DecodeTimestamp (reader, proto);
602
- if (!reader->status ().ok ()) return absl::nullopt;
603
- return SnapshotVersion{*version};
588
+ return SnapshotVersion{DecodeTimestamp (reader, proto)};
604
589
}
605
590
606
- absl::optional< Timestamp> Serializer::DecodeTimestamp (
591
+ Timestamp Serializer::DecodeTimestamp (
607
592
nanopb::Reader* reader, const google_protobuf_Timestamp& timestamp_proto) {
608
593
// The Timestamp ctor will assert if we provide values outside the valid
609
594
// range. However, since we're decoding, a single corrupt byte could cause
@@ -619,7 +604,7 @@ absl::optional<Timestamp> Serializer::DecodeTimestamp(
619
604
" Invalid message: timestamp nanos must be between 0 and 999999999" );
620
605
}
621
606
622
- if (!reader->status ().ok ()) return absl::nullopt ;
607
+ if (!reader->status ().ok ()) return Timestamp () ;
623
608
return Timestamp{timestamp_proto.seconds , timestamp_proto.nanos };
624
609
}
625
610
0 commit comments