Václav Kubernát | 96344a1 | 2018-05-28 16:33:39 +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 | */ |
| 8 | |
Václav Kubernát | 5b8a8f3 | 2020-05-20 00:57:22 +0200 | [diff] [blame] | 9 | #include <boost/algorithm/string/predicate.hpp> |
Václav Kubernát | 509ce65 | 2019-05-29 19:46:44 +0200 | [diff] [blame] | 10 | #include <boost/mpl/for_each.hpp> |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 11 | #include <iostream> |
Václav Kubernát | 9cfcd87 | 2020-02-18 12:34:02 +0100 | [diff] [blame] | 12 | #include <sstream> |
Václav Kubernát | e066fe2 | 2022-04-06 00:32:26 +0200 | [diff] [blame] | 13 | #include "UniqueResource.hpp" |
Václav Kubernát | 6415b82 | 2018-08-22 17:40:01 +0200 | [diff] [blame] | 14 | #include "datastore_access.hpp" |
Václav Kubernát | 96344a1 | 2018-05-28 16:33:39 +0200 | [diff] [blame] | 15 | #include "interpreter.hpp" |
Václav Kubernát | 509ce65 | 2019-05-29 19:46:44 +0200 | [diff] [blame] | 16 | #include "utils.hpp" |
Václav Kubernát | 96344a1 | 2018-05-28 16:33:39 +0200 | [diff] [blame] | 17 | |
Václav Kubernát | 4c3d22f | 2020-06-12 16:05:01 +0200 | [diff] [blame] | 18 | struct pathToStringVisitor : boost::static_visitor<std::string> { |
| 19 | std::string operator()(const module_& path) const |
| 20 | { |
| 21 | using namespace std::string_literals; |
| 22 | return "/"s + boost::get<module_>(path).m_name + ":*"; |
| 23 | } |
| 24 | std::string operator()(const schemaPath_& path) const |
| 25 | { |
| 26 | return pathToSchemaString(path, Prefixes::WhenNeeded); |
| 27 | } |
| 28 | std::string operator()(const dataPath_& path) const |
| 29 | { |
| 30 | return pathToDataString(path, Prefixes::WhenNeeded); |
| 31 | } |
| 32 | }; |
| 33 | |
Václav Kubernát | e7248b2 | 2020-06-26 15:38:59 +0200 | [diff] [blame] | 34 | namespace { |
| 35 | void printTree(const DatastoreAccess::Tree tree) |
| 36 | { |
| 37 | for (auto it = tree.begin(); it != tree.end(); it++) { |
| 38 | auto [path, value] = *it; |
| 39 | if (value.type() == typeid(special_) && boost::get<special_>(value).m_value == SpecialValue::LeafList) { |
| 40 | auto leafListPrefix = path; |
| 41 | std::cout << path << " = " << leafDataToString(value) << std::endl; |
| 42 | |
| 43 | while (it + 1 != tree.end() && boost::starts_with((it + 1)->first, leafListPrefix)) { |
| 44 | ++it; |
| 45 | std::cout << stripLeafListValueFromPath(it->first) << " = " << leafDataToString(it->second) << std::endl; |
| 46 | } |
| 47 | } else { |
| 48 | std::cout << path << " = " << leafDataToString(value) << std::endl; |
| 49 | } |
| 50 | } |
| 51 | } |
| 52 | } |
| 53 | |
Václav Kubernát | 4c3d22f | 2020-06-12 16:05:01 +0200 | [diff] [blame] | 54 | template <typename PathType> |
| 55 | std::string pathToString(const PathType& path) |
| 56 | { |
| 57 | return boost::apply_visitor(pathToStringVisitor(), path); |
| 58 | } |
| 59 | |
aleskucera@cesnet.cz | 87480ef | 2023-10-27 13:13:59 +0200 | [diff] [blame] | 60 | void Interpreter::checkRpcPath(const dataPath_& commandPath) const |
| 61 | { |
| 62 | if (auto inputPath = m_datastore.inputDatastorePath()) { |
| 63 | dataPath_ newPath = realPath(m_parser.currentPath(), commandPath); |
| 64 | if (!pathToDataString(newPath, Prefixes::WhenNeeded).starts_with(*inputPath)) { |
| 65 | throw std::runtime_error("Can't execute `cd` outside of the `prepare` context."); |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | |
Václav Kubernát | 812ee28 | 2018-08-30 17:10:03 +0200 | [diff] [blame] | 70 | void Interpreter::operator()(const commit_&) const |
| 71 | { |
| 72 | m_datastore.commitChanges(); |
| 73 | } |
| 74 | |
Václav Kubernát | 6d79143 | 2018-10-25 16:00:35 +0200 | [diff] [blame] | 75 | void Interpreter::operator()(const discard_&) const |
| 76 | { |
| 77 | m_datastore.discardChanges(); |
| 78 | } |
| 79 | |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 80 | void Interpreter::operator()(const set_& set) const |
| 81 | { |
Václav Kubernát | eeb3884 | 2019-03-20 19:46:05 +0100 | [diff] [blame] | 82 | auto data = set.m_data; |
| 83 | |
| 84 | // If the user didn't supply a module prefix for identityref, we need to add it ourselves |
| 85 | if (data.type() == typeid(identityRef_)) { |
| 86 | auto identityRef = boost::get<identityRef_>(data); |
| 87 | if (!identityRef.m_prefix) { |
| 88 | identityRef.m_prefix = set.m_path.m_nodes.front().m_prefix.value(); |
| 89 | data = identityRef; |
| 90 | } |
| 91 | } |
Václav Kubernát | 4c3d22f | 2020-06-12 16:05:01 +0200 | [diff] [blame] | 92 | m_datastore.setLeaf(pathToString(toCanonicalPath(set.m_path)), data); |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 93 | } |
| 94 | |
Václav Kubernát | b6ff0b6 | 2018-08-30 16:14:53 +0200 | [diff] [blame] | 95 | void Interpreter::operator()(const get_& get) const |
| 96 | { |
Václav Kubernát | e066fe2 | 2022-04-06 00:32:26 +0200 | [diff] [blame] | 97 | auto targetSwitcher = make_unique_resource([] {}, [this, oldTarget = m_datastore.target()] { |
| 98 | m_datastore.setTarget(oldTarget); |
| 99 | }); |
| 100 | |
| 101 | if (get.m_dsTarget) { |
| 102 | m_datastore.setTarget(*get.m_dsTarget); |
| 103 | } |
| 104 | |
Václav Kubernát | 4c3d22f | 2020-06-12 16:05:01 +0200 | [diff] [blame] | 105 | auto items = m_datastore.getItems(pathToString(toCanonicalPath(get.m_path))); |
Václav Kubernát | e7248b2 | 2020-06-26 15:38:59 +0200 | [diff] [blame] | 106 | printTree(items); |
Václav Kubernát | b6ff0b6 | 2018-08-30 16:14:53 +0200 | [diff] [blame] | 107 | } |
| 108 | |
Václav Kubernát | 96344a1 | 2018-05-28 16:33:39 +0200 | [diff] [blame] | 109 | void Interpreter::operator()(const cd_& cd) const |
| 110 | { |
aleskucera@cesnet.cz | 87480ef | 2023-10-27 13:13:59 +0200 | [diff] [blame] | 111 | checkRpcPath(cd.m_path); |
Václav Kubernát | 96344a1 | 2018-05-28 16:33:39 +0200 | [diff] [blame] | 112 | m_parser.changeNode(cd.m_path); |
| 113 | } |
| 114 | |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 115 | void Interpreter::operator()(const create_& create) const |
| 116 | { |
Jan Kundrát | cbf288b | 2020-06-18 20:44:39 +0200 | [diff] [blame] | 117 | m_datastore.createItem(pathToString(toCanonicalPath(create.m_path))); |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | void Interpreter::operator()(const delete_& delet) const |
| 121 | { |
Jan Kundrát | cbf288b | 2020-06-18 20:44:39 +0200 | [diff] [blame] | 122 | m_datastore.deleteItem(pathToString(toCanonicalPath(delet.m_path))); |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 123 | } |
| 124 | |
Václav Kubernát | 11afac7 | 2018-07-18 14:59:53 +0200 | [diff] [blame] | 125 | void Interpreter::operator()(const ls_& ls) const |
| 126 | { |
| 127 | std::cout << "Possible nodes:" << std::endl; |
Václav Kubernát | e7d4aea | 2018-09-11 18:15:48 +0200 | [diff] [blame] | 128 | auto recursion{Recursion::NonRecursive}; |
| 129 | for (auto it : ls.m_options) { |
Václav Kubernát | 3a43323 | 2020-07-08 17:52:50 +0200 | [diff] [blame] | 130 | if (it == LsOption::Recursive) { |
Václav Kubernát | e7d4aea | 2018-09-11 18:15:48 +0200 | [diff] [blame] | 131 | recursion = Recursion::Recursive; |
Václav Kubernát | 3a43323 | 2020-07-08 17:52:50 +0200 | [diff] [blame] | 132 | } |
Václav Kubernát | e7d4aea | 2018-09-11 18:15:48 +0200 | [diff] [blame] | 133 | } |
Václav Kubernát | 11afac7 | 2018-07-18 14:59:53 +0200 | [diff] [blame] | 134 | |
Václav Kubernát | 4c3d22f | 2020-06-12 16:05:01 +0200 | [diff] [blame] | 135 | auto toPrint = m_datastore.schema()->availableNodes(toCanonicalPath(ls.m_path), recursion); |
Václav Kubernát | 8208687 | 2020-04-29 01:09:50 +0200 | [diff] [blame] | 136 | |
Václav Kubernát | 95b0887 | 2020-04-28 01:04:17 +0200 | [diff] [blame] | 137 | for (const auto& it : toPrint) { |
Václav Kubernát | b4e5b18 | 2020-11-16 19:55:09 +0100 | [diff] [blame] | 138 | std::cout << (it.first ? *it.first + ":" : "") + it.second << std::endl; |
Václav Kubernát | 95b0887 | 2020-04-28 01:04:17 +0200 | [diff] [blame] | 139 | } |
Václav Kubernát | 11afac7 | 2018-07-18 14:59:53 +0200 | [diff] [blame] | 140 | } |
| 141 | |
Václav Kubernát | 7160a13 | 2020-04-03 02:11:01 +0200 | [diff] [blame] | 142 | void Interpreter::operator()(const copy_& copy) const |
| 143 | { |
| 144 | m_datastore.copyConfig(copy.m_source, copy.m_destination); |
| 145 | } |
| 146 | |
Václav Kubernát | 9cfcd87 | 2020-02-18 12:34:02 +0100 | [diff] [blame] | 147 | std::string Interpreter::buildTypeInfo(const std::string& path) const |
| 148 | { |
| 149 | std::ostringstream ss; |
Václav Kubernát | 1ae24f4 | 2020-12-01 02:32:04 +0100 | [diff] [blame] | 150 | std::string typeDescription; |
Václav Kubernát | 9cfcd87 | 2020-02-18 12:34:02 +0100 | [diff] [blame] | 151 | switch (m_datastore.schema()->nodeType(path)) { |
| 152 | case yang::NodeTypes::Container: |
| 153 | ss << "container"; |
| 154 | break; |
| 155 | case yang::NodeTypes::PresenceContainer: |
| 156 | ss << "presence container"; |
| 157 | break; |
Václav Kubernát | b4e5b18 | 2020-11-16 19:55:09 +0100 | [diff] [blame] | 158 | case yang::NodeTypes::Leaf: { |
Václav Kubernát | 9cfcd87 | 2020-02-18 12:34:02 +0100 | [diff] [blame] | 159 | auto leafType = m_datastore.schema()->leafType(path); |
| 160 | auto typedefName = m_datastore.schema()->leafTypeName(path); |
| 161 | std::string baseTypeStr; |
Václav Kubernát | 13b23d7 | 2020-04-16 21:49:51 +0200 | [diff] [blame] | 162 | if (std::holds_alternative<yang::LeafRef>(leafType.m_type)) { |
Václav Kubernát | 9cfcd87 | 2020-02-18 12:34:02 +0100 | [diff] [blame] | 163 | ss << "-> "; |
| 164 | ss << m_datastore.schema()->leafrefPath(path) << " "; |
Václav Kubernát | 13b23d7 | 2020-04-16 21:49:51 +0200 | [diff] [blame] | 165 | baseTypeStr = leafDataTypeToString(std::get<yang::LeafRef>(leafType.m_type).m_targetType->m_type); |
Václav Kubernát | 9cfcd87 | 2020-02-18 12:34:02 +0100 | [diff] [blame] | 166 | } else { |
Václav Kubernát | 13b23d7 | 2020-04-16 21:49:51 +0200 | [diff] [blame] | 167 | baseTypeStr = leafDataTypeToString(leafType.m_type); |
Václav Kubernát | 9cfcd87 | 2020-02-18 12:34:02 +0100 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | if (typedefName) { |
| 171 | ss << *typedefName << " (" << baseTypeStr << ")"; |
| 172 | } else { |
| 173 | ss << baseTypeStr; |
| 174 | } |
| 175 | |
Václav Kubernát | 13b23d7 | 2020-04-16 21:49:51 +0200 | [diff] [blame] | 176 | if (leafType.m_units) { |
| 177 | ss << " [" + *leafType.m_units + "]"; |
Václav Kubernát | 9cfcd87 | 2020-02-18 12:34:02 +0100 | [diff] [blame] | 178 | } |
| 179 | |
Václav Kubernát | 1ae24f4 | 2020-12-01 02:32:04 +0100 | [diff] [blame] | 180 | if (leafType.m_description) { |
| 181 | typeDescription = "\nType description: " + *leafType.m_description; |
| 182 | } |
| 183 | |
Václav Kubernát | 9cfcd87 | 2020-02-18 12:34:02 +0100 | [diff] [blame] | 184 | if (m_datastore.schema()->leafIsKey(path)) { |
| 185 | ss << " (key)"; |
| 186 | } |
Václav Kubernát | b1a75c6 | 2020-04-21 15:20:16 +0200 | [diff] [blame] | 187 | |
| 188 | if (auto defaultValue = m_datastore.schema()->defaultValue(path)) { |
| 189 | ss << " default: " << leafDataToString(*defaultValue); |
| 190 | } |
Václav Kubernát | 9cfcd87 | 2020-02-18 12:34:02 +0100 | [diff] [blame] | 191 | break; |
| 192 | } |
| 193 | case yang::NodeTypes::List: |
| 194 | ss << "list"; |
| 195 | break; |
Václav Kubernát | e7248b2 | 2020-06-26 15:38:59 +0200 | [diff] [blame] | 196 | case yang::NodeTypes::Rpc: |
| 197 | ss << "RPC"; |
| 198 | break; |
Václav Kubernát | aaafeae | 2020-05-05 15:41:45 +0200 | [diff] [blame] | 199 | case yang::NodeTypes::LeafList: |
Václav Kubernát | 160e5a2 | 2020-12-01 01:11:28 +0100 | [diff] [blame] | 200 | ss << "leaf-list"; |
| 201 | break; |
| 202 | case yang::NodeTypes::Action: |
| 203 | ss << "action"; |
| 204 | break; |
| 205 | case yang::NodeTypes::AnyXml: |
| 206 | throw std::logic_error("Sorry, anyxml isn't supported yet."); |
Václav Kubernát | aaafeae | 2020-05-05 15:41:45 +0200 | [diff] [blame] | 207 | case yang::NodeTypes::Notification: |
Václav Kubernát | 160e5a2 | 2020-12-01 01:11:28 +0100 | [diff] [blame] | 208 | throw std::logic_error("Sorry, notifications aren't supported yet."); |
Václav Kubernát | 9cfcd87 | 2020-02-18 12:34:02 +0100 | [diff] [blame] | 209 | } |
Václav Kubernát | 0599e9f | 2020-04-21 09:51:33 +0200 | [diff] [blame] | 210 | |
| 211 | if (!m_datastore.schema()->isConfig(path)) { |
Václav Kubernát | 1ae24f4 | 2020-12-01 02:32:04 +0100 | [diff] [blame] | 212 | ss << " (ro)\n"; |
Václav Kubernát | 0599e9f | 2020-04-21 09:51:33 +0200 | [diff] [blame] | 213 | } |
| 214 | |
Václav Kubernát | a1c4c9e | 2020-04-22 00:37:52 +0200 | [diff] [blame] | 215 | auto status = m_datastore.schema()->status(path); |
| 216 | auto statusStr = status == yang::Status::Deprecated ? " (deprecated)" : |
| 217 | status == yang::Status::Obsolete ? " (obsolete)" : |
| 218 | ""; |
| 219 | |
Václav Kubernát | 1ae24f4 | 2020-12-01 02:32:04 +0100 | [diff] [blame] | 220 | ss << statusStr; |
| 221 | |
Václav Kubernát | 9cfcd87 | 2020-02-18 12:34:02 +0100 | [diff] [blame] | 222 | if (auto description = m_datastore.schema()->description(path)) { |
Václav Kubernát | 1ae24f4 | 2020-12-01 02:32:04 +0100 | [diff] [blame] | 223 | ss << std::endl << *description << std::endl; |
Václav Kubernát | 9cfcd87 | 2020-02-18 12:34:02 +0100 | [diff] [blame] | 224 | } |
Václav Kubernát | 1ae24f4 | 2020-12-01 02:32:04 +0100 | [diff] [blame] | 225 | |
| 226 | ss << typeDescription; |
| 227 | return ss.str(); |
| 228 | } |
| 229 | |
| 230 | void Interpreter::operator()(const describe_& describe) const |
| 231 | { |
| 232 | auto fullPath = pathToString(toCanonicalPath(describe.m_path)); |
| 233 | |
| 234 | std::cout << pathToString(describe.m_path) << ": " << buildTypeInfo(fullPath) << std::endl; |
Václav Kubernát | 9cfcd87 | 2020-02-18 12:34:02 +0100 | [diff] [blame] | 235 | } |
| 236 | |
Václav Kubernát | bf65dd7 | 2020-05-28 02:32:31 +0200 | [diff] [blame] | 237 | void Interpreter::operator()(const move_& move) const |
| 238 | { |
| 239 | m_datastore.moveItem(pathToDataString(move.m_source, Prefixes::WhenNeeded), move.m_destination); |
| 240 | } |
| 241 | |
Václav Kubernát | 70d7f7a | 2020-06-23 14:40:40 +0200 | [diff] [blame] | 242 | void Interpreter::operator()(const dump_& dump) const |
| 243 | { |
| 244 | std::cout << m_datastore.dump(dump.m_format) << "\n"; |
| 245 | } |
| 246 | |
Václav Kubernát | aa4250a | 2020-07-22 00:02:23 +0200 | [diff] [blame] | 247 | void Interpreter::operator()(const prepare_& prepare) const |
Václav Kubernát | e7248b2 | 2020-06-26 15:38:59 +0200 | [diff] [blame] | 248 | { |
Václav Kubernát | b3960f8 | 2020-12-01 03:21:48 +0100 | [diff] [blame] | 249 | m_datastore.initiate(pathToString(toCanonicalPath(prepare.m_path))); |
Václav Kubernát | aa4250a | 2020-07-22 00:02:23 +0200 | [diff] [blame] | 250 | m_parser.changeNode(prepare.m_path); |
Václav Kubernát | e7248b2 | 2020-06-26 15:38:59 +0200 | [diff] [blame] | 251 | } |
| 252 | |
Václav Kubernát | d8408e0 | 2020-12-02 05:13:27 +0100 | [diff] [blame] | 253 | void Interpreter::operator()(const exec_& exec) const |
Václav Kubernát | e7248b2 | 2020-06-26 15:38:59 +0200 | [diff] [blame] | 254 | { |
| 255 | m_parser.changeNode({Scope::Absolute, {}}); |
Václav Kubernát | d8408e0 | 2020-12-02 05:13:27 +0100 | [diff] [blame] | 256 | if (exec.m_path) { |
| 257 | m_datastore.initiate(pathToString(toCanonicalPath(*exec.m_path))); |
| 258 | } |
Václav Kubernát | aa4250a | 2020-07-22 00:02:23 +0200 | [diff] [blame] | 259 | auto output = m_datastore.execute(); |
| 260 | std::cout << "RPC/action output:\n"; |
Václav Kubernát | e7248b2 | 2020-06-26 15:38:59 +0200 | [diff] [blame] | 261 | printTree(output); |
| 262 | } |
| 263 | |
| 264 | void Interpreter::operator()(const cancel_&) const |
| 265 | { |
| 266 | m_parser.changeNode({Scope::Absolute, {}}); |
Václav Kubernát | aa4250a | 2020-07-22 00:02:23 +0200 | [diff] [blame] | 267 | m_datastore.cancel(); |
Václav Kubernát | e7248b2 | 2020-06-26 15:38:59 +0200 | [diff] [blame] | 268 | } |
| 269 | |
Václav Kubernát | 162165e | 2021-02-22 09:46:53 +0100 | [diff] [blame] | 270 | void Interpreter::operator()(const switch_& switch_cmd) const |
| 271 | { |
| 272 | m_datastore.setTarget(switch_cmd.m_target); |
| 273 | } |
| 274 | |
Václav Kubernát | 054cc99 | 2019-02-21 14:23:52 +0100 | [diff] [blame] | 275 | struct commandLongHelpVisitor : boost::static_visitor<const char*> { |
| 276 | template <typename T> |
| 277 | auto constexpr operator()(boost::type<T>) const |
| 278 | { |
| 279 | return T::longHelp; |
| 280 | } |
| 281 | }; |
| 282 | |
| 283 | struct commandShortHelpVisitor : boost::static_visitor<const char*> { |
| 284 | template <typename T> |
| 285 | auto constexpr operator()(boost::type<T>) const |
| 286 | { |
| 287 | return T::shortHelp; |
| 288 | } |
| 289 | }; |
| 290 | |
| 291 | void Interpreter::operator()(const help_& help) const |
| 292 | { |
Václav Kubernát | 3a43323 | 2020-07-08 17:52:50 +0200 | [diff] [blame] | 293 | if (help.m_cmd) { |
Václav Kubernát | 054cc99 | 2019-02-21 14:23:52 +0100 | [diff] [blame] | 294 | std::cout << boost::apply_visitor(commandLongHelpVisitor(), help.m_cmd.get()) << std::endl; |
Václav Kubernát | 3a43323 | 2020-07-08 17:52:50 +0200 | [diff] [blame] | 295 | } else { |
Václav Kubernát | 054cc99 | 2019-02-21 14:23:52 +0100 | [diff] [blame] | 296 | boost::mpl::for_each<CommandTypes, boost::type<boost::mpl::_>>([](auto cmd) { |
| 297 | std::cout << commandShortHelpVisitor()(cmd) << std::endl; |
| 298 | }); |
Václav Kubernát | 3a43323 | 2020-07-08 17:52:50 +0200 | [diff] [blame] | 299 | } |
Václav Kubernát | 054cc99 | 2019-02-21 14:23:52 +0100 | [diff] [blame] | 300 | } |
| 301 | |
Petr Gotthard | 1c76dea | 2022-04-13 17:56:58 +0200 | [diff] [blame] | 302 | void Interpreter::operator()(const quit_&) const |
| 303 | { |
| 304 | // no operation |
| 305 | } |
| 306 | |
Václav Kubernát | 7e16769 | 2020-06-12 09:53:01 +0200 | [diff] [blame] | 307 | template <typename PathType> |
Václav Kubernát | 4c3d22f | 2020-06-12 16:05:01 +0200 | [diff] [blame] | 308 | boost::variant<dataPath_, schemaPath_, module_> Interpreter::toCanonicalPath(const boost::optional<PathType>& optPath) const |
Václav Kubernát | 6415b82 | 2018-08-22 17:40:01 +0200 | [diff] [blame] | 309 | { |
Václav Kubernát | 7e16769 | 2020-06-12 09:53:01 +0200 | [diff] [blame] | 310 | if (!optPath) { |
Václav Kubernát | 4c3d22f | 2020-06-12 16:05:01 +0200 | [diff] [blame] | 311 | return m_parser.currentPath(); |
Václav Kubernát | 7e16769 | 2020-06-12 09:53:01 +0200 | [diff] [blame] | 312 | } |
| 313 | return toCanonicalPath(*optPath); |
Václav Kubernát | 6415b82 | 2018-08-22 17:40:01 +0200 | [diff] [blame] | 314 | } |
| 315 | |
Václav Kubernát | 7e16769 | 2020-06-12 09:53:01 +0200 | [diff] [blame] | 316 | struct impl_toCanonicalPath { |
| 317 | const dataPath_& m_parserPath; |
| 318 | |
Václav Kubernát | 4c3d22f | 2020-06-12 16:05:01 +0200 | [diff] [blame] | 319 | using ReturnType = boost::variant<dataPath_, schemaPath_, module_>; |
| 320 | |
Václav Kubernát | 7e16769 | 2020-06-12 09:53:01 +0200 | [diff] [blame] | 321 | impl_toCanonicalPath(const dataPath_& parserPath) |
| 322 | : m_parserPath(parserPath) |
| 323 | { |
| 324 | } |
Václav Kubernát | 4c3d22f | 2020-06-12 16:05:01 +0200 | [diff] [blame] | 325 | ReturnType operator()(const module_& path) const |
Václav Kubernát | d624799 | 2020-05-27 00:17:56 +0200 | [diff] [blame] | 326 | { |
Václav Kubernát | 4c3d22f | 2020-06-12 16:05:01 +0200 | [diff] [blame] | 327 | return path; |
Václav Kubernát | d624799 | 2020-05-27 00:17:56 +0200 | [diff] [blame] | 328 | } |
Václav Kubernát | 4c3d22f | 2020-06-12 16:05:01 +0200 | [diff] [blame] | 329 | ReturnType operator()(const schemaPath_& path) const |
Václav Kubernát | 5c75b25 | 2018-10-10 18:33:47 +0200 | [diff] [blame] | 330 | { |
Václav Kubernát | 4c3d22f | 2020-06-12 16:05:01 +0200 | [diff] [blame] | 331 | return impl(path); |
Václav Kubernát | 5c75b25 | 2018-10-10 18:33:47 +0200 | [diff] [blame] | 332 | } |
Václav Kubernát | 4c3d22f | 2020-06-12 16:05:01 +0200 | [diff] [blame] | 333 | ReturnType operator()(const dataPath_& path) const |
Václav Kubernát | 5c75b25 | 2018-10-10 18:33:47 +0200 | [diff] [blame] | 334 | { |
Václav Kubernát | 4c3d22f | 2020-06-12 16:05:01 +0200 | [diff] [blame] | 335 | return impl(path); |
Václav Kubernát | 5c75b25 | 2018-10-10 18:33:47 +0200 | [diff] [blame] | 336 | } |
Václav Kubernát | 5c75b25 | 2018-10-10 18:33:47 +0200 | [diff] [blame] | 337 | |
Václav Kubernát | 7e16769 | 2020-06-12 09:53:01 +0200 | [diff] [blame] | 338 | private: |
| 339 | template <typename PathType> |
Václav Kubernát | 59e4ee4 | 2020-07-08 17:32:45 +0200 | [diff] [blame] | 340 | [[nodiscard]] ReturnType impl(const PathType& suffix) const |
Václav Kubernát | d624799 | 2020-05-27 00:17:56 +0200 | [diff] [blame] | 341 | { |
Václav Kubernát | 7e16769 | 2020-06-12 09:53:01 +0200 | [diff] [blame] | 342 | PathType res = [this] { |
| 343 | if constexpr (std::is_same<PathType, schemaPath_>()) { |
| 344 | return dataPathToSchemaPath(m_parserPath); |
| 345 | } else { |
| 346 | return m_parserPath; |
| 347 | } |
| 348 | }(); |
Václav Kubernát | d624799 | 2020-05-27 00:17:56 +0200 | [diff] [blame] | 349 | |
Václav Kubernát | 7e16769 | 2020-06-12 09:53:01 +0200 | [diff] [blame] | 350 | if (suffix.m_scope == Scope::Absolute) { |
Václav Kubernát | 59be0de | 2020-06-15 13:58:45 +0200 | [diff] [blame] | 351 | res = {Scope::Absolute, {}}; |
Václav Kubernát | 9456b5c | 2019-10-02 21:14:52 +0200 | [diff] [blame] | 352 | } |
Václav Kubernát | b6ff0b6 | 2018-08-30 16:14:53 +0200 | [diff] [blame] | 353 | |
Václav Kubernát | 7e16769 | 2020-06-12 09:53:01 +0200 | [diff] [blame] | 354 | for (const auto& fragment : suffix.m_nodes) { |
Václav Kubernát | e781b90 | 2020-06-15 14:35:11 +0200 | [diff] [blame] | 355 | res.pushFragment(fragment); |
Václav Kubernát | 7e16769 | 2020-06-12 09:53:01 +0200 | [diff] [blame] | 356 | } |
Václav Kubernát | 4c3d22f | 2020-06-12 16:05:01 +0200 | [diff] [blame] | 357 | |
Václav Kubernát | 7e16769 | 2020-06-12 09:53:01 +0200 | [diff] [blame] | 358 | return res; |
| 359 | } |
| 360 | }; |
| 361 | |
| 362 | template <typename PathType> |
Václav Kubernát | 4c3d22f | 2020-06-12 16:05:01 +0200 | [diff] [blame] | 363 | boost::variant<dataPath_, schemaPath_, module_> Interpreter::toCanonicalPath(const PathType& path) const |
Václav Kubernát | 9cfcd87 | 2020-02-18 12:34:02 +0100 | [diff] [blame] | 364 | { |
Václav Kubernát | 7e16769 | 2020-06-12 09:53:01 +0200 | [diff] [blame] | 365 | if constexpr (std::is_same<PathType, dataPath_>()) { |
| 366 | return impl_toCanonicalPath(m_parser.currentPath())(path); |
| 367 | } else { |
| 368 | return boost::apply_visitor(impl_toCanonicalPath(m_parser.currentPath()), path); |
| 369 | } |
Václav Kubernát | 9cfcd87 | 2020-02-18 12:34:02 +0100 | [diff] [blame] | 370 | } |
| 371 | |
Václav Kubernát | 48e9dfa | 2020-07-08 10:55:12 +0200 | [diff] [blame] | 372 | Interpreter::Interpreter(Parser& parser, ProxyDatastore& datastore) |
Václav Kubernát | 812ee28 | 2018-08-30 17:10:03 +0200 | [diff] [blame] | 373 | : m_parser(parser) |
| 374 | , m_datastore(datastore) |
Václav Kubernát | 96344a1 | 2018-05-28 16:33:39 +0200 | [diff] [blame] | 375 | { |
| 376 | } |