Fix handling of choice statements in YangSchema
Now that I'm using `libyang::Context::get_node`, the code got a little
less complicated, since it always returns a Schema_Node instead of
a Set.
Change-Id: Iab3734ed4cb5957cb635f268b882963c95d36206
Issue: https://tree.taiga.io/project/jktjkt-netconf-cli/issue/119
diff --git a/src/yang_schema.cpp b/src/yang_schema.cpp
index c9e3d3c..2619943 100644
--- a/src/yang_schema.cpp
+++ b/src/yang_schema.cpp
@@ -188,7 +188,7 @@
return keys.find(key) != keys.end();
}
-libyang::S_Set YangSchema::getNodeSet(const schemaPath_& location, const ModuleNodePair& node) const
+libyang::S_Schema_Node YangSchema::getSchemaNode(const schemaPath_& location, const ModuleNodePair& node) const
{
std::string absPath = location.m_nodes.empty() ? "" : "/";
absPath += pathToAbsoluteSchemaString(location) + "/" + fullNodeName(location, node);
@@ -205,21 +205,10 @@
[&oldOptions]() {
libyang::set_log_options(oldOptions);
});
- return m_context->find_path(absPath.c_str());
+ return m_context->get_node(nullptr, absPath.c_str());
}
}
-libyang::S_Schema_Node YangSchema::getSchemaNode(const schemaPath_& location, const ModuleNodePair& node) const
-{
- const auto set = getNodeSet(location, node);
- if (!set)
- return nullptr;
- const auto& schemaSet = set->schema();
- if (set->number() != 1)
- return nullptr;
- return *schemaSet.begin();
-}
-
const std::set<std::string> YangSchema::listKeys(const schemaPath_& location, const ModuleNodePair& node) const
{
std::set<std::string> keys;
diff --git a/src/yang_schema.hpp b/src/yang_schema.hpp
index e4de5de..12a250c 100644
--- a/src/yang_schema.hpp
+++ b/src/yang_schema.hpp
@@ -16,7 +16,6 @@
namespace libyang {
class Context;
-class Set;
class Schema_Node;
class Data_Node;
class Module;
@@ -68,7 +67,6 @@
std::set<std::string> modules() const;
/** @short Returns a set of nodes, that match the location and name criteria. */
- std::shared_ptr<libyang::Set> getNodeSet(const schemaPath_& location, const ModuleNodePair& node) const;
/** @short Returns a single Schema_Node if the criteria matches only one, otherwise nullptr. */
std::shared_ptr<libyang::Schema_Node> getSchemaNode(const schemaPath_& location, const ModuleNodePair& node) const;
diff --git a/tests/yang.cpp b/tests/yang.cpp
index 9c44008..156d94b 100644
--- a/tests/yang.cpp
+++ b/tests/yang.cpp
@@ -230,6 +230,25 @@
}
uses flags;
+
+ choice interface {
+ case caseLoopback {
+ container loopback {
+ leaf ip {
+ type string;
+ }
+ }
+ }
+
+ case caseEthernet {
+ container ethernet {
+ leaf ip {
+ type string;
+ }
+ }
+ }
+ }
+
})";
TEST_CASE("yangschema")
@@ -261,6 +280,18 @@
node.second = "a2";
}
+ SECTION("example-schema:ethernet")
+ {
+ node.first = "example-schema";
+ node.second = "ethernet";
+ }
+
+ SECTION("example-schema:loopback")
+ {
+ node.first = "example-schema";
+ node.second = "loopback";
+ }
+
REQUIRE(ys.isContainer(path, node));
}
SECTION("isLeaf")
@@ -649,7 +680,8 @@
"example-schema:foodIdentLeaf", "example-schema:pizzaIdentLeaf", "example-schema:foodDrinkIdentLeaf",
"example-schema:_list", "example-schema:twoKeyList", "second-schema:bla",
"example-schema:carry", "example-schema:zero", "example-schema:direction",
- "example-schema:interrupt"};
+ "example-schema:interrupt",
+ "example-schema:ethernet", "example-schema:loopback"};
}
SECTION("example-schema:a")
@@ -786,5 +818,39 @@
REQUIRE(!ys.isLeaf(path, node));
REQUIRE(!ys.isContainer(path, node));
}
+
+ SECTION("choice is not a node")
+ {
+ SECTION("example-schema:interface")
+ {
+ node.first = "example-schema";
+ node.second = "interface";
+ }
+
+ REQUIRE(!ys.isPresenceContainer(path, node));
+ REQUIRE(!ys.isList(path, node));
+ REQUIRE(!ys.isLeaf(path, node));
+ REQUIRE(!ys.isContainer(path, node));
+ }
+
+ SECTION("case is not a node")
+ {
+ SECTION("example-schema:caseLoopback")
+ {
+ node.first = "example-schema";
+ node.second = "caseLoopback";
+ }
+
+ SECTION("example-schema:caseEthernet")
+ {
+ node.first = "example-schema";
+ node.second = "caseEthernet";
+ }
+
+ REQUIRE(!ys.isPresenceContainer(path, node));
+ REQUIRE(!ys.isList(path, node));
+ REQUIRE(!ys.isLeaf(path, node));
+ REQUIRE(!ys.isContainer(path, node));
+ }
}
}