Skip to content

Commit 7683f8a

Browse files
polycubed: further fix to list validation
18becf4 fixed some problems with the list validation, however this also introduced another problems as the that fix was not complete. This commit adds some logic to fill the keys in a list request according to its type. Fixes: 18becf4 ("polycubed: fix list validation") Signed-off-by: Mauricio Vasquez B <mauriciovasquezbernal@gmail.com>
1 parent 0b525e9 commit 7683f8a

5 files changed

Lines changed: 62 additions & 4 deletions

File tree

src/polycubed/src/server/Resources/Body/ListResource.cpp

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
#include "JsonNodeField.h"
2626
#include "ListKey.h"
2727

28+
#include "../../Types/lexical_cast.h"
29+
2830
namespace polycube::polycubed::Rest::Resources::Body {
2931
ListResource::ListResource(const std::string &name,
3032
const std::string &description,
@@ -107,11 +109,41 @@ void ListResource::SetDefaultIfMissing(nlohmann::json &body,
107109

108110
void ListResource::FillKeys(nlohmann::json &body, const ListKeyValues &keys) {
109111
// TODO: is there a more efficient implementation of this?
110-
for (auto &key: keys_) {
112+
for (auto &key : keys_) {
111113
for (auto &kv : keys) {
112114
if (key.Name() == kv.name) {
113115
// TODO: check if the key is present in the body and compare the data
114-
body[key.OriginalName()] = kv.value;
116+
switch (key.Type()) {
117+
case ListType::kBool:
118+
body[key.OriginalName()] = Types::lexical_cast<bool>(kv.value);
119+
break;
120+
case ListType::kInt8:
121+
body[key.OriginalName()] = Types::lexical_cast<int8_t>(kv.value);
122+
break;
123+
case ListType::kInt16:
124+
body[key.OriginalName()] = Types::lexical_cast<int16_t>(kv.value);
125+
break;
126+
case ListType::kInt32:
127+
body[key.OriginalName()] = Types::lexical_cast<int32_t>(kv.value);
128+
break;
129+
case ListType::kInt64:
130+
body[key.OriginalName()] = Types::lexical_cast<int64_t>(kv.value);
131+
break;
132+
case ListType::kUint8:
133+
body[key.OriginalName()] = Types::lexical_cast<uint8_t>(kv.value);
134+
break;
135+
case ListType::kUint16:
136+
body[key.OriginalName()] = Types::lexical_cast<uint16_t>(kv.value);
137+
break;
138+
case ListType::kUint32:
139+
body[key.OriginalName()] = Types::lexical_cast<uint32_t>(kv.value);
140+
break;
141+
case ListType::kUint64:
142+
body[key.OriginalName()] = Types::lexical_cast<uint64_t>(kv.value);
143+
break;
144+
default:
145+
body[key.OriginalName()] = kv.value;
146+
}
115147
break;
116148
}
117149
}

src/polycubed/src/server/Resources/Endpoint/ListResource.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ class ListResource : public ParentResource, public Body::ListResource {
7373
private:
7474
// try to get as much as possible Keys
7575
void GetListKeys(const Pistache::Rest::Request &request,
76-
ListKeyValues &parsed) const;
76+
ListKeyValues &parsed) const;
7777

7878
std::vector<PathParamField> key_params_;
7979
std::string multiple_endpoint_;

src/polycubed/src/server/Resources/Endpoint/ParentResource.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ void ParentResource::CreateReplaceUpdate(
107107
Keys(request, keys);
108108

109109
// fill keys if they are missing
110-
if (auto list = dynamic_cast<ListResource*>(this)) {
110+
if (auto list = dynamic_cast<ListResource *>(this)) {
111111
list->FillKeys(jbody, keys);
112112
}
113113

src/polycubed/src/server/Types/lexical_cast.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,30 @@
1515
*/
1616
#include "lexical_cast.h"
1717

18+
#include <algorithm>
19+
#include <string>
20+
1821
namespace polycube::polycubed::Rest::Types {
22+
template <>
23+
bool lexical_cast<bool>(const std::string &value) {
24+
try {
25+
std::string lower;
26+
lower.reserve(value.length());
27+
std::transform(std::begin(value), std::end(value),
28+
std::back_inserter(lower),
29+
static_cast<int (*)(int)>(std::tolower));
30+
if (lower == "false") {
31+
return false;
32+
} else if (lower == "true") {
33+
return true;
34+
} else {
35+
throw bad_lexical_cast();
36+
}
37+
} catch (...) {
38+
throw bad_lexical_cast();
39+
}
40+
}
41+
1942
template <>
2043
int8_t lexical_cast<int8_t>(const std::string &value) {
2144
try {

src/polycubed/src/server/Types/lexical_cast.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ struct bad_lexical_cast : public std::exception {};
2525
template <class T>
2626
T lexical_cast(const std::string &value) = delete;
2727

28+
template <>
29+
bool lexical_cast<bool>(const std::string &value);
30+
2831
template <>
2932
int8_t lexical_cast<int8_t>(const std::string &value);
3033

0 commit comments

Comments
 (0)