Václav Kubernát | babbab9 | 2021-01-27 09:25:05 +0100 | [diff] [blame] | 1 | #include <fmt/format.h> |
Václav Kubernát | 7efd6d5 | 2021-11-09 01:31:11 +0100 | [diff] [blame] | 2 | #include <libyang-cpp/DataNode.hpp> |
| 3 | #include <libyang-cpp/Set.hpp> |
Václav Kubernát | cbdc98a | 2021-01-28 13:24:37 +0100 | [diff] [blame] | 4 | #include "utils/libyang.h" |
| 5 | |
Tomáš Pecka | fd5ebd3 | 2021-10-07 09:48:13 +0200 | [diff] [blame] | 6 | namespace velia::utils { |
| 7 | |
Václav Kubernát | 7efd6d5 | 2021-11-09 01:31:11 +0100 | [diff] [blame] | 8 | const char* getValueAsString(const libyang::DataNode& node) |
Václav Kubernát | cbdc98a | 2021-01-28 13:24:37 +0100 | [diff] [blame] | 9 | { |
Václav Kubernát | 7efd6d5 | 2021-11-09 01:31:11 +0100 | [diff] [blame] | 10 | if (node.schema().nodeType() != libyang::NodeType::Leaf) { |
Václav Kubernát | cbdc98a | 2021-01-28 13:24:37 +0100 | [diff] [blame] | 11 | throw std::logic_error("retrieveString: invalid node"); |
| 12 | } |
| 13 | |
Václav Kubernát | 7efd6d5 | 2021-11-09 01:31:11 +0100 | [diff] [blame] | 14 | return node.asTerm().valueStr().data(); |
Václav Kubernát | cbdc98a | 2021-01-28 13:24:37 +0100 | [diff] [blame] | 15 | } |
| 16 | |
Jan Kundrát | b3e9998 | 2022-03-18 17:38:20 +0100 | [diff] [blame] | 17 | std::optional<libyang::DataNode> getUniqueSubtree(const libyang::DataNode& start, const std::string& path) |
Václav Kubernát | cbdc98a | 2021-01-28 13:24:37 +0100 | [diff] [blame] | 18 | { |
Václav Kubernát | 7efd6d5 | 2021-11-09 01:31:11 +0100 | [diff] [blame] | 19 | auto set = start.findXPath(path); |
Václav Kubernát | cbdc98a | 2021-01-28 13:24:37 +0100 | [diff] [blame] | 20 | |
Václav Kubernát | 7efd6d5 | 2021-11-09 01:31:11 +0100 | [diff] [blame] | 21 | switch(set.size()) { |
Tomáš Pecka | fd90efb | 2021-10-07 10:40:44 +0200 | [diff] [blame] | 22 | case 0: |
| 23 | return std::nullopt; |
| 24 | case 1: |
Václav Kubernát | 7efd6d5 | 2021-11-09 01:31:11 +0100 | [diff] [blame] | 25 | return set.front(); |
Tomáš Pecka | fd90efb | 2021-10-07 10:40:44 +0200 | [diff] [blame] | 26 | default: |
Jan Kundrát | b3e9998 | 2022-03-18 17:38:20 +0100 | [diff] [blame] | 27 | throw std::runtime_error(fmt::format("getUniqueSubtree({}, {}): more than one match (got {})", start.path(), path, set.size())); |
Tomáš Pecka | fd90efb | 2021-10-07 10:40:44 +0200 | [diff] [blame] | 28 | } |
Václav Kubernát | cbdc98a | 2021-01-28 13:24:37 +0100 | [diff] [blame] | 29 | } |
Tomáš Pecka | fd5ebd3 | 2021-10-07 09:48:13 +0200 | [diff] [blame] | 30 | } |