Browse Source

implemented getItemList and getFullConfig in cpp version (+tests)

git-svn-id: svn://bind10.isc.org/svn/bind10/trunk@920 e5f2f494-b856-4b98-b285-d166d9295462
Jelte Jansen 15 years ago
parent
commit
8a98890b6f

+ 48 - 28
src/lib/config/cpp/config_data.cc

@@ -26,12 +26,12 @@ using namespace isc::data;
 namespace isc {
 namespace isc {
 namespace config {
 namespace config {
 
 
-ElementPtr
+static ElementPtr
 find_spec_part(ElementPtr spec, const std::string& identifier)
 find_spec_part(ElementPtr spec, const std::string& identifier)
 {
 {
-    //std::cout << "[XX] find_spec_part" << std::endl;
+    //std::cout << "[XX] find_spec_part for " << identifier << std::endl;
     if (!spec) { return ElementPtr(); }
     if (!spec) { return ElementPtr(); }
-    //std::cout << "in: " << spec << std::endl;
+    //std::cout << "in: " << std::endl << spec << std::endl;
     ElementPtr spec_part = spec;
     ElementPtr spec_part = spec;
     if (identifier == "") {
     if (identifier == "") {
         //std::cout << "[XX] empty id" << std::endl;
         //std::cout << "[XX] empty id" << std::endl;
@@ -48,7 +48,7 @@ find_spec_part(ElementPtr spec, const std::string& identifier)
                 if (list_el->getType() == Element::map &&
                 if (list_el->getType() == Element::map &&
                     list_el->contains("item_name") &&
                     list_el->contains("item_name") &&
                     list_el->get("item_name")->stringValue() == part) {
                     list_el->get("item_name")->stringValue() == part) {
-                    spec_part = list_el->get("item_name");
+                    spec_part = list_el;
                     found = true;
                     found = true;
                 }
                 }
             }
             }
@@ -63,7 +63,7 @@ find_spec_part(ElementPtr spec, const std::string& identifier)
                     if (list_el->getType() == Element::map &&
                     if (list_el->getType() == Element::map &&
                         list_el->contains("item_name") &&
                         list_el->contains("item_name") &&
                         list_el->get("item_name")->stringValue() == part) {
                         list_el->get("item_name")->stringValue() == part) {
-                        spec_part = list_el->get("item_name");
+                        spec_part = list_el;
                         found = true;
                         found = true;
                     }
                     }
                 }
                 }
@@ -73,7 +73,12 @@ find_spec_part(ElementPtr spec, const std::string& identifier)
                 }
                 }
             }
             }
         }
         }
-        id = id.substr(sep + 1);
+        if (sep < id.size()) {
+            id = id.substr(sep + 1);
+        } else {
+            id = "";
+        }
+        sep = id.find("/");
     }
     }
     if (id != "" && id != "/") {
     if (id != "" && id != "/") {
         if (spec_part->getType() == Element::list) {
         if (spec_part->getType() == Element::list) {
@@ -108,9 +113,35 @@ find_spec_part(ElementPtr spec, const std::string& identifier)
             }
             }
         }
         }
     }
     }
+    //std::cout << "[XX] found spec part: " << std::endl << spec_part << std::endl;
     return spec_part;
     return spec_part;
 }
 }
 
 
+static void
+spec_name_list(ElementPtr result, ElementPtr spec_part, std::string prefix, bool recurse = false)
+{
+    if (spec_part->getType() == Element::list) {
+        BOOST_FOREACH(ElementPtr list_el, spec_part->listValue()) {
+            if (list_el->getType() == Element::map &&
+                list_el->contains("item_name")) {
+                std::string new_prefix = prefix;
+                if (prefix != "") { new_prefix += "/"; }
+                new_prefix += list_el->get("item_name")->stringValue();
+                if (recurse && list_el->get("item_type")->stringValue() == "map") {
+                    spec_name_list(result, list_el->get("map_item_spec"), new_prefix, recurse);
+                } else {
+                    if (list_el->get("item_type")->stringValue() == "map" ||
+                        list_el->get("item_type")->stringValue() == "list"
+                    ) {
+                        new_prefix += "/";
+                    }
+                    result->add(Element::create(new_prefix));
+                }
+            }
+        }
+    }
+}
+
 ElementPtr
 ElementPtr
 ConfigData::getValue(const std::string& identifier)
 ConfigData::getValue(const std::string& identifier)
 {
 {
@@ -137,27 +168,9 @@ ConfigData::getValue(bool& is_default, const std::string& identifier)
     return value;
     return value;
 }
 }
 
 
-void
-spec_name_list(ElementPtr result, ElementPtr spec_part, std::string prefix, bool recurse = false)
-{
-    if (spec_part->getType() == Element::list) {
-        BOOST_FOREACH(ElementPtr list_el, spec_part->listValue()) {
-            if (list_el->getType() == Element::map &&
-                list_el->contains("item_name")) {
-                    result->add(Element::create(prefix + "/" + list_el->get("item_name")->stringValue()));
-            }
-        }
-    } else if (spec_part->getType() == Element::map &&
-               spec_part->contains("map_item_spec")
-    ) {
-        if (recurse) {
-            spec_name_list(result, spec_part->get("map_item_spec"), prefix + "/" + spec_part->get("item_name")->stringValue(), recurse);
-        } else {
-            result->add(Element::create(prefix + "/" + spec_part->get("item_name")->stringValue()));
-        }
-    }
-}
-
+/// Returns an ElementPtr pointing to a ListElement containing
+/// StringElements with the names of the options at the given
+/// identifier. If recurse is true, maps will be expanded as well
 ElementPtr
 ElementPtr
 ConfigData::getItemList(const std::string& identifier, bool recurse)
 ConfigData::getItemList(const std::string& identifier, bool recurse)
 {
 {
@@ -170,10 +183,17 @@ ConfigData::getItemList(const std::string& identifier, bool recurse)
     return result;
     return result;
 }
 }
 
 
+/// Returns an ElementPtr containing a MapElement with identifier->value
+/// pairs.
 ElementPtr
 ElementPtr
 ConfigData::getFullConfig()
 ConfigData::getFullConfig()
 {
 {
-    return ElementPtr();
+    ElementPtr result = Element::createFromString("{}");
+    ElementPtr items = getItemList("", true);
+    BOOST_FOREACH(ElementPtr item, items->listValue()) {
+        result->set(item->stringValue(), getValue(item->stringValue()));
+    }
+    return result;
 }
 }
 
 
 }
 }

+ 1 - 1
src/lib/config/cpp/config_data.h

@@ -43,7 +43,7 @@ public:
     void setModuleSpec(ModuleSpec module_spec) { _module_spec = module_spec; };
     void setModuleSpec(ModuleSpec module_spec) { _module_spec = module_spec; };
     void setLocalConfig(ElementPtr config) { _config = config; }
     void setLocalConfig(ElementPtr config) { _config = config; }
     ElementPtr getLocalConfig() { return _config; }
     ElementPtr getLocalConfig() { return _config; }
-    ElementPtr getItemList(const std::string& identifier, bool recurse = false);
+    ElementPtr getItemList(const std::string& identifier = "", bool recurse = false);
     ElementPtr getFullConfig();
     ElementPtr getFullConfig();
 
 
 private:
 private:

+ 15 - 1
src/lib/config/cpp/config_data_unittests.cc

@@ -98,6 +98,20 @@ TEST(ConfigData, getItemList) {
     ModuleSpec spec2 = moduleSpecFromFile(std::string(TEST_DATA_PATH) + "/spec2.spec");
     ModuleSpec spec2 = moduleSpecFromFile(std::string(TEST_DATA_PATH) + "/spec2.spec");
     ConfigData cd = ConfigData(spec2);
     ConfigData cd = ConfigData(spec2);
 
 
-    //EXPECT_EQ("", cd.getItemList("")->str());
+    EXPECT_EQ("[ \"item1\", \"item2\", \"item3\", \"item4\", \"item5/\", \"item6/\" ]", cd.getItemList()->str());
+    EXPECT_EQ("[ \"item1\", \"item2\", \"item3\", \"item4\", \"item5/\", \"item6/value1\", \"item6/value2\" ]", cd.getItemList("", true)->str());
+}
+
+TEST(ConfigData, getFullConfig) {
+    ModuleSpec spec2 = moduleSpecFromFile(std::string(TEST_DATA_PATH) + "/spec2.spec");
+    ConfigData cd = ConfigData(spec2);
+
+    EXPECT_EQ("{\"item1\": 1, \"item2\": 1.1, \"item3\": True, \"item4\": \"test\", \"item5/\": [ \"a\", \"b\" ], \"item6/value1\": \"default\", \"item6/value2\": None}", cd.getFullConfig()->str());
+    ElementPtr my_config = Element::createFromString("{\"item1\": 2}");
+    cd.setLocalConfig(my_config);
+    EXPECT_EQ("{\"item1\": 2, \"item2\": 1.1, \"item3\": True, \"item4\": \"test\", \"item5/\": [ \"a\", \"b\" ], \"item6/value1\": \"default\", \"item6/value2\": None}", cd.getFullConfig()->str());
+    ElementPtr my_config2 = Element::createFromString("{\"item6\": { \"value1\": \"a\" } }");
+    cd.setLocalConfig(my_config2);
+    EXPECT_EQ("{\"item1\": 1, \"item2\": 1.1, \"item3\": True, \"item4\": \"test\", \"item5/\": [ \"a\", \"b\" ], \"item6/value1\": \"a\", \"item6/value2\": None}", cd.getFullConfig()->str());
 }
 }