Skip to content

Commit

Permalink
Support yaml string tag '!!str' (#999)
Browse files Browse the repository at this point in the history
* Support yaml string tag '!!str'

Signed-off-by: Barry Xu <[email protected]>

* Address review comments

Signed-off-by: Barry Xu <[email protected]>

* Update based on review comments

Signed-off-by: Barry Xu <[email protected]>

Signed-off-by: Barry Xu <[email protected]>
  • Loading branch information
Barry-Xu-2018 authored Jan 4, 2023
1 parent 6c0cc99 commit d651a5c
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 1 deletion.
1 change: 1 addition & 0 deletions rcl_yaml_param_parser/src/impl/parse.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ RCUTILS_WARN_UNUSED
void * get_value(
const char * const value,
yaml_scalar_style_t style,
const yaml_char_t * const tag,
data_types_t * val_type,
const rcutils_allocator_t allocator);

Expand Down
10 changes: 9 additions & 1 deletion rcl_yaml_param_parser/src/parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ _validate_name(const char * name, rcutils_allocator_t allocator);
void * get_value(
const char * const value,
yaml_scalar_style_t style,
const yaml_char_t * const tag,
data_types_t * val_type,
const rcutils_allocator_t allocator)
{
Expand All @@ -87,6 +88,12 @@ void * get_value(
RCUTILS_CHECK_ALLOCATOR_WITH_MSG(
&allocator, "allocator is invalid", return NULL);

/// Check for yaml string tag
if (tag != NULL && strcmp(YAML_STR_TAG, (char *)tag) == 0) {
*val_type = DATA_TYPE_STRING;
return rcutils_strdup(value, allocator);
}

/// Check if it is bool
if (style != YAML_SINGLE_QUOTED_SCALAR_STYLE &&
style != YAML_DOUBLE_QUOTED_SCALAR_STYLE)
Expand Down Expand Up @@ -233,6 +240,7 @@ rcutils_ret_t parse_value(
const size_t val_size = event.data.scalar.length;
const char * value = (char *)event.data.scalar.value;
yaml_scalar_style_t style = event.data.scalar.style;
const yaml_char_t * const tag = event.data.scalar.tag;
const uint32_t line_num = ((uint32_t)(event.start_mark.line) + 1U);

RCUTILS_CHECK_FOR_NULL_WITH_MSG(
Expand All @@ -254,7 +262,7 @@ rcutils_ret_t parse_value(
rcl_variant_t * param_value = &(params_st->params[node_idx].parameter_values[parameter_idx]);

data_types_t val_type;
void * ret_val = get_value(value, style, &val_type, allocator);
void * ret_val = get_value(value, style, tag, &val_type, allocator);
if (NULL == ret_val) {
RCUTILS_SET_ERROR_MSG_WITH_FORMAT_STRING(
"Error parsing value %s at line %d", value, line_num);
Expand Down
5 changes: 5 additions & 0 deletions rcl_yaml_param_parser/test/correct_config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,8 @@ intel:
ros__parameters:
num_cores: 8
arch: x86_64
string_tag:
ros__parameters:
string_bool: !!str yes
string_int: !!str 1234
string_double: !!str 12.34
13 changes: 13 additions & 0 deletions rcl_yaml_param_parser/test/test_parse_yaml.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,19 @@ TEST(test_parser, correct_syntax) {
ASSERT_EQ(1U, param_value->string_array_value->size);
EXPECT_STREQ("Mobius", param_value->string_array_value->data[0]);

param_value = rcl_yaml_node_struct_get("string_tag", "string_bool", params);
ASSERT_TRUE(NULL != param_value) << rcutils_get_error_string().str;
ASSERT_TRUE(NULL != param_value->string_value);
EXPECT_STREQ("yes", param_value->string_value);
param_value = rcl_yaml_node_struct_get("string_tag", "string_int", params);
ASSERT_TRUE(NULL != param_value) << rcutils_get_error_string().str;
ASSERT_TRUE(NULL != param_value->string_value);
EXPECT_STREQ("1234", param_value->string_value);
param_value = rcl_yaml_node_struct_get("string_tag", "string_double", params);
ASSERT_TRUE(NULL != param_value) << rcutils_get_error_string().str;
ASSERT_TRUE(NULL != param_value->string_value);
EXPECT_STREQ("12.34", param_value->string_value);

rcl_yaml_node_struct_print(params);
}
}
Expand Down

0 comments on commit d651a5c

Please sign in to comment.