Commit fee95ed3 authored by Jaroslava Fiedlerova's avatar Jaroslava Fiedlerova

Merge remote-tracking branch 'origin/align-yaml-to-config' into integration_2024_w49 (!3138)

Align YAML config getlist with libconfig implementation

This aligns the behavior of params_yaml with params_config library:

- when a mapping is read using getlist it returns the number of elements in
  the mapping instead of 0
- when a mapping is read using get it returns the number of parameters used
  for input
parents 429e98e4 08279ab9
...@@ -265,7 +265,7 @@ extern "C" int config_yaml_get(configmodule_interface_t *cfg, paramdef_t *cfgopt ...@@ -265,7 +265,7 @@ extern "C" int config_yaml_get(configmodule_interface_t *cfg, paramdef_t *cfgopt
} }
} }
config_yaml::GetParams(cfg, node, cfgoptions, numoptions); config_yaml::GetParams(cfg, node, cfgoptions, numoptions);
return 0; return numoptions;
} }
extern "C" int config_yaml_getlist(configmodule_interface_t *cfg, extern "C" int config_yaml_getlist(configmodule_interface_t *cfg,
...@@ -286,10 +286,10 @@ extern "C" int config_yaml_getlist(configmodule_interface_t *cfg, ...@@ -286,10 +286,10 @@ extern "C" int config_yaml_getlist(configmodule_interface_t *cfg,
return -1; return -1;
} }
ParamList->numelt = node.size();
if (!node.IsSequence()) { if (!node.IsSequence()) {
return -1; return -1;
} }
ParamList->numelt = node.size();
if (ParamList->numelt > 0 && params != NULL) { if (ParamList->numelt > 0 && params != NULL) {
ParamList->paramarray = static_cast<paramdef_t **>(config_allocate_new(cfg, ParamList->numelt * sizeof(paramdef_t *), true)); ParamList->paramarray = static_cast<paramdef_t **>(config_allocate_new(cfg, ParamList->numelt * sizeof(paramdef_t *), true));
......
...@@ -9,3 +9,4 @@ configure_file(test_list.yaml test_list.yaml COPYONLY) ...@@ -9,3 +9,4 @@ configure_file(test_list.yaml test_list.yaml COPYONLY)
configure_file(test_string.yaml test_string.yaml COPYONLY) configure_file(test_string.yaml test_string.yaml COPYONLY)
configure_file(test_list_of_mappings.yml test_list_of_mappings.yml COPYONLY) configure_file(test_list_of_mappings.yml test_list_of_mappings.yml COPYONLY)
configure_file(test_int_array.yaml test_int_array.yaml COPYONLY) configure_file(test_int_array.yaml test_int_array.yaml COPYONLY)
configure_file(test_read_mapping_as_list.yaml test_read_mapping_as_list.yaml COPYONLY)
fhi_72:
element1: value1
element2: value2
element3: value3
...@@ -125,7 +125,7 @@ TEST(yaml_config, test_high_recusion) { ...@@ -125,7 +125,7 @@ TEST(yaml_config, test_high_recusion) {
p.type = TYPE_UINT16; p.type = TYPE_UINT16;
p.u16ptr = &value; p.u16ptr = &value;
char prefix[] = "test.test1.test2.test3.test4"; char prefix[] = "test.test1.test2.test3.test4";
EXPECT_EQ(config_yaml_get(cfg, &p, 1, prefix), 0); EXPECT_EQ(config_yaml_get(cfg, &p, 1, prefix), 1);
EXPECT_EQ(value, i); EXPECT_EQ(value, i);
} }
...@@ -175,7 +175,7 @@ TEST(yaml_config, test_string_auto_alloc) { ...@@ -175,7 +175,7 @@ TEST(yaml_config, test_string_auto_alloc) {
// Test automatic allocation of strings // Test automatic allocation of strings
char prefix[] = "test"; char prefix[] = "test";
EXPECT_EQ(config_yaml_get(cfg, &param, 1, prefix), 0); EXPECT_EQ(config_yaml_get(cfg, &param, 1, prefix), 1);
EXPECT_NE(param.strptr, nullptr); EXPECT_NE(param.strptr, nullptr);
EXPECT_EQ(strcmp(*param.strptr, "testvalue"), 0); EXPECT_EQ(strcmp(*param.strptr, "testvalue"), 0);
EXPECT_EQ(cfg->numptrs, 2); EXPECT_EQ(cfg->numptrs, 2);
...@@ -200,7 +200,7 @@ TEST(yaml_config, test_string_no_realloc) { ...@@ -200,7 +200,7 @@ TEST(yaml_config, test_string_no_realloc) {
char prefix[] = "test"; char prefix[] = "test";
char* non_null_pointer_to_pointer = nullptr; char* non_null_pointer_to_pointer = nullptr;
param.strptr = &non_null_pointer_to_pointer; param.strptr = &non_null_pointer_to_pointer;
EXPECT_EQ(config_yaml_get(cfg, &param, 1, prefix), 0); EXPECT_EQ(config_yaml_get(cfg, &param, 1, prefix), 1);
EXPECT_EQ(param.strptr, &non_null_pointer_to_pointer); EXPECT_EQ(param.strptr, &non_null_pointer_to_pointer);
EXPECT_EQ(strcmp(*param.strptr, "testvalue"), 0); EXPECT_EQ(strcmp(*param.strptr, "testvalue"), 0);
EXPECT_EQ(cfg->numptrs, 1); EXPECT_EQ(cfg->numptrs, 1);
...@@ -225,7 +225,7 @@ TEST(yaml_config, test_string_pointer_available) { ...@@ -225,7 +225,7 @@ TEST(yaml_config, test_string_pointer_available) {
char prefix[] = "test"; char prefix[] = "test";
char *container = (char *)malloc(sizeof(char) * 30); char *container = (char *)malloc(sizeof(char) * 30);
param.strptr = &container; param.strptr = &container;
EXPECT_EQ(config_yaml_get(cfg, &param, 1, prefix), 0); EXPECT_EQ(config_yaml_get(cfg, &param, 1, prefix), 1);
EXPECT_EQ(param.strptr, &container); EXPECT_EQ(param.strptr, &container);
EXPECT_EQ(strcmp(*param.strptr, "testvalue"), 0); EXPECT_EQ(strcmp(*param.strptr, "testvalue"), 0);
EXPECT_EQ(cfg->numptrs, 0); EXPECT_EQ(cfg->numptrs, 0);
...@@ -253,7 +253,7 @@ TEST(yaml_config, test_string_default_value) { ...@@ -253,7 +253,7 @@ TEST(yaml_config, test_string_default_value) {
param.defstrval = default_value; param.defstrval = default_value;
param.strptr = nullptr; param.strptr = nullptr;
sprintf(param.optname, "%s", "stringvalue_missing"); sprintf(param.optname, "%s", "stringvalue_missing");
EXPECT_EQ(config_yaml_get(cfg, &param, 1, prefix), 0); EXPECT_EQ(config_yaml_get(cfg, &param, 1, prefix), 1);
EXPECT_NE(param.strptr, nullptr); EXPECT_NE(param.strptr, nullptr);
EXPECT_EQ(strcmp(*param.strptr, "default"), 0); EXPECT_EQ(strcmp(*param.strptr, "default"), 0);
EXPECT_EQ(cfg->numptrs, 2) << " 2 pointers required, 1 for the string, one for the pointer-to string"; EXPECT_EQ(cfg->numptrs, 2) << " 2 pointers required, 1 for the string, one for the pointer-to string";
...@@ -276,7 +276,7 @@ TEST(yaml_config, test_stringlist) { ...@@ -276,7 +276,7 @@ TEST(yaml_config, test_stringlist) {
// Test automatic allocation of string lists // Test automatic allocation of string lists
char prefix[] = "test"; char prefix[] = "test";
EXPECT_EQ(config_yaml_get(cfg, &param, 1, prefix), 0); EXPECT_EQ(config_yaml_get(cfg, &param, 1, prefix), 1);
EXPECT_NE(param.strptr, nullptr); EXPECT_NE(param.strptr, nullptr);
for (auto i = 0; i < param.numelt; i++) { for (auto i = 0; i < param.numelt; i++) {
std::cout << (param.strlistptr)[i] << std::endl; std::cout << (param.strlistptr)[i] << std::endl;
...@@ -302,7 +302,7 @@ TEST(yaml_config, test_list_of_mappings) { ...@@ -302,7 +302,7 @@ TEST(yaml_config, test_list_of_mappings) {
// Test automatic allocation of string lists // Test automatic allocation of string lists
char prefix1[] = "test.list.[0]"; char prefix1[] = "test.list.[0]";
EXPECT_EQ(config_yaml_get(cfg, &param, 1, prefix1), 0); EXPECT_EQ(config_yaml_get(cfg, &param, 1, prefix1), 1);
EXPECT_NE(param.strptr, nullptr); EXPECT_NE(param.strptr, nullptr);
EXPECT_STREQ("value1", param.strlistptr[0]); EXPECT_STREQ("value1", param.strlistptr[0]);
EXPECT_STREQ("value2", param.strlistptr[1]); EXPECT_STREQ("value2", param.strlistptr[1]);
...@@ -310,7 +310,7 @@ TEST(yaml_config, test_list_of_mappings) { ...@@ -310,7 +310,7 @@ TEST(yaml_config, test_list_of_mappings) {
char prefix2[] = "test.list.[1]"; char prefix2[] = "test.list.[1]";
param.strlistptr = nullptr; param.strlistptr = nullptr;
EXPECT_EQ(config_yaml_get(cfg, &param, 1, prefix2), 0); EXPECT_EQ(config_yaml_get(cfg, &param, 1, prefix2), 1);
EXPECT_NE(param.strptr, nullptr); EXPECT_NE(param.strptr, nullptr);
EXPECT_STREQ("value4", param.strlistptr[0]); EXPECT_STREQ("value4", param.strlistptr[0]);
EXPECT_STREQ("value5", param.strlistptr[1]); EXPECT_STREQ("value5", param.strlistptr[1]);
...@@ -334,7 +334,7 @@ TEST(yaml_config, test_int_array) { ...@@ -334,7 +334,7 @@ TEST(yaml_config, test_int_array) {
// Test automatic allocation of int arrays // Test automatic allocation of int arrays
char prefix[] = "test"; char prefix[] = "test";
EXPECT_EQ(config_yaml_get(cfg, &param, 1, prefix), 0); EXPECT_EQ(config_yaml_get(cfg, &param, 1, prefix), 1);
EXPECT_NE(param.iptr, nullptr); EXPECT_NE(param.iptr, nullptr);
ASSERT_EQ(param.numelt, 4); ASSERT_EQ(param.numelt, 4);
EXPECT_EQ(1, param.iptr[0]); EXPECT_EQ(1, param.iptr[0]);
...@@ -345,7 +345,7 @@ TEST(yaml_config, test_int_array) { ...@@ -345,7 +345,7 @@ TEST(yaml_config, test_int_array) {
param.uptr = nullptr; param.uptr = nullptr;
param.numelt = 0; param.numelt = 0;
sprintf(param.optname, "%s", "array2"); sprintf(param.optname, "%s", "array2");
EXPECT_EQ(config_yaml_get(cfg, &param, 1, prefix), 0); EXPECT_EQ(config_yaml_get(cfg, &param, 1, prefix), 1);
EXPECT_NE(param.uptr, nullptr); EXPECT_NE(param.uptr, nullptr);
ASSERT_EQ(param.numelt, 3); ASSERT_EQ(param.numelt, 3);
EXPECT_EQ(1U, param.uptr[0]); EXPECT_EQ(1U, param.uptr[0]);
...@@ -355,7 +355,7 @@ TEST(yaml_config, test_int_array) { ...@@ -355,7 +355,7 @@ TEST(yaml_config, test_int_array) {
param.uptr = nullptr; param.uptr = nullptr;
param.numelt = 0; param.numelt = 0;
sprintf(param.optname, "%s", "non-existent-array"); sprintf(param.optname, "%s", "non-existent-array");
EXPECT_EQ(config_yaml_get(cfg, &param, 1, prefix), 0); EXPECT_EQ(config_yaml_get(cfg, &param, 1, prefix), 1);
EXPECT_EQ(param.uptr, nullptr); EXPECT_EQ(param.uptr, nullptr);
ASSERT_EQ(param.numelt, 0); ASSERT_EQ(param.numelt, 0);
...@@ -364,6 +364,33 @@ TEST(yaml_config, test_int_array) { ...@@ -364,6 +364,33 @@ TEST(yaml_config, test_int_array) {
end_configmodule(cfg); end_configmodule(cfg);
} }
TEST(yaml_config, test_read_mapping_as_list) {
configmodule_interface_t *cfg = static_cast<configmodule_interface_t*>(calloc(1, sizeof(*cfg)));
cfg->cfgP[0] = strdup("test_read_mapping_as_list.yaml");
EXPECT_EQ(config_yaml_init(cfg), 0);
char cfgstring[] = "fhi_72";
paramlist_def_t pl = {0};
strncpy(pl.listname, cfgstring, sizeof(pl.listname) - 1);
config_yaml_getlist(cfg, &pl, NULL, 0, /* prefix */ NULL);
EXPECT_NE(pl.numelt, 0);
EXPECT_EQ(pl.numelt, 3);
paramdef_t params[4] = {0};
for (auto i = 0U; i < sizeofArray(params); i++) {
sprintf(params[i].optname, "%s%d", "element_", i+1);
params[i].type = TYPE_STRING;
params[i].paramflags = PARAMFLAG_MANDATORY;
}
params[sizeofArray(params) - 1].paramflags = 0;
int ret = config_yaml_get(cfg, params, sizeofArray(params), cfgstring);
EXPECT_EQ(ret, 4);
config_yaml_end(cfg);
free(cfg->cfgP[0]);
end_configmodule(cfg);
}
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
logInit(); logInit();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment