mirror of
https://github.com/Cantera/cantera.git
synced 2026-08-17 16:35:17 -05:00
[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:
committed by
Ray Speth
parent
29f6b94ed3
commit
11f4243376
+6
-1
@@ -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&) {
|
||||
|
||||
@@ -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(
|
||||
|
||||
Reference in New Issue
Block a user