make ConfigSetting<bool>::getStringValue() return true/false

Summary:
The `FieldConverter<bool>` code previously relied on `folly::to<string>()` for
all arithmetic types, including `bool`.  Unfortunately `folly::to<string>()`
returns `"1"` and `"0"` for booleans, which isn't terribly human-friendly.

This updates the code to explicitly return `"true"` and `"false"` as the
output for converting boolean values to strings.

Reviewed By: strager

Differential Revision: D15550014

fbshipit-source-id: 69a3385e529e2940a8be20dbcbeda92e5836d969
This commit is contained in:
Adam Simpkins 2019-06-11 12:59:03 -07:00 committed by Facebook Github Bot
parent 0c63605e75
commit e03b440e1c
2 changed files with 6 additions and 5 deletions

View File

@ -61,13 +61,11 @@ class FieldConverter<std::string> {
/*
* FieldConverter implementation for integers, floating point, and bool types
* using folly::to<T>(string)
*/
template <typename T>
class FieldConverter<
T,
typename std::enable_if<
std::is_arithmetic<T>::value || std::is_same<T, bool>::value>::type> {
typename std::enable_if<std::is_arithmetic<T>::value>::type> {
public:
/**
* Convert the passed string piece to a boolean.
@ -87,6 +85,9 @@ class FieldConverter<
}
std::string toDebugString(T value) const {
if constexpr (std::is_same<T, bool>::value) {
return value ? "true" : "false";
}
return folly::to<std::string>(value);
}
};

View File

@ -321,12 +321,12 @@ TEST(ConfigSettingTest, setBool) {
checkSet(setting, true, "yes");
checkSet(setting, true, "Y");
checkSet(setting, true, "on");
EXPECT_EQ("1", setting.getStringValue());
EXPECT_EQ("true", setting.getStringValue());
checkSet(setting, false, "n");
checkSet(setting, false, "0");
checkSet(setting, false, "false");
checkSet(setting, false, "off");
EXPECT_EQ("0", setting.getStringValue());
EXPECT_EQ("false", setting.getStringValue());
checkSetError(setting, "Empty input string", "");
checkSetError(setting, "Invalid value for bool: \"bogus\"", "bogus");