Add support for TYPE_IPV4ADDR in yaml config module
This commit is contained in:
committed by
Guido Casati
parent
c7deef8203
commit
5354ad8a26
@@ -25,6 +25,8 @@ extern "C" {
|
|||||||
#include "common/config/config_userapi.h"
|
#include "common/config/config_userapi.h"
|
||||||
void *config_allocate_new(configmodule_interface_t *cfg, int sz, bool autoFree);
|
void *config_allocate_new(configmodule_interface_t *cfg, int sz, bool autoFree);
|
||||||
void config_check_valptr(configmodule_interface_t *cfg, paramdef_t *cfgoptions, int elt_sz, int nb_elt);
|
void config_check_valptr(configmodule_interface_t *cfg, paramdef_t *cfgoptions, int elt_sz, int nb_elt);
|
||||||
|
#include "../config_userapi.h"
|
||||||
|
#include "../config_common.h"
|
||||||
}
|
}
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <string>
|
#include <string>
|
||||||
@@ -98,6 +100,9 @@ void SetDefault(configmodule_interface_t *cfg, paramdef_t *param)
|
|||||||
param->uptr = (uint32_t *)param->defintarrayval;
|
param->uptr = (uint32_t *)param->defintarrayval;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
case TYPE_IPV4ADDR:
|
||||||
|
config_setdefault_ipv4addr(cfg, param, param->defstrval);
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
AssertFatal(false, "Unhandled type %d", param->type);
|
AssertFatal(false, "Unhandled type %d", param->type);
|
||||||
}
|
}
|
||||||
@@ -186,6 +191,16 @@ void SetNonDefault(configmodule_interface_t *cfg, const YAML::Node &node, paramd
|
|||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
case TYPE_IPV4ADDR: {
|
||||||
|
std::string ipv4addr = node[optname].as<std::string>();
|
||||||
|
char* ipv4addr_ptr = strdup(ipv4addr.c_str());
|
||||||
|
int rst = config_assign_ipv4addr(cfg, param, ipv4addr_ptr);
|
||||||
|
free(ipv4addr_ptr);
|
||||||
|
if (rst < 0) {
|
||||||
|
fprintf(stderr,"[LIBCONFIG] %s not valid for %s \n", ipv4addr.c_str(), optname.c_str());
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
default:
|
default:
|
||||||
AssertFatal(false, "Unhandled type %d", param->type);
|
AssertFatal(false, "Unhandled type %d", param->type);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,3 +10,4 @@ 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)
|
configure_file(test_read_mapping_as_list.yaml test_read_mapping_as_list.yaml COPYONLY)
|
||||||
|
configure_file(test_ipv4.yaml test_ipv4.yaml COPYONLY)
|
||||||
|
|||||||
@@ -0,0 +1,4 @@
|
|||||||
|
test:
|
||||||
|
ipv4_1: 0.0.0.0
|
||||||
|
ipv4_2: 192.168.0.1
|
||||||
|
#ipv4_3: check default
|
||||||
@@ -391,6 +391,36 @@ TEST(yaml_config, test_read_mapping_as_list) {
|
|||||||
end_configmodule(cfg);
|
end_configmodule(cfg);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST(yaml_config, test_read_ipv4) {
|
||||||
|
configmodule_interface_t *cfg = static_cast<configmodule_interface_t*>(calloc(1, sizeof(*cfg)));
|
||||||
|
cfg->cfgP[0] = strdup("test_ipv4.yaml");
|
||||||
|
EXPECT_EQ(config_yaml_init(cfg), 0);
|
||||||
|
|
||||||
|
paramdef_t p = {};
|
||||||
|
p.type = TYPE_IPV4ADDR;
|
||||||
|
strncpy(p.optname, "ipv4_1", sizeof(p.optname) - 1);
|
||||||
|
uint32_t addr = 0;
|
||||||
|
p.uptr = &addr;
|
||||||
|
p.defstrval = nullptr;
|
||||||
|
char prefix[] = "test";
|
||||||
|
config_yaml_get(cfg, &p, 1, prefix);
|
||||||
|
printf("%x\n", addr);
|
||||||
|
|
||||||
|
strncpy(p.optname, "ipv4_2", sizeof(p.optname) - 1);
|
||||||
|
config_yaml_get(cfg, &p, 1, prefix);
|
||||||
|
printf("%x\n", addr);
|
||||||
|
|
||||||
|
p.defstrval = strdup("10.0.0.1");
|
||||||
|
strncpy(p.optname, "ipv4_3", sizeof(p.optname) - 1);
|
||||||
|
config_yaml_get(cfg, &p, 1, prefix);
|
||||||
|
printf("%x\n", addr);
|
||||||
|
|
||||||
|
config_yaml_end(cfg);
|
||||||
|
free(cfg->cfgP[0]);
|
||||||
|
end_configmodule(cfg);
|
||||||
|
free(p.defstrval);
|
||||||
|
}
|
||||||
|
|
||||||
int main(int argc, char** argv)
|
int main(int argc, char** argv)
|
||||||
{
|
{
|
||||||
logInit();
|
logInit();
|
||||||
|
|||||||
Reference in New Issue
Block a user