Refactor NetconfAccess::getItems algorithm

I'll be using the same algorithm in YangAccess.

Change-Id: Iff7eb70e64525111dab71fee2fe4e5e08c80dfc0
diff --git a/src/libyang_utils.cpp b/src/libyang_utils.cpp
index b4d584f..aa2b3f0 100644
--- a/src/libyang_utils.cpp
+++ b/src/libyang_utils.cpp
@@ -1,3 +1,4 @@
+#include <boost/algorithm/string/predicate.hpp>
 #include <cmath>
 #include "datastore_access.hpp"
 #include "libyang_utils.hpp"
@@ -45,10 +46,8 @@
     }
 }
 
-// This is very similar to the fillMap lambda in SysrepoAccess, however,
-// Sysrepo returns a weird array-like structure, while libnetconf
-// returns libyang::Data_Node
-void lyNodesToTree(DatastoreAccess::Tree& res, const std::vector<std::shared_ptr<libyang::Data_Node>> items, std::optional<std::string> ignoredXPathPrefix)
+namespace {
+void impl_lyNodesToTree(DatastoreAccess::Tree& res, const std::vector<std::shared_ptr<libyang::Data_Node>> items, std::optional<std::string> ignoredXPathPrefix)
 {
     auto stripXPathPrefix = [&ignoredXPathPrefix] (auto path) {
         return ignoredXPathPrefix ? path.substr(ignoredXPathPrefix->size()) : path;
@@ -73,3 +72,23 @@
         }
     }
 }
+}
+
+// This is very similar to the fillMap lambda in SysrepoAccess, however,
+// Sysrepo returns a weird array-like structure, while libnetconf
+// returns libyang::Data_Node
+void lyNodesToTree(DatastoreAccess::Tree& res, const std::vector<std::shared_ptr<libyang::Data_Node>> items, std::optional<std::string> ignoredXPathPrefix)
+{
+    for (auto it = items.begin(); it < items.end(); it++) {
+        if ((*it)->schema()->nodetype() == LYS_LEAFLIST) {
+            auto leafListPath = stripLeafListValueFromPath((*it)->path());
+            res.emplace_back(leafListPath, special_{SpecialValue::LeafList});
+            while (it != items.end() && boost::starts_with((*it)->path(), leafListPath)) {
+                impl_lyNodesToTree(res, (*it)->tree_dfs(), ignoredXPathPrefix);
+                it++;
+            }
+        } else {
+            impl_lyNodesToTree(res, (*it)->tree_dfs(), ignoredXPathPrefix);
+        }
+    }
+}