diff options
Diffstat (limited to 'Firestore/core/src')
30 files changed, 373 insertions, 100 deletions
diff --git a/Firestore/core/src/firebase/firestore/geo_point.cc b/Firestore/core/src/firebase/firestore/geo_point.cc index fb01023..1ed5126 100644 --- a/Firestore/core/src/firebase/firestore/geo_point.cc +++ b/Firestore/core/src/firebase/firestore/geo_point.cc @@ -16,10 +16,12 @@ #include "Firestore/core/include/firebase/firestore/geo_point.h" -#include <math.h> +#include <cmath> #include "Firestore/core/src/firebase/firestore/util/firebase_assert.h" +using std::isnan; + namespace firebase { namespace firestore { diff --git a/Firestore/core/src/firebase/firestore/immutable/sorted_map.h b/Firestore/core/src/firebase/firestore/immutable/sorted_map.h index 5ed16b3..ef6f54e 100644 --- a/Firestore/core/src/firebase/firestore/immutable/sorted_map.h +++ b/Firestore/core/src/firebase/firestore/immutable/sorted_map.h @@ -144,6 +144,7 @@ class SortedMap : public impl::SortedMapBase { case Tag::Tree: return SortedMap{tree_.insert(key, value)}; } + FIREBASE_UNREACHABLE(); } /** @@ -159,6 +160,7 @@ class SortedMap : public impl::SortedMapBase { case Tag::Tree: return SortedMap{tree_.erase(key)}; } + FIREBASE_UNREACHABLE(); } /** Returns true if the map contains no elements. */ @@ -169,6 +171,7 @@ class SortedMap : public impl::SortedMapBase { case Tag::Tree: return tree_.empty(); } + FIREBASE_UNREACHABLE(); } /** Returns the number of items in this map. */ @@ -179,6 +182,7 @@ class SortedMap : public impl::SortedMapBase { case Tag::Tree: return tree_.size(); } + FIREBASE_UNREACHABLE(); } private: diff --git a/Firestore/core/src/firebase/firestore/immutable/sorted_map_base.h b/Firestore/core/src/firebase/firestore/immutable/sorted_map_base.h index accb5ef..cfb19c1 100644 --- a/Firestore/core/src/firebase/firestore/immutable/sorted_map_base.h +++ b/Firestore/core/src/firebase/firestore/immutable/sorted_map_base.h @@ -17,7 +17,7 @@ #ifndef FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_IMMUTABLE_SORTED_MAP_BASE_H_ #define FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_IMMUTABLE_SORTED_MAP_BASE_H_ -#include <stdint.h> +#include <cstdint> namespace firebase { namespace firestore { diff --git a/Firestore/core/src/firebase/firestore/immutable/tree_sorted_map.h b/Firestore/core/src/firebase/firestore/immutable/tree_sorted_map.h index e3102e7..dfe270d 100644 --- a/Firestore/core/src/firebase/firestore/immutable/tree_sorted_map.h +++ b/Firestore/core/src/firebase/firestore/immutable/tree_sorted_map.h @@ -17,9 +17,8 @@ #ifndef FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_IMMUTABLE_TREE_SORTED_MAP_H_ #define FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_IMMUTABLE_TREE_SORTED_MAP_H_ -#include <assert.h> - #include <algorithm> +#include <cassert> #include <functional> #include <memory> #include <utility> diff --git a/Firestore/core/src/firebase/firestore/local/leveldb_transaction.cc b/Firestore/core/src/firebase/firestore/local/leveldb_transaction.cc index d84d441..f998550 100644 --- a/Firestore/core/src/firebase/firestore/local/leveldb_transaction.cc +++ b/Firestore/core/src/firebase/firestore/local/leveldb_transaction.cc @@ -16,12 +16,11 @@ #include "Firestore/core/src/firebase/firestore/local/leveldb_transaction.h" -#include <leveldb/write_batch.h> - #include "Firestore/core/src/firebase/firestore/local/leveldb_key.h" #include "Firestore/core/src/firebase/firestore/util/firebase_assert.h" #include "Firestore/core/src/firebase/firestore/util/log.h" #include "absl/memory/memory.h" +#include "leveldb/write_batch.h" using leveldb::DB; using leveldb::ReadOptions; diff --git a/Firestore/core/src/firebase/firestore/local/leveldb_transaction.h b/Firestore/core/src/firebase/firestore/local/leveldb_transaction.h index 56a9a77..a6ddce2 100644 --- a/Firestore/core/src/firebase/firestore/local/leveldb_transaction.h +++ b/Firestore/core/src/firebase/firestore/local/leveldb_transaction.h @@ -17,16 +17,16 @@ #ifndef FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_LOCAL_LEVELDB_TRANSACTION_H_ #define FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_LOCAL_LEVELDB_TRANSACTION_H_ -#include <absl/strings/string_view.h> -#include <leveldb/db.h> - -#include <stdint.h> +#include <cstdint> #include <map> #include <memory> #include <set> #include <string> #include <utility> +#include "absl/strings/string_view.h" +#include "leveldb/db.h" + #if __OBJC__ #import <Protobuf/GPBProtocolBuffers.h> #endif diff --git a/Firestore/core/src/firebase/firestore/model/CMakeLists.txt b/Firestore/core/src/firebase/firestore/model/CMakeLists.txt index 78f5cd6..de783ad 100644 --- a/Firestore/core/src/firebase/firestore/model/CMakeLists.txt +++ b/Firestore/core/src/firebase/firestore/model/CMakeLists.txt @@ -22,8 +22,10 @@ cc_library( document.h document_key.cc document_key.h + field_mask.h field_path.cc field_path.h + field_transform.h field_value.cc field_value.h maybe_document.cc @@ -34,6 +36,7 @@ cc_library( resource_path.h snapshot_version.cc snapshot_version.h + transform_operations.h types.h DEPENDS absl_strings diff --git a/Firestore/core/src/firebase/firestore/model/database_id.h b/Firestore/core/src/firebase/firestore/model/database_id.h index 2ad1332..e1feca9 100644 --- a/Firestore/core/src/firebase/firestore/model/database_id.h +++ b/Firestore/core/src/firebase/firestore/model/database_id.h @@ -17,8 +17,7 @@ #ifndef FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_MODEL_DATABASE_ID_H_ #define FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_MODEL_DATABASE_ID_H_ -#include <stdint.h> - +#include <cstdint> #include <string> #include "absl/strings/string_view.h" diff --git a/Firestore/core/src/firebase/firestore/model/field_mask.h b/Firestore/core/src/firebase/firestore/model/field_mask.h new file mode 100644 index 0000000..a9f509a --- /dev/null +++ b/Firestore/core/src/firebase/firestore/model/field_mask.h @@ -0,0 +1,97 @@ +/* + * Copyright 2018 Google + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_MODEL_FIELD_MASK_H_ +#define FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_MODEL_FIELD_MASK_H_ + +#include <initializer_list> +#include <string> +#include <utility> +#include <vector> + +#include "Firestore/core/src/firebase/firestore/model/field_path.h" + +namespace firebase { +namespace firestore { +namespace model { + +/** + * Provides a set of fields that can be used to partially patch a document. + * FieldMask is used in conjunction with FieldValue of Object type. + * + * Examples: + * foo - Overwrites foo entirely with the provided value. If foo is not + * present in the companion FieldValue, the field is deleted. + * foo.bar - Overwrites only the field bar of the object foo. If foo is not an + * object, foo is replaced with an object containing bar. + */ +class FieldMask { + public: + using const_iterator = std::vector<FieldPath>::const_iterator; + + FieldMask(std::initializer_list<FieldPath> list) : fields_{list} { + } + explicit FieldMask(const std::vector<FieldPath>& fields) : fields_{fields} { + } + explicit FieldMask(std::vector<FieldPath>&& fields) + : fields_{std::move(fields)} { + } + + const_iterator begin() const { + return fields_.begin(); + } + const_iterator end() const { + return fields_.end(); + } + + std::string ToString() const { + // Ideally, one should use a string builder. Since this is only non-critical + // code for logging and debugging, the logic is kept simple here. + std::string result("{ "); + for (const FieldPath& field : fields_) { + result += field.CanonicalString() + " "; + } + return result + "}"; + } + +#if defined(__OBJC__) + FieldMask() { + } + + NSUInteger Hash() const { + NSUInteger hashResult = 0; + for (const FieldPath& field : fields_) { + hashResult = hashResult * 31u + field.Hash(); + } + return hashResult; + } +#endif + + friend bool operator==(const FieldMask& lhs, const FieldMask& rhs); + + private: + std::vector<FieldPath> fields_; +}; + +inline bool operator==(const FieldMask& lhs, const FieldMask& rhs) { + return lhs.fields_ == rhs.fields_; +} + +} // namespace model +} // namespace firestore +} // namespace firebase + +#endif // FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_MODEL_FIELD_MASK_H_ diff --git a/Firestore/core/src/firebase/firestore/model/field_transform.h b/Firestore/core/src/firebase/firestore/model/field_transform.h new file mode 100644 index 0000000..a1dd96c --- /dev/null +++ b/Firestore/core/src/firebase/firestore/model/field_transform.h @@ -0,0 +1,70 @@ +/* + * Copyright 2018 Google + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_MODEL_FIELD_TRANSFORM_H_ +#define FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_MODEL_FIELD_TRANSFORM_H_ + +#include <memory> +#include <utility> + +#include "Firestore/core/src/firebase/firestore/model/field_path.h" +#include "Firestore/core/src/firebase/firestore/model/transform_operations.h" + +namespace firebase { +namespace firestore { +namespace model { + +/** A field path and the TransformOperation to perform upon it. */ +class FieldTransform { + public: + FieldTransform(FieldPath path, + std::unique_ptr<TransformOperation> transformation) noexcept + : path_{std::move(path)}, transformation_{std::move(transformation)} { + } + + const FieldPath& path() const { + return path_; + } + + const TransformOperation& transformation() const { + return *transformation_.get(); + } + + bool operator==(const FieldTransform& other) const { + return path_ == other.path_ && *transformation_ == *other.transformation_; + } + +#if defined(__OBJC__) + // For Objective-C++ hash; to be removed after migration. + // Do NOT use in C++ code. + NSUInteger Hash() const { + NSUInteger hash = path_.Hash(); + hash = hash * 31 + transformation_->Hash(); + return hash; + } +#endif // defined(__OBJC__) + + private: + FieldPath path_; + // Shared by copies of the same FieldTransform. + std::shared_ptr<const TransformOperation> transformation_; +}; + +} // namespace model +} // namespace firestore +} // namespace firebase + +#endif // FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_MODEL_FIELD_TRANSFORM_H_ diff --git a/Firestore/core/src/firebase/firestore/model/field_value.cc b/Firestore/core/src/firebase/firestore/model/field_value.cc index 2c1af53..1a40331 100644 --- a/Firestore/core/src/firebase/firestore/model/field_value.cc +++ b/Firestore/core/src/firebase/firestore/model/field_value.cc @@ -16,9 +16,8 @@ #include "Firestore/core/src/firebase/firestore/model/field_value.h" -#include <math.h> - #include <algorithm> +#include <cmath> #include <memory> #include <utility> #include <vector> diff --git a/Firestore/core/src/firebase/firestore/model/field_value.h b/Firestore/core/src/firebase/firestore/model/field_value.h index c42d533..c70e332 100644 --- a/Firestore/core/src/firebase/firestore/model/field_value.h +++ b/Firestore/core/src/firebase/firestore/model/field_value.h @@ -17,8 +17,7 @@ #ifndef FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_MODEL_FIELD_VALUE_H_ #define FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_MODEL_FIELD_VALUE_H_ -#include <stdint.h> - +#include <cstdint> #include <map> #include <memory> #include <string> diff --git a/Firestore/core/src/firebase/firestore/model/transform_operations.h b/Firestore/core/src/firebase/firestore/model/transform_operations.h new file mode 100644 index 0000000..6ff4dbc --- /dev/null +++ b/Firestore/core/src/firebase/firestore/model/transform_operations.h @@ -0,0 +1,97 @@ +/* + * Copyright 2018 Google + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_MODEL_TRANSFORM_OPERATIONS_H_ +#define FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_MODEL_TRANSFORM_OPERATIONS_H_ + +namespace firebase { +namespace firestore { +namespace model { + +// TODO(zxu123): We might want to refactor transform_operations.h into several +// files when the number of different types of operations grows gigantically. +// For now, put the interface and the only operation here. + +/** Represents a transform within a TransformMutation. */ +class TransformOperation { + public: + /** All the different kinds to TransformOperation. */ + enum class Type { + ServerTimestamp, + Test, // Purely for test purpose. + }; + + virtual ~TransformOperation() { + } + + /** Returns the actual type. */ + virtual Type type() const = 0; + + /** Returns whether the two are equal. */ + virtual bool operator==(const TransformOperation& other) const = 0; + + /** Returns whether the two are not equal. */ + bool operator!=(const TransformOperation& other) const { + return !operator==(other); + } + +#if defined(__OBJC__) + // For Objective-C++ hash; to be removed after migration. + // Do NOT use in C++ code. + virtual NSUInteger Hash() const = 0; +#endif // defined(__OBJC__) +}; + +/** Transforms a value into a server-generated timestamp. */ +class ServerTimestampTransform : public TransformOperation { + public: + ~ServerTimestampTransform() override { + } + + Type type() const override { + return Type::ServerTimestamp; + } + + bool operator==(const TransformOperation& other) const override { + // All ServerTimestampTransform objects are equal. + return other.type() == Type::ServerTimestamp; + } + + static const ServerTimestampTransform& Get() { + static ServerTimestampTransform shared_instance; + return shared_instance; + } + +#if defined(__OBJC__) + // For Objective-C++ hash; to be removed after migration. + // Do NOT use in C++ code. + NSUInteger Hash() const override { + // arbitrary number, the same as used in ObjC implementation, since all + // instances are equal. + return 37; + } +#endif // defined(__OBJC__) + + private: + ServerTimestampTransform() { + } +}; + +} // namespace model +} // namespace firestore +} // namespace firebase + +#endif // FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_MODEL_TRANSFORM_OPERATIONS_H_ diff --git a/Firestore/core/src/firebase/firestore/model/types.h b/Firestore/core/src/firebase/firestore/model/types.h index fc1b196..3f813be 100644 --- a/Firestore/core/src/firebase/firestore/model/types.h +++ b/Firestore/core/src/firebase/firestore/model/types.h @@ -17,7 +17,7 @@ #ifndef FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_MODEL_TYPES_H_ #define FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_MODEL_TYPES_H_ -#include <stdint.h> +#include <cstdint> namespace firebase { namespace firestore { diff --git a/Firestore/core/src/firebase/firestore/remote/serializer.cc b/Firestore/core/src/firebase/firestore/remote/serializer.cc index 5483e1f..ff135fd 100644 --- a/Firestore/core/src/firebase/firestore/remote/serializer.cc +++ b/Firestore/core/src/firebase/firestore/remote/serializer.cc @@ -32,6 +32,7 @@ namespace remote { using firebase::firestore::model::FieldValue; using firebase::firestore::model::ObjectValue; +using firebase::firestore::util::Status; namespace { @@ -43,7 +44,7 @@ ObjectValue DecodeObject(pb_istream_t* stream); /** * Docs TODO(rsgowman). But currently, this just wraps the underlying nanopb - * pb_ostream_t. + * pb_ostream_t. Also doc how to check status. */ class Writer { public: @@ -105,7 +106,13 @@ class Writer { return stream_.bytes_written; } + Status status() const { + return status_; + } + private: + Status status_ = Status::OK(); + /** * Creates a new Writer, based on the given nanopb pb_ostream_t. Note that * a shallow copy will be taken. (Non-null pointers within this struct must @@ -166,10 +173,10 @@ Writer Writer::Wrap(std::vector<uint8_t>* out_bytes) { // together (probably within their own file rather than here). void Writer::WriteTag(pb_wire_type_t wiretype, uint32_t field_number) { - bool status = pb_encode_tag(&stream_, wiretype, field_number); - if (!status) { - // TODO(rsgowman): figure out error handling - abort(); + if (!status_.ok()) return; + + if (!pb_encode_tag(&stream_, wiretype, field_number)) { + FIREBASE_ASSERT_MESSAGE(false, PB_GET_ERROR(&stream_)); } } @@ -178,10 +185,10 @@ void Writer::WriteSize(size_t size) { } void Writer::WriteVarint(uint64_t value) { - bool status = pb_encode_varint(&stream_, value); - if (!status) { - // TODO(rsgowman): figure out error handling - abort(); + if (!status_.ok()) return; + + if (!pb_encode_varint(&stream_, value)) { + FIREBASE_ASSERT_MESSAGE(false, PB_GET_ERROR(&stream_)); } } @@ -196,8 +203,7 @@ void Writer::WriteVarint(uint64_t value) { */ uint64_t DecodeVarint(pb_istream_t* stream) { uint64_t varint_value; - bool status = pb_decode_varint(stream, &varint_value); - if (!status) { + if (!pb_decode_varint(stream, &varint_value)) { // TODO(rsgowman): figure out error handling abort(); } @@ -242,27 +248,25 @@ int64_t DecodeInteger(pb_istream_t* stream) { } void Writer::WriteString(const std::string& string_value) { - bool status = pb_encode_string( - &stream_, reinterpret_cast<const pb_byte_t*>(string_value.c_str()), - string_value.length()); - if (!status) { - // TODO(rsgowman): figure out error handling - abort(); + if (!status_.ok()) return; + + if (!pb_encode_string( + &stream_, reinterpret_cast<const pb_byte_t*>(string_value.c_str()), + string_value.length())) { + FIREBASE_ASSERT_MESSAGE(false, PB_GET_ERROR(&stream_)); } } std::string DecodeString(pb_istream_t* stream) { pb_istream_t substream; - bool status = pb_make_string_substream(stream, &substream); - if (!status) { + if (!pb_make_string_substream(stream, &substream)) { // TODO(rsgowman): figure out error handling abort(); } std::string result(substream.bytes_left, '\0'); - status = pb_read(&substream, reinterpret_cast<pb_byte_t*>(&result[0]), - substream.bytes_left); - if (!status) { + if (!pb_read(&substream, reinterpret_cast<pb_byte_t*>(&result[0]), + substream.bytes_left)) { // TODO(rsgowman): figure out error handling abort(); } @@ -330,8 +334,7 @@ FieldValue DecodeFieldValueImpl(pb_istream_t* stream) { pb_wire_type_t wire_type; uint32_t tag; bool eof; - bool status = pb_decode_tag(stream, &wire_type, &tag, &eof); - if (!status) { + if (!pb_decode_tag(stream, &wire_type, &tag, &eof)) { // TODO(rsgowman): figure out error handling abort(); } @@ -380,31 +383,35 @@ FieldValue DecodeFieldValueImpl(pb_istream_t* stream) { void Writer::WriteNestedMessage( const std::function<void(Writer*)>& write_message_fn) { + if (!status_.ok()) return; + // First calculate the message size using a non-writing substream. Writer sizer = Writer::Sizing(); write_message_fn(&sizer); + status_ = sizer.status(); + if (!status_.ok()) return; size_t size = sizer.bytes_written(); // Write out the size to the output writer. WriteSize(size); + if (!status_.ok()) return; // If this stream is itself a sizing stream, then we don't need to actually // parse field_value a second time; just update the bytes_written via a call // to pb_write. (If we try to write the contents into a sizing stream, it'll // fail since sizing streams don't actually have any buffer space.) if (stream_.callback == nullptr) { - bool status = pb_write(&stream_, nullptr, size); - if (!status) { - // TODO(rsgowman): figure out error handling - abort(); + if (!pb_write(&stream_, nullptr, size)) { + FIREBASE_ASSERT_MESSAGE(false, PB_GET_ERROR(&stream_)); } return; } // Ensure the output stream has enough space if (stream_.bytes_written + size > stream_.max_size) { - // TODO(rsgowman): figure out error handling - abort(); + FIREBASE_ASSERT_MESSAGE( + false, + "Insufficient space in the output stream to write the given message"); } // Use a substream to verify that a callback doesn't write more than what it @@ -415,6 +422,8 @@ void Writer::WriteNestedMessage( /*max_size=*/size, /*bytes_written=*/0, /*errmsg=*/nullptr}); write_message_fn(&writer); + status_ = writer.status(); + if (!status_.ok()) return; stream_.bytes_written += writer.stream_.bytes_written; stream_.state = writer.stream_.state; @@ -422,8 +431,8 @@ void Writer::WriteNestedMessage( if (writer.bytes_written() != size) { // submsg size changed - // TODO(rsgowman): figure out error handling - abort(); + FIREBASE_ASSERT_MESSAGE( + false, "Parsing the nested message twice yielded different sizes"); } } @@ -431,8 +440,7 @@ FieldValue DecodeNestedFieldValue(pb_istream_t* stream) { // Implementation note: This is roughly modeled on pb_decode_delimited, // adjusted to account for the oneof in FieldValue. pb_istream_t substream; - bool status = pb_make_string_substream(stream, &substream); - if (!status) { + if (!pb_make_string_substream(stream, &substream)) { // TODO(rsgowman): figure out error handling abort(); } @@ -490,21 +498,23 @@ ObjectValue::Map::value_type DecodeFieldsEntry(pb_istream_t* stream) { pb_wire_type_t wire_type; uint32_t tag; bool eof; - bool status = pb_decode_tag(stream, &wire_type, &tag, &eof); + if (!pb_decode_tag(stream, &wire_type, &tag, &eof)) { + abort(); + } // TODO(rsgowman): figure out error handling: We can do better than a failed // assertion. FIREBASE_ASSERT(tag == google_firestore_v1beta1_MapValue_FieldsEntry_key_tag); FIREBASE_ASSERT(wire_type == PB_WT_STRING); FIREBASE_ASSERT(!eof); - FIREBASE_ASSERT(status); std::string key = DecodeString(stream); - status = pb_decode_tag(stream, &wire_type, &tag, &eof); + if (!pb_decode_tag(stream, &wire_type, &tag, &eof)) { + abort(); + } FIREBASE_ASSERT(tag == google_firestore_v1beta1_MapValue_FieldsEntry_value_tag); FIREBASE_ASSERT(wire_type == PB_WT_STRING); FIREBASE_ASSERT(!eof); - FIREBASE_ASSERT(status); FieldValue value = DecodeNestedFieldValue(stream); @@ -512,16 +522,15 @@ ObjectValue::Map::value_type DecodeFieldsEntry(pb_istream_t* stream) { } void EncodeObject(Writer* writer, const ObjectValue& object_value) { - writer->WriteNestedMessage([&object_value](Writer* writer) { + return writer->WriteNestedMessage([&object_value](Writer* writer) { // Write each FieldsEntry (i.e. key-value pair.) for (const auto& kv : object_value.internal_value) { writer->WriteTag(PB_WT_STRING, google_firestore_v1beta1_MapValue_FieldsEntry_key_tag); + writer->WriteNestedMessage( - [&kv](Writer* writer) { EncodeFieldsEntry(writer, kv); }); + [&kv](Writer* writer) { return EncodeFieldsEntry(writer, kv); }); } - - return true; }); } @@ -550,9 +559,8 @@ ObjectValue DecodeObject(pb_istream_t* stream) { }; map_value.fields.arg = &result; - bool status = pb_decode_delimited( - stream, google_firestore_v1beta1_MapValue_fields, &map_value); - if (!status) { + if (!pb_decode_delimited(stream, google_firestore_v1beta1_MapValue_fields, + &map_value)) { // TODO(rsgowman): figure out error handling abort(); } @@ -562,10 +570,11 @@ ObjectValue DecodeObject(pb_istream_t* stream) { } // namespace -void Serializer::EncodeFieldValue(const FieldValue& field_value, - std::vector<uint8_t>* out_bytes) { +Status Serializer::EncodeFieldValue(const FieldValue& field_value, + std::vector<uint8_t>* out_bytes) { Writer writer = Writer::Wrap(out_bytes); EncodeFieldValueImpl(&writer, field_value); + return writer.status(); } FieldValue Serializer::DecodeFieldValue(const uint8_t* bytes, size_t length) { diff --git a/Firestore/core/src/firebase/firestore/remote/serializer.h b/Firestore/core/src/firebase/firestore/remote/serializer.h index 10cacbc..3b2b667 100644 --- a/Firestore/core/src/firebase/firestore/remote/serializer.h +++ b/Firestore/core/src/firebase/firestore/remote/serializer.h @@ -17,13 +17,15 @@ #ifndef FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_REMOTE_SERIALIZER_H_ #define FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_REMOTE_SERIALIZER_H_ -#include <stdint.h> -#include <stdlib.h> +#include <cstdint> +#include <cstdlib> #include <vector> #include "Firestore/Protos/nanopb/google/firestore/v1beta1/document.pb.h" #include "Firestore/core/src/firebase/firestore/model/field_value.h" #include "Firestore/core/src/firebase/firestore/util/firebase_assert.h" +#include "Firestore/core/src/firebase/firestore/util/status.h" +#include "absl/base/attributes.h" namespace firebase { namespace firestore { @@ -70,7 +72,7 @@ class Serializer { // TODO(rsgowman): If we never support any output except to a vector, it may // make sense to have Serializer own the vector and provide an accessor rather // than asking the user to create it first. - static void EncodeFieldValue( + static util::Status EncodeFieldValue( const firebase::firestore::model::FieldValue& field_value, std::vector<uint8_t>* out_bytes); diff --git a/Firestore/core/src/firebase/firestore/util/assert_apple.mm b/Firestore/core/src/firebase/firestore/util/assert_apple.mm index 83b76e1..9b6a651 100644 --- a/Firestore/core/src/firebase/firestore/util/assert_apple.mm +++ b/Firestore/core/src/firebase/firestore/util/assert_apple.mm @@ -14,12 +14,10 @@ * limitations under the License. */ -#include "Firestore/core/src/firebase/firestore/util/firebase_assert.h" - #import <Foundation/Foundation.h> -#include <string.h> - +// TODO(wilhuff): match basenames so this can move up top +#include "Firestore/core/src/firebase/firestore/util/firebase_assert.h" #include "Firestore/core/src/firebase/firestore/util/string_apple.h" namespace firebase { diff --git a/Firestore/core/src/firebase/firestore/util/assert_stdio.cc b/Firestore/core/src/firebase/firestore/util/assert_stdio.cc index 1d2e333..e01e564 100644 --- a/Firestore/core/src/firebase/firestore/util/assert_stdio.cc +++ b/Firestore/core/src/firebase/firestore/util/assert_stdio.cc @@ -14,8 +14,7 @@ * limitations under the License. */ -#include <stdarg.h> - +#include <cstdarg> #include <stdexcept> #include <string> diff --git a/Firestore/core/src/firebase/firestore/util/bits.h b/Firestore/core/src/firebase/firestore/util/bits.h index 185273f..fec1228 100644 --- a/Firestore/core/src/firebase/firestore/util/bits.h +++ b/Firestore/core/src/firebase/firestore/util/bits.h @@ -22,7 +22,7 @@ // Munging bits in _signed_ integers is fraught with peril! For example, // -5 << n has undefined behavior (for some values of n). -#include <stdint.h> +#include <cstdint> class Bits_Port32_Test; class Bits_Port64_Test; diff --git a/Firestore/core/src/firebase/firestore/util/comparison.cc b/Firestore/core/src/firebase/firestore/util/comparison.cc index 4bef843..b346277 100644 --- a/Firestore/core/src/firebase/firestore/util/comparison.cc +++ b/Firestore/core/src/firebase/firestore/util/comparison.cc @@ -16,10 +16,11 @@ #include "Firestore/core/src/firebase/firestore/util/comparison.h" -#include <math.h> - +#include <cmath> #include <limits> +using std::isnan; + namespace firebase { namespace firestore { namespace util { diff --git a/Firestore/core/src/firebase/firestore/util/comparison.h b/Firestore/core/src/firebase/firestore/util/comparison.h index 6fd1e2b..23207f5 100644 --- a/Firestore/core/src/firebase/firestore/util/comparison.h +++ b/Firestore/core/src/firebase/firestore/util/comparison.h @@ -21,9 +21,9 @@ #import <Foundation/Foundation.h> #endif -#include <stdint.h> #include <sys/types.h> +#include <cstdint> #include <functional> #include <string> #include <vector> diff --git a/Firestore/core/src/firebase/firestore/util/firebase_assert.h b/Firestore/core/src/firebase/firestore/util/firebase_assert.h index 20c8429..6a9c2eb 100644 --- a/Firestore/core/src/firebase/firestore/util/firebase_assert.h +++ b/Firestore/core/src/firebase/firestore/util/firebase_assert.h @@ -20,7 +20,7 @@ #ifndef FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_UTIL_FIREBASE_ASSERT_H_ #define FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_UTIL_FIREBASE_ASSERT_H_ -#include <stdlib.h> +#include <cstdlib> #include "Firestore/core/src/firebase/firestore/util/log.h" #include "absl/base/attributes.h" @@ -76,10 +76,6 @@ } \ } while (0) -// Assert with custom message that is not compiled out in release builds. -#define FIREBASE_ASSERT_MESSAGE(expression, ...) \ - FIREBASE_ASSERT_MESSAGE_WITH_EXPRESSION(expression, expression, __VA_ARGS__) - // Assert condition is true otherwise display the specified expression, // message and abort. Compiled out of release builds. #if defined(NDEBUG) @@ -103,16 +99,18 @@ FIREBASE_DEV_ASSERT_MESSAGE_WITH_EXPRESSION(expression, expression, \ __VA_ARGS__) +// Indicates an area of the code that cannot be reached (except possibly due to +// undefined behaviour or other similar badness). The only reasonable thing to +// do in these cases is to immediately abort. +#define FIREBASE_UNREACHABLE() abort() + namespace firebase { namespace firestore { namespace util { // A no-return helper function. To raise an assertion, use Macro instead. -ABSL_ATTRIBUTE_NORETURN void FailAssert(const char* file, - const char* func, - const int line, - const char* format, - ...); +ABSL_ATTRIBUTE_NORETURN void FailAssert( + const char* file, const char* func, int line, const char* format, ...); } // namespace util } // namespace firestore diff --git a/Firestore/core/src/firebase/firestore/util/log.h b/Firestore/core/src/firebase/firestore/util/log.h index d0cff4d..1944596 100644 --- a/Firestore/core/src/firebase/firestore/util/log.h +++ b/Firestore/core/src/firebase/firestore/util/log.h @@ -17,7 +17,7 @@ #ifndef FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_UTIL_LOG_H_ #define FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_UTIL_LOG_H_ -#include <stdarg.h> +#include <cstdarg> namespace firebase { namespace firestore { diff --git a/Firestore/core/src/firebase/firestore/util/log_stdio.cc b/Firestore/core/src/firebase/firestore/util/log_stdio.cc index bca2dc9..b277406 100644 --- a/Firestore/core/src/firebase/firestore/util/log_stdio.cc +++ b/Firestore/core/src/firebase/firestore/util/log_stdio.cc @@ -16,7 +16,7 @@ #include "Firestore/core/src/firebase/firestore/util/log.h" -#include <stdio.h> +#include <cstdio> #include <string> namespace firebase { diff --git a/Firestore/core/src/firebase/firestore/util/ordered_code.cc b/Firestore/core/src/firebase/firestore/util/ordered_code.cc index 0eadf18..cb53b09 100644 --- a/Firestore/core/src/firebase/firestore/util/ordered_code.cc +++ b/Firestore/core/src/firebase/firestore/util/ordered_code.cc @@ -16,12 +16,11 @@ #include "Firestore/core/src/firebase/firestore/util/ordered_code.h" -#include <absl/base/internal/endian.h> -#include <absl/base/internal/unaligned_access.h> -#include <absl/base/port.h> - #include "Firestore/core/src/firebase/firestore/util/bits.h" #include "Firestore/core/src/firebase/firestore/util/firebase_assert.h" +#include "absl/base/internal/endian.h" +#include "absl/base/internal/unaligned_access.h" +#include "absl/base/port.h" #define UNALIGNED_LOAD32 ABSL_INTERNAL_UNALIGNED_LOAD32 #define UNALIGNED_LOAD64 ABSL_INTERNAL_UNALIGNED_LOAD64 diff --git a/Firestore/core/src/firebase/firestore/util/secure_random.h b/Firestore/core/src/firebase/firestore/util/secure_random.h index 95b41e1..f030b5e 100644 --- a/Firestore/core/src/firebase/firestore/util/secure_random.h +++ b/Firestore/core/src/firebase/firestore/util/secure_random.h @@ -17,7 +17,7 @@ #ifndef FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_UTIL_SECURE_RANDOM_H_ #define FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_UTIL_SECURE_RANDOM_H_ -#include <stdint.h> +#include <cstdint> #include <limits> diff --git a/Firestore/core/src/firebase/firestore/util/secure_random_arc4random.cc b/Firestore/core/src/firebase/firestore/util/secure_random_arc4random.cc index 83f72b5..d7e9be3 100644 --- a/Firestore/core/src/firebase/firestore/util/secure_random_arc4random.cc +++ b/Firestore/core/src/firebase/firestore/util/secure_random_arc4random.cc @@ -20,7 +20,7 @@ #if HAVE_ARC4RANDOM -#include <stdlib.h> +#include <cstdlib> namespace firebase { namespace firestore { diff --git a/Firestore/core/src/firebase/firestore/util/secure_random_openssl.cc b/Firestore/core/src/firebase/firestore/util/secure_random_openssl.cc index d3f6e63..e024846 100644 --- a/Firestore/core/src/firebase/firestore/util/secure_random_openssl.cc +++ b/Firestore/core/src/firebase/firestore/util/secure_random_openssl.cc @@ -20,8 +20,8 @@ #if HAVE_OPENSSL_RAND_H -#include <openssl/err.h> -#include <openssl/rand.h> +#include "openssl/err.h" +#include "openssl/rand.h" namespace firebase { namespace firestore { diff --git a/Firestore/core/src/firebase/firestore/util/string_printf.cc b/Firestore/core/src/firebase/firestore/util/string_printf.cc index 9c4e31c..c5483f4 100644 --- a/Firestore/core/src/firebase/firestore/util/string_printf.cc +++ b/Firestore/core/src/firebase/firestore/util/string_printf.cc @@ -16,7 +16,7 @@ #include "Firestore/core/src/firebase/firestore/util/string_printf.h" -#include <stdio.h> +#include <cstdio> namespace firebase { namespace firestore { diff --git a/Firestore/core/src/firebase/firestore/util/string_printf.h b/Firestore/core/src/firebase/firestore/util/string_printf.h index 10dfae9..553af66 100644 --- a/Firestore/core/src/firebase/firestore/util/string_printf.h +++ b/Firestore/core/src/firebase/firestore/util/string_printf.h @@ -17,8 +17,7 @@ #ifndef FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_UTIL_STRING_PRINTF_H_ #define FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_UTIL_STRING_PRINTF_H_ -#include <stdarg.h> - +#include <cstdarg> #include <string> #include "absl/base/attributes.h" |