From adf9fb31eeef639ef23b2ff22a71adaa91a263b7 Mon Sep 17 00:00:00 2001 From: zxu Date: Mon, 12 Feb 2018 17:00:25 -0500 Subject: Update FieldValue of type Reference (#775) * update FieldValue of type Reference * address change * fix bad path string literal in test * use ReferenceValue and qualified name --- .../src/firebase/firestore/model/field_value.cc | 41 ++++++++++++++++++++++ .../src/firebase/firestore/model/field_value.h | 15 +++++++- 2 files changed, 55 insertions(+), 1 deletion(-) (limited to 'Firestore/core/src/firebase/firestore/model') diff --git a/Firestore/core/src/firebase/firestore/model/field_value.cc b/Firestore/core/src/firebase/firestore/model/field_value.cc index 570226e..012a90d 100644 --- a/Firestore/core/src/firebase/firestore/model/field_value.cc +++ b/Firestore/core/src/firebase/firestore/model/field_value.cc @@ -99,6 +99,9 @@ FieldValue& FieldValue::operator=(const FieldValue& value) { std::swap(blob_value_, tmp); break; } + case Type::Reference: + reference_value_ = value.reference_value_; + break; case Type::GeoPoint: geo_point_value_ = value.geo_point_value_; break; @@ -131,6 +134,11 @@ FieldValue& FieldValue::operator=(FieldValue&& value) { SwitchTo(Type::Blob); std::swap(blob_value_, value.blob_value_); return *this; + case Type::Reference: + SwitchTo(Type::Reference); + std::swap(reference_value_.reference, value.reference_value_.reference); + reference_value_.database_id = value.reference_value_.database_id; + return *this; case Type::Array: SwitchTo(Type::Array); std::swap(array_value_, value.array_value_); @@ -233,6 +241,26 @@ FieldValue FieldValue::BlobValue(const uint8_t* source, size_t size) { return result; } +// Does NOT pass ownership of database_id. +FieldValue FieldValue::ReferenceValue(const DocumentKey& value, + const DatabaseId* database_id) { + FieldValue result; + result.SwitchTo(Type::Reference); + result.reference_value_.reference = value; + result.reference_value_.database_id = database_id; + return result; +} + +// Does NOT pass ownership of database_id. +FieldValue FieldValue::ReferenceValue(DocumentKey&& value, + const DatabaseId* database_id) { + FieldValue result; + result.SwitchTo(Type::Reference); + std::swap(result.reference_value_.reference, value); + result.reference_value_.database_id = database_id; + return result; +} + FieldValue FieldValue::GeoPointValue(const GeoPoint& value) { FieldValue result; result.SwitchTo(Type::GeoPoint); @@ -309,6 +337,12 @@ bool operator<(const FieldValue& lhs, const FieldValue& rhs) { return lhs.string_value_.compare(rhs.string_value_) < 0; case Type::Blob: return lhs.blob_value_ < rhs.blob_value_; + case Type::Reference: + return *lhs.reference_value_.database_id < + *rhs.reference_value_.database_id || + (*lhs.reference_value_.database_id == + *rhs.reference_value_.database_id && + lhs.reference_value_.reference < rhs.reference_value_.reference); case Type::GeoPoint: return lhs.geo_point_value_ < rhs.geo_point_value_; case Type::Array: @@ -343,6 +377,9 @@ void FieldValue::SwitchTo(const Type type) { case Type::Blob: blob_value_.~vector(); break; + case Type::Reference: + reference_value_.~ReferenceValue(); + break; case Type::GeoPoint: geo_point_value_.~GeoPoint(); break; @@ -370,6 +407,10 @@ void FieldValue::SwitchTo(const Type type) { // Do not even bother to allocate a new array of size 0. new (&blob_value_) std::vector(); break; + case Type::Reference: + // Qualified name to avoid conflict with the member function of same name. + new (&reference_value_) firebase::firestore::model::ReferenceValue(); + break; case Type::GeoPoint: new (&geo_point_value_) GeoPoint(); break; diff --git a/Firestore/core/src/firebase/firestore/model/field_value.h b/Firestore/core/src/firebase/firestore/model/field_value.h index d43b23f..e547be3 100644 --- a/Firestore/core/src/firebase/firestore/model/field_value.h +++ b/Firestore/core/src/firebase/firestore/model/field_value.h @@ -25,6 +25,8 @@ #include #include "Firestore/core/include/firebase/firestore/geo_point.h" +#include "Firestore/core/src/firebase/firestore/model/database_id.h" +#include "Firestore/core/src/firebase/firestore/model/document_key.h" #include "Firestore/core/src/firebase/firestore/model/timestamp.h" namespace firebase { @@ -38,6 +40,12 @@ struct ServerTimestamp { bool has_previous_value_; }; +struct ReferenceValue { + DocumentKey reference; + // Does not own the DatabaseId instance. + const DatabaseId* database_id; +}; + /** * tagged-union class representing an immutable data value as stored in * Firestore. FieldValue represents all the different kinds of values @@ -103,7 +111,10 @@ class FieldValue { static FieldValue StringValue(const std::string& value); static FieldValue StringValue(std::string&& value); static FieldValue BlobValue(const uint8_t* source, size_t size); - // static FieldValue ReferenceValue(); + static FieldValue ReferenceValue(const DocumentKey& value, + const DatabaseId* database_id); + static FieldValue ReferenceValue(DocumentKey&& value, + const DatabaseId* database_id); static FieldValue GeoPointValue(const GeoPoint& value); static FieldValue ArrayValue(const std::vector& value); static FieldValue ArrayValue(std::vector&& value); @@ -133,6 +144,8 @@ class FieldValue { ServerTimestamp server_timestamp_value_; std::string string_value_; std::vector blob_value_; + // Qualified name to avoid conflict with the member function of same name. + firebase::firestore::model::ReferenceValue reference_value_; GeoPoint geo_point_value_; std::vector array_value_; std::map object_value_; -- cgit v1.2.3