diff --git a/src/bsoncxx/include/bsoncxx/v_noabi/bsoncxx/array/element.hpp b/src/bsoncxx/include/bsoncxx/v_noabi/bsoncxx/array/element.hpp index 2967978f99..c5e971b750 100644 --- a/src/bsoncxx/include/bsoncxx/v_noabi/bsoncxx/array/element.hpp +++ b/src/bsoncxx/include/bsoncxx/v_noabi/bsoncxx/array/element.hpp @@ -80,8 +80,6 @@ class element : private document::element { friend ::bsoncxx::v_noabi::array::view; explicit element(std::uint8_t const* raw, std::uint32_t length, std::uint32_t offset, std::uint32_t keylen); - - explicit element(stdx::string_view const key); }; /// diff --git a/src/bsoncxx/include/bsoncxx/v_noabi/bsoncxx/document/element.hpp b/src/bsoncxx/include/bsoncxx/v_noabi/bsoncxx/document/element.hpp index 60e91e10ce..5d96670321 100644 --- a/src/bsoncxx/include/bsoncxx/v_noabi/bsoncxx/document/element.hpp +++ b/src/bsoncxx/include/bsoncxx/v_noabi/bsoncxx/document/element.hpp @@ -357,9 +357,6 @@ class element { /// explicit element(std::uint8_t const* raw, std::uint32_t length, std::uint32_t offset, std::uint32_t keylen); - // Construct an invalid element with a key. Useful for exceptions. - explicit element(stdx::string_view const key); - friend ::bsoncxx::v_noabi::array::element; friend ::bsoncxx::v_noabi::document::view; @@ -367,10 +364,6 @@ class element { std::uint32_t _length; std::uint32_t _offset; std::uint32_t _keylen; - // _key will only exist when a caller attempts to find a key in the BSON but is unsuccessful. - // The key is stored for a more helpful error message if the user tries to access the value of - // a key that does not exist. - stdx::optional _key; }; /// diff --git a/src/bsoncxx/lib/bsoncxx/v_noabi/bsoncxx/array/element.cpp b/src/bsoncxx/lib/bsoncxx/v_noabi/bsoncxx/array/element.cpp index 058c9dbd65..f384b12e1f 100644 --- a/src/bsoncxx/lib/bsoncxx/v_noabi/bsoncxx/array/element.cpp +++ b/src/bsoncxx/lib/bsoncxx/v_noabi/bsoncxx/array/element.cpp @@ -26,8 +26,6 @@ element::element() : document::element() {} element::element(std::uint8_t const* raw, std::uint32_t length, std::uint32_t offset, std::uint32_t keylen) : document::element(raw, length, offset, keylen) {} -element::element(stdx::string_view const key) : document::element(key) {} - bool operator==(element const& elem, types::bson_value::view const& v) { return elem.get_value() == v; } diff --git a/src/bsoncxx/lib/bsoncxx/v_noabi/bsoncxx/array/view.cpp b/src/bsoncxx/lib/bsoncxx/v_noabi/bsoncxx/array/view.cpp index 4e256d9e1f..ed462a8c7d 100644 --- a/src/bsoncxx/lib/bsoncxx/v_noabi/bsoncxx/array/view.cpp +++ b/src/bsoncxx/lib/bsoncxx/v_noabi/bsoncxx/array/view.cpp @@ -115,15 +115,15 @@ view::const_iterator view::find(std::uint32_t i) const { bson_iter_t iter; if (!bson_init_static(&b, data(), length())) { - return const_iterator(element(key.c_str())); + return const_iterator(); } if (!bson_iter_init(&iter, &b)) { - return const_iterator(element(key.c_str())); + return const_iterator(); } if (!bson_iter_init_find(&iter, &b, key.c_str())) { - return const_iterator(element(key.c_str())); + return const_iterator(); } return const_iterator( diff --git a/src/bsoncxx/lib/bsoncxx/v_noabi/bsoncxx/document/element.cpp b/src/bsoncxx/lib/bsoncxx/v_noabi/bsoncxx/document/element.cpp index a94861f3fc..c1f1398e83 100644 --- a/src/bsoncxx/lib/bsoncxx/v_noabi/bsoncxx/document/element.cpp +++ b/src/bsoncxx/lib/bsoncxx/v_noabi/bsoncxx/document/element.cpp @@ -44,8 +44,6 @@ element::element() : element(nullptr, 0, 0, 0) {} element::element(std::uint8_t const* raw, std::uint32_t length, std::uint32_t offset, std::uint32_t keylen) : _raw(raw), _length(length), _offset(offset), _keylen(keylen) {} -element::element(stdx::string_view const key) : _raw(nullptr), _length(0), _offset(0), _keylen(0), _key(key) {} - std::uint8_t const* element::raw() const { return _raw; } @@ -64,9 +62,7 @@ std::uint32_t element::keylen() const { bsoncxx::v_noabi::type element::type() const { if (_raw == nullptr) { throw bsoncxx::v_noabi::exception{ - error_code::k_unset_element, - "cannot return the type of uninitialized element" + - std::string(_key ? " with key \"" + std::string(_key.value().data()) + "\"" : "")}; + error_code::k_unset_element, "cannot return the type of uninitialized element"}; } BSONCXX_CITER; @@ -76,9 +72,7 @@ bsoncxx::v_noabi::type element::type() const { stdx::string_view element::key() const { if (_raw == nullptr) { throw bsoncxx::v_noabi::exception{ - error_code::k_unset_element, - "cannot return the key from an uninitialized element" + - std::string(_key ? " with key \"" + std::string(_key.value().data()) + "\"" : "")}; + error_code::k_unset_element, "cannot return the key from an uninitialized element"}; } BSONCXX_CITER; @@ -88,16 +82,14 @@ stdx::string_view element::key() const { return stdx::string_view{key}; } -#define BSONCXX_ENUM(name, val) \ - types::b_##name element::get_##name() const { \ - if (_raw == nullptr) { \ - throw bsoncxx::v_noabi::exception{ \ - error_code::k_unset_element, \ - "cannot get " #name " from an uninitialized element" + \ - std::string(_key ? " with key \"" + std::string(_key.value().data()) + "\"" : "")}; \ - } \ - types::bson_value::view v{_raw, _length, _offset, _keylen}; \ - return v.get_##name(); \ +#define BSONCXX_ENUM(name, val) \ + types::b_##name element::get_##name() const { \ + if (_raw == nullptr) { \ + throw bsoncxx::v_noabi::exception{ \ + error_code::k_unset_element, "cannot get " #name " from an uninitialized element"}; \ + } \ + types::bson_value::view v{_raw, _length, _offset, _keylen}; \ + return v.get_##name(); \ } #include #undef BSONCXX_ENUM diff --git a/src/bsoncxx/lib/bsoncxx/v_noabi/bsoncxx/document/view.cpp b/src/bsoncxx/lib/bsoncxx/v_noabi/bsoncxx/document/view.cpp index 4109c86f56..38c5a057f9 100644 --- a/src/bsoncxx/lib/bsoncxx/v_noabi/bsoncxx/document/view.cpp +++ b/src/bsoncxx/lib/bsoncxx/v_noabi/bsoncxx/document/view.cpp @@ -111,8 +111,7 @@ view::const_iterator view::end() const { view::const_iterator view::find(stdx::string_view key) const { bson_t b; if (!bson_init_static(&b, _data, _length)) { - // return invalid element with key to provide more helpful exception message. - return const_iterator(element(key)); + return const_iterator(); } bson_iter_t iter; @@ -132,8 +131,7 @@ view::const_iterator view::find(stdx::string_view key) const { if (!bson_iter_init_find_w_len(&iter, &b, key.data(), static_cast(key.size()))) { // returning `cend()` returns an element without a key or value. - // return invalid element with key to provide more helpful exception - return const_iterator(element(key)); + return const_iterator(); } return const_iterator( diff --git a/src/bsoncxx/test/v_noabi/bson_types.cpp b/src/bsoncxx/test/v_noabi/bson_types.cpp index 0c22786b1a..957c3547ed 100644 --- a/src/bsoncxx/test/v_noabi/bson_types.cpp +++ b/src/bsoncxx/test/v_noabi/bson_types.cpp @@ -372,15 +372,11 @@ TEST_CASE("document uninitialized element throws exceptions", "") { REQUIRE_THROWS_WITH( doc["doesnotexist"].get_string().value, - Catch::Matchers::ContainsSubstring( - "cannot get string from an uninitialized element with key " - "\"doesnotexist\": unset document::element")); + Catch::Matchers::Equals("cannot get string from an uninitialized element: unset document::element")); REQUIRE_THROWS_WITH( doc["alsodoesnotexist"].get_value(), - Catch::Matchers::ContainsSubstring( - "cannot return the type of uninitialized element with key " - "\"alsodoesnotexist\": unset document::element")); + Catch::Matchers::Equals("cannot return the type of uninitialized element: unset document::element")); // Ensure a non-existing element evaluates to false. REQUIRE(!doc["doesnotexist"]); @@ -389,9 +385,7 @@ TEST_CASE("document uninitialized element throws exceptions", "") { // Ensure getting a key from a non-existing element results in an exception. REQUIRE_THROWS_WITH( doc["doesnotexist"].key(), - Catch::Matchers::ContainsSubstring( - "cannot return the key from an uninitialized element with key " - "\"doesnotexist\": unset document::element")); + Catch::Matchers::Equals("cannot return the key from an uninitialized element: unset document::element")); } TEST_CASE("array uninitialized element throws exceptions", "") { @@ -401,9 +395,7 @@ TEST_CASE("array uninitialized element throws exceptions", "") { REQUIRE_THROWS_WITH( arr.view()[3].get_string().value, - Catch::Matchers::ContainsSubstring( - "cannot get string from an uninitialized element with key " - "\"3\": unset document::element")); + Catch::Matchers::Equals("cannot get string from an uninitialized element: unset document::element")); // Ensure a non-existing element evaluates to false. REQUIRE(!arr.view()[3]); // Ensure finding a non-existing element results in an end iterator. @@ -411,8 +403,7 @@ TEST_CASE("array uninitialized element throws exceptions", "") { // Ensure getting a key from a non-existing element results in an exception. REQUIRE_THROWS_WITH( arr.view()[3].key(), - Catch::Matchers::ContainsSubstring( - "cannot return the key from an uninitialized element with key " - "\"3\": unset document::element")); + Catch::Matchers::Equals("cannot return the key from an uninitialized element: unset document::element")); } + } // namespace