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

Add yaml tag full support. #1032

Draft
wants to merge 4 commits into
base: rolling
Choose a base branch
from
Draft
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
363 changes: 267 additions & 96 deletions rcl_yaml_param_parser/src/parse.c

Large diffs are not rendered by default.

31 changes: 31 additions & 0 deletions rcl_yaml_param_parser/test/correct_config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,34 @@ string_tag:
string_bool: !!str yes
string_int: !!str 1234
string_double: !!str 12.34
bool_tag:
ros__parameters:
bool_true: !!bool true
bool_yes: !!bool yes
bool_on: !!bool on
bool_false: !!bool false
bool_no: !!bool no
bool_off: !!bool off
int_tag:
ros__parameters:
int_pos: !!int 10
int_zero: !!int 0
int_neg: !!int -10
float_tag:
ros__parameters:
float_pos: !!float 1.1
float_zero: !!float 0.0
float_neg: !!float -1.122
float_nan: !!float .nan
float_inf: !!float .inf
float_infn: !!float +.inf
float_infp: !!float -.inf
seq_tag:
ros__parameters:
seq_string: !!seq [ab, bc, cd, de]
seq_bool: !!seq [true, yes, on, false, no, off]
seq_int: !!seq [10, 0, -10]
seq_float: !!seq [1.1, 0.0, -1.122, .nan, .inf, +.inf, -.inf]
map_tag:
ros__parameters:
map_test: !!map {str: string, bool: true, int: 10, float: 1.1}
3 changes: 3 additions & 0 deletions rcl_yaml_param_parser/test/tag_incorrect_config_01.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
bool_tag:
ros__parameters:
test: !!bool eurt
3 changes: 3 additions & 0 deletions rcl_yaml_param_parser/test/tag_incorrect_config_02.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
int_tag:
ros__parameters:
test: !!int a10
3 changes: 3 additions & 0 deletions rcl_yaml_param_parser/test/tag_incorrect_config_03.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
float_tag:
ros__parameters:
test: !!float a10
3 changes: 3 additions & 0 deletions rcl_yaml_param_parser/test/tag_incorrect_config_04.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
seq_tag:
ros__parameters:
test: !!seq {a: 1, b: 2}
3 changes: 3 additions & 0 deletions rcl_yaml_param_parser/test/tag_incorrect_config_05.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
map_tag:
ros__parameters:
test: !!map [1, 2]
2 changes: 2 additions & 0 deletions rcl_yaml_param_parser/test/test_parse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,7 @@ TEST(TestParse, parse_file_events_mock_yaml_parser_parse) {
"lib:rcl_yaml_param_parser", yaml_parser_parse, [](yaml_parser_t *, yaml_event_t * event) {
event->start_mark.line = 0u;
event->type = YAML_NO_EVENT;
event->data.scalar.tag = NULL;
return 1;
});
EXPECT_EQ(RCUTILS_RET_ERROR, parse_file_events(&parser, &ns_tracker, params_hdl));
Expand All @@ -518,6 +519,7 @@ TEST(TestParse, parse_value_events_mock_yaml_parser_parse) {
"lib:rcl_yaml_param_parser", yaml_parser_parse, [](yaml_parser_t *, yaml_event_t * event) {
event->start_mark.line = 0u;
event->type = YAML_NO_EVENT;
event->data.scalar.tag = NULL;
return 1;
});
EXPECT_FALSE(rcl_parse_yaml_value(node_name, param_name, yaml_value, params_st));
Expand Down
161 changes: 161 additions & 0 deletions rcl_yaml_param_parser/test/test_parse_yaml.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,130 @@ TEST(test_parser, correct_syntax) {
ASSERT_TRUE(NULL != param_value->string_value);
EXPECT_STREQ("12.34", param_value->string_value);

param_value = rcl_yaml_node_struct_get("bool_tag", "bool_true", params);
ASSERT_TRUE(NULL != param_value) << rcutils_get_error_string().str;
ASSERT_TRUE(NULL != param_value->bool_value);
EXPECT_TRUE(*param_value->bool_value);
param_value = rcl_yaml_node_struct_get("bool_tag", "bool_yes", params);
ASSERT_TRUE(NULL != param_value) << rcutils_get_error_string().str;
ASSERT_TRUE(NULL != param_value->bool_value);
EXPECT_TRUE(*param_value->bool_value);
param_value = rcl_yaml_node_struct_get("bool_tag", "bool_on", params);
ASSERT_TRUE(NULL != param_value) << rcutils_get_error_string().str;
ASSERT_TRUE(NULL != param_value->bool_value);
EXPECT_TRUE(*param_value->bool_value);
param_value = rcl_yaml_node_struct_get("bool_tag", "bool_false", params);
ASSERT_TRUE(NULL != param_value) << rcutils_get_error_string().str;
ASSERT_TRUE(NULL != param_value->bool_value);
EXPECT_FALSE(*param_value->bool_value);
param_value = rcl_yaml_node_struct_get("bool_tag", "bool_no", params);
ASSERT_TRUE(NULL != param_value) << rcutils_get_error_string().str;
ASSERT_TRUE(NULL != param_value->bool_value);
EXPECT_FALSE(*param_value->bool_value);
param_value = rcl_yaml_node_struct_get("bool_tag", "bool_off", params);
ASSERT_TRUE(NULL != param_value) << rcutils_get_error_string().str;
ASSERT_TRUE(NULL != param_value->bool_value);
EXPECT_FALSE(*param_value->bool_value);

param_value = rcl_yaml_node_struct_get("int_tag", "int_pos", params);
ASSERT_TRUE(NULL != param_value) << rcutils_get_error_string().str;
ASSERT_TRUE(NULL != param_value->integer_value);
EXPECT_EQ(10, *param_value->integer_value);
param_value = rcl_yaml_node_struct_get("int_tag", "int_zero", params);
ASSERT_TRUE(NULL != param_value) << rcutils_get_error_string().str;
ASSERT_TRUE(NULL != param_value->integer_value);
EXPECT_EQ(0, *param_value->integer_value);
param_value = rcl_yaml_node_struct_get("int_tag", "int_neg", params);
ASSERT_TRUE(NULL != param_value) << rcutils_get_error_string().str;
ASSERT_TRUE(NULL != param_value->integer_value);
EXPECT_EQ(-10, *param_value->integer_value);

param_value = rcl_yaml_node_struct_get("float_tag", "float_pos", params);
ASSERT_TRUE(NULL != param_value) << rcutils_get_error_string().str;
ASSERT_TRUE(NULL != param_value->double_value);
EXPECT_DOUBLE_EQ(1.1, *param_value->double_value);
param_value = rcl_yaml_node_struct_get("float_tag", "float_zero", params);
ASSERT_TRUE(NULL != param_value) << rcutils_get_error_string().str;
ASSERT_TRUE(NULL != param_value->double_value);
EXPECT_DOUBLE_EQ(0.0, *param_value->double_value);
param_value = rcl_yaml_node_struct_get("float_tag", "float_neg", params);
ASSERT_TRUE(NULL != param_value) << rcutils_get_error_string().str;
ASSERT_TRUE(NULL != param_value->double_value);
EXPECT_DOUBLE_EQ(-1.122, *param_value->double_value);
param_value = rcl_yaml_node_struct_get("float_tag", "float_nan", params);
ASSERT_TRUE(NULL != param_value) << rcutils_get_error_string().str;
ASSERT_TRUE(NULL != param_value->double_value);
EXPECT_TRUE(std::isnan(*param_value->double_value));
param_value = rcl_yaml_node_struct_get("float_tag", "float_inf", params);
ASSERT_TRUE(NULL != param_value) << rcutils_get_error_string().str;
ASSERT_TRUE(NULL != param_value->double_value);
EXPECT_DOUBLE_EQ(INFINITY, *param_value->double_value);
param_value = rcl_yaml_node_struct_get("float_tag", "float_infn", params);
ASSERT_TRUE(NULL != param_value) << rcutils_get_error_string().str;
ASSERT_TRUE(NULL != param_value->double_value);
EXPECT_DOUBLE_EQ(INFINITY, *param_value->double_value);
param_value = rcl_yaml_node_struct_get("float_tag", "float_infp", params);
ASSERT_TRUE(NULL != param_value) << rcutils_get_error_string().str;
ASSERT_TRUE(NULL != param_value->double_value);
EXPECT_DOUBLE_EQ(-INFINITY, *param_value->double_value);

param_value = rcl_yaml_node_struct_get("seq_tag", "seq_string", params_hdl);
ASSERT_TRUE(NULL != param_value) << rcutils_get_error_string().str;
ASSERT_TRUE(NULL != param_value->string_array_value);
ASSERT_EQ(4U, param_value->string_array_value->size);
EXPECT_STREQ("ab", param_value->string_array_value->data[0]);
EXPECT_STREQ("bc", param_value->string_array_value->data[1]);
EXPECT_STREQ("cd", param_value->string_array_value->data[2]);
EXPECT_STREQ("de", param_value->string_array_value->data[3]);

param_value = rcl_yaml_node_struct_get("seq_tag", "seq_bool", params_hdl);
ASSERT_TRUE(NULL != param_value) << rcutils_get_error_string().str;
ASSERT_TRUE(NULL != param_value->bool_array_value);
ASSERT_EQ(6U, param_value->bool_array_value->size);
EXPECT_TRUE(param_value->bool_array_value->values[0]);
EXPECT_TRUE(param_value->bool_array_value->values[1]);
EXPECT_TRUE(param_value->bool_array_value->values[2]);
EXPECT_FALSE(param_value->bool_array_value->values[3]);
EXPECT_FALSE(param_value->bool_array_value->values[4]);
EXPECT_FALSE(param_value->bool_array_value->values[5]);

param_value = rcl_yaml_node_struct_get("seq_tag", "seq_int", params_hdl);
ASSERT_TRUE(NULL != param_value) << rcutils_get_error_string().str;
ASSERT_TRUE(NULL != param_value->integer_array_value);
ASSERT_EQ(3U, param_value->integer_array_value->size);
EXPECT_EQ(10, param_value->integer_array_value->values[0]);
EXPECT_EQ(0, param_value->integer_array_value->values[1]);
EXPECT_EQ(-10, param_value->integer_array_value->values[2]);

param_value = rcl_yaml_node_struct_get("seq_tag", "seq_float", params_hdl);
ASSERT_TRUE(NULL != param_value) << rcutils_get_error_string().str;
ASSERT_TRUE(NULL != param_value->double_array_value);
ASSERT_EQ(7U, param_value->double_array_value->size);
EXPECT_DOUBLE_EQ(1.1, param_value->double_array_value->values[0]);
EXPECT_DOUBLE_EQ(0.0, param_value->double_array_value->values[1]);
EXPECT_DOUBLE_EQ(-1.122, param_value->double_array_value->values[2]);
EXPECT_TRUE(std::isnan(param_value->double_array_value->values[3]));
EXPECT_DOUBLE_EQ(INFINITY, param_value->double_array_value->values[4]);
EXPECT_DOUBLE_EQ(INFINITY, param_value->double_array_value->values[5]);
EXPECT_DOUBLE_EQ(-INFINITY, param_value->double_array_value->values[6]);

param_value = rcl_yaml_node_struct_get("map_tag", "map_test.str", params);
ASSERT_TRUE(NULL != param_value) << rcutils_get_error_string().str;
ASSERT_TRUE(NULL != param_value->string_value);
EXPECT_STREQ("string", param_value->string_value);
param_value = rcl_yaml_node_struct_get("map_tag", "map_test.bool", params);
ASSERT_TRUE(NULL != param_value) << rcutils_get_error_string().str;
ASSERT_TRUE(NULL != param_value->bool_value);
EXPECT_TRUE(*param_value->bool_value);
param_value = rcl_yaml_node_struct_get("map_tag", "map_test.int", params);
ASSERT_TRUE(NULL != param_value) << rcutils_get_error_string().str;
ASSERT_TRUE(NULL != param_value->integer_value);
EXPECT_EQ(10, *param_value->integer_value);
param_value = rcl_yaml_node_struct_get("map_tag", "map_test.float", params);
ASSERT_TRUE(NULL != param_value) << rcutils_get_error_string().str;
ASSERT_TRUE(NULL != param_value->double_value);
EXPECT_DOUBLE_EQ(1.1, *param_value->double_value);

rcl_yaml_node_struct_print(params);
}
}
Expand Down Expand Up @@ -674,6 +798,43 @@ TEST(test_file_parser, wildcards_partial) {
}
}

TEST(test_file_parser, tag_incorrect_config) {
rcutils_reset_error();
EXPECT_TRUE(rcutils_get_cwd(cur_dir, 1024));
rcutils_allocator_t allocator = rcutils_get_default_allocator();
char * test_path = rcutils_join_path(cur_dir, "test", allocator);
ASSERT_TRUE(NULL != test_path) << rcutils_get_error_string().str;
OSRF_TESTING_TOOLS_CPP_SCOPE_EXIT(
{
allocator.deallocate(test_path, allocator.state);
});
const std::vector<std::string> filenames = {
"tag_incorrect_config_01.yaml",
"tag_incorrect_config_02.yaml",
"tag_incorrect_config_03.yaml",
"tag_incorrect_config_04.yaml",
"tag_incorrect_config_05.yaml"
};

for (auto & filename : filenames) {
char * path = rcutils_join_path(test_path, filename.c_str(), allocator);
ASSERT_TRUE(NULL != path) << rcutils_get_error_string().str;
OSRF_TESTING_TOOLS_CPP_SCOPE_EXIT(
{
allocator.deallocate(path, allocator.state);
});
EXPECT_TRUE(rcutils_exists(path));
rcl_params_t * params_hdl = rcl_yaml_node_struct_init(allocator);
ASSERT_TRUE(NULL != params_hdl) << rcutils_get_error_string().str;
OSRF_TESTING_TOOLS_CPP_SCOPE_EXIT(
{
rcl_yaml_node_struct_fini(params_hdl);
});
bool res = rcl_parse_yaml_file(path, params_hdl);
EXPECT_FALSE(res);
}
}

int32_t main(int32_t argc, char ** argv)
{
::testing::InitGoogleTest(&argc, argv);
Expand Down