Skip to content

Commit c5b70a9

Browse files
committed
Refactor parser.c for better testability (#754)
* Refactor rcl_yaml_param_parser for better testability Signed-off-by: Stephen Brawner <[email protected]> * Reorder parser.c to match parser.h Signed-off-by: Stephen Brawner <[email protected]> squash! Reorder parser.c to match parser.h * Refactor yaml_variant.c for deduplication Signed-off-by: Stephen Brawner <[email protected]> * ADD_VALUE_TO_SIMPLE_ARRAY for deduplication Signed-off-by: Stephen Brawner <[email protected]> * Remove fprintf Signed-off-by: Stephen Brawner <[email protected]> * PR Fixup Signed-off-by: Stephen Brawner <[email protected]> * Move headers to src directory Signed-off-by: Stephen Brawner <[email protected]> * PR Fixup Signed-off-by: Stephen Brawner <[email protected]> * Rebase #780 Signed-off-by: Stephen Brawner <[email protected]>
1 parent 2798b69 commit c5b70a9

File tree

13 files changed

+1900
-1586
lines changed

13 files changed

+1900
-1586
lines changed

rcl_yaml_param_parser/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,12 @@ if(CMAKE_COMPILER_IS_GNUCC OR CMAKE_C_COMPILER_ID MATCHES "Clang")
1616
endif()
1717

1818
set(rcl_yaml_parser_sources
19+
src/add_to_arrays.c
20+
src/namespace.c
21+
src/node_params.c
22+
src/parse.c
1923
src/parser.c
24+
src/yaml_variant.c
2025
)
2126

2227
add_library(
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
// Copyright 2018 Apex.AI, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include "./impl/add_to_arrays.h"
16+
17+
#define ADD_VALUE_TO_SIMPLE_ARRAY(val_array, value, value_type, allocator) \
18+
do { \
19+
if (NULL == val_array->values) { \
20+
val_array->values = value; \
21+
val_array->size = 1; \
22+
} else { \
23+
/* Increase the array size by one and add the new value */ \
24+
value_type * tmp_arr = val_array->values; \
25+
val_array->values = allocator.zero_allocate( \
26+
val_array->size + 1U, sizeof(value_type), allocator.state); \
27+
if (NULL == val_array->values) { \
28+
val_array->values = tmp_arr; \
29+
RCUTILS_SAFE_FWRITE_TO_STDERR("Error allocating mem\n"); \
30+
return RCUTILS_RET_BAD_ALLOC; \
31+
} \
32+
memcpy(val_array->values, tmp_arr, (val_array->size * sizeof(value_type))); \
33+
val_array->values[val_array->size] = *value; \
34+
val_array->size++; \
35+
allocator.deallocate(value, allocator.state); \
36+
allocator.deallocate(tmp_arr, allocator.state); \
37+
} \
38+
return RCUTILS_RET_OK; \
39+
} while (0)
40+
41+
rcutils_ret_t add_val_to_bool_arr(
42+
rcl_bool_array_t * const val_array,
43+
bool * value,
44+
const rcutils_allocator_t allocator)
45+
{
46+
RCUTILS_CHECK_ARGUMENT_FOR_NULL(val_array, RCUTILS_RET_INVALID_ARGUMENT);
47+
RCUTILS_CHECK_ARGUMENT_FOR_NULL(value, RCUTILS_RET_INVALID_ARGUMENT);
48+
RCUTILS_CHECK_ALLOCATOR_WITH_MSG(
49+
&allocator, "invalid allocator", return RCUTILS_RET_INVALID_ARGUMENT);
50+
51+
ADD_VALUE_TO_SIMPLE_ARRAY(val_array, value, bool, allocator);
52+
}
53+
54+
///
55+
/// Add a value to an integer array. Create the array if it does not exist
56+
///
57+
rcutils_ret_t add_val_to_int_arr(
58+
rcl_int64_array_t * const val_array,
59+
int64_t * value,
60+
const rcutils_allocator_t allocator)
61+
{
62+
RCUTILS_CHECK_ARGUMENT_FOR_NULL(val_array, RCUTILS_RET_INVALID_ARGUMENT);
63+
RCUTILS_CHECK_ARGUMENT_FOR_NULL(value, RCUTILS_RET_INVALID_ARGUMENT);
64+
RCUTILS_CHECK_ALLOCATOR_WITH_MSG(
65+
&allocator, "invalid allocator", return RCUTILS_RET_INVALID_ARGUMENT);
66+
67+
ADD_VALUE_TO_SIMPLE_ARRAY(val_array, value, int64_t, allocator);
68+
}
69+
70+
///
71+
/// Add a value to a double array. Create the array if it does not exist
72+
///
73+
rcutils_ret_t add_val_to_double_arr(
74+
rcl_double_array_t * const val_array,
75+
double * value,
76+
const rcutils_allocator_t allocator)
77+
{
78+
RCUTILS_CHECK_ARGUMENT_FOR_NULL(val_array, RCUTILS_RET_INVALID_ARGUMENT);
79+
RCUTILS_CHECK_ARGUMENT_FOR_NULL(value, RCUTILS_RET_INVALID_ARGUMENT);
80+
RCUTILS_CHECK_ALLOCATOR_WITH_MSG(
81+
&allocator, "invalid allocator", return RCUTILS_RET_INVALID_ARGUMENT);
82+
83+
ADD_VALUE_TO_SIMPLE_ARRAY(val_array, value, double, allocator);
84+
}
85+
86+
///
87+
/// Add a value to a string array. Create the array if it does not exist
88+
///
89+
rcutils_ret_t add_val_to_string_arr(
90+
rcutils_string_array_t * const val_array,
91+
char * value,
92+
const rcutils_allocator_t allocator)
93+
{
94+
RCUTILS_CHECK_ARGUMENT_FOR_NULL(val_array, RCUTILS_RET_INVALID_ARGUMENT);
95+
RCUTILS_CHECK_ARGUMENT_FOR_NULL(value, RCUTILS_RET_INVALID_ARGUMENT);
96+
RCUTILS_CHECK_ALLOCATOR_WITH_MSG(
97+
&allocator, "invalid allocator", return RCUTILS_RET_INVALID_ARGUMENT);
98+
99+
if (NULL == val_array->data) {
100+
rcutils_ret_t ret = rcutils_string_array_init(val_array, 1, &allocator);
101+
if (RCUTILS_RET_OK != ret) {
102+
return ret;
103+
}
104+
val_array->data[0U] = value;
105+
} else {
106+
/// Increase the array size by one and add the new value
107+
char ** new_string_arr_ptr = allocator.reallocate(
108+
val_array->data,
109+
((val_array->size + 1U) * sizeof(char *)), allocator.state);
110+
if (NULL == new_string_arr_ptr) {
111+
RCUTILS_SAFE_FWRITE_TO_STDERR("Error allocating mem\n");
112+
return RCUTILS_RET_BAD_ALLOC;
113+
}
114+
val_array->data = new_string_arr_ptr;
115+
val_array->data[val_array->size] = value;
116+
val_array->size++;
117+
}
118+
return RCUTILS_RET_OK;
119+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// Copyright 2018 Apex.AI, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#ifndef IMPL__ADD_TO_ARRAYS_H_
16+
#define IMPL__ADD_TO_ARRAYS_H_
17+
18+
#include <yaml.h>
19+
20+
#include "rcutils/types.h"
21+
22+
#include "./types.h"
23+
#include "rcl_yaml_param_parser/types.h"
24+
#include "rcl_yaml_param_parser/visibility_control.h"
25+
26+
#ifdef __cplusplus
27+
extern "C"
28+
{
29+
#endif
30+
31+
///
32+
/// Add a value to a bool array. Create the array if it does not exist
33+
///
34+
RCL_YAML_PARAM_PARSER_PUBLIC
35+
RCUTILS_WARN_UNUSED
36+
rcutils_ret_t add_val_to_bool_arr(
37+
rcl_bool_array_t * const val_array,
38+
bool * value,
39+
const rcutils_allocator_t allocator);
40+
41+
///
42+
/// Add a value to an integer array. Create the array if it does not exist
43+
///
44+
RCL_YAML_PARAM_PARSER_PUBLIC
45+
RCUTILS_WARN_UNUSED
46+
rcutils_ret_t add_val_to_int_arr(
47+
rcl_int64_array_t * const val_array,
48+
int64_t * value,
49+
const rcutils_allocator_t allocator);
50+
51+
///
52+
/// Add a value to a double array. Create the array if it does not exist
53+
///
54+
RCL_YAML_PARAM_PARSER_PUBLIC
55+
RCUTILS_WARN_UNUSED
56+
rcutils_ret_t add_val_to_double_arr(
57+
rcl_double_array_t * const val_array,
58+
double * value,
59+
const rcutils_allocator_t allocator);
60+
61+
///
62+
/// Add a value to a string array. Create the array if it does not exist
63+
///
64+
RCL_YAML_PARAM_PARSER_PUBLIC
65+
RCUTILS_WARN_UNUSED
66+
rcutils_ret_t add_val_to_string_arr(
67+
rcutils_string_array_t * const val_array,
68+
char * value,
69+
const rcutils_allocator_t allocator);
70+
71+
///
72+
/// TODO (anup.pemmaiah): Support byte array
73+
///
74+
75+
#ifdef __cplusplus
76+
}
77+
#endif
78+
79+
#endif // IMPL__ADD_TO_ARRAYS_H_
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// Copyright 2018 Apex.AI, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#ifndef IMPL__NAMESPACE_H_
16+
#define IMPL__NAMESPACE_H_
17+
18+
#include <yaml.h>
19+
20+
#include "rcutils/types.h"
21+
22+
#include "./types.h"
23+
#include "rcl_yaml_param_parser/types.h"
24+
#include "rcl_yaml_param_parser/visibility_control.h"
25+
26+
#ifdef __cplusplus
27+
extern "C"
28+
{
29+
#endif
30+
31+
///
32+
/// Add name to namespace tracker
33+
///
34+
RCL_YAML_PARAM_PARSER_PUBLIC
35+
RCUTILS_WARN_UNUSED
36+
rcutils_ret_t add_name_to_ns(
37+
namespace_tracker_t * ns_tracker,
38+
const char * name,
39+
const namespace_type_t namespace_type,
40+
rcutils_allocator_t allocator);
41+
42+
///
43+
/// Remove name from namespace tracker
44+
///
45+
RCL_YAML_PARAM_PARSER_PUBLIC
46+
RCUTILS_WARN_UNUSED
47+
rcutils_ret_t rem_name_from_ns(
48+
namespace_tracker_t * ns_tracker,
49+
const namespace_type_t namespace_type,
50+
rcutils_allocator_t allocator);
51+
52+
///
53+
/// Replace namespace in namespace tracker
54+
///
55+
RCL_YAML_PARAM_PARSER_PUBLIC
56+
RCUTILS_WARN_UNUSED
57+
rcutils_ret_t replace_ns(
58+
namespace_tracker_t * ns_tracker,
59+
char * const new_ns,
60+
const uint32_t new_ns_count,
61+
const namespace_type_t namespace_type,
62+
rcutils_allocator_t allocator);
63+
64+
#ifdef __cplusplus
65+
}
66+
#endif
67+
68+
#endif // IMPL__NAMESPACE_H_
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Copyright 2018 Apex.AI, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#ifndef IMPL__NODE_PARAMS_H_
16+
#define IMPL__NODE_PARAMS_H_
17+
18+
#include "rcl_yaml_param_parser/types.h"
19+
#include "rcl_yaml_param_parser/visibility_control.h"
20+
21+
#ifdef __cplusplus
22+
extern "C"
23+
{
24+
#endif
25+
26+
///
27+
/// Create rcl_node_params_t structure
28+
///
29+
RCL_YAML_PARAM_PARSER_PUBLIC
30+
RCUTILS_WARN_UNUSED
31+
rcutils_ret_t node_params_init(
32+
rcl_node_params_t * node_params,
33+
const rcutils_allocator_t allocator);
34+
35+
///
36+
/// Finalize rcl_node_params_t structure
37+
///
38+
RCL_YAML_PARAM_PARSER_PUBLIC
39+
void rcl_yaml_node_params_fini(
40+
rcl_node_params_t * node_params,
41+
const rcutils_allocator_t allocator);
42+
43+
#ifdef __cplusplus
44+
}
45+
#endif
46+
47+
#endif // IMPL__NODE_PARAMS_H_

0 commit comments

Comments
 (0)