[Input] Ensure YAML strings are preserved

Ensure that a YAML field `some-field: '12345'`
is read as a string by the C++ parser
This commit is contained in:
Ingmar Schoegl
2020-08-20 13:40:18 -04:00
committed by Ray Speth
parent 29f6b94ed3
commit 11f4243376
2 changed files with 19 additions and 1 deletions
+6 -1
View File
@@ -190,7 +190,12 @@ struct convert<Cantera::AnyValue> {
if (node.IsScalar()) {
// Scalar nodes are int, doubles, or strings
std::string nodestr = node.as<std::string>();
if (isInt(nodestr)) {
if (node.Tag() == "!") {
// Prevent quoted strings from being implicitly converted to
// numeric types, e.g. the quoted YAML string '12345' should not
// be interpreted as an integer
target = nodestr;
} else if (isInt(nodestr)) {
try {
target = node.as<long int>();
} catch (YAML::BadConversion&) {
+13
View File
@@ -40,6 +40,19 @@ TEST(AnyValue, getMapWhere_initial_list)
EXPECT_EQ(m["data"].getMapWhere("a", "baz")["x"].asInt(), 4);
}
TEST(AnyValue, numeric_yaml_string)
{
AnyMap m = AnyMap::fromYamlString(
"data: {a: '12345', b: '123.45', 'c': 12345, 'd': 123.45}");
EXPECT_THROW(m["data"]["a"].asInt(), CanteraError);
EXPECT_EQ(m["data"]["a"].asString(), "12345");
EXPECT_THROW(m["data"]["b"].asDouble(), CanteraError);
EXPECT_EQ(m["data"]["b"].asString(), "123.45");
EXPECT_EQ(m["data"]["c"].asInt(), 12345);
EXPECT_EQ(m["data"]["d"].asDouble(), 123.45);
}
TEST(AnyValue, getMapWhere_initial_map)
{
AnyMap m = AnyMap::fromYamlString(