Skip to content

Commit

Permalink
Teuchos: improve support for bool parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
cwschilly committed Jan 18, 2024
1 parent 9eddcb7 commit 492d668
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 9 deletions.
29 changes: 20 additions & 9 deletions packages/teuchos/parameterlist/src/Teuchos_YamlParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1339,18 +1339,29 @@ void processKeyValueNode(const std::string& key, const ::YAML::Node& node, Teuch
}
catch(...)
{
std::string raw_string = quoted_as<std::string>(node);
if(raw_string == "true" || raw_string == "yes")
{
safe_set_entry<bool>(parent, key, true);
}
else if(raw_string == "false" || raw_string == "no")
try
{
safe_set_entry<bool>(parent, key, false);
bool raw_bool = quoted_as<bool>(node);

/* yaml-cpp parses ON/OFF as a bool, but the in-house parser does not.
To preserve backwards compatibility, make sure the string passes
the in-house parser's is_parseable_as<bool> function (which protects
against the ON/OFF case).
Otherwise, a failure is observed in YAML_ConvertFromXML unit test.*/

std::string raw_string = quoted_as<std::string>(node);
if (is_parseable_as<bool>(raw_string))
{
safe_set_entry<bool>(parent, key, raw_bool);
}
else
{
safe_set_entry<std::string>(parent, key, raw_string);
}
}
else
catch(...)
{
safe_set_entry<std::string>(parent, key, raw_string);
safe_set_entry<std::string>(parent, key, quoted_as<std::string>(node));
}
}
}
Expand Down
12 changes: 12 additions & 0 deletions packages/teuchos/parameterlist/test/yaml/YamlParameterList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -296,17 +296,29 @@ namespace TeuchosTests
"List:\n"
" input_true: true\n"
" input_false: false\n"
" input_TRUE: TRUE\n"
" input_FALSE: FALSE\n"
" input_True: True\n"
" input_False: False\n"
" input_yes: yes\n"
" input_no: no\n"
);
TEST_EQUALITY(pl->isType<bool>("input_true"), true);
TEST_EQUALITY(pl->isType<bool>("input_false"), true);
TEST_EQUALITY(pl->isType<bool>("input_yes"), true);
TEST_EQUALITY(pl->isType<bool>("input_no"), true);
TEST_EQUALITY(pl->isType<bool>("input_TRUE"), true);
TEST_EQUALITY(pl->isType<bool>("input_True"), true);
TEST_EQUALITY(pl->isType<bool>("input_FALSE"), true);
TEST_EQUALITY(pl->isType<bool>("input_False"), true);
TEST_EQUALITY(pl->get<bool>("input_true"), true);
TEST_EQUALITY(pl->get<bool>("input_false"), false);
TEST_EQUALITY(pl->get<bool>("input_yes"), true);
TEST_EQUALITY(pl->get<bool>("input_no"), false);
TEST_EQUALITY(pl->get<bool>("input_TRUE"), true);
TEST_EQUALITY(pl->get<bool>("input_True"), true);
TEST_EQUALITY(pl->get<bool>("input_FALSE"), false);
TEST_EQUALITY(pl->get<bool>("input_False"), false);
}

TEUCHOS_UNIT_TEST(YAML, flow_map)
Expand Down

0 comments on commit 492d668

Please sign in to comment.