Skip to content

Commit d41446a

Browse files
fixup
1 parent 8d7d9f1 commit d41446a

File tree

3 files changed

+68
-22
lines changed

3 files changed

+68
-22
lines changed

tesseract_common/include/tesseract_common/property_tree.h

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,31 @@ namespace tesseract_common
1313
{
1414
namespace property_type
1515
{
16+
// Integral Types
1617
constexpr std::string_view BOOL{ "bool" };
17-
constexpr std::string_view STRING{ "string" };
18+
constexpr std::string_view CHAR{ "char" };
19+
constexpr std::string_view STRING{ "std::string" };
1820
constexpr std::string_view INT{ "int" };
21+
constexpr std::string_view UNSIGNED_INT{ "unsigned int" };
22+
constexpr std::string_view LONG_INT{ "long int" };
23+
constexpr std::string_view LONG_UNSIGNED_INT{ "long unsigned int" };
1924
constexpr std::string_view FLOAT{ "float" };
25+
constexpr std::string_view DOUBLE{ "double" };
26+
27+
// Eigen Types
28+
constexpr std::string_view EIGEN_ISOMETRY_3D{ "Eigen::Isometry3d" };
29+
constexpr std::string_view EIGEN_MATRIX_XD{ "Eigen::MatrixXd" };
30+
constexpr std::string_view EIGEN_VECTOR_XD{ "Eigen::VectorXd" };
31+
constexpr std::string_view EIGEN_MATRIX_2D{ "Eigen::Matrix2d" };
32+
constexpr std::string_view EIGEN_VECTOR_2D{ "Eigen::Vector2d" };
33+
constexpr std::string_view EIGEN_MATRIX_3D{ "Eigen::Matrix3d" };
34+
constexpr std::string_view EIGEN_VECTOR_3D{ "Eigen::Vector3d" };
2035
} // namespace property_type
2136

2237
namespace property_attribute
2338
{
39+
constexpr std::string_view TYPE{ "type" };
40+
constexpr std::string_view DOC{ "doc" };
2441
constexpr std::string_view REQUIRED{ "required" };
2542
constexpr std::string_view DEFAULT{ "default" };
2643
constexpr std::string_view ENUM{ "enum" };
@@ -116,6 +133,12 @@ class PropertyTree
116133
*/
117134
const YAML::Node& getValue() const;
118135

136+
/**
137+
* @brief Check if property value is null
138+
* @return True if required, otherwise false
139+
*/
140+
bool isNull() const;
141+
119142
/**
120143
* @brief List all immediate child keys.
121144
* @return Vector of child key strings.
@@ -159,6 +182,12 @@ class PropertyTree
159182
*/
160183
std::vector<std::string> getAttributeKeys() const;
161184

185+
/**
186+
* @brief Check if property is requried by checking for attribute and its value
187+
* @return True if required, otherwise false
188+
*/
189+
bool isRequired() const;
190+
162191
/**
163192
* @brief Create a PropertyTree from a YAML::Node.
164193
* @param node Root YAML::Node to convert.

tesseract_common/src/property_tree.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ const PropertyTree* PropertyTree::find(std::string_view key) const
7979
void PropertyTree::setValue(const YAML::Node& v) { value_ = v; }
8080
const YAML::Node& PropertyTree::getValue() const { return value_; }
8181

82+
bool PropertyTree::isNull() const { return value_.IsNull(); }
83+
8284
std::vector<std::string> PropertyTree::keys() const
8385
{
8486
std::vector<std::string> res;
@@ -132,6 +134,15 @@ std::vector<std::string> PropertyTree::getAttributeKeys() const
132134
return res;
133135
}
134136

137+
bool PropertyTree::isRequired() const
138+
{
139+
std::optional<YAML::Node> required = getAttribute(property_attribute::REQUIRED);
140+
if (!required.has_value())
141+
return false;
142+
143+
return required.value().as<bool>();
144+
}
145+
135146
PropertyTree PropertyTree::fromYAML(const YAML::Node& node)
136147
{
137148
// Handle 'follow' directive: load external YAML or schema file
@@ -200,7 +211,13 @@ YAML::Node PropertyTree::toYAML(bool exclude_attributes) const
200211
}
201212
// emit children
202213
for (const auto& pair : children_)
214+
{
215+
// If the property is not required and is null then skip when excluding attributes
216+
if (exclude_attributes && !pair.second.isRequired() && pair.second.isNull())
217+
continue;
218+
203219
node[pair.first] = pair.second.toYAML(exclude_attributes);
220+
}
204221

205222
// if leaf (no children) but value present, emit under 'value'
206223
if (children_.empty() && value_)

tesseract_common/src/property_tree_demo.cpp

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ PropertyTree buildConfigSchema()
1010
{
1111
PropertyTree schema;
1212
auto& cfg = schema.get("config");
13-
cfg.setAttribute("description", "Main config for plugin");
14-
cfg.setAttribute("required", true);
13+
cfg.setAttribute(property_attribute::DOC, "Main config for plugin");
14+
cfg.setAttribute(property_attribute::REQUIRED, true);
1515
std::map<int, std::string> return_options;
1616
return_options[0] = "Error";
1717
return_options[1] = "Successful";
@@ -21,40 +21,40 @@ PropertyTree buildConfigSchema()
2121
// conditional
2222
{
2323
auto& prop = cfg.get("conditional");
24-
prop.setAttribute("type", property_type::BOOL);
25-
prop.setAttribute("default", true);
26-
prop.setAttribute("description", "Enable conditional execution");
27-
prop.setAttribute("required", true);
24+
prop.setAttribute(property_attribute::TYPE, property_type::BOOL);
25+
prop.setAttribute(property_attribute::DEFAULT, true);
26+
prop.setAttribute(property_attribute::DOC, "Enable conditional execution");
27+
prop.setAttribute(property_attribute::REQUIRED, true);
2828
prop.addValidator(validateRequired);
2929
}
3030
// inputs
3131
{
3232
auto& inputs = cfg.get("inputs");
33-
inputs.setAttribute("description", "Input sources");
34-
inputs.setAttribute("required", true);
33+
inputs.setAttribute(property_attribute::DOC, "Input sources");
34+
inputs.setAttribute(property_attribute::REQUIRED, true);
3535
// program
3636
{
3737
auto& prop = inputs.get("program");
38-
prop.setAttribute("type", property_type::STRING);
39-
prop.setAttribute("description", "The composite instruction");
40-
prop.setAttribute("required", true);
38+
prop.setAttribute(property_attribute::TYPE, property_type::STRING);
39+
prop.setAttribute(property_attribute::DOC, "The composite instruction");
40+
prop.setAttribute(property_attribute::REQUIRED, true);
4141
prop.addValidator(validateRequired);
4242
}
4343
// environment
4444
{
4545
auto& prop = inputs.get("environment");
4646
// env.setAttribute("enum", YAML::Load(R"(["dev","stag","prod"])"));
47-
prop.setAttribute("type", property_type::STRING);
48-
prop.setAttribute("description", "The tesseract environment");
49-
prop.setAttribute("required", true);
47+
prop.setAttribute(property_attribute::TYPE, property_type::STRING);
48+
prop.setAttribute(property_attribute::DOC, "The tesseract environment");
49+
prop.setAttribute(property_attribute::REQUIRED, true);
5050
prop.addValidator(validateRequired);
5151
}
5252
// profiles
5353
{
5454
auto& prop = inputs.get("profiles");
55-
prop.setAttribute("type", property_type::STRING);
56-
prop.setAttribute("description", "The tesseract profiles");
57-
prop.setAttribute("required", true);
55+
prop.setAttribute(property_attribute::TYPE, property_type::STRING);
56+
prop.setAttribute(property_attribute::DOC, "The tesseract profiles");
57+
prop.setAttribute(property_attribute::REQUIRED, true);
5858
prop.addValidator(validateRequired);
5959
// prof.setAttribute("enum", YAML::Load(R"(["A","B"])"));
6060
// prof.addValidator(validateEnum);
@@ -72,15 +72,15 @@ PropertyTree buildConfigSchema()
7272
{
7373
auto& outs = cfg.get("outputs");
7474
auto& prop = outs.get("program");
75-
prop.setAttribute("type", property_type::STRING);
76-
prop.setAttribute("required", true);
75+
prop.setAttribute(property_attribute::TYPE, property_type::STRING);
76+
prop.setAttribute(property_attribute::REQUIRED, true);
7777
prop.addValidator(validateRequired);
7878
}
7979
// format_result_as_input
8080
{
8181
auto& prop = cfg.get("format_result_as_input");
82-
prop.setAttribute("type", property_type::BOOL);
83-
prop.setAttribute("default", false);
82+
prop.setAttribute(property_attribute::TYPE, property_type::BOOL);
83+
prop.setAttribute(property_attribute::DEFAULT, false);
8484
}
8585
return schema;
8686
}

0 commit comments

Comments
 (0)