Skip to content

Commit 7ccc2d4

Browse files
authored
CXX-1817 Rename get_utf8() to get_string() to make it more user-friendly (#721)
1 parent 81ebf66 commit 7ccc2d4

39 files changed

+267
-203
lines changed

docs/content/mongocxx-v3/tutorial.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ bsoncxx::document::element element = view["name"];
198198
if(element.type() != bsoncxx::type::k_utf8) {
199199
// Error
200200
}
201-
std::string name = element.get_utf8().value.to_string();
201+
std::string name = element.get_string().value.to_string();
202202
```
203203

204204
If the value in the `name` field is not a string and you do not

examples/bsoncxx/view_and_value.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ int main(int, char**) {
8181
array::view subarr{ele.get_array().value};
8282
for (array::element ele : subarr) {
8383
std::cout << "array element: "
84-
<< bsoncxx::string::to_string(ele.get_utf8().value) << std::endl;
84+
<< bsoncxx::string::to_string(ele.get_string().value) << std::endl;
8585
}
8686
break;
8787
}

examples/mongocxx/change_streams.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ std::string get_server_version(const mongocxx::client& client) {
3131
server_status.append(bsoncxx::builder::basic::kvp("serverStatus", 1));
3232
bsoncxx::document::value output = client["test"].run_command(server_status.extract());
3333

34-
return bsoncxx::string::to_string(output.view()["version"].get_utf8().value);
34+
return bsoncxx::string::to_string(output.view()["version"].get_string().value);
3535
}
3636

3737
void watch_until(const mongocxx::client& client,

examples/mongocxx/get_values_from_documents.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ void iterate_messagelist(const bsoncxx::document::element& ele) {
7676
std::cout << "status: " << status.get_int32().value << std::endl;
7777
}
7878
if (msg && msg.type() == type::k_utf8) {
79-
std::cout << "msg: " << msg.get_utf8().value << std::endl;
79+
std::cout << "msg: " << msg.get_string().value << std::endl;
8080
}
8181
} else {
8282
std::cout << "Message is not a document" << std::endl;

examples/mongocxx/mongodb.com/aggregation_examples.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ std::string get_server_version(const client& client) {
3636
server_status.append(bsoncxx::builder::basic::kvp("serverStatus", 1));
3737
bsoncxx::document::value output = client["test"].run_command(server_status.extract());
3838

39-
return bsoncxx::string::to_string(output.view()["version"].get_utf8().value);
39+
return bsoncxx::string::to_string(output.view()["version"].get_string().value);
4040
}
4141

4242
void aggregation_examples(const mongocxx::client& client, const mongocxx::database& db) {

examples/mongocxx/mongodb.com/documentation_examples.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1036,11 +1036,11 @@ void update_examples(mongocxx::database db) {
10361036
// End Example 52
10371037

10381038
for (auto&& document : db["inventory"].find(make_document(kvp("item", "paper")))) {
1039-
if (document["size"].get_document().value["uom"].get_utf8().value !=
1039+
if (document["size"].get_document().value["uom"].get_string().value !=
10401040
bsoncxx::stdx::string_view{"cm"}) {
10411041
throw std::logic_error("error in example 52");
10421042
}
1043-
if (document["status"].get_utf8().value != bsoncxx::stdx::string_view{"P"}) {
1043+
if (document["status"].get_string().value != bsoncxx::stdx::string_view{"P"}) {
10441044
throw std::logic_error("error in example 52");
10451045
}
10461046
check_has_field(document, "lastModified", 52);
@@ -1060,11 +1060,11 @@ void update_examples(mongocxx::database db) {
10601060

10611061
for (auto&& document :
10621062
db["inventory"].find(make_document(kvp("qty", make_document(kvp("$lt", 50)))))) {
1063-
if (document["size"].get_document().value["uom"].get_utf8().value !=
1063+
if (document["size"].get_document().value["uom"].get_string().value !=
10641064
bsoncxx::stdx::string_view{"in"}) {
10651065
throw std::logic_error("error in example 53");
10661066
}
1067-
if (document["status"].get_utf8().value != bsoncxx::stdx::string_view{"P"}) {
1067+
if (document["status"].get_string().value != bsoncxx::stdx::string_view{"P"}) {
10681068
throw std::logic_error("error in example 53");
10691069
}
10701070
check_has_field(document, "lastModified", 53);

src/bsoncxx/array/element.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class BSONCXX_API element : private document::element {
4343

4444
using document::element::get_double;
4545
using document::element::get_utf8;
46+
using document::element::get_string;
4647
using document::element::get_document;
4748
using document::element::get_array;
4849
using document::element::get_binary;

src/bsoncxx/builder/core.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <bsoncxx/private/itoa.hh>
2222
#include <bsoncxx/private/libbson.hh>
2323
#include <bsoncxx/private/stack.hh>
24+
#include <bsoncxx/private/suppress_deprecation_warnings.hh>
2425
#include <bsoncxx/stdx/make_unique.hpp>
2526
#include <bsoncxx/stdx/string_view.hpp>
2627
#include <bsoncxx/string/to_string.hpp>
@@ -646,12 +647,15 @@ core& core::concatenate(const bsoncxx::document::view& view) {
646647

647648
core& core::append(const bsoncxx::types::bson_value::view& value) {
648649
switch (static_cast<int>(value.type())) {
650+
// CXX-1817; deprecation warning suppressed for get_utf8()
651+
BSONCXX_SUPPRESS_DEPRECATION_WARNINGS_BEGIN
649652
#define BSONCXX_ENUM(type, val) \
650653
case val: \
651654
append(value.get_##type()); \
652655
break;
653656
#include <bsoncxx/enums/type.hpp>
654657
#undef BSONCXX_ENUM
658+
BSONCXX_SUPPRESS_DEPRECATION_WARNINGS_END
655659
}
656660

657661
return *this;

src/bsoncxx/document/element.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <bsoncxx/exception/exception.hpp>
2222
#include <bsoncxx/json.hpp>
2323
#include <bsoncxx/private/libbson.hh>
24+
#include <bsoncxx/private/suppress_deprecation_warnings.hh>
2425
#include <bsoncxx/types.hpp>
2526
#include <bsoncxx/types/bson_value/value.hpp>
2627
#include <bsoncxx/types/bson_value/view.hpp>
@@ -79,21 +80,32 @@ stdx::string_view element::key() const {
7980
return stdx::string_view{key};
8081
}
8182

83+
// CXX-1817; deprecation warning suppressed for get_utf8()
84+
BSONCXX_SUPPRESS_DEPRECATION_WARNINGS_BEGIN
8285
#define BSONCXX_ENUM(name, val) \
8386
types::b_##name element::get_##name() const { \
8487
types::bson_value::view v{_raw, _length, _offset, _keylen}; \
8588
return v.get_##name(); \
8689
}
8790
#include <bsoncxx/enums/type.hpp>
8891
#undef BSONCXX_ENUM
92+
BSONCXX_SUPPRESS_DEPRECATION_WARNINGS_END
93+
94+
types::b_utf8 element::get_string() const {
95+
types::bson_value::view v{_raw, _length, _offset, _keylen};
96+
return v.get_string();
97+
}
8998

9099
types::bson_value::view element::get_value() const {
91100
switch (static_cast<int>(type())) {
101+
// CXX-1817; deprecation warning suppressed for get_utf8()
102+
BSONCXX_SUPPRESS_DEPRECATION_WARNINGS_BEGIN
92103
#define BSONCXX_ENUM(type, val) \
93104
case val: \
94105
return types::bson_value::view{get_##type()};
95106
#include <bsoncxx/enums/type.hpp>
96107
#undef BSONCXX_ENUM
108+
BSONCXX_SUPPRESS_DEPRECATION_WARNINGS_END
97109
}
98110

99111
BSONCXX_UNREACHABLE;

src/bsoncxx/document/element.hpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,11 +147,23 @@ class BSONCXX_API element {
147147
///
148148
/// Getter for elements of the b_utf8 type.
149149
///
150+
/// @deprecated use document::element::get_string() instead.
151+
///
152+
/// @throws bsoncxx::exception if this element is not a b_utf8.
153+
///
154+
/// @return the element's value.
155+
///
156+
BSONCXX_DEPRECATED types::b_utf8 get_utf8() const;
157+
158+
///
159+
/// Getter for elements of the b_utf8, or string, type. This function acts as a
160+
/// wrapper for get_utf8().
161+
///
150162
/// @throws bsoncxx::exception if this element is not a b_utf8.
151163
///
152164
/// @return the element's value.
153165
///
154-
types::b_utf8 get_utf8() const;
166+
types::b_utf8 get_string() const;
155167

156168
///
157169
/// Getter for elements of the b_document type.

src/bsoncxx/test/bson_builder.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -813,7 +813,7 @@ TEST_CASE("core method chaining to build array works", "[bsoncxx::builder::core]
813813

814814
REQUIRE(std::distance(array_view.begin(), array_view.end()) == 3);
815815
REQUIRE(array_view[0].type() == type::k_utf8);
816-
REQUIRE(string::to_string(array_view[0].get_utf8().value) == "foo");
816+
REQUIRE(string::to_string(array_view[0].get_string().value) == "foo");
817817
REQUIRE(array_view[1].type() == type::k_int32);
818818
REQUIRE(array_view[1].get_int32().value == 1);
819819
REQUIRE(array_view[2].type() == type::k_bool);
@@ -1198,7 +1198,7 @@ TEST_CASE("builder::basic::make_array works", "[bsoncxx::builder::basic::make_ar
11981198

11991199
REQUIRE(std::distance(array_view.begin(), array_view.end()) == 3);
12001200
REQUIRE(array_view[0].type() == type::k_utf8);
1201-
REQUIRE(string::to_string(array_view[0].get_utf8().value) == "foo");
1201+
REQUIRE(string::to_string(array_view[0].get_string().value) == "foo");
12021202
REQUIRE(array_view[1].type() == type::k_int32);
12031203
REQUIRE(array_view[1].get_int32().value == 1);
12041204
REQUIRE(array_view[2].type() == type::k_bool);

src/bsoncxx/test/bson_get_values.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
#include <string>
1516
#include <utility>
1617

1718
#include <bsoncxx/builder/basic/array.hpp>

src/bsoncxx/test/bson_types.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ TEST_CASE("bson_value::view with b_double", "[bsoncxx::types::bson_value::view]"
198198

199199
TEST_CASE("bson_value::view with b_utf8", "[bsoncxx::types::bson_value::view]") {
200200
b_utf8 utf8_val{"hello"};
201-
REQUIRE(bson_value::view{utf8_val}.get_utf8() == utf8_val);
201+
REQUIRE(bson_value::view{utf8_val}.get_string() == utf8_val);
202202
}
203203

204204
TEST_CASE("bson_value::view with b_document", "[bsoncxx::types::bson_value::view]") {

src/bsoncxx/types/bson_value/view.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <bsoncxx/exception/exception.hpp>
2222
#include <bsoncxx/json.hpp>
2323
#include <bsoncxx/private/libbson.hh>
24+
#include <bsoncxx/private/suppress_deprecation_warnings.hh>
2425
#include <bsoncxx/types/private/convert.hh>
2526

2627
#include <bsoncxx/config/private/prelude.hh>
@@ -66,12 +67,15 @@ view::view() noexcept : view(nullptr) {}
6667

6768
view::view(const view& rhs) noexcept {
6869
switch (static_cast<int>(rhs._type)) {
70+
// CXX-1817; deprecation warning suppressed for get_utf8()
71+
BSONCXX_SUPPRESS_DEPRECATION_WARNINGS_BEGIN
6972
#define BSONCXX_ENUM(type, val) \
7073
case val: \
7174
new (&_b_##type) b_##type(rhs.get_##type()); \
7275
break;
7376
#include <bsoncxx/enums/type.hpp>
7477
#undef BSONCXX_ENUM
78+
BSONCXX_SUPPRESS_DEPRECATION_WARNINGS_END
7579
}
7680

7781
_type = rhs._type;
@@ -85,12 +89,15 @@ view& view::operator=(const view& rhs) noexcept {
8589
destroy();
8690

8791
switch (static_cast<int>(rhs._type)) {
92+
// CXX-1817; deprecation warning suppressed for get_utf8()
93+
BSONCXX_SUPPRESS_DEPRECATION_WARNINGS_BEGIN
8894
#define BSONCXX_ENUM(type, val) \
8995
case val: \
9096
new (&_b_##type) b_##type(rhs.get_##type()); \
9197
break;
9298
#include <bsoncxx/enums/type.hpp>
9399
#undef BSONCXX_ENUM
100+
BSONCXX_SUPPRESS_DEPRECATION_WARNINGS_END
94101
}
95102

96103
_type = rhs._type;
@@ -113,6 +120,10 @@ bsoncxx::type view::type() const {
113120
#include <bsoncxx/enums/type.hpp>
114121
#undef BSONCXX_ENUM
115122

123+
const b_utf8& view::get_string() const {
124+
return _b_utf8;
125+
}
126+
116127
view::view(const std::uint8_t* raw,
117128
std::uint32_t length,
118129
std::uint32_t offset,
@@ -157,11 +168,14 @@ bool operator==(const view& lhs, const view& rhs) {
157168
}
158169

159170
switch (static_cast<int>(lhs.type())) {
171+
// CXX-1817; deprecation warning suppressed for get_utf8()
172+
BSONCXX_SUPPRESS_DEPRECATION_WARNINGS_BEGIN
160173
#define BSONCXX_ENUM(type, val) \
161174
case val: \
162175
return lhs.get_##type() == rhs.get_##type();
163176
#include <bsoncxx/enums/type.hpp>
164177
#undef BSONCXX_ENUM
178+
BSONCXX_SUPPRESS_DEPRECATION_WARNINGS_END
165179
}
166180

167181
// Silence compiler warnings about failing to return a value.

src/bsoncxx/types/bson_value/view.hpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,15 @@ class BSONCXX_API view {
106106
/// @warning
107107
/// Calling the wrong get_<type> method will cause an exception to be thrown.
108108
///
109-
const b_utf8& get_utf8() const;
109+
BSONCXX_DEPRECATED const b_utf8& get_utf8() const;
110+
111+
///
112+
/// @return The underlying BSON UTF-8 string value.
113+
///
114+
/// @warning
115+
/// Calling the wrong get_<type> method will cause an exception to be thrown.
116+
///
117+
const b_utf8& get_string() const;
110118

111119
///
112120
/// @return The underlying BSON document value.

src/bsoncxx/types/private/convert.hh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#pragma once
1616

1717
#include <bsoncxx/private/libbson.hh>
18+
#include <bsoncxx/private/suppress_deprecation_warnings.hh>
1819
#include <bsoncxx/types.hpp>
1920
#include <bsoncxx/types/bson_value/view.hpp>
2021
#include <cstdlib>
@@ -186,6 +187,8 @@ BSONCXX_INLINE void convert_to_libbson(const bsoncxx::types::b_array& arr, bson_
186187
//
187188
BSONCXX_INLINE void convert_to_libbson(bson_value_t* v, const class bson_value::view& bson_view) {
188189
switch (bson_view.type()) {
190+
// CXX-1817; deprecation warning suppressed for get_utf8()
191+
BSONCXX_SUPPRESS_DEPRECATION_WARNINGS_BEGIN
189192
#define BSONCXX_ENUM(name, val) \
190193
case bsoncxx::type::k_##name: { \
191194
auto value = bson_view.get_##name(); \
@@ -194,6 +197,7 @@ BSONCXX_INLINE void convert_to_libbson(bson_value_t* v, const class bson_value::
194197
}
195198
#include <bsoncxx/enums/type.hpp>
196199
#undef BSONCXX_ENUM
200+
BSONCXX_SUPPRESS_DEPRECATION_WARNINGS_END
197201
default:
198202
BSONCXX_UNREACHABLE;
199203
}

src/mongocxx/index_view.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ void index_view::drop_one(const bsoncxx::document::view_or_value& keys,
9595
bsoncxx::document::view opts_view = index_options.view();
9696

9797
if (opts_view["name"]) {
98-
drop_one(bsoncxx::string::to_string(opts_view["name"].get_utf8().value), options);
98+
drop_one(bsoncxx::string::to_string(opts_view["name"].get_string().value), options);
9999
} else {
100100
drop_one(_get_impl().get_index_name_from_keys(keys), options);
101101
}
@@ -108,7 +108,8 @@ void index_view::drop_one(const client_session& session,
108108
bsoncxx::document::view opts_view = index_options.view();
109109

110110
if (opts_view["name"]) {
111-
drop_one(session, bsoncxx::string::to_string(opts_view["name"].get_utf8().value), options);
111+
drop_one(
112+
session, bsoncxx::string::to_string(opts_view["name"].get_string().value), options);
112113
} else {
113114
drop_one(session, _get_impl().get_index_name_from_keys(keys), options);
114115
}

src/mongocxx/private/index_view.hh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,14 @@ class index_view::impl {
7979
bsoncxx::document::view result_view = result.view();
8080

8181
if (result_view["note"] &&
82-
bsoncxx::string::to_string(result_view["note"].get_utf8().value) ==
82+
bsoncxx::string::to_string(result_view["note"].get_string().value) ==
8383
"all indexes already exist") {
8484
return bsoncxx::stdx::nullopt;
8585
}
8686

8787
if (auto name = model.options()["name"]) {
8888
return bsoncxx::stdx::make_optional(
89-
bsoncxx::string::to_string(name.get_value().get_utf8().value));
89+
bsoncxx::string::to_string(name.get_value().get_string().value));
9090
}
9191

9292
return bsoncxx::stdx::make_optional(get_index_name_from_keys(model.keys()));

src/mongocxx/test/change_streams.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ TEST_CASE("Mock streams and error-handling") {
320320
REQUIRE(opts["batchSize"].get_int32() == batch_size);
321321
REQUIRE(opts["maxAwaitTimeMS"].get_int64() == 4);
322322
REQUIRE(opts["collation"].get_document().view() == collation);
323-
REQUIRE(opts["fullDocument"].get_utf8().value ==
323+
REQUIRE(opts["fullDocument"].get_string().value ==
324324
bsoncxx::stdx::string_view{full_document});
325325
REQUIRE(opts["resumeAfter"].get_document().view() == resume_after);
326326
};
@@ -620,7 +620,7 @@ TEST_CASE("Watch a Collection", "[min36]") {
620620

621621
SECTION("Can receive it") {
622622
auto it = *(x.begin());
623-
REQUIRE(it["fullDocument"]["a"].get_utf8().value == stdx::string_view("b"));
623+
REQUIRE(it["fullDocument"]["a"].get_string().value == stdx::string_view("b"));
624624
}
625625

626626
SECTION("iterator equals itself") {
@@ -638,8 +638,8 @@ TEST_CASE("Watch a Collection", "[min36]") {
638638
auto it = x.begin();
639639
auto a = *it;
640640
auto b = *it;
641-
REQUIRE(a["fullDocument"]["a"].get_utf8().value == stdx::string_view("b"));
642-
REQUIRE(b["fullDocument"]["a"].get_utf8().value == stdx::string_view("b"));
641+
REQUIRE(a["fullDocument"]["a"].get_string().value == stdx::string_view("b"));
642+
REQUIRE(b["fullDocument"]["a"].get_string().value == stdx::string_view("b"));
643643
}
644644

645645
SECTION("Calling .begin multiple times doesn't advance state") {

0 commit comments

Comments
 (0)