Merge bitcoin/bitcoin#26673: univalue: Remove confusing getBool method

293849a260 univalue: Remove confusing getBool method (Ryan Ofsky)

Pull request description:

  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 exception.

  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 https://github.com/bitcoin/bitcoin/pull/26213 but were dropped to limit the scope of that PR.

ACKs for top commit:
  justinpickering:
    ACK 293849a260
  sipa:
    utACK 293849a260
  w0xlt:
    ACK 293849a260
  hebasto:
    ACK 293849a260, also verified that the removed `getBool` method is not mentioned in any docs:
  furszy:
    ACK 293849a2

Tree-SHA512: 9fbfe5e2083410f123b18703a0cc0161ecbbb4958f331c9ff808dcfcc6ad499b0e896abd16fb8ea200c53ba29878db9812ce141e59cc5e0fd174741b0bcb192d
This commit is contained in:
fanquake 2022-12-10 09:59:21 +00:00
commit a28fb36c47
No known key found for this signature in database
GPG Key ID: 2EEB9F5CC09526C1
4 changed files with 5 additions and 6 deletions

View File

@ -47,7 +47,7 @@ BOOST_AUTO_TEST_CASE(run_command)
BOOST_CHECK(result.isObject()); BOOST_CHECK(result.isObject());
const UniValue& success = find_value(result, "success"); const UniValue& success = find_value(result, "success");
BOOST_CHECK(!success.isNull()); BOOST_CHECK(!success.isNull());
BOOST_CHECK_EQUAL(success.getBool(), true); BOOST_CHECK_EQUAL(success.get_bool(), true);
} }
{ {
// An invalid command is handled by Boost // An invalid command is handled by Boost
@ -95,7 +95,7 @@ BOOST_AUTO_TEST_CASE(run_command)
BOOST_CHECK(result.isObject()); BOOST_CHECK(result.isObject());
const UniValue& success = find_value(result, "success"); const UniValue& success = find_value(result, "success");
BOOST_CHECK(!success.isNull()); BOOST_CHECK(!success.isNull());
BOOST_CHECK_EQUAL(success.getBool(), true); BOOST_CHECK_EQUAL(success.get_bool(), true);
} }
#endif #endif
} }

View File

@ -66,7 +66,6 @@ public:
size_t size() const { return values.size(); } size_t size() const { return values.size(); }
bool getBool() const { return isTrue(); }
void getObjMap(std::map<std::string,UniValue>& kv) const; void getObjMap(std::map<std::string,UniValue>& kv) const;
bool checkObject(const std::map<std::string,UniValue::VType>& memberTypes) const; bool checkObject(const std::map<std::string,UniValue::VType>& memberTypes) const;
const UniValue& operator[](const std::string& key) const; const UniValue& operator[](const std::string& key) const;

View File

@ -60,7 +60,7 @@ const std::vector<UniValue>& UniValue::getValues() const
bool UniValue::get_bool() const bool UniValue::get_bool() const
{ {
checkType(VBOOL); checkType(VBOOL);
return getBool(); return isTrue();
} }
const std::string& UniValue::get_str() const const std::string& UniValue::get_str() const

View File

@ -193,13 +193,13 @@ void univalue_set()
BOOST_CHECK_EQUAL(v.isBool(), true); BOOST_CHECK_EQUAL(v.isBool(), true);
BOOST_CHECK_EQUAL(v.isTrue(), false); BOOST_CHECK_EQUAL(v.isTrue(), false);
BOOST_CHECK_EQUAL(v.isFalse(), true); BOOST_CHECK_EQUAL(v.isFalse(), true);
BOOST_CHECK_EQUAL(v.getBool(), false); BOOST_CHECK_EQUAL(v.get_bool(), false);
v.setBool(true); v.setBool(true);
BOOST_CHECK_EQUAL(v.isBool(), true); BOOST_CHECK_EQUAL(v.isBool(), true);
BOOST_CHECK_EQUAL(v.isTrue(), true); BOOST_CHECK_EQUAL(v.isTrue(), true);
BOOST_CHECK_EQUAL(v.isFalse(), false); BOOST_CHECK_EQUAL(v.isFalse(), false);
BOOST_CHECK_EQUAL(v.getBool(), true); BOOST_CHECK_EQUAL(v.get_bool(), true);
BOOST_CHECK_THROW(v.setNumStr("zombocom"), std::runtime_error); BOOST_CHECK_THROW(v.setNumStr("zombocom"), std::runtime_error);