[foxy backport] Refactor parser.c for better testability (#754) (#784)

* Refactor parser.c for better testability (#754)

* Refactor rcl_yaml_param_parser for better testability

Signed-off-by: Stephen Brawner <brawner@gmail.com>

* Reorder parser.c to match parser.h

Signed-off-by: Stephen Brawner <brawner@gmail.com>

squash! Reorder parser.c to match parser.h

* Refactor yaml_variant.c for deduplication

Signed-off-by: Stephen Brawner <brawner@gmail.com>

* ADD_VALUE_TO_SIMPLE_ARRAY for deduplication

Signed-off-by: Stephen Brawner <brawner@gmail.com>

* Remove fprintf

Signed-off-by: Stephen Brawner <brawner@gmail.com>

* PR Fixup

Signed-off-by: Stephen Brawner <brawner@gmail.com>

* Move headers to src directory

Signed-off-by: Stephen Brawner <brawner@gmail.com>

* PR Fixup

Signed-off-by: Stephen Brawner <brawner@gmail.com>

* Rebase #780

Signed-off-by: Stephen Brawner <brawner@gmail.com>

* Missing includes

Signed-off-by: Stephen Brawner <brawner@gmail.com>
This commit is contained in:
brawner 2020-10-05 18:01:07 -07:00 committed by GitHub
parent 6a137f1aba
commit 2c513aa0ed
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
14 changed files with 1929 additions and 1601 deletions

View file

@ -0,0 +1,79 @@
// Copyright 2018 Apex.AI, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef IMPL__ADD_TO_ARRAYS_H_
#define IMPL__ADD_TO_ARRAYS_H_
#include <yaml.h>
#include "rcutils/types.h"
#include "./types.h"
#include "rcl_yaml_param_parser/types.h"
#include "rcl_yaml_param_parser/visibility_control.h"
#ifdef __cplusplus
extern "C"
{
#endif
///
/// Add a value to a bool array. Create the array if it does not exist
///
RCL_YAML_PARAM_PARSER_PUBLIC
RCUTILS_WARN_UNUSED
rcutils_ret_t add_val_to_bool_arr(
rcl_bool_array_t * const val_array,
bool * value,
const rcutils_allocator_t allocator);
///
/// Add a value to an integer array. Create the array if it does not exist
///
RCL_YAML_PARAM_PARSER_PUBLIC
RCUTILS_WARN_UNUSED
rcutils_ret_t add_val_to_int_arr(
rcl_int64_array_t * const val_array,
int64_t * value,
const rcutils_allocator_t allocator);
///
/// Add a value to a double array. Create the array if it does not exist
///
RCL_YAML_PARAM_PARSER_PUBLIC
RCUTILS_WARN_UNUSED
rcutils_ret_t add_val_to_double_arr(
rcl_double_array_t * const val_array,
double * value,
const rcutils_allocator_t allocator);
///
/// Add a value to a string array. Create the array if it does not exist
///
RCL_YAML_PARAM_PARSER_PUBLIC
RCUTILS_WARN_UNUSED
rcutils_ret_t add_val_to_string_arr(
rcutils_string_array_t * const val_array,
char * value,
const rcutils_allocator_t allocator);
///
/// TODO (anup.pemmaiah): Support byte array
///
#ifdef __cplusplus
}
#endif
#endif // IMPL__ADD_TO_ARRAYS_H_

View file

@ -0,0 +1,68 @@
// Copyright 2018 Apex.AI, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef IMPL__NAMESPACE_H_
#define IMPL__NAMESPACE_H_
#include <yaml.h>
#include "rcutils/types.h"
#include "./types.h"
#include "rcl_yaml_param_parser/types.h"
#include "rcl_yaml_param_parser/visibility_control.h"
#ifdef __cplusplus
extern "C"
{
#endif
///
/// Add name to namespace tracker
///
RCL_YAML_PARAM_PARSER_PUBLIC
RCUTILS_WARN_UNUSED
rcutils_ret_t add_name_to_ns(
namespace_tracker_t * ns_tracker,
const char * name,
const namespace_type_t namespace_type,
rcutils_allocator_t allocator);
///
/// Remove name from namespace tracker
///
RCL_YAML_PARAM_PARSER_PUBLIC
RCUTILS_WARN_UNUSED
rcutils_ret_t rem_name_from_ns(
namespace_tracker_t * ns_tracker,
const namespace_type_t namespace_type,
rcutils_allocator_t allocator);
///
/// Replace namespace in namespace tracker
///
RCL_YAML_PARAM_PARSER_PUBLIC
RCUTILS_WARN_UNUSED
rcutils_ret_t replace_ns(
namespace_tracker_t * ns_tracker,
char * const new_ns,
const uint32_t new_ns_count,
const namespace_type_t namespace_type,
rcutils_allocator_t allocator);
#ifdef __cplusplus
}
#endif
#endif // IMPL__NAMESPACE_H_

View file

@ -0,0 +1,47 @@
// Copyright 2018 Apex.AI, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef IMPL__NODE_PARAMS_H_
#define IMPL__NODE_PARAMS_H_
#include "rcl_yaml_param_parser/types.h"
#include "rcl_yaml_param_parser/visibility_control.h"
#ifdef __cplusplus
extern "C"
{
#endif
///
/// Create rcl_node_params_t structure
///
RCL_YAML_PARAM_PARSER_PUBLIC
RCUTILS_WARN_UNUSED
rcutils_ret_t node_params_init(
rcl_node_params_t * node_params,
const rcutils_allocator_t allocator);
///
/// Finalize rcl_node_params_t structure
///
RCL_YAML_PARAM_PARSER_PUBLIC
void rcl_yaml_node_params_fini(
rcl_node_params_t * node_params,
const rcutils_allocator_t allocator);
#ifdef __cplusplus
}
#endif
#endif // IMPL__NODE_PARAMS_H_

View file

@ -0,0 +1,94 @@
// Copyright 2018 Apex.AI, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef IMPL__PARSE_H_
#define IMPL__PARSE_H_
#include <yaml.h>
#include "rcutils/types.h"
#include "./types.h"
#include "rcl_yaml_param_parser/types.h"
#include "rcl_yaml_param_parser/visibility_control.h"
#ifdef __cplusplus
extern "C"
{
#endif
RCL_YAML_PARAM_PARSER_PUBLIC
RCUTILS_WARN_UNUSED
void * get_value(
const char * const value,
yaml_scalar_style_t style,
data_types_t * val_type,
const rcutils_allocator_t allocator);
RCL_YAML_PARAM_PARSER_PUBLIC
RCUTILS_WARN_UNUSED
rcutils_ret_t parse_value(
const yaml_event_t event,
const bool is_seq,
const size_t node_idx,
const size_t parameter_idx,
data_types_t * seq_data_type,
rcl_params_t * params_st);
RCL_YAML_PARAM_PARSER_PUBLIC
RCUTILS_WARN_UNUSED
rcutils_ret_t parse_key(
const yaml_event_t event,
uint32_t * map_level,
bool * is_new_map,
size_t * node_idx,
size_t * parameter_idx,
namespace_tracker_t * ns_tracker,
rcl_params_t * params_st);
RCL_YAML_PARAM_PARSER_PUBLIC
RCUTILS_WARN_UNUSED
rcutils_ret_t parse_file_events(
yaml_parser_t * parser,
namespace_tracker_t * ns_tracker,
rcl_params_t * params_st);
RCL_YAML_PARAM_PARSER_PUBLIC
RCUTILS_WARN_UNUSED
rcutils_ret_t parse_value_events(
yaml_parser_t * parser,
const size_t node_idx,
const size_t parameter_idx,
rcl_params_t * params_st);
RCL_YAML_PARAM_PARSER_PUBLIC
RCUTILS_WARN_UNUSED
rcutils_ret_t find_node(
const char * node_name,
rcl_params_t * param_st,
size_t * node_idx);
RCL_YAML_PARAM_PARSER_PUBLIC
RCUTILS_WARN_UNUSED
rcutils_ret_t find_parameter(
const size_t node_idx,
const char * parameter_name,
rcl_params_t * param_st,
size_t * parameter_idx);
#ifdef __cplusplus
}
#endif
#endif // IMPL__PARSE_H_

View file

@ -0,0 +1,72 @@
// Copyright 2018 Apex.AI, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/// NOTE: Will allow a max YAML mapping depth of 5
/// map level 1 : Node name mapping
/// map level 2 : Params mapping
#ifndef IMPL__TYPES_H_
#define IMPL__TYPES_H_
#include <inttypes.h>
#include <stdint.h>
#ifdef __cplusplus
extern "C"
{
#endif
#define PARAMS_KEY "ros__parameters"
#define NODE_NS_SEPERATOR "/"
#define PARAMETER_NS_SEPERATOR "."
#define MAX_NUM_PARAMS_PER_NODE 512U
typedef enum yaml_map_lvl_e
{
MAP_UNINIT_LVL = 0U,
MAP_NODE_NAME_LVL = 1U,
MAP_PARAMS_LVL = 2U,
} yaml_map_lvl_t;
/// Basic supported data types in the yaml file
typedef enum data_types_e
{
DATA_TYPE_UNKNOWN = 0U,
DATA_TYPE_BOOL = 1U,
DATA_TYPE_INT64 = 2U,
DATA_TYPE_DOUBLE = 3U,
DATA_TYPE_STRING = 4U
} data_types_t;
typedef enum namespace_type_e
{
NS_TYPE_NODE = 1U,
NS_TYPE_PARAM = 2U
} namespace_type_t;
/// Keep track of node and parameter name spaces
typedef struct namespace_tracker_s
{
char * node_ns;
uint32_t num_node_ns;
char * parameter_ns;
uint32_t num_parameter_ns;
} namespace_tracker_t;
#ifdef __cplusplus
}
#endif
#endif // IMPL__TYPES_H_

View file

@ -0,0 +1,49 @@
// Copyright 2018 Apex.AI, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef IMPL__YAML_VARIANT_H_
#define IMPL__YAML_VARIANT_H_
#include "rcutils/allocator.h"
#include "./types.h"
#include "rcl_yaml_param_parser/types.h"
#include "rcl_yaml_param_parser/visibility_control.h"
#ifdef __cplusplus
extern "C"
{
#endif
///
/// Finalize an rcl_yaml_variant_t
///
RCL_YAML_PARAM_PARSER_PUBLIC
void rcl_yaml_variant_fini(
rcl_variant_t * param_var,
const rcutils_allocator_t allocator);
///
/// Copy a yaml_variant_t from param_var to out_param_var
///
RCL_YAML_PARAM_PARSER_PUBLIC
RCUTILS_WARN_UNUSED
bool rcl_yaml_variant_copy(
rcl_variant_t * out_param_var, const rcl_variant_t * param_var, rcutils_allocator_t allocator);
#ifdef __cplusplus
}
#endif
#endif // IMPL__YAML_VARIANT_H_