Skip to content

Commit

Permalink
Enforce a precedence for wildcard matching in parameter overrides. (r…
Browse files Browse the repository at this point in the history
…os2#1094)

Signed-off-by: Michel Hidalgo <[email protected]>
  • Loading branch information
hidmic authored and Joshua Hampp committed Jul 7, 2020
1 parent 8f34d88 commit 64a2f14
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
11 changes: 7 additions & 4 deletions rclcpp/src/rclcpp/node_interfaces/node_parameters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,14 @@ NodeParameters::NodeParameters(
rcl_yaml_node_struct_fini(params);
});
rclcpp::ParameterMap initial_map = rclcpp::parameter_map_from(params);
for (auto iter = initial_map.begin(); initial_map.end() != iter; iter++) {
// TODO(cottsay) implement further wildcard matching
if (iter->first == "/**" || iter->first == combined_name_) {

// Enforce wildcard matching precedence
// TODO(cottsay) implement further wildcard matching
const std::vector<std::string> node_matching_names{"/**", combined_name_};
for (const auto & node_name : node_matching_names) {
if (initial_map.count(node_name) > 0) {
// Combine parameter yaml files, overwriting values in older ones
for (auto & param : iter->second) {
for (const rclcpp::Parameter & param : initial_map.at(node_name)) {
parameter_overrides_[param.get_name()] =
rclcpp::ParameterValue(param.get_value_message());
}
Expand Down
6 changes: 5 additions & 1 deletion rclcpp/test/resources/test_node/test_parameters.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
/**:
ros__parameters:
parameter_int: 21
parameter_bool: true
parameter_int: 42
parameter_string_array: [baz, baz, baz]
test_declare_parameter_node:
ros__parameters:
parameter_int: 21
5 changes: 3 additions & 2 deletions rclcpp/test/test_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -691,7 +691,7 @@ TEST_F(TestNode, declare_parameter_with_cli_overrides) {
no.arguments(
{
"--ros-args",
"-p", "parameter_bool:=true",
"-p", "parameter_bool:=false",
"-p", "parameter_int:=42",
"-p", "parameter_double:=0.42",
"-p", "parameter_string:=foo",
Expand All @@ -702,7 +702,8 @@ TEST_F(TestNode, declare_parameter_with_cli_overrides) {
"-p", "parameter_string_array:=[foo, bar]"
});

auto node = std::make_shared<rclcpp::Node>("test_declare_parameter_node"_unq, no);
// To match parameters YAML file content, use a well-known node name for this test only.
auto node = std::make_shared<rclcpp::Node>("test_declare_parameter_node", no);
{
rclcpp::ParameterValue value = node->declare_parameter("parameter_bool");
EXPECT_EQ(value.get_type(), rclcpp::PARAMETER_BOOL);
Expand Down

0 comments on commit 64a2f14

Please sign in to comment.