diff --git a/include/cantera/base/AnyMap.inl.h b/include/cantera/base/AnyMap.inl.h index fcf5c1abc6..1346e17d5d 100644 --- a/include/cantera/base/AnyMap.inl.h +++ b/include/cantera/base/AnyMap.inl.h @@ -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 '{}'", @@ -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 '{}'", diff --git a/src/base/AnyMap.cpp b/src/base/AnyMap.cpp index af4b215162..d436e63c82 100644 --- a/src/base/AnyMap.cpp +++ b/src/base/AnyMap.cpp @@ -245,6 +245,9 @@ struct convert { } else if (node.IsMap()) { target = node.as(); return true; + } else if (node.IsNull()) { + target = Empty; + return true; } return false; } diff --git a/test/general/test_containers.cpp b/test/general/test_containers.cpp index 5b47ab6b5f..6555a3f9b1 100644 --- a/test/general/test_containers.cpp +++ b/test/general/test_containers.cpp @@ -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()); + + try { + m.at("b").asString(); + FAIL(); + } catch (CanteraError& err) { + EXPECT_THAT(err.what(), + testing::HasSubstr("Key 'b' not found or contains no value")); + } +} + TEST(AnyMap, loadYaml) { AnyMap m = AnyMap::fromYamlString(