Skip to content

Commit 5a523e7

Browse files
Update property tree toYaml to support excluding attributes
1 parent 55c1c2e commit 5a523e7

File tree

3 files changed

+28
-32
lines changed

3 files changed

+28
-32
lines changed

tesseract_common/include/tesseract_common/property_tree.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ class PropertyTree
6060

6161
static PropertyTree fromYAML(const YAML::Node& node);
6262

63-
YAML::Node toYAML() const;
63+
YAML::Node toYAML(bool exclude_attributes = true) const;
6464

6565
private:
6666
YAML::Node value_;

tesseract_common/src/property_tree.cpp

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -155,34 +155,36 @@ PropertyTree PropertyTree::fromYAML(const YAML::Node& node)
155155
return tree;
156156
}
157157

158-
YAML::Node PropertyTree::toYAML() const
158+
YAML::Node PropertyTree::toYAML(bool exclude_attributes) const
159159
{
160-
// Always emit a mapping if attributes exist or children exist
161-
if (!attributes_.empty() || !children_.empty())
162-
{
163-
YAML::Node node(YAML::NodeType::Map);
164-
// emit attributes first
165-
if (!attributes_.empty())
166-
{
167-
YAML::Node attr_node(YAML::NodeType::Map);
168-
for (const auto& pair : attributes_)
169-
attr_node[pair.first] = pair.second;
160+
// pure leaf without attributes or children: emit scalar/sequence directly
161+
if (attributes_.empty() && children_.empty())
162+
return value_;
170163

171-
node[ATTRIBUTES_KEY] = attr_node;
172-
}
173-
// emit children
174-
for (const auto& pair : children_)
175-
node[pair.first] = pair.second.toYAML();
164+
// pure leaf with attributes excluded and no children: emit scalar/sequence directly
165+
if (exclude_attributes && children_.empty())
166+
return value_;
176167

177-
// if leaf (no children) but value present, emit under 'value'
178-
if (children_.empty() && value_)
179-
node[VALUE_KEY] = value_;
168+
// Always emit a mapping if attributes exist or children exist
169+
YAML::Node node(YAML::NodeType::Map);
170+
// emit attributes first
171+
if (!exclude_attributes && !attributes_.empty())
172+
{
173+
YAML::Node attr_node(YAML::NodeType::Map);
174+
for (const auto& pair : attributes_)
175+
attr_node[pair.first] = pair.second;
180176

181-
return node;
177+
node[ATTRIBUTES_KEY] = attr_node;
182178
}
179+
// emit children
180+
for (const auto& pair : children_)
181+
node[pair.first] = pair.second.toYAML(exclude_attributes);
182+
183+
// if leaf (no children) but value present, emit under 'value'
184+
if (children_.empty() && value_)
185+
node[VALUE_KEY] = value_;
183186

184-
// pure leaf without attributes: emit scalar/sequence directly
185-
return value_;
187+
return node;
186188
}
187189

188190
void validateRequired(const PropertyTree& node)

tesseract_common/src/property_tree_demo.cpp

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -111,14 +111,15 @@ std::string str3 = R"(config:
111111
int main()
112112
{
113113
// Load configuration from YAML
114-
PropertyTree prop = PropertyTree::fromYAML(YAML::Load(str3));
114+
PropertyTree prop = PropertyTree::fromYAML(YAML::Load(str2));
115115

116116
// Parse schema from external file (config_schema.yaml)
117117
PropertyTree schema = buildConfigSchema();
118118

119119
// Merge schema metadata into config tree
120120
prop.mergeSchema(schema);
121-
std::cout << prop.toYAML() << "\n";
121+
std::cout << "Exclude attrubutes:\n" << prop.toYAML() << "\n\n";
122+
std::cout << "Include attrubutes:\n" << prop.toYAML(false) << "\n\n";
122123

123124
try
124125
{
@@ -130,13 +131,6 @@ int main()
130131
return 1;
131132
}
132133

133-
// if (!prop.validate(errors, "config"))
134-
// {
135-
// std::cerr << "Validation errors:\n";
136-
// for (auto const& e : errors)
137-
// std::cerr << " - " << e << "\n";
138-
// return 1;
139-
// }
140134
bool cond = prop.get("config").get("conditional").getValue().as<bool>();
141135
std::cout << "conditional = " << std::boolalpha << cond << "\n";
142136
return 0;

0 commit comments

Comments
 (0)