blob: 719f28b72a44906c7658fdaf68a269c926776ca0 [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át509ce652019-05-29 19:46:44 +02008#include <sstream>
Václav Kubernátcb3af402020-02-12 16:49:17 +01009#include "completion.hpp"
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áta44bdf22020-01-24 12:15:31 +010014 // These two if statements are essential for the algorithm:
15 // The first one solves joining nothing and a relative path - the algorithm
16 // down below adds a leading slash, turning it into an absolute path.
17 // The second one would always add a trailing slash to the path.
18 if (prefix.empty()) {
19 return suffix;
20 }
21
22 if (suffix.empty()) {
23 return prefix;
24 }
25
26 // Otherwise, strip slashes where the join is going to happen. This will
27 // also change "/" to "", but the return statement takes care of that and
28 // inserts the slash again.
29 auto prefixWithoutTrailingSlash = !prefix.empty() && prefix.back() == '/' ? prefix.substr(0, prefix.length() - 1) : prefix;
30 auto suffixWithoutLeadingSlash = !suffix.empty() && suffix.front() == '/' ? suffix.substr(1) : suffix;
31
32 // And join the result with a slash.
33 return prefixWithoutTrailingSlash + '/' + suffixWithoutLeadingSlash;
Václav Kubernát94938b72018-05-04 15:12:24 +020034}
Václav Kubernát60d6f292018-05-25 09:45:32 +020035
36std::string stripLastNodeFromPath(const std::string& path)
37{
38 std::string res = path;
39 auto pos = res.find_last_of('/');
Václav Kubernátefcac932020-01-10 15:26:32 +010040 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 +020041 res.clear();
Václav Kubernátefcac932020-01-10 15:26:32 +010042 } else if (pos == 0) { // path has one backslash at the start - it's either "/" or "/one-path-fragment"
43 return "/";
44 } else {
Václav Kubernát60d6f292018-05-25 09:45:32 +020045 res.erase(pos);
Václav Kubernátefcac932020-01-10 15:26:32 +010046 }
Václav Kubernát60d6f292018-05-25 09:45:32 +020047 return res;
48}
Václav Kubernátebca2552018-06-08 19:06:02 +020049
Václav Kubernát2eaceb82018-10-08 19:56:30 +020050schemaPath_ pathWithoutLastNode(const schemaPath_& path)
Václav Kubernátebca2552018-06-08 19:06:02 +020051{
Václav Kubernát2eaceb82018-10-08 19:56:30 +020052 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 +020053}
Václav Kubernát0b0272f2018-06-13 14:13:08 +020054
Václav Kubernát3a99f002020-03-31 02:27:41 +020055struct impl_leafDataTypeToString {
56 std::string operator()(const yang::String)
57 {
Václav Kubernát0b0272f2018-06-13 14:13:08 +020058 return "a string";
Václav Kubernát0b0272f2018-06-13 14:13:08 +020059 }
Václav Kubernát3a99f002020-03-31 02:27:41 +020060 std::string operator()(const yang::Decimal)
61 {
62 return "a decimal";
63 }
64 std::string operator()(const yang::Bool)
65 {
66 return "a boolean";
67 }
68 std::string operator()(const yang::Int8)
69 {
70 return "an 8-bit integer";
71 }
72 std::string operator()(const yang::Uint8)
73 {
74 return "an 8-bit unsigned integer";
75 }
76 std::string operator()(const yang::Int16)
77 {
78 return "a 16-bit integer";
79 }
80 std::string operator()(const yang::Uint16)
81 {
82 return "a 16-bit unsigned integer";
83 }
84 std::string operator()(const yang::Int32)
85 {
86 return "a 32-bit integer";
87 }
88 std::string operator()(const yang::Uint32)
89 {
90 return "a 32-bit unsigned integer";
91 }
92 std::string operator()(const yang::Int64)
93 {
94 return "a 64-bit integer";
95 }
96 std::string operator()(const yang::Uint64)
97 {
98 return "a 64-bit unsigned integer";
99 }
100 std::string operator()(const yang::Binary)
101 {
102 return "a base64-encoded binary value";
103 }
104 std::string operator()(const yang::Enum&)
105 {
106 return "an enum";
107 }
108 std::string operator()(const yang::IdentityRef&)
109 {
110 return "an identity";
111 }
112 std::string operator()(const yang::LeafRef&)
113 {
114 return "a leafref";
115 }
116};
117
118std::string leafDataTypeToString(const yang::LeafDataType& type)
119{
120 return std::visit(impl_leafDataTypeToString{}, type);
Václav Kubernát0b0272f2018-06-13 14:13:08 +0200121}
Václav Kubernát744f57f2018-06-29 22:46:26 +0200122
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200123std::string fullNodeName(const schemaPath_& location, const ModuleNodePair& pair)
Václav Kubernát744f57f2018-06-29 22:46:26 +0200124{
125 if (!pair.first) {
126 return location.m_nodes.at(0).m_prefix.value().m_name + ":" + pair.second;
127 } else {
128 return pair.first.value() + ":" + pair.second;
129 }
130}
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200131
132std::string fullNodeName(const dataPath_& location, const ModuleNodePair& pair)
133{
134 return fullNodeName(dataPathToSchemaPath(location), pair);
135}
Václav Kubernát4108e0d2018-10-29 13:32:22 +0100136
Václav Kubernát9b725992019-05-29 16:39:47 +0200137struct leafDataToStringVisitor : boost::static_visitor<std::string> {
138 std::string operator()(const enum_& data) const
139 {
140 return data.m_value;
141 }
142
143 std::string operator()(const binary_& data) const
144 {
145 return data.m_value;
146 }
147
148 std::string operator()(const identityRef_& data) const
149 {
Václav Kubernátc31bd602019-03-07 11:44:48 +0100150 return data.m_prefix.value().m_name + ":" + data.m_value;
Václav Kubernát9b725992019-05-29 16:39:47 +0200151 }
152
Václav Kubernát144729d2020-01-08 15:20:35 +0100153 std::string operator()(const special_& data) const
154 {
155 return specialValueToString(data);
156 }
157
Jan Kundrátc43acf72019-07-02 19:28:22 +0200158 std::string operator()(const std::string& data) const
159 {
160 return data;
161 }
162
Václav Kubernát8e121ff2019-10-15 15:47:45 +0200163 std::string operator()(const bool& data) const
164 {
165 if (data)
166 return "true";
167 else
168 return "false";
169 }
170
Václav Kubernát9b725992019-05-29 16:39:47 +0200171 template <typename T>
172 std::string operator()(const T& data) const
173 {
Jan Kundrátc43acf72019-07-02 19:28:22 +0200174 return std::to_string(data);
Václav Kubernát9b725992019-05-29 16:39:47 +0200175 }
176};
177
178std::string leafDataToString(const leaf_data_ value)
179{
180 return boost::apply_visitor(leafDataToStringVisitor(), value);
181}