Commit bf635ee4 authored by Jelte Jansen's avatar Jelte Jansen

[trac926] add 'named_map' type in cpp module check

parent 78763269
......@@ -511,6 +511,8 @@ Element::nameToType(const std::string& type_name) {
return (Element::list);
} else if (type_name == "map") {
return (Element::map);
} else if (type_name == "named_map") {
return (Element::map);
} else if (type_name == "null") {
return (Element::null);
} else if (type_name == "any") {
......
......@@ -67,10 +67,13 @@ check_config_item(ConstElementPtr spec) {
check_leaf_item(spec, "list_item_spec", Element::map, true);
check_config_item(spec->get("list_item_spec"));
}
// todo: add stuff for type map
if (Element::nameToType(spec->get("item_type")->stringValue()) == Element::map) {
if (spec->get("item_type")->stringValue() == "map") {
check_leaf_item(spec, "map_item_spec", Element::list, true);
check_config_item_list(spec->get("map_item_spec"));
} else if (spec->get("item_type")->stringValue() == "named_map") {
check_leaf_item(spec, "named_map_item_spec", Element::map, true);
check_config_item(spec->get("named_map_item_spec"));
}
}
......@@ -286,7 +289,8 @@ check_type(ConstElementPtr spec, ConstElementPtr element) {
return (cur_item_type == "list");
break;
case Element::map:
return (cur_item_type == "map");
return (cur_item_type == "map" ||
cur_item_type == "named_map");
break;
}
return (false);
......@@ -323,8 +327,19 @@ ModuleSpec::validateItem(ConstElementPtr spec, ConstElementPtr data,
}
}
if (data->getType() == Element::map) {
if (!validateSpecList(spec->get("map_item_spec"), data, full, errors)) {
return (false);
// either a 'normal' map or a 'named' map
if (spec->contains("map_item_spec")) {
if (!validateSpecList(spec->get("map_item_spec"), data, full, errors)) {
return (false);
}
} else {
typedef std::pair<std::string, ConstElementPtr> maptype;
BOOST_FOREACH(maptype m, data->mapValue()) {
if (!validateItem(spec->get("named_map_item_spec"), m.second, full, errors)) {
return (false);
}
}
}
}
return (true);
......
......@@ -211,3 +211,13 @@ TEST(ModuleSpec, CommandValidation) {
EXPECT_EQ(errors->get(0)->stringValue(), "Type mismatch");
}
TEST(ModuleSpec, NamedMapValidation) {
ModuleSpec dd = moduleSpecFromFile(specfile("spec32.spec"));
ElementPtr errors = Element::createList();
EXPECT_TRUE(dataTestWithErrors(dd, "data32_1.data", errors));
std::cout << "[XX] ERRORS: " << *errors << std::endl;
EXPECT_FALSE(dataTest(dd, "data32_2.data"));
EXPECT_FALSE(dataTest(dd, "data32_3.data"));
}
{
"named_map_item": { "foo": 1, "bar": 2 }
}
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