-
Notifications
You must be signed in to change notification settings - Fork 1.6k
ValueUtil changes for FieldValue migration #7991
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,8 +19,10 @@ | |
|
||
#include <ostream> | ||
#include <string> | ||
#include <vector> | ||
|
||
#include "Firestore/Protos/nanopb/google/firestore/v1/document.nanopb.h" | ||
#include "absl/types/optional.h" | ||
|
||
namespace firebase { | ||
namespace firestore { | ||
|
@@ -31,6 +33,9 @@ enum class ComparisonResult; | |
|
||
namespace model { | ||
|
||
class DocumentKey; | ||
class DatabaseId; | ||
|
||
/** | ||
* The order of types in Firestore. This order is based on the backend's | ||
* ordering, but modified to support server timestamps. | ||
|
@@ -58,14 +63,70 @@ util::ComparisonResult Compare(const google_firestore_v1_Value& left, | |
bool Equals(const google_firestore_v1_Value& left, | ||
const google_firestore_v1_Value& right); | ||
|
||
bool Equals(const google_firestore_v1_ArrayValue& left, | ||
const google_firestore_v1_ArrayValue& right); | ||
|
||
/** | ||
* Generate the canonical ID for the provided field value (as used in Target | ||
* serialization). | ||
*/ | ||
std::string CanonicalId(const google_firestore_v1_Value& value); | ||
|
||
/** | ||
* Generate the canonical ID for the provided array value (as used in Target | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "Generates" There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
* serialization). | ||
*/ | ||
std::string CanonicalId(const google_firestore_v1_ArrayValue& value); | ||
|
||
/** Returns true if the array value contains the specified element. */ | ||
bool Contains(google_firestore_v1_ArrayValue haystack, | ||
google_firestore_v1_Value needle); | ||
|
||
/** Returns a null Protobuf value. */ | ||
google_firestore_v1_Value NullValue(); | ||
|
||
/** Returns `true` if `value` is null in its Protobuf representation. */ | ||
bool IsNullValue(const google_firestore_v1_Value& value); | ||
|
||
/** Returns `NaN` in its Protobuf representation. */ | ||
google_firestore_v1_Value NaNValue(); | ||
|
||
/** Returns `true` if `value` is `NaN` in its Protobuf representation. */ | ||
bool IsNaNValue(const google_firestore_v1_Value& value); | ||
|
||
/** Returns a Protobuf reference value representing the given location. */ | ||
google_firestore_v1_Value RefValue(const DatabaseId& database_id, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add comment. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
const DocumentKey& document_key); | ||
|
||
/** Creates a copy of the contents of the Value proto. */ | ||
google_firestore_v1_Value DeepClone(google_firestore_v1_Value source); | ||
google_firestore_v1_Value DeepClone(const google_firestore_v1_Value& source); | ||
|
||
/** Creates a copy of the contents of the ArrayValue proto. */ | ||
google_firestore_v1_ArrayValue DeepClone( | ||
const google_firestore_v1_ArrayValue& source); | ||
|
||
/** Returns true if `value` is a INTEGER_VALUE. */ | ||
inline bool IsInteger(const absl::optional<google_firestore_v1_Value>& value) { | ||
return value && | ||
value->which_value_type == google_firestore_v1_Value_integer_value_tag; | ||
} | ||
|
||
/** Returns true if `value` is a DOUBLE_VALUE. */ | ||
inline bool IsDouble(const absl::optional<google_firestore_v1_Value>& value) { | ||
return value && | ||
value->which_value_type == google_firestore_v1_Value_double_value_tag; | ||
} | ||
|
||
/** Returns true if `value` is either a INTEGER_VALUE or a DOUBLE_VALUE. */ | ||
inline bool IsNumber(const absl::optional<google_firestore_v1_Value>& value) { | ||
return IsInteger(value) || IsDouble(value); | ||
} | ||
|
||
/** Returns true if `value` is an ARRAY_VALUE. */ | ||
inline bool IsArray(const absl::optional<google_firestore_v1_Value>& value) { | ||
return value && | ||
value->which_value_type == google_firestore_v1_Value_array_value_tag; | ||
} | ||
|
||
} // namespace model | ||
|
||
|
@@ -79,6 +140,16 @@ inline bool operator!=(const google_firestore_v1_Value& lhs, | |
return !model::Equals(lhs, rhs); | ||
} | ||
|
||
inline bool operator==(const google_firestore_v1_ArrayValue& lhs, | ||
const google_firestore_v1_ArrayValue& rhs) { | ||
return model::Equals(lhs, rhs); | ||
} | ||
|
||
inline bool operator!=(const google_firestore_v1_ArrayValue& lhs, | ||
const google_firestore_v1_ArrayValue& rhs) { | ||
return !model::Equals(lhs, rhs); | ||
} | ||
|
||
inline std::ostream& operator<<(std::ostream& out, | ||
const google_firestore_v1_Value& value) { | ||
return out << model::CanonicalId(value); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Question: this seems to be a behavior change from below? Can you explain why? Not that I am against..
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not meant to be a behavior change but rather a bug fix. When we create an empty bytes array directly with NanoPB, it uses
nullptr
and does not actually allocate memory. When we receive documents from the backend, these empty byte arrays are represented using a zero-length array, but one with an actual address in memory. We need these two representations to yield the same comparison behavior so our users don't see any behavior change.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good, thanks.