[svn] commit: r920 - in /trunk/src/lib/config/cpp: config_data.cc config_data.h config_data_unittests.cc

BIND 10 source code commits bind10-changes at lists.isc.org
Tue Feb 23 11:10:15 UTC 2010


Author: jelte
Date: Tue Feb 23 11:10:14 2010
New Revision: 920

Log:
implemented getItemList and getFullConfig in cpp version (+tests)

Modified:
    trunk/src/lib/config/cpp/config_data.cc
    trunk/src/lib/config/cpp/config_data.h
    trunk/src/lib/config/cpp/config_data_unittests.cc

Modified: trunk/src/lib/config/cpp/config_data.cc
==============================================================================
--- trunk/src/lib/config/cpp/config_data.cc (original)
+++ trunk/src/lib/config/cpp/config_data.cc Tue Feb 23 11:10:14 2010
@@ -26,12 +26,12 @@
 namespace isc {
 namespace config {
 
-ElementPtr
+static ElementPtr
 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(); }
-    //std::cout << "in: " << spec << std::endl;
+    //std::cout << "in: " << std::endl << spec << std::endl;
     ElementPtr spec_part = spec;
     if (identifier == "") {
         //std::cout << "[XX] empty id" << std::endl;
@@ -48,7 +48,7 @@
                 if (list_el->getType() == Element::map &&
                     list_el->contains("item_name") &&
                     list_el->get("item_name")->stringValue() == part) {
-                    spec_part = list_el->get("item_name");
+                    spec_part = list_el;
                     found = true;
                 }
             }
@@ -63,7 +63,7 @@
                     if (list_el->getType() == Element::map &&
                         list_el->contains("item_name") &&
                         list_el->get("item_name")->stringValue() == part) {
-                        spec_part = list_el->get("item_name");
+                        spec_part = list_el;
                         found = true;
                     }
                 }
@@ -73,7 +73,12 @@
                 }
             }
         }
-        id = id.substr(sep + 1);
+        if (sep < id.size()) {
+            id = id.substr(sep + 1);
+        } else {
+            id = "";
+        }
+        sep = id.find("/");
     }
     if (id != "" && id != "/") {
         if (spec_part->getType() == Element::list) {
@@ -108,7 +113,33 @@
             }
         }
     }
+    //std::cout << "[XX] found spec part: " << std::endl << spec_part << std::endl;
     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
@@ -137,27 +168,9 @@
     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
 ConfigData::getItemList(const std::string& identifier, bool recurse)
 {
@@ -170,11 +183,18 @@
     return result;
 }
 
+/// Returns an ElementPtr containing a MapElement with identifier->value
+/// pairs.
 ElementPtr
 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;
+}
+
+}
+}

Modified: trunk/src/lib/config/cpp/config_data.h
==============================================================================
--- trunk/src/lib/config/cpp/config_data.h (original)
+++ trunk/src/lib/config/cpp/config_data.h Tue Feb 23 11:10:14 2010
@@ -43,7 +43,7 @@
     void setModuleSpec(ModuleSpec module_spec) { _module_spec = module_spec; };
     void setLocalConfig(ElementPtr config) { _config = 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();
 
 private:

Modified: trunk/src/lib/config/cpp/config_data_unittests.cc
==============================================================================
--- trunk/src/lib/config/cpp/config_data_unittests.cc (original)
+++ trunk/src/lib/config/cpp/config_data_unittests.cc Tue Feb 23 11:10:14 2010
@@ -98,6 +98,20 @@
     ModuleSpec spec2 = moduleSpecFromFile(std::string(TEST_DATA_PATH) + "/spec2.spec");
     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());
+}
+




More information about the bind10-changes mailing list