Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};

///
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -357,20 +357,13 @@ 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;

std::uint8_t const* _raw;
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<stdx::string_view> _key;
};

///
Expand Down
2 changes: 0 additions & 2 deletions src/bsoncxx/lib/bsoncxx/v_noabi/bsoncxx/array/element.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
6 changes: 3 additions & 3 deletions src/bsoncxx/lib/bsoncxx/v_noabi/bsoncxx/array/view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
28 changes: 10 additions & 18 deletions src/bsoncxx/lib/bsoncxx/v_noabi/bsoncxx/document/element.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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 <bsoncxx/enums/type.hpp>
#undef BSONCXX_ENUM
Expand Down
6 changes: 2 additions & 4 deletions src/bsoncxx/lib/bsoncxx/v_noabi/bsoncxx/document/view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<int>(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(
Expand Down
21 changes: 6 additions & 15 deletions src/bsoncxx/test/v_noabi/bson_types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"]);
Expand All @@ -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", "") {
Expand All @@ -401,18 +395,15 @@ 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.
REQUIRE(arr.view().find(3) == arr.view().cend());
// 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