|
45 | 45 | namespace firebase { |
46 | 46 | namespace firestore { |
47 | 47 | namespace bundle { |
48 | | -namespace { |
49 | 48 |
|
50 | 49 | using absl::Time; |
51 | 50 | using core::Bound; |
@@ -73,21 +72,16 @@ using nanopb::Message; |
73 | 72 | using nanopb::SetRepeatedField; |
74 | 73 | using nanopb::SharedMessage; |
75 | 74 | using nlohmann::json; |
| 75 | +using util::JsonReader; |
76 | 76 | using util::NoDestructor; |
77 | 77 | using util::StatusOr; |
78 | 78 | using util::StringFormat; |
79 | 79 | using Operator = FieldFilter::Operator; |
80 | 80 |
|
81 | 81 | namespace { |
| 82 | + |
82 | 83 | const NoDestructor<Bound> kDefaultBound{Bound::FromValue( |
83 | 84 | MakeSharedMessage<google_firestore_v1_ArrayValue>({}), false)}; |
84 | | -} // namespace |
85 | | - |
86 | | -template <typename T> |
87 | | -const std::vector<T>& EmptyVector() { |
88 | | - static NoDestructor<std::vector<T>> empty; |
89 | | - return *empty; |
90 | | -} |
91 | 85 |
|
92 | 86 | Timestamp DecodeTimestamp(JsonReader& reader, const json& version) { |
93 | 87 | StatusOr<Timestamp> decoded; |
@@ -326,180 +320,6 @@ pb_bytes_array_t* DecodeBytesValue(JsonReader& reader, |
326 | 320 |
|
327 | 321 | } // namespace |
328 | 322 |
|
329 | | -// Mark: JsonReader |
330 | | - |
331 | | -const std::string& JsonReader::RequiredString(const char* name, |
332 | | - const json& json_object) { |
333 | | - if (json_object.contains(name)) { |
334 | | - const json& child = json_object.at(name); |
335 | | - if (child.is_string()) { |
336 | | - return child.get_ref<const std::string&>(); |
337 | | - } |
338 | | - } |
339 | | - |
340 | | - Fail("'%s' is missing or is not a string", name); |
341 | | - return util::EmptyString(); |
342 | | -} |
343 | | - |
344 | | -const std::string& JsonReader::OptionalString( |
345 | | - const char* name, |
346 | | - const json& json_object, |
347 | | - const std::string& default_value) { |
348 | | - if (json_object.contains(name)) { |
349 | | - const json& child = json_object.at(name); |
350 | | - if (child.is_string()) { |
351 | | - return child.get_ref<const std::string&>(); |
352 | | - } |
353 | | - } |
354 | | - |
355 | | - return default_value; |
356 | | -} |
357 | | - |
358 | | -const std::vector<json>& JsonReader::RequiredArray(const char* name, |
359 | | - const json& json_object) { |
360 | | - if (json_object.contains(name)) { |
361 | | - const json& child = json_object.at(name); |
362 | | - if (child.is_array()) { |
363 | | - return child.get_ref<const std::vector<json>&>(); |
364 | | - } |
365 | | - } |
366 | | - |
367 | | - Fail("'%s' is missing or is not an array", name); |
368 | | - return EmptyVector<json>(); |
369 | | -} |
370 | | - |
371 | | -const std::vector<json>& JsonReader::OptionalArray( |
372 | | - const char* name, |
373 | | - const json& json_object, |
374 | | - const std::vector<json>& default_value) { |
375 | | - if (!json_object.contains(name)) { |
376 | | - return default_value; |
377 | | - } |
378 | | - |
379 | | - const json& child = json_object.at(name); |
380 | | - if (child.is_array()) { |
381 | | - return child.get_ref<const std::vector<json>&>(); |
382 | | - } else { |
383 | | - Fail("'%s' is not an array", name); |
384 | | - return EmptyVector<json>(); |
385 | | - } |
386 | | -} |
387 | | - |
388 | | -bool JsonReader::OptionalBool(const char* name, |
389 | | - const json& json_object, |
390 | | - bool default_value) { |
391 | | - return (json_object.contains(name) && json_object.at(name).is_boolean() && |
392 | | - json_object.at(name).get<bool>()) || |
393 | | - default_value; |
394 | | -} |
395 | | - |
396 | | -const nlohmann::json& JsonReader::RequiredObject(const char* child_name, |
397 | | - const json& json_object) { |
398 | | - if (!json_object.contains(child_name)) { |
399 | | - Fail("Missing child '%s'", child_name); |
400 | | - return json_object; |
401 | | - } |
402 | | - return json_object.at(child_name); |
403 | | -} |
404 | | - |
405 | | -const nlohmann::json& JsonReader::OptionalObject( |
406 | | - const char* child_name, |
407 | | - const json& json_object, |
408 | | - const nlohmann::json& default_value) { |
409 | | - if (json_object.contains(child_name)) { |
410 | | - return json_object.at(child_name); |
411 | | - } |
412 | | - return default_value; |
413 | | -} |
414 | | - |
415 | | -double JsonReader::RequiredDouble(const char* name, const json& json_object) { |
416 | | - if (json_object.contains(name)) { |
417 | | - double result = DecodeDouble(json_object.at(name)); |
418 | | - if (ok()) { |
419 | | - return result; |
420 | | - } |
421 | | - } |
422 | | - |
423 | | - Fail("'%s' is missing or is not a double", name); |
424 | | - return 0.0; |
425 | | -} |
426 | | - |
427 | | -double JsonReader::OptionalDouble(const char* name, |
428 | | - const json& json_object, |
429 | | - double default_value) { |
430 | | - if (json_object.contains(name)) { |
431 | | - double result = DecodeDouble(json_object.at(name)); |
432 | | - if (ok()) { |
433 | | - return result; |
434 | | - } |
435 | | - } |
436 | | - |
437 | | - return default_value; |
438 | | -} |
439 | | - |
440 | | -double JsonReader::DecodeDouble(const nlohmann::json& value) { |
441 | | - if (value.is_number()) { |
442 | | - return value.get<double>(); |
443 | | - } |
444 | | - |
445 | | - double result = 0; |
446 | | - if (value.is_string()) { |
447 | | - const auto& s = value.get_ref<const std::string&>(); |
448 | | - auto ok = absl::SimpleAtod(s, &result); |
449 | | - if (!ok) { |
450 | | - Fail("Failed to parse into double: " + s); |
451 | | - } |
452 | | - } |
453 | | - return result; |
454 | | -} |
455 | | - |
456 | | -template <typename IntType> |
457 | | -IntType ParseInt(const json& value, JsonReader& reader) { |
458 | | - if (value.is_number_integer()) { |
459 | | - return value.get<IntType>(); |
460 | | - } |
461 | | - |
462 | | - IntType result = 0; |
463 | | - if (value.is_string()) { |
464 | | - const auto& s = value.get_ref<const std::string&>(); |
465 | | - auto ok = absl::SimpleAtoi<IntType>(s, &result); |
466 | | - if (!ok) { |
467 | | - reader.Fail("Failed to parse into integer: " + s); |
468 | | - return 0; |
469 | | - } |
470 | | - |
471 | | - return result; |
472 | | - } |
473 | | - |
474 | | - reader.Fail("Only integer and string can be parsed into int type"); |
475 | | - return 0; |
476 | | -} |
477 | | - |
478 | | -template <typename IntType> |
479 | | -IntType JsonReader::RequiredInt(const char* name, const json& json_object) { |
480 | | - if (!json_object.contains(name)) { |
481 | | - Fail("'%s' is missing or is not a double", name); |
482 | | - return 0; |
483 | | - } |
484 | | - |
485 | | - const json& value = json_object.at(name); |
486 | | - return ParseInt<IntType>(value, *this); |
487 | | -} |
488 | | - |
489 | | -template <typename IntType> |
490 | | -IntType JsonReader::OptionalInt(const char* name, |
491 | | - const json& json_object, |
492 | | - IntType default_value) { |
493 | | - if (!json_object.contains(name)) { |
494 | | - return default_value; |
495 | | - } |
496 | | - |
497 | | - const json& value = json_object.at(name); |
498 | | - return ParseInt<IntType>(value, *this); |
499 | | -} |
500 | | - |
501 | | -// Mark: BundleSerializer |
502 | | - |
503 | 323 | BundleMetadata BundleSerializer::DecodeBundleMetadata( |
504 | 324 | JsonReader& reader, const json& metadata) const { |
505 | 325 | return BundleMetadata( |
|
0 commit comments