From 5f0d33171213fcb7b882583a067ace8df399a942 Mon Sep 17 00:00:00 2001 From: Ralf Anton Beier Date: Fri, 24 May 2019 17:57:47 +0200 Subject: [PATCH] Allow empty strings if they are quoted. (#450) Signed-off-by: Ralf Anton Beier --- rcl_yaml_param_parser/src/parser.c | 5 ++++- rcl_yaml_param_parser/test/empty_string.yaml | 9 +++++++++ .../test/test_parse_yaml.cpp | 19 +++++++++++++++++++ 3 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 rcl_yaml_param_parser/test/empty_string.yaml diff --git a/rcl_yaml_param_parser/src/parser.c b/rcl_yaml_param_parser/src/parser.c index d6f41d2..4dff023 100644 --- a/rcl_yaml_param_parser/src/parser.c +++ b/rcl_yaml_param_parser/src/parser.c @@ -849,7 +849,10 @@ static rcl_ret_t parse_value( "Scalar value at line %d is bigger than %d bytes", line_num, MAX_STRING_SIZE); return RCL_RET_ERROR; } else { - if (0U == val_size) { + if (style != YAML_SINGLE_QUOTED_SCALAR_STYLE && + style != YAML_DOUBLE_QUOTED_SCALAR_STYLE && + 0U == val_size) + { RCL_SET_ERROR_MSG_WITH_FORMAT_STRING("No value at line %d", line_num); return RCL_RET_ERROR; } diff --git a/rcl_yaml_param_parser/test/empty_string.yaml b/rcl_yaml_param_parser/test/empty_string.yaml new file mode 100644 index 0000000..fb7876e --- /dev/null +++ b/rcl_yaml_param_parser/test/empty_string.yaml @@ -0,0 +1,9 @@ +# config/test_yaml +--- + +lidar_ns: + lidar_1: + ros__parameters: + id: 10 + name: "" + another: '' diff --git a/rcl_yaml_param_parser/test/test_parse_yaml.cpp b/rcl_yaml_param_parser/test/test_parse_yaml.cpp index 53d5b8a..03934a3 100644 --- a/rcl_yaml_param_parser/test/test_parse_yaml.cpp +++ b/rcl_yaml_param_parser/test/test_parse_yaml.cpp @@ -172,6 +172,25 @@ TEST(test_file_parser, max_string_sz) { allocator.deallocate(path, allocator.state); } +TEST(test_file_parser, empty_string) { + 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); + char * path = rcutils_join_path(test_path, "empty_string.yaml", allocator); + fprintf(stderr, "cur_path: %s\n", path); + EXPECT_TRUE(rcutils_exists(path)); + rcl_params_t * params_hdl = rcl_yaml_node_struct_init(allocator); + EXPECT_FALSE(NULL == params_hdl); + bool res = rcl_parse_yaml_file(path, params_hdl); + fprintf(stderr, "%s\n", rcutils_get_error_string().str); + EXPECT_TRUE(res); + rcl_yaml_node_struct_print(params_hdl); + rcl_yaml_node_struct_fini(params_hdl); + allocator.deallocate(test_path, allocator.state); + allocator.deallocate(path, allocator.state); +} + TEST(test_file_parser, no_value1) { rcutils_reset_error(); EXPECT_TRUE(rcutils_get_cwd(cur_dir, 1024));