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