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

Respect YAML::Null node by parsing #1847

Merged
merged 1 commit into from
Nov 12, 2024
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
20 changes: 15 additions & 5 deletions libs/ymltree/src/YmlFormatter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,12 @@ static void EmitElement(YAML::Emitter& emitter, XMLTreeElement* element)
auto& attributes = element->GetAttributes();
const string& text = element->GetText();
auto& children = element->GetChildren();
if (!text.empty() && attributes.empty() && children.empty()) {
emitter << text;
if (attributes.empty() && children.empty()) {
if(!text.empty()) {
emitter << text;
} else {
emitter << YAML::Null;
}
return;
}

Expand All @@ -40,9 +44,14 @@ static void EmitElement(YAML::Emitter& emitter, XMLTreeElement* element)
}
// treat everything else as a map
emitter << YAML::BeginMap;
if (!attributes.empty()) {
for (auto [k, v] : attributes) {
emitter << YAML::Key << k << YAML::Value << v;
if(!attributes.empty()) {
for(auto [k, v] : attributes) {
emitter << YAML::Key << k;
if(!v.empty()) {
emitter << YAML::Value << v;
} else {
emitter << YAML::Null;
}
}
}

Expand Down Expand Up @@ -70,6 +79,7 @@ std::string YmlFormatter::FormatElement(XMLTreeElement* rootElement,
return RteUtils::EMPTY_STRING;
}
YAML::Emitter emitter;
emitter.SetNullFormat(YAML::EmptyNull);
emitter << YAML::BeginMap;
if (rootElement->GetChildCount() > 1 && rootElement->GetTag().empty()) {
for (auto child : rootElement->GetChildren()) {
Expand Down
6 changes: 3 additions & 3 deletions libs/ymltree/src/YmlTreeParserInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,9 @@ bool YmlTreeParserInterface::ParseMapNode(const YAML::Node& node)
auto& key = it.first;
auto& val = it.second;
const string tag = key.as<string>();
if (val.IsScalar() && builder->HasRoot()) {
builder->AddAttribute(tag, val.as<string>());
if ((val.IsScalar() || val.IsNull()) && builder->HasRoot()) {
const string value = val.IsScalar() ? val.as<string>() : "";
builder->AddAttribute(tag, value);
} else {
if(!ParseNode(val, tag)) {
return false;
Expand Down Expand Up @@ -134,7 +135,6 @@ bool YmlTreeParserInterface::DoParseNode(const YAML::Node& node, const string& t
return ParseMapNode(node);

case NodeType::Null:
return false;
case NodeType::Undefined:
default:
break;
Expand Down
29 changes: 26 additions & 3 deletions libs/ymltree/test/YmlTreeTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,20 +130,42 @@ TEST(YmlTreeTest, KeyVal) {
EXPECT_TRUE(tree.GetRoot());
root = tree.GetRoot() ? tree.GetRoot()->GetFirstChild() : nullptr;
EXPECT_TRUE(root);
EXPECT_EQ(root->GetTag(), "key1");
EXPECT_EQ(root->GetText(), "val1");

xmlContent = xmlFormatter.FormatElement(root);
EXPECT_EQ(xmlContent, expected_output1);
string ymlContent1 = ymlFormatter.FormatElement(root);
EXPECT_EQ(ymlContent1, yaml_input1);

const string yaml_input2 = "key2: "; // null node type (trailing space is intentional because added by emitter)
const string expected_output2 = XML_HEADER + "<key2/>\n";

tree.Clear();
EXPECT_TRUE(tree.ParseString(yaml_input2));
EXPECT_TRUE(tree.GetRoot());
root = tree.GetRoot() ? tree.GetRoot()->GetFirstChild() : nullptr;
EXPECT_TRUE(root);
EXPECT_EQ(root->GetTag(), "key2");
EXPECT_EQ(root->GetText(), "");

xmlContent = xmlFormatter.FormatElement(root);
EXPECT_EQ(xmlContent, expected_output2);
string ymlContent2 = ymlFormatter.FormatElement(root);
EXPECT_EQ(ymlContent2, yaml_input2);

}

TEST(YmlTreeTest, Map) {
const string yaml_input =
"map:\n\
nul: \n\
one: 1\n\
two: 2";

const string json_input = "map: {one: 1, two: 2}\n";
const string json_input = "map: {nul:, one: 1, two: 2}\n";

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

YmlTree tree;
EXPECT_TRUE(tree.ParseString(yaml_input));
Expand Down Expand Up @@ -175,6 +197,7 @@ TEST(YmlTreeTest, Nested) {
one: 1\n\
two: 2\n\
three:\n\
s_null: \n\
s_one: 3.1\n\
s_two: 3.2\n\
four:\n\
Expand All @@ -183,7 +206,7 @@ TEST(YmlTreeTest, Nested) {
4.2.b: b";
const string expected_output = XML_HEADER +
"<nested one=\"1\" two=\"2\">\n\
<three s_one=\"3.1\" s_two=\"3.2\"/>\n\
<three s_null=\"\" s_one=\"3.1\" s_two=\"3.2\"/>\n\
<four>\n\
<->4.1</->\n\
<- 4.2.a=\"a\" 4.2.b=\"b\"/>\n\
Expand Down
Loading