Skip to content

Commit

Permalink
Update code again.
Browse files Browse the repository at this point in the history
Signed-off-by: Lei Liu <[email protected]>
  • Loading branch information
llapx committed Feb 6, 2023
1 parent ba4046a commit 60224ee
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 55 deletions.
79 changes: 37 additions & 42 deletions rcl_yaml_param_parser/src/parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,12 @@ _validate_name(const char * name, rcutils_allocator_t allocator);
///
/// Check a tag whether it is valid
///
/// \param tag the tag to check
/// \param tag the tag to check, include tags:
/// YAML_BOOL_TAG, YAML_STR_TAG, YAML_INT_TAG,
/// YAML_FLOAT_TAG, YAML_SEQ_TAG, YAML_MAP_TAG
/// NOTE: YAML_NULL_TAG & YAML_TIMESTAMP_TAG are
/// not supported by ROS2 Parameters, so they are
/// excluded.
/// \param line_num the line number error happened
/// \return RCUTILS_RET_OK if tag is valid, or
/// \return RCUTILS_RET_ERROR if tag is not valid
Expand All @@ -79,48 +84,48 @@ rcutils_ret_t
_validate_tag(const char * tag, uint32_t line_num);

///
/// Check a bool value whether it is valid
/// Get a bool value when it is valid
///
/// \param value the bool value to check
/// \param value the bool value to get
/// \param val_type the value type
/// \param ret_val the converted value when value is valid
/// \return RCUTILS_RET_OK if value is valid, or
/// \return RCUTILS_RET_ERROR if value is not valid
RCL_YAML_PARAM_PARSER_LOCAL
rcutils_ret_t
_validate_bool_value(
_get_bool_value(
const char * const value,
data_types_t * val_type,
void ** ret_val,
const rcutils_allocator_t allocator);

///
/// Check a int value whether it is valid
/// Get a int value when it is valid
///
/// \param value the int value to check
/// \param value the int value to get
/// \param val_type the value type
/// \param ret_val the converted value when value is valid
/// \return RCUTILS_RET_OK if value is valid, or
/// \return RCUTILS_RET_ERROR if value is not valid
RCL_YAML_PARAM_PARSER_LOCAL
rcutils_ret_t
_validate_int_value(
_get_int_value(
const char * const value,
data_types_t * val_type,
void ** ret_val,
const rcutils_allocator_t allocator);

///
/// Check a float value whether it is valid
/// Get a float value when it is valid
///
/// \param value the float value to check
/// \param value the float value to get
/// \param val_type the value type
/// \param ret_val the converted value when value is valid
/// \return RCUTILS_RET_OK if value is valid, or
/// \return RCUTILS_RET_ERROR if value is not valid
RCL_YAML_PARAM_PARSER_LOCAL
rcutils_ret_t
_validate_float_value(
_get_float_value(
const char * const value,
data_types_t * val_type,
void ** ret_val,
Expand Down Expand Up @@ -150,22 +155,9 @@ void * get_value(
return rcutils_strdup(value, allocator);
}

/// Check for yaml null tag
if (tag != NULL && strcmp(YAML_NULL_TAG, (char *)tag) == 0) {
*val_type = DATA_TYPE_STRING;
if ((0 == strcmp(value, "null")) ||
(0 == strcmp(value, "Null")) ||
(0 == strcmp(value, "none")) ||
(0 == strcmp(value, "None")))
{
return rcutils_strdup("null", allocator);
}
return NULL;
}

/// Check for yaml bool tag
if (tag != NULL && strcmp(YAML_BOOL_TAG, (char *)tag) == 0) {
if (_validate_bool_value(value, val_type, &ret_val, allocator) == RCUTILS_RET_OK) {
if (_get_bool_value(value, val_type, &ret_val, allocator) != RCUTILS_RET_ERROR) {
return ret_val;
} else {
return NULL;
Expand All @@ -174,7 +166,7 @@ void * get_value(

/// Check for yaml int tag
if (tag != NULL && strcmp(YAML_INT_TAG, (char *)tag) == 0) {
if (_validate_int_value(value, val_type, &ret_val, allocator) == RCUTILS_RET_OK) {
if (_get_int_value(value, val_type, &ret_val, allocator) != RCUTILS_RET_ERROR) {
return ret_val;
} else {
return NULL;
Expand All @@ -183,7 +175,7 @@ void * get_value(

/// Check for yaml float tag
if (tag != NULL && strcmp(YAML_FLOAT_TAG, (char *)tag) == 0) {
if (_validate_float_value(value, val_type, &ret_val, allocator) == RCUTILS_RET_OK) {
if (_get_float_value(value, val_type, &ret_val, allocator) != RCUTILS_RET_ERROR) {
return ret_val;
} else {
return NULL;
Expand All @@ -194,7 +186,7 @@ void * get_value(
if (style != YAML_SINGLE_QUOTED_SCALAR_STYLE &&
style != YAML_DOUBLE_QUOTED_SCALAR_STYLE)
{
if (_validate_bool_value(value, val_type, &ret_val, allocator) == RCUTILS_RET_OK) {
if (_get_bool_value(value, val_type, &ret_val, allocator) != RCUTILS_RET_ERROR) {
return ret_val;
}
}
Expand All @@ -203,7 +195,7 @@ void * get_value(
if (style != YAML_SINGLE_QUOTED_SCALAR_STYLE &&
style != YAML_DOUBLE_QUOTED_SCALAR_STYLE)
{
if (_validate_int_value(value, val_type, &ret_val, allocator) == RCUTILS_RET_OK) {
if (_get_int_value(value, val_type, &ret_val, allocator) != RCUTILS_RET_ERROR) {
return ret_val;
}
}
Expand All @@ -212,7 +204,7 @@ void * get_value(
if (style != YAML_SINGLE_QUOTED_SCALAR_STYLE &&
style != YAML_DOUBLE_QUOTED_SCALAR_STYLE)
{
if (_validate_float_value(value, val_type, &ret_val, allocator) == RCUTILS_RET_OK) {
if (_get_float_value(value, val_type, &ret_val, allocator) != RCUTILS_RET_ERROR) {
return ret_val;
}
}
Expand Down Expand Up @@ -601,8 +593,7 @@ _validate_tag(const char * tag, uint32_t line_num)
return RCUTILS_RET_ERROR;
}

if ((0 == strcmp(tag, YAML_NULL_TAG)) ||
(0 == strcmp(tag, YAML_BOOL_TAG)) ||
if ((0 == strcmp(tag, YAML_BOOL_TAG)) ||
(0 == strcmp(tag, YAML_STR_TAG)) ||
(0 == strcmp(tag, YAML_INT_TAG)) ||
(0 == strcmp(tag, YAML_FLOAT_TAG)) ||
Expand All @@ -618,7 +609,7 @@ _validate_tag(const char * tag, uint32_t line_num)
}

rcutils_ret_t
_validate_bool_value(
_get_bool_value(
const char * const value,
data_types_t * val_type,
void ** ret_val,
Expand All @@ -638,9 +629,10 @@ _validate_bool_value(
{
*val_type = DATA_TYPE_BOOL;
*ret_val = allocator.zero_allocate(1U, sizeof(bool), allocator.state);
if (NULL != *ret_val) {
*((bool *)*ret_val) = true;
if (NULL == *ret_val) {
return RCUTILS_RET_BAD_ALLOC;
}
*((bool *)*ret_val) = true;
return RCUTILS_RET_OK;
}

Expand All @@ -658,17 +650,18 @@ _validate_bool_value(
{
*val_type = DATA_TYPE_BOOL;
*ret_val = allocator.zero_allocate(1U, sizeof(bool), allocator.state);
if (NULL != *ret_val) {
*((bool *)*ret_val) = false;
if (NULL == *ret_val) {
return RCUTILS_RET_BAD_ALLOC;
}
*((bool *)*ret_val) = false;
return RCUTILS_RET_OK;
}

return RCUTILS_RET_ERROR;
}

rcutils_ret_t
_validate_int_value(
_get_int_value(
const char * const value,
data_types_t * val_type,
void ** ret_val,
Expand All @@ -684,9 +677,10 @@ _validate_int_value(
if (('\0' != *value) && ('\0' == *endptr)) {
*val_type = DATA_TYPE_INT64;
*ret_val = allocator.zero_allocate(1U, sizeof(int64_t), allocator.state);
if (NULL != *ret_val) {
*((int64_t *)*ret_val) = ival;
if (NULL == *ret_val) {
return RCUTILS_RET_BAD_ALLOC;
}
*((int64_t *)*ret_val) = ival;
return RCUTILS_RET_OK;
}
}
Expand All @@ -696,7 +690,7 @@ _validate_int_value(
}

rcutils_ret_t
_validate_float_value(
_get_float_value(
const char * const value,
data_types_t * val_type,
void ** ret_val,
Expand Down Expand Up @@ -735,9 +729,10 @@ _validate_float_value(
if (('\0' != *value) && ('\0' == *endptr)) {
*val_type = DATA_TYPE_DOUBLE;
*ret_val = allocator.zero_allocate(1U, sizeof(double), allocator.state);
if (NULL != *ret_val) {
*((double *)*ret_val) = dval;
if (NULL == *ret_val) {
return RCUTILS_RET_BAD_ALLOC;
}
*((double *)*ret_val) = dval;
return RCUTILS_RET_OK;
}
}
Expand Down
13 changes: 9 additions & 4 deletions rcl_yaml_param_parser/test/correct_config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,6 @@ bool_tag:
bool_false: !!bool false
bool_no: !!bool no
bool_off: !!bool off
null_tag:
ros__parameters:
null_null: !!null null
null_none: !!null none
int_tag:
ros__parameters:
int_pos: !!int 10
Expand All @@ -80,3 +76,12 @@ float_tag:
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}
66 changes: 57 additions & 9 deletions rcl_yaml_param_parser/test/test_parse_yaml.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -232,15 +232,6 @@ TEST(test_parser, correct_syntax) {
ASSERT_TRUE(NULL != param_value->bool_value);
EXPECT_FALSE(*param_value->bool_value);

param_value = rcl_yaml_node_struct_get("null_tag", "null_null", params);
ASSERT_TRUE(NULL != param_value) << rcutils_get_error_string().str;
ASSERT_TRUE(NULL != param_value->string_value);
EXPECT_STREQ("null", param_value->string_value);
param_value = rcl_yaml_node_struct_get("null_tag", "null_none", params);
ASSERT_TRUE(NULL != param_value) << rcutils_get_error_string().str;
ASSERT_TRUE(NULL != param_value->string_value);
EXPECT_STREQ("null", param_value->string_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);
Expand Down Expand Up @@ -283,6 +274,63 @@ TEST(test_parser, correct_syntax) {
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

0 comments on commit 60224ee

Please sign in to comment.