Skip to content

Commit 19bf883

Browse files
authored
Partially revert "CXX-2120 handle nonexistent document fields (#984)" (#1445)
This partially reverts commit 16777df.
1 parent 63f8ecd commit 19bf883

File tree

7 files changed

+21
-51
lines changed

7 files changed

+21
-51
lines changed

src/bsoncxx/include/bsoncxx/v_noabi/bsoncxx/array/element.hpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,6 @@ class element : private document::element {
8080
friend ::bsoncxx::v_noabi::array::view;
8181

8282
explicit element(std::uint8_t const* raw, std::uint32_t length, std::uint32_t offset, std::uint32_t keylen);
83-
84-
explicit element(stdx::string_view const key);
8583
};
8684

8785
///

src/bsoncxx/include/bsoncxx/v_noabi/bsoncxx/document/element.hpp

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -357,20 +357,13 @@ class element {
357357
///
358358
explicit element(std::uint8_t const* raw, std::uint32_t length, std::uint32_t offset, std::uint32_t keylen);
359359

360-
// Construct an invalid element with a key. Useful for exceptions.
361-
explicit element(stdx::string_view const key);
362-
363360
friend ::bsoncxx::v_noabi::array::element;
364361
friend ::bsoncxx::v_noabi::document::view;
365362

366363
std::uint8_t const* _raw;
367364
std::uint32_t _length;
368365
std::uint32_t _offset;
369366
std::uint32_t _keylen;
370-
// _key will only exist when a caller attempts to find a key in the BSON but is unsuccessful.
371-
// The key is stored for a more helpful error message if the user tries to access the value of
372-
// a key that does not exist.
373-
stdx::optional<stdx::string_view> _key;
374367
};
375368

376369
///

src/bsoncxx/lib/bsoncxx/v_noabi/bsoncxx/array/element.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ element::element() : document::element() {}
2626
element::element(std::uint8_t const* raw, std::uint32_t length, std::uint32_t offset, std::uint32_t keylen)
2727
: document::element(raw, length, offset, keylen) {}
2828

29-
element::element(stdx::string_view const key) : document::element(key) {}
30-
3129
bool operator==(element const& elem, types::bson_value::view const& v) {
3230
return elem.get_value() == v;
3331
}

src/bsoncxx/lib/bsoncxx/v_noabi/bsoncxx/array/view.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,15 +115,15 @@ view::const_iterator view::find(std::uint32_t i) const {
115115
bson_iter_t iter;
116116

117117
if (!bson_init_static(&b, data(), length())) {
118-
return const_iterator(element(key.c_str()));
118+
return const_iterator();
119119
}
120120

121121
if (!bson_iter_init(&iter, &b)) {
122-
return const_iterator(element(key.c_str()));
122+
return const_iterator();
123123
}
124124

125125
if (!bson_iter_init_find(&iter, &b, key.c_str())) {
126-
return const_iterator(element(key.c_str()));
126+
return const_iterator();
127127
}
128128

129129
return const_iterator(

src/bsoncxx/lib/bsoncxx/v_noabi/bsoncxx/document/element.cpp

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,6 @@ element::element() : element(nullptr, 0, 0, 0) {}
4444
element::element(std::uint8_t const* raw, std::uint32_t length, std::uint32_t offset, std::uint32_t keylen)
4545
: _raw(raw), _length(length), _offset(offset), _keylen(keylen) {}
4646

47-
element::element(stdx::string_view const key) : _raw(nullptr), _length(0), _offset(0), _keylen(0), _key(key) {}
48-
4947
std::uint8_t const* element::raw() const {
5048
return _raw;
5149
}
@@ -64,9 +62,7 @@ std::uint32_t element::keylen() const {
6462
bsoncxx::v_noabi::type element::type() const {
6563
if (_raw == nullptr) {
6664
throw bsoncxx::v_noabi::exception{
67-
error_code::k_unset_element,
68-
"cannot return the type of uninitialized element" +
69-
std::string(_key ? " with key \"" + std::string(_key.value().data()) + "\"" : "")};
65+
error_code::k_unset_element, "cannot return the type of uninitialized element"};
7066
}
7167

7268
BSONCXX_CITER;
@@ -76,9 +72,7 @@ bsoncxx::v_noabi::type element::type() const {
7672
stdx::string_view element::key() const {
7773
if (_raw == nullptr) {
7874
throw bsoncxx::v_noabi::exception{
79-
error_code::k_unset_element,
80-
"cannot return the key from an uninitialized element" +
81-
std::string(_key ? " with key \"" + std::string(_key.value().data()) + "\"" : "")};
75+
error_code::k_unset_element, "cannot return the key from an uninitialized element"};
8276
}
8377

8478
BSONCXX_CITER;
@@ -88,16 +82,14 @@ stdx::string_view element::key() const {
8882
return stdx::string_view{key};
8983
}
9084

91-
#define BSONCXX_ENUM(name, val) \
92-
types::b_##name element::get_##name() const { \
93-
if (_raw == nullptr) { \
94-
throw bsoncxx::v_noabi::exception{ \
95-
error_code::k_unset_element, \
96-
"cannot get " #name " from an uninitialized element" + \
97-
std::string(_key ? " with key \"" + std::string(_key.value().data()) + "\"" : "")}; \
98-
} \
99-
types::bson_value::view v{_raw, _length, _offset, _keylen}; \
100-
return v.get_##name(); \
85+
#define BSONCXX_ENUM(name, val) \
86+
types::b_##name element::get_##name() const { \
87+
if (_raw == nullptr) { \
88+
throw bsoncxx::v_noabi::exception{ \
89+
error_code::k_unset_element, "cannot get " #name " from an uninitialized element"}; \
90+
} \
91+
types::bson_value::view v{_raw, _length, _offset, _keylen}; \
92+
return v.get_##name(); \
10193
}
10294
#include <bsoncxx/enums/type.hpp>
10395
#undef BSONCXX_ENUM

src/bsoncxx/lib/bsoncxx/v_noabi/bsoncxx/document/view.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,7 @@ view::const_iterator view::end() const {
111111
view::const_iterator view::find(stdx::string_view key) const {
112112
bson_t b;
113113
if (!bson_init_static(&b, _data, _length)) {
114-
// return invalid element with key to provide more helpful exception message.
115-
return const_iterator(element(key));
114+
return const_iterator();
116115
}
117116

118117
bson_iter_t iter;
@@ -132,8 +131,7 @@ view::const_iterator view::find(stdx::string_view key) const {
132131

133132
if (!bson_iter_init_find_w_len(&iter, &b, key.data(), static_cast<int>(key.size()))) {
134133
// returning `cend()` returns an element without a key or value.
135-
// return invalid element with key to provide more helpful exception
136-
return const_iterator(element(key));
134+
return const_iterator();
137135
}
138136

139137
return const_iterator(

src/bsoncxx/test/v_noabi/bson_types.cpp

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -372,15 +372,11 @@ TEST_CASE("document uninitialized element throws exceptions", "") {
372372
REQUIRE_THROWS_WITH(
373373
doc["doesnotexist"].get_string().value,
374374

375-
Catch::Matchers::ContainsSubstring(
376-
"cannot get string from an uninitialized element with key "
377-
"\"doesnotexist\": unset document::element"));
375+
Catch::Matchers::Equals("cannot get string from an uninitialized element: unset document::element"));
378376

379377
REQUIRE_THROWS_WITH(
380378
doc["alsodoesnotexist"].get_value(),
381-
Catch::Matchers::ContainsSubstring(
382-
"cannot return the type of uninitialized element with key "
383-
"\"alsodoesnotexist\": unset document::element"));
379+
Catch::Matchers::Equals("cannot return the type of uninitialized element: unset document::element"));
384380

385381
// Ensure a non-existing element evaluates to false.
386382
REQUIRE(!doc["doesnotexist"]);
@@ -389,9 +385,7 @@ TEST_CASE("document uninitialized element throws exceptions", "") {
389385
// Ensure getting a key from a non-existing element results in an exception.
390386
REQUIRE_THROWS_WITH(
391387
doc["doesnotexist"].key(),
392-
Catch::Matchers::ContainsSubstring(
393-
"cannot return the key from an uninitialized element with key "
394-
"\"doesnotexist\": unset document::element"));
388+
Catch::Matchers::Equals("cannot return the key from an uninitialized element: unset document::element"));
395389
}
396390

397391
TEST_CASE("array uninitialized element throws exceptions", "") {
@@ -401,18 +395,15 @@ TEST_CASE("array uninitialized element throws exceptions", "") {
401395

402396
REQUIRE_THROWS_WITH(
403397
arr.view()[3].get_string().value,
404-
Catch::Matchers::ContainsSubstring(
405-
"cannot get string from an uninitialized element with key "
406-
"\"3\": unset document::element"));
398+
Catch::Matchers::Equals("cannot get string from an uninitialized element: unset document::element"));
407399
// Ensure a non-existing element evaluates to false.
408400
REQUIRE(!arr.view()[3]);
409401
// Ensure finding a non-existing element results in an end iterator.
410402
REQUIRE(arr.view().find(3) == arr.view().cend());
411403
// Ensure getting a key from a non-existing element results in an exception.
412404
REQUIRE_THROWS_WITH(
413405
arr.view()[3].key(),
414-
Catch::Matchers::ContainsSubstring(
415-
"cannot return the key from an uninitialized element with key "
416-
"\"3\": unset document::element"));
406+
Catch::Matchers::Equals("cannot return the key from an uninitialized element: unset document::element"));
417407
}
408+
418409
} // namespace

0 commit comments

Comments
 (0)