Rename fillMap and move it for reuse

The tree is still being filled, but it's no longer a map, so let's
change the name. Also, I want to use this function in the upcoming
in-memory data tree.

Change-Id: I472c0b323d6097fbf23305085df8f905aa22cac5
diff --git a/src/libyang_utils.cpp b/src/libyang_utils.cpp
index 2669151..15b1b06 100644
--- a/src/libyang_utils.cpp
+++ b/src/libyang_utils.cpp
@@ -1,5 +1,7 @@
-#include "libyang_utils.hpp"
 #include <cmath>
+#include "datastore_access.hpp"
+#include "libyang_utils.hpp"
+#include "utils.hpp"
 
 leaf_data_ leafValueFromValue(const libyang::S_Value& value, LY_DATA_TYPE type)
 {
@@ -42,3 +44,34 @@
         return "(can't print)"s;
     }
 }
+
+// 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)
+{
+    auto stripXPathPrefix = [&ignoredXPathPrefix] (auto path) {
+        return ignoredXPathPrefix ? path.substr(ignoredXPathPrefix->size()) : path;
+    };
+
+    for (const auto& it : items) {
+        if (!it)
+            continue;
+        if (it->schema()->nodetype() == LYS_CONTAINER) {
+            if (libyang::Schema_Node_Container{it->schema()}.presence()) {
+                // The fact that the container is included in the data tree
+                // means that it is present and I don't need to check any
+                // value.
+                res.emplace_back(stripXPathPrefix(it->path()), special_{SpecialValue::PresenceContainer});
+            }
+        }
+        if (it->schema()->nodetype() == LYS_LIST) {
+            res.push_back({stripXPathPrefix(it->path()), special_{SpecialValue::List}});
+        }
+        if (it->schema()->nodetype() == LYS_LEAF || it->schema()->nodetype() == LYS_LEAFLIST) {
+            libyang::Data_Node_Leaf_List leaf(it);
+            auto value = leafValueFromValue(leaf.value(), leaf.leaf_type()->base());
+            res.emplace_back(stripXPathPrefix(it->path()), value);
+        }
+    }
+}