Václav Kubernát | 94938b7 | 2018-05-04 15:12:24 +0200 | [diff] [blame] | 1 | /* |
| 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át | 2984f44 | 2020-02-20 17:43:35 +0100 | [diff] [blame] | 8 | #include <experimental/iterator> |
Václav Kubernát | 509ce65 | 2019-05-29 19:46:44 +0200 | [diff] [blame] | 9 | #include <sstream> |
Václav Kubernát | cb3af40 | 2020-02-12 16:49:17 +0100 | [diff] [blame] | 10 | #include "completion.hpp" |
Václav Kubernát | 94938b7 | 2018-05-04 15:12:24 +0200 | [diff] [blame] | 11 | #include "utils.hpp" |
| 12 | |
| 13 | std::string joinPaths(const std::string& prefix, const std::string& suffix) |
| 14 | { |
Václav Kubernát | a44bdf2 | 2020-01-24 12:15:31 +0100 | [diff] [blame] | 15 | // These two if statements are essential for the algorithm: |
| 16 | // The first one solves joining nothing and a relative path - the algorithm |
| 17 | // down below adds a leading slash, turning it into an absolute path. |
| 18 | // The second one would always add a trailing slash to the path. |
| 19 | if (prefix.empty()) { |
| 20 | return suffix; |
| 21 | } |
| 22 | |
| 23 | if (suffix.empty()) { |
| 24 | return prefix; |
| 25 | } |
| 26 | |
| 27 | // Otherwise, strip slashes where the join is going to happen. This will |
| 28 | // also change "/" to "", but the return statement takes care of that and |
| 29 | // inserts the slash again. |
| 30 | auto prefixWithoutTrailingSlash = !prefix.empty() && prefix.back() == '/' ? prefix.substr(0, prefix.length() - 1) : prefix; |
| 31 | auto suffixWithoutLeadingSlash = !suffix.empty() && suffix.front() == '/' ? suffix.substr(1) : suffix; |
| 32 | |
| 33 | // And join the result with a slash. |
| 34 | return prefixWithoutTrailingSlash + '/' + suffixWithoutLeadingSlash; |
Václav Kubernát | 94938b7 | 2018-05-04 15:12:24 +0200 | [diff] [blame] | 35 | } |
Václav Kubernát | 60d6f29 | 2018-05-25 09:45:32 +0200 | [diff] [blame] | 36 | |
| 37 | std::string stripLastNodeFromPath(const std::string& path) |
| 38 | { |
| 39 | std::string res = path; |
| 40 | auto pos = res.find_last_of('/'); |
Václav Kubernát | efcac93 | 2020-01-10 15:26:32 +0100 | [diff] [blame] | 41 | if (pos == res.npos) { // path has no backslash - it's either empty, or is a relative path with one fragment |
Václav Kubernát | 60d6f29 | 2018-05-25 09:45:32 +0200 | [diff] [blame] | 42 | res.clear(); |
Václav Kubernát | efcac93 | 2020-01-10 15:26:32 +0100 | [diff] [blame] | 43 | } else if (pos == 0) { // path has one backslash at the start - it's either "/" or "/one-path-fragment" |
| 44 | return "/"; |
| 45 | } else { |
Václav Kubernát | 60d6f29 | 2018-05-25 09:45:32 +0200 | [diff] [blame] | 46 | res.erase(pos); |
Václav Kubernát | efcac93 | 2020-01-10 15:26:32 +0100 | [diff] [blame] | 47 | } |
Václav Kubernát | 60d6f29 | 2018-05-25 09:45:32 +0200 | [diff] [blame] | 48 | return res; |
| 49 | } |
Václav Kubernát | ebca255 | 2018-06-08 19:06:02 +0200 | [diff] [blame] | 50 | |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 51 | schemaPath_ pathWithoutLastNode(const schemaPath_& path) |
Václav Kubernát | ebca255 | 2018-06-08 19:06:02 +0200 | [diff] [blame] | 52 | { |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 53 | return schemaPath_{path.m_scope, decltype(schemaPath_::m_nodes)(path.m_nodes.begin(), path.m_nodes.end() - 1)}; |
Václav Kubernát | ebca255 | 2018-06-08 19:06:02 +0200 | [diff] [blame] | 54 | } |
Václav Kubernát | 0b0272f | 2018-06-13 14:13:08 +0200 | [diff] [blame] | 55 | |
Jan Kundrát | 6ebcc6c | 2020-05-07 01:58:51 +0200 | [diff] [blame] | 56 | ModuleNodePair splitModuleNode(const std::string& input) |
| 57 | { |
| 58 | auto colonLocation = input.find_first_of(':'); |
| 59 | if (colonLocation != std::string::npos) { |
| 60 | return ModuleNodePair{input.substr(0, colonLocation), input.substr(colonLocation + 1)}; |
| 61 | } |
| 62 | throw std::logic_error("Internal error: got module-unqualified node name"); |
| 63 | } |
| 64 | |
Václav Kubernát | 3a99f00 | 2020-03-31 02:27:41 +0200 | [diff] [blame] | 65 | struct impl_leafDataTypeToString { |
| 66 | std::string operator()(const yang::String) |
| 67 | { |
Václav Kubernát | 0b0272f | 2018-06-13 14:13:08 +0200 | [diff] [blame] | 68 | return "a string"; |
Václav Kubernát | 0b0272f | 2018-06-13 14:13:08 +0200 | [diff] [blame] | 69 | } |
Václav Kubernát | 3a99f00 | 2020-03-31 02:27:41 +0200 | [diff] [blame] | 70 | std::string operator()(const yang::Decimal) |
| 71 | { |
| 72 | return "a decimal"; |
| 73 | } |
| 74 | std::string operator()(const yang::Bool) |
| 75 | { |
| 76 | return "a boolean"; |
| 77 | } |
| 78 | std::string operator()(const yang::Int8) |
| 79 | { |
| 80 | return "an 8-bit integer"; |
| 81 | } |
| 82 | std::string operator()(const yang::Uint8) |
| 83 | { |
| 84 | return "an 8-bit unsigned integer"; |
| 85 | } |
| 86 | std::string operator()(const yang::Int16) |
| 87 | { |
| 88 | return "a 16-bit integer"; |
| 89 | } |
| 90 | std::string operator()(const yang::Uint16) |
| 91 | { |
| 92 | return "a 16-bit unsigned integer"; |
| 93 | } |
| 94 | std::string operator()(const yang::Int32) |
| 95 | { |
| 96 | return "a 32-bit integer"; |
| 97 | } |
| 98 | std::string operator()(const yang::Uint32) |
| 99 | { |
| 100 | return "a 32-bit unsigned integer"; |
| 101 | } |
| 102 | std::string operator()(const yang::Int64) |
| 103 | { |
| 104 | return "a 64-bit integer"; |
| 105 | } |
| 106 | std::string operator()(const yang::Uint64) |
| 107 | { |
| 108 | return "a 64-bit unsigned integer"; |
| 109 | } |
| 110 | std::string operator()(const yang::Binary) |
| 111 | { |
| 112 | return "a base64-encoded binary value"; |
| 113 | } |
| 114 | std::string operator()(const yang::Enum&) |
| 115 | { |
| 116 | return "an enum"; |
| 117 | } |
| 118 | std::string operator()(const yang::IdentityRef&) |
| 119 | { |
| 120 | return "an identity"; |
| 121 | } |
| 122 | std::string operator()(const yang::LeafRef&) |
| 123 | { |
| 124 | return "a leafref"; |
| 125 | } |
Jan Kundrát | 379bb57 | 2020-05-07 03:23:13 +0200 | [diff] [blame] | 126 | std::string operator()(const yang::Empty&) |
| 127 | { |
| 128 | return "an empty leaf"; |
| 129 | } |
Václav Kubernát | 2984f44 | 2020-02-20 17:43:35 +0100 | [diff] [blame] | 130 | std::string operator()(const yang::Union& type) |
| 131 | { |
| 132 | std::ostringstream ss; |
| 133 | std::transform(type.m_unionTypes.begin(), type.m_unionTypes.end(), std::experimental::make_ostream_joiner(ss, ", "), [this](const auto& unionType) { |
Václav Kubernát | 13b23d7 | 2020-04-16 21:49:51 +0200 | [diff] [blame] | 134 | return std::visit(*this, unionType.m_type); |
Václav Kubernát | 2984f44 | 2020-02-20 17:43:35 +0100 | [diff] [blame] | 135 | }); |
| 136 | return ss.str(); |
| 137 | } |
Václav Kubernát | 3a99f00 | 2020-03-31 02:27:41 +0200 | [diff] [blame] | 138 | }; |
| 139 | |
| 140 | std::string leafDataTypeToString(const yang::LeafDataType& type) |
| 141 | { |
| 142 | return std::visit(impl_leafDataTypeToString{}, type); |
Václav Kubernát | 0b0272f | 2018-06-13 14:13:08 +0200 | [diff] [blame] | 143 | } |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 144 | |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 145 | std::string fullNodeName(const schemaPath_& location, const ModuleNodePair& pair) |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 146 | { |
| 147 | if (!pair.first) { |
| 148 | return location.m_nodes.at(0).m_prefix.value().m_name + ":" + pair.second; |
| 149 | } else { |
| 150 | return pair.first.value() + ":" + pair.second; |
| 151 | } |
| 152 | } |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 153 | |
| 154 | std::string fullNodeName(const dataPath_& location, const ModuleNodePair& pair) |
| 155 | { |
| 156 | return fullNodeName(dataPathToSchemaPath(location), pair); |
| 157 | } |
Václav Kubernát | 4108e0d | 2018-10-29 13:32:22 +0100 | [diff] [blame] | 158 | |
Václav Kubernát | 9b72599 | 2019-05-29 16:39:47 +0200 | [diff] [blame] | 159 | struct leafDataToStringVisitor : boost::static_visitor<std::string> { |
| 160 | std::string operator()(const enum_& data) const |
| 161 | { |
| 162 | return data.m_value; |
| 163 | } |
| 164 | |
| 165 | std::string operator()(const binary_& data) const |
| 166 | { |
| 167 | return data.m_value; |
| 168 | } |
| 169 | |
Jan Kundrát | 379bb57 | 2020-05-07 03:23:13 +0200 | [diff] [blame] | 170 | std::string operator()(const empty_) const |
| 171 | { |
| 172 | return "[empty]"; |
| 173 | } |
| 174 | |
Václav Kubernát | 9b72599 | 2019-05-29 16:39:47 +0200 | [diff] [blame] | 175 | std::string operator()(const identityRef_& data) const |
| 176 | { |
Jan Kundrát | 0d8abd1 | 2020-05-07 02:00:14 +0200 | [diff] [blame] | 177 | return data.m_prefix ? (data.m_prefix.value().m_name + ":" + data.m_value) : data.m_value; |
Václav Kubernát | 9b72599 | 2019-05-29 16:39:47 +0200 | [diff] [blame] | 178 | } |
| 179 | |
Václav Kubernát | 144729d | 2020-01-08 15:20:35 +0100 | [diff] [blame] | 180 | std::string operator()(const special_& data) const |
| 181 | { |
| 182 | return specialValueToString(data); |
| 183 | } |
| 184 | |
Jan Kundrát | c43acf7 | 2019-07-02 19:28:22 +0200 | [diff] [blame] | 185 | std::string operator()(const std::string& data) const |
| 186 | { |
| 187 | return data; |
| 188 | } |
| 189 | |
Václav Kubernát | 8e121ff | 2019-10-15 15:47:45 +0200 | [diff] [blame] | 190 | std::string operator()(const bool& data) const |
| 191 | { |
Václav Kubernát | 3a43323 | 2020-07-08 17:52:50 +0200 | [diff] [blame] | 192 | if (data) { |
Václav Kubernát | 8e121ff | 2019-10-15 15:47:45 +0200 | [diff] [blame] | 193 | return "true"; |
Václav Kubernát | 3a43323 | 2020-07-08 17:52:50 +0200 | [diff] [blame] | 194 | } else { |
Václav Kubernát | 8e121ff | 2019-10-15 15:47:45 +0200 | [diff] [blame] | 195 | return "false"; |
Václav Kubernát | 3a43323 | 2020-07-08 17:52:50 +0200 | [diff] [blame] | 196 | } |
Václav Kubernát | 8e121ff | 2019-10-15 15:47:45 +0200 | [diff] [blame] | 197 | } |
| 198 | |
Václav Kubernát | 19097f3 | 2020-10-05 10:08:29 +0200 | [diff] [blame] | 199 | std::string operator()(const bits_& data) const |
| 200 | { |
| 201 | std::stringstream ss; |
| 202 | std::copy(data.m_bits.begin(), data.m_bits.end(), std::experimental::make_ostream_joiner(ss, " ")); |
| 203 | return ss.str(); |
| 204 | } |
| 205 | |
Václav Kubernát | 9b72599 | 2019-05-29 16:39:47 +0200 | [diff] [blame] | 206 | template <typename T> |
| 207 | std::string operator()(const T& data) const |
| 208 | { |
Jan Kundrát | c43acf7 | 2019-07-02 19:28:22 +0200 | [diff] [blame] | 209 | return std::to_string(data); |
Václav Kubernát | 9b72599 | 2019-05-29 16:39:47 +0200 | [diff] [blame] | 210 | } |
| 211 | }; |
| 212 | |
| 213 | std::string leafDataToString(const leaf_data_ value) |
| 214 | { |
| 215 | return boost::apply_visitor(leafDataToStringVisitor(), value); |
| 216 | } |
Václav Kubernát | 3a823f4 | 2020-04-29 23:40:21 +0200 | [diff] [blame] | 217 | |
| 218 | struct getSchemaPathVisitor : boost::static_visitor<schemaPath_> { |
| 219 | schemaPath_ operator()(const dataPath_& path) const |
| 220 | { |
| 221 | return dataPathToSchemaPath(path); |
| 222 | } |
| 223 | |
| 224 | schemaPath_ operator()(const schemaPath_& path) const |
| 225 | { |
| 226 | return path; |
| 227 | } |
| 228 | |
| 229 | [[noreturn]] schemaPath_ operator()([[maybe_unused]] const module_& path) const |
| 230 | { |
| 231 | throw std::logic_error("getSchemaPathVisitor: Tried getting a schema path from a module"); |
| 232 | } |
| 233 | }; |
| 234 | |
| 235 | schemaPath_ anyPathToSchemaPath(const boost::variant<dataPath_, schemaPath_, module_>& path) |
| 236 | { |
| 237 | return boost::apply_visitor(getSchemaPathVisitor(), path); |
| 238 | } |
Václav Kubernát | 5b8a8f3 | 2020-05-20 00:57:22 +0200 | [diff] [blame] | 239 | |
| 240 | std::string stripLeafListValueFromPath(const std::string& path) |
| 241 | { |
| 242 | auto res = path; |
| 243 | res.erase(res.find_last_of('[')); |
| 244 | return res; |
| 245 | } |
Václav Kubernát | bf65dd7 | 2020-05-28 02:32:31 +0200 | [diff] [blame] | 246 | |
| 247 | std::string stripLastListInstanceFromPath(const std::string& path) |
| 248 | { |
| 249 | auto res = path; |
| 250 | res.erase(res.find_first_of('[', res.find_last_of('/'))); |
| 251 | return res; |
| 252 | } |
| 253 | |
Václav Kubernát | 2c4778b | 2020-06-18 11:55:20 +0200 | [diff] [blame] | 254 | std::string instanceToString(const ListInstance& instance, const std::optional<std::string>& modName) |
Václav Kubernát | bf65dd7 | 2020-05-28 02:32:31 +0200 | [diff] [blame] | 255 | { |
| 256 | std::string instanceStr; |
Václav Kubernát | 2c4778b | 2020-06-18 11:55:20 +0200 | [diff] [blame] | 257 | auto modulePrefix = modName ? *modName + ":" : ""; |
Václav Kubernát | bf65dd7 | 2020-05-28 02:32:31 +0200 | [diff] [blame] | 258 | for (const auto& [key, value] : instance) { |
| 259 | using namespace std::string_literals; |
Václav Kubernát | 2c4778b | 2020-06-18 11:55:20 +0200 | [diff] [blame] | 260 | instanceStr += "[" + modulePrefix + key + "=" + escapeListKeyString(leafDataToString(value)) + "]"; |
Václav Kubernát | bf65dd7 | 2020-05-28 02:32:31 +0200 | [diff] [blame] | 261 | } |
| 262 | return instanceStr; |
| 263 | } |