Skip to content

Commit c721983

Browse files
Respect YAML::Null node by parsing
Co-authored-by: Evgueni Driouk <[email protected]>
1 parent 5fa61b5 commit c721983

File tree

3 files changed

+44
-11
lines changed

3 files changed

+44
-11
lines changed

libs/ymltree/src/YmlFormatter.cpp

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,12 @@ static void EmitElement(YAML::Emitter& emitter, XMLTreeElement* element)
2525
auto& attributes = element->GetAttributes();
2626
const string& text = element->GetText();
2727
auto& children = element->GetChildren();
28-
if (!text.empty() && attributes.empty() && children.empty()) {
29-
emitter << text;
28+
if (attributes.empty() && children.empty()) {
29+
if(!text.empty()) {
30+
emitter << text;
31+
} else {
32+
emitter << YAML::Null;
33+
}
3034
return;
3135
}
3236

@@ -40,9 +44,14 @@ static void EmitElement(YAML::Emitter& emitter, XMLTreeElement* element)
4044
}
4145
// treat everything else as a map
4246
emitter << YAML::BeginMap;
43-
if (!attributes.empty()) {
44-
for (auto [k, v] : attributes) {
45-
emitter << YAML::Key << k << YAML::Value << v;
47+
if(!attributes.empty()) {
48+
for(auto [k, v] : attributes) {
49+
emitter << YAML::Key << k;
50+
if(!v.empty()) {
51+
emitter << YAML::Value << v;
52+
} else {
53+
emitter << YAML::Null;
54+
}
4655
}
4756
}
4857

@@ -70,6 +79,7 @@ std::string YmlFormatter::FormatElement(XMLTreeElement* rootElement,
7079
return RteUtils::EMPTY_STRING;
7180
}
7281
YAML::Emitter emitter;
82+
emitter.SetNullFormat(YAML::EmptyNull);
7383
emitter << YAML::BeginMap;
7484
if (rootElement->GetChildCount() > 1 && rootElement->GetTag().empty()) {
7585
for (auto child : rootElement->GetChildren()) {

libs/ymltree/src/YmlTreeParserInterface.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,9 @@ bool YmlTreeParserInterface::ParseMapNode(const YAML::Node& node)
9292
auto& key = it.first;
9393
auto& val = it.second;
9494
const string tag = key.as<string>();
95-
if (val.IsScalar() && builder->HasRoot()) {
96-
builder->AddAttribute(tag, val.as<string>());
95+
if ((val.IsScalar() || val.IsNull()) && builder->HasRoot()) {
96+
const string value = val.IsScalar() ? val.as<string>() : "";
97+
builder->AddAttribute(tag, value);
9798
} else {
9899
if(!ParseNode(val, tag)) {
99100
return false;
@@ -134,7 +135,6 @@ bool YmlTreeParserInterface::DoParseNode(const YAML::Node& node, const string& t
134135
return ParseMapNode(node);
135136

136137
case NodeType::Null:
137-
return false;
138138
case NodeType::Undefined:
139139
default:
140140
break;

libs/ymltree/test/YmlTreeTest.cpp

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,20 +130,42 @@ TEST(YmlTreeTest, KeyVal) {
130130
EXPECT_TRUE(tree.GetRoot());
131131
root = tree.GetRoot() ? tree.GetRoot()->GetFirstChild() : nullptr;
132132
EXPECT_TRUE(root);
133+
EXPECT_EQ(root->GetTag(), "key1");
134+
EXPECT_EQ(root->GetText(), "val1");
133135

134136
xmlContent = xmlFormatter.FormatElement(root);
135137
EXPECT_EQ(xmlContent, expected_output1);
138+
string ymlContent1 = ymlFormatter.FormatElement(root);
139+
EXPECT_EQ(ymlContent1, yaml_input1);
140+
141+
const string yaml_input2 = "key2: "; // null node type (trailing space is intentional because added by emitter)
142+
const string expected_output2 = XML_HEADER + "<key2/>\n";
143+
144+
tree.Clear();
145+
EXPECT_TRUE(tree.ParseString(yaml_input2));
146+
EXPECT_TRUE(tree.GetRoot());
147+
root = tree.GetRoot() ? tree.GetRoot()->GetFirstChild() : nullptr;
148+
EXPECT_TRUE(root);
149+
EXPECT_EQ(root->GetTag(), "key2");
150+
EXPECT_EQ(root->GetText(), "");
151+
152+
xmlContent = xmlFormatter.FormatElement(root);
153+
EXPECT_EQ(xmlContent, expected_output2);
154+
string ymlContent2 = ymlFormatter.FormatElement(root);
155+
EXPECT_EQ(ymlContent2, yaml_input2);
156+
136157
}
137158

138159
TEST(YmlTreeTest, Map) {
139160
const string yaml_input =
140161
"map:\n\
162+
nul: \n\
141163
one: 1\n\
142164
two: 2";
143165

144-
const string json_input = "map: {one: 1, two: 2}\n";
166+
const string json_input = "map: {nul:, one: 1, two: 2}\n";
145167

146-
const string expected_output = XML_HEADER + "<map one=\"1\" two=\"2\"/>\n";
168+
const string expected_output = XML_HEADER + "<map nul=\"\" one=\"1\" two=\"2\"/>\n";
147169

148170
YmlTree tree;
149171
EXPECT_TRUE(tree.ParseString(yaml_input));
@@ -175,6 +197,7 @@ TEST(YmlTreeTest, Nested) {
175197
one: 1\n\
176198
two: 2\n\
177199
three:\n\
200+
s_null: \n\
178201
s_one: 3.1\n\
179202
s_two: 3.2\n\
180203
four:\n\
@@ -183,7 +206,7 @@ TEST(YmlTreeTest, Nested) {
183206
4.2.b: b";
184207
const string expected_output = XML_HEADER +
185208
"<nested one=\"1\" two=\"2\">\n\
186-
<three s_one=\"3.1\" s_two=\"3.2\"/>\n\
209+
<three s_null=\"\" s_one=\"3.1\" s_two=\"3.2\"/>\n\
187210
<four>\n\
188211
<->4.1</->\n\
189212
<- 4.2.a=\"a\" 4.2.b=\"b\"/>\n\

0 commit comments

Comments
 (0)