blob: cd0858f9de32fb5f4e5925fe4c0e6bb7313b838d [file] [log] [blame]
Václav Kubernát94938b72018-05-04 15:12:24 +02001/*
2 * Copyright (C) 2018 CESNET, https://photonics.cesnet.cz/
3 * Copyright (C) 2018 FIT CVUT, https://fit.cvut.cz/
4 *
5 * Written by Václav Kubernát <kubervac@fit.cvut.cz>
6 *
7*/
Václav Kubernát4108e0d2018-10-29 13:32:22 +01008#include <boost/algorithm/string/predicate.hpp>
Václav Kubernát509ce652019-05-29 19:46:44 +02009#include <sstream>
Václav Kubernát94938b72018-05-04 15:12:24 +020010#include "utils.hpp"
11
12std::string joinPaths(const std::string& prefix, const std::string& suffix)
13{
Václav Kubernát50949e62018-08-30 12:50:08 +020014 if (prefix.empty() || suffix.empty() || prefix == "/")
Václav Kubernát94938b72018-05-04 15:12:24 +020015 return prefix + suffix;
16 else
17 return prefix + '/' + suffix;
18}
Václav Kubernát60d6f292018-05-25 09:45:32 +020019
20std::string stripLastNodeFromPath(const std::string& path)
21{
22 std::string res = path;
23 auto pos = res.find_last_of('/');
Václav Kubernátefcac932020-01-10 15:26:32 +010024 if (pos == res.npos) { // path has no backslash - it's either empty, or is a relative path with one fragment
Václav Kubernát60d6f292018-05-25 09:45:32 +020025 res.clear();
Václav Kubernátefcac932020-01-10 15:26:32 +010026 } else if (pos == 0) { // path has one backslash at the start - it's either "/" or "/one-path-fragment"
27 return "/";
28 } else {
Václav Kubernát60d6f292018-05-25 09:45:32 +020029 res.erase(pos);
Václav Kubernátefcac932020-01-10 15:26:32 +010030 }
Václav Kubernát60d6f292018-05-25 09:45:32 +020031 return res;
32}
Václav Kubernátebca2552018-06-08 19:06:02 +020033
Václav Kubernát2eaceb82018-10-08 19:56:30 +020034schemaPath_ pathWithoutLastNode(const schemaPath_& path)
Václav Kubernátebca2552018-06-08 19:06:02 +020035{
Václav Kubernát2eaceb82018-10-08 19:56:30 +020036 return schemaPath_{path.m_scope, decltype(schemaPath_::m_nodes)(path.m_nodes.begin(), path.m_nodes.end() - 1)};
Václav Kubernátebca2552018-06-08 19:06:02 +020037}
Václav Kubernát0b0272f2018-06-13 14:13:08 +020038
39std::string leafDataTypeToString(yang::LeafDataTypes type)
40{
41 switch (type) {
42 case yang::LeafDataTypes::String:
43 return "a string";
44 case yang::LeafDataTypes::Decimal:
45 return "a decimal";
46 case yang::LeafDataTypes::Bool:
47 return "a boolean";
Ivona Oboňová88c78ca2019-07-02 18:40:07 +020048 case yang::LeafDataTypes::Int8:
49 return "an 8-bit integer";
50 case yang::LeafDataTypes::Uint8:
51 return "an 8-bit unsigned integer";
52 case yang::LeafDataTypes::Int16:
53 return "a 16-bit integer";
54 case yang::LeafDataTypes::Uint16:
55 return "a 16-bit unsigned integer";
56 case yang::LeafDataTypes::Int32:
57 return "a 32-bit integer";
58 case yang::LeafDataTypes::Uint32:
59 return "a 32-bit unsigned integer";
60 case yang::LeafDataTypes::Int64:
61 return "a 64-bit integer";
62 case yang::LeafDataTypes::Uint64:
63 return "a 64-bit unsigned integer";
Václav Kubernát0b0272f2018-06-13 14:13:08 +020064 case yang::LeafDataTypes::Enum:
65 return "an enum";
Václav Kubernáteeb38842019-03-20 19:46:05 +010066 case yang::LeafDataTypes::IdentityRef:
67 return "an identity";
Václav Kubernát6a8d1d92019-04-24 20:30:36 +020068 case yang::LeafDataTypes::LeafRef:
69 return "a leafref";
Václav Kubernát0b0272f2018-06-13 14:13:08 +020070 default:
Václav Kubernáteeb38842019-03-20 19:46:05 +010071 throw std::runtime_error("leafDataTypeToString: unsupported leaf data type");
Václav Kubernát0b0272f2018-06-13 14:13:08 +020072 }
73}
Václav Kubernát744f57f2018-06-29 22:46:26 +020074
Václav Kubernát2eaceb82018-10-08 19:56:30 +020075std::string fullNodeName(const schemaPath_& location, const ModuleNodePair& pair)
Václav Kubernát744f57f2018-06-29 22:46:26 +020076{
77 if (!pair.first) {
78 return location.m_nodes.at(0).m_prefix.value().m_name + ":" + pair.second;
79 } else {
80 return pair.first.value() + ":" + pair.second;
81 }
82}
Václav Kubernát2eaceb82018-10-08 19:56:30 +020083
84std::string fullNodeName(const dataPath_& location, const ModuleNodePair& pair)
85{
86 return fullNodeName(dataPathToSchemaPath(location), pair);
87}
Václav Kubernát4108e0d2018-10-29 13:32:22 +010088
Václav Kubernáta395d332019-02-13 16:49:20 +010089std::set<std::string> filterByPrefix(const std::set<std::string>& set, const std::string_view prefix)
Václav Kubernát4108e0d2018-10-29 13:32:22 +010090{
91 std::set<std::string> filtered;
92 std::copy_if(set.begin(), set.end(),
93 std::inserter(filtered, filtered.end()),
94 [prefix] (auto it) { return boost::starts_with(it, prefix); });
Václav Kubernáta395d332019-02-13 16:49:20 +010095 return filtered;
Václav Kubernát4108e0d2018-10-29 13:32:22 +010096}
Václav Kubernát9b725992019-05-29 16:39:47 +020097
98struct leafDataToStringVisitor : boost::static_visitor<std::string> {
99 std::string operator()(const enum_& data) const
100 {
101 return data.m_value;
102 }
103
104 std::string operator()(const binary_& data) const
105 {
106 return data.m_value;
107 }
108
109 std::string operator()(const identityRef_& data) const
110 {
Václav Kubernátc31bd602019-03-07 11:44:48 +0100111 return data.m_prefix.value().m_name + ":" + data.m_value;
Václav Kubernát9b725992019-05-29 16:39:47 +0200112 }
113
Václav Kubernát144729d2020-01-08 15:20:35 +0100114 std::string operator()(const special_& data) const
115 {
116 return specialValueToString(data);
117 }
118
Jan Kundrátc43acf72019-07-02 19:28:22 +0200119 std::string operator()(const std::string& data) const
120 {
121 return data;
122 }
123
Václav Kubernát8e121ff2019-10-15 15:47:45 +0200124 std::string operator()(const bool& data) const
125 {
126 if (data)
127 return "true";
128 else
129 return "false";
130 }
131
Václav Kubernát9b725992019-05-29 16:39:47 +0200132 template <typename T>
133 std::string operator()(const T& data) const
134 {
Jan Kundrátc43acf72019-07-02 19:28:22 +0200135 return std::to_string(data);
Václav Kubernát9b725992019-05-29 16:39:47 +0200136 }
137};
138
139std::string leafDataToString(const leaf_data_ value)
140{
141 return boost::apply_visitor(leafDataToStringVisitor(), value);
142}