Skip to content

Commit 293849a

Browse files
ryanofskyMarcoFalke
and
MarcoFalke
committed
univalue: Remove confusing getBool method
Drop UniValue::getBool method because it is easy to confuse with the UniValue::get_bool method, and could potentially cause bugs. Unlike get_bool, getBool doesn't ensure that the value is a boolean and returns false for all integer, string, array, and object values instead of throwing an exceptions. The getBool method is also redundant because it is an alias for isTrue. There were only 5 getBool() calls in the codebase, so this commit replaces them with isTrue() or get_bool() calls as appropriate. These changes were originally made by MarcoFalke in bitcoin#26213 but were dropped to limit the scope of that PR. Co-authored-by: MarcoFalke <*~=`'#}+{/-|&$^[email protected]>
1 parent 16624e6 commit 293849a

File tree

4 files changed

+5
-6
lines changed

4 files changed

+5
-6
lines changed

src/test/system_tests.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ BOOST_AUTO_TEST_CASE(run_command)
4747
BOOST_CHECK(result.isObject());
4848
const UniValue& success = find_value(result, "success");
4949
BOOST_CHECK(!success.isNull());
50-
BOOST_CHECK_EQUAL(success.getBool(), true);
50+
BOOST_CHECK_EQUAL(success.get_bool(), true);
5151
}
5252
{
5353
// An invalid command is handled by Boost
@@ -95,7 +95,7 @@ BOOST_AUTO_TEST_CASE(run_command)
9595
BOOST_CHECK(result.isObject());
9696
const UniValue& success = find_value(result, "success");
9797
BOOST_CHECK(!success.isNull());
98-
BOOST_CHECK_EQUAL(success.getBool(), true);
98+
BOOST_CHECK_EQUAL(success.get_bool(), true);
9999
}
100100
#endif
101101
}

src/univalue/include/univalue.h

-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ class UniValue {
6666

6767
size_t size() const { return values.size(); }
6868

69-
bool getBool() const { return isTrue(); }
7069
void getObjMap(std::map<std::string,UniValue>& kv) const;
7170
bool checkObject(const std::map<std::string,UniValue::VType>& memberTypes) const;
7271
const UniValue& operator[](const std::string& key) const;

src/univalue/lib/univalue_get.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ const std::vector<UniValue>& UniValue::getValues() const
6060
bool UniValue::get_bool() const
6161
{
6262
checkType(VBOOL);
63-
return getBool();
63+
return isTrue();
6464
}
6565

6666
const std::string& UniValue::get_str() const

src/univalue/test/object.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -193,13 +193,13 @@ void univalue_set()
193193
BOOST_CHECK_EQUAL(v.isBool(), true);
194194
BOOST_CHECK_EQUAL(v.isTrue(), false);
195195
BOOST_CHECK_EQUAL(v.isFalse(), true);
196-
BOOST_CHECK_EQUAL(v.getBool(), false);
196+
BOOST_CHECK_EQUAL(v.get_bool(), false);
197197

198198
v.setBool(true);
199199
BOOST_CHECK_EQUAL(v.isBool(), true);
200200
BOOST_CHECK_EQUAL(v.isTrue(), true);
201201
BOOST_CHECK_EQUAL(v.isFalse(), false);
202-
BOOST_CHECK_EQUAL(v.getBool(), true);
202+
BOOST_CHECK_EQUAL(v.get_bool(), true);
203203

204204
BOOST_CHECK_THROW(v.setNumStr("zombocom"), std::runtime_error);
205205

0 commit comments

Comments
 (0)