Add Schema::leaftype(const std::string&)

Will be used in the CLI.

Change-Id: I31ddc63e92bfad4c37b5b2b65ae72c33e939af2f
diff --git a/src/schema.hpp b/src/schema.hpp
index e4636cf..ef90542 100644
--- a/src/schema.hpp
+++ b/src/schema.hpp
@@ -75,6 +75,7 @@
     virtual bool listHasKey(const schemaPath_& location, const ModuleNodePair& node, const std::string& key) const = 0;
     virtual const std::set<std::string> listKeys(const schemaPath_& location, const ModuleNodePair& node) const = 0;
     virtual yang::LeafDataTypes leafType(const schemaPath_& location, const ModuleNodePair& node) const = 0;
+    virtual yang::LeafDataTypes leafType(const std::string& path) const = 0;
     virtual yang::LeafDataTypes leafrefBase(const schemaPath_& location, const ModuleNodePair& node) const = 0;
     virtual std::optional<std::string> description(const std::string& location) const = 0;
     virtual std::optional<std::string> units(const std::string& location) const = 0;
diff --git a/src/static_schema.cpp b/src/static_schema.cpp
index 9b4f00b..43a8cdc 100644
--- a/src/static_schema.cpp
+++ b/src/static_schema.cpp
@@ -204,6 +204,11 @@
     return boost::get<yang::leaf>(children(locationString).at(fullNodeName(location, node))).m_type;
 }
 
+yang::LeafDataTypes StaticSchema::leafType([[maybe_unused]] const std::string& path) const
+{
+    throw std::runtime_error{"StaticSchema::leafType not implemented"};
+}
+
 const std::set<std::string> StaticSchema::enumValues(const schemaPath_& location, const ModuleNodePair& node) const
 {
     std::string locationString = pathToSchemaString(location, Prefixes::Always);
diff --git a/src/static_schema.hpp b/src/static_schema.hpp
index a63f92c..aae1c90 100644
--- a/src/static_schema.hpp
+++ b/src/static_schema.hpp
@@ -56,6 +56,7 @@
     bool listHasKey(const schemaPath_& location, const ModuleNodePair& node, const std::string& key) const override;
     const std::set<std::string> listKeys(const schemaPath_& location, const ModuleNodePair& node) const override;
     yang::LeafDataTypes leafType(const schemaPath_& location, const ModuleNodePair& node) const override;
+    yang::LeafDataTypes leafType(const std::string& path) const override;
     yang::LeafDataTypes leafrefBase(const schemaPath_& location, const ModuleNodePair& node) const override;
     const std::set<std::string> enumValues(const schemaPath_& location, const ModuleNodePair& node) const override;
     const std::set<std::string> validIdentities(const schemaPath_& location, const ModuleNodePair& node, const Prefixes prefixes) const override;
diff --git a/src/yang_schema.cpp b/src/yang_schema.cpp
index d915eef..06749ba 100644
--- a/src/yang_schema.cpp
+++ b/src/yang_schema.cpp
@@ -257,20 +257,29 @@
     }
 }
 
-yang::LeafDataTypes YangSchema::leafType(const schemaPath_& location, const ModuleNodePair& node) const
+namespace {
+yang::LeafDataTypes impl_leafType(const libyang::S_Schema_Node& node)
 {
     using namespace std::string_literals;
-    if (!isLeaf(location, node))
-        throw InvalidSchemaQueryException(fullNodeName(location, node) + " is not a leaf");
-
-    libyang::Schema_Node_Leaf leaf(getSchemaNode(location, node));
+    libyang::Schema_Node_Leaf leaf(node);
     auto baseType{leaf.type()->base()};
     try {
         return lyTypeToLeafDataTypes(baseType);
     } catch (std::logic_error& ex) {
-        throw UnsupportedYangTypeException("the type of "s + fullNodeName(location, node) + " is not supported: " + ex.what());
+        throw UnsupportedYangTypeException("the type of "s + node->name() + " is not supported: " + ex.what());
     }
 }
+}
+
+yang::LeafDataTypes YangSchema::leafType(const schemaPath_& location, const ModuleNodePair& node) const
+{
+    return impl_leafType(getSchemaNode(location, node));
+}
+
+yang::LeafDataTypes YangSchema::leafType(const std::string& path) const
+{
+    return impl_leafType(getSchemaNode(path));
+}
 
 yang::LeafDataTypes YangSchema::leafrefBase(const schemaPath_& location, const ModuleNodePair& node) const
 {
diff --git a/src/yang_schema.hpp b/src/yang_schema.hpp
index 8471bea..8a8826c 100644
--- a/src/yang_schema.hpp
+++ b/src/yang_schema.hpp
@@ -38,6 +38,7 @@
     bool listHasKey(const schemaPath_& location, const ModuleNodePair& node, const std::string& key) const override;
     const std::set<std::string> listKeys(const schemaPath_& location, const ModuleNodePair& node) const override;
     yang::LeafDataTypes leafType(const schemaPath_& location, const ModuleNodePair& node) const override;
+    yang::LeafDataTypes leafType(const std::string& path) const override;
     yang::LeafDataTypes leafrefBase(const schemaPath_& location, const ModuleNodePair& node) const override;
     const std::set<std::string> validIdentities(const schemaPath_& location, const ModuleNodePair& node, const Prefixes prefixes) const override;
     const std::set<std::string> enumValues(const schemaPath_& location, const ModuleNodePair& node) const override;