Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Input] Improve handling of YAML null values #897

Merged
merged 1 commit into from
Jul 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions include/cantera/base/AnyMap.inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const T &AnyValue::as() const {
if (m_value->type() == typeid(void)) {
// Values that have not been set are of type 'void'
throw InputFileError("AnyValue::as", *this,
"Key '{}' not found", m_key);
"Key '{}' not found or contains no value", m_key);
} else {
throw InputFileError("AnyValue::as", *this,
"Key '{}' contains a '{}',\nnot a '{}'",
Expand All @@ -48,7 +48,7 @@ T &AnyValue::as() {
if (m_value->type() == typeid(void)) {
// Values that have not been set are of type 'void'
throw InputFileError("AnyValue::as", *this,
"Key '{}' not found", m_key);
"Key '{}' not found or contains no value", m_key);
} else {
throw InputFileError("AnyValue::as", *this,
"Key '{}' contains a '{}',\nnot a '{}'",
Expand Down
3 changes: 3 additions & 0 deletions src/base/AnyMap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,9 @@ struct convert<Cantera::AnyValue> {
} else if (node.IsMap()) {
target = node.as<AnyMap>();
return true;
} else if (node.IsNull()) {
target = Empty;
return true;
}
return false;
}
Expand Down
17 changes: 17 additions & 0 deletions test/general/test_containers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,23 @@ TEST(AnyMap, iterators)
EXPECT_TRUE(std::find(keys.begin(), keys.end(), "bar") != keys.end());
}

TEST(AnyMap, null_values)
{
AnyMap m = AnyMap::fromYamlString(
"{a: 1, b: ~, c: , d: 5}"
);
EXPECT_EQ(m.size(), (size_t) 4);
EXPECT_TRUE(m.at("c").is<void>());

try {
m.at("b").asString();
FAIL();
} catch (CanteraError& err) {
EXPECT_THAT(err.what(),
testing::HasSubstr("Key 'b' not found or contains no value"));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see that the tests pass, but I'm surprised this isn't raised for key 'c'. Is that a limitation of yaml-cpp?

Copy link
Member Author

@speth speth Jul 11, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yaml-cpp correctly converts both the blank value and ~ to null. The difference here is that the exception is raised by asString(), not at("b"). I guess in this sense, the AnyMap class isn't quite a faithful representation of YAML in that in AnyMap, a key with a null value is equivalent to the key not being present, while YAML makes a distinction. Since we don't use null values anywhere in the Cantera YAML format, I'm not sure whether it's worth the extra effort.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense, thanks!

}
}

TEST(AnyMap, loadYaml)
{
AnyMap m = AnyMap::fromYamlString(
Expand Down