1717#include " Firestore/core/src/core/order_by.h"
1818
1919#include < ostream>
20+ #include < sstream>
2021
2122#include " Firestore/core/src/model/document.h"
2223#include " Firestore/core/src/model/value_util.h"
@@ -31,6 +32,38 @@ using model::Document;
3132using model::FieldPath;
3233using util::ComparisonResult;
3334
35+ namespace {
36+
37+ void AssertBothOptionalsHaveValues (
38+ const model::FieldPath& field_path,
39+ const absl::optional<google_firestore_v1_Value>& value1,
40+ const absl::optional<google_firestore_v1_Value>& value2,
41+ const Document& lhs,
42+ const Document& rhs) {
43+ if (value1.has_value () && value2.has_value ()) {
44+ return ;
45+ }
46+
47+ std::ostringstream ss;
48+ ss << " Trying to compare documents on fields that don't exist;"
49+ << " field_path=" << field_path.CanonicalString ()
50+ << " , lhs=" << lhs->key ().ToString () << " , rhs=" << rhs->key ().ToString ()
51+ << " , value1.has_value()=" << (value1.has_value () ? " true" : " false" )
52+ << " , value2.has_value()=" << (value2.has_value () ? " true" : " false" );
53+
54+ if (value1.has_value ()) {
55+ ss << " , value1=" << value1->ToString ();
56+ }
57+ if (value2.has_value ()) {
58+ ss << " , value2=" << value2->ToString ();
59+ }
60+
61+ std::string message = ss.str ();
62+ HARD_FAIL (message.c_str ());
63+ }
64+
65+ } // namespace
66+
3467ComparisonResult OrderBy::Compare (const Document& lhs,
3568 const Document& rhs) const {
3669 ComparisonResult result;
@@ -39,8 +72,7 @@ ComparisonResult OrderBy::Compare(const Document& lhs,
3972 } else {
4073 absl::optional<google_firestore_v1_Value> value1 = lhs->field (field_);
4174 absl::optional<google_firestore_v1_Value> value2 = rhs->field (field_);
42- HARD_ASSERT (value1.has_value () && value2.has_value (),
43- " Trying to compare documents on fields that don't exist." );
75+ AssertBothOptionalsHaveValues (field_, value1, value2, lhs, rhs);
4476 result = model::Compare (*value1, *value2);
4577 }
4678
0 commit comments