|
25 | 25 | #include "JsonNodeField.h" |
26 | 26 | #include "ListKey.h" |
27 | 27 |
|
| 28 | +#include "../../Types/lexical_cast.h" |
| 29 | + |
28 | 30 | namespace polycube::polycubed::Rest::Resources::Body { |
29 | 31 | ListResource::ListResource(const std::string &name, |
30 | 32 | const std::string &description, |
@@ -107,11 +109,41 @@ void ListResource::SetDefaultIfMissing(nlohmann::json &body, |
107 | 109 |
|
108 | 110 | void ListResource::FillKeys(nlohmann::json &body, const ListKeyValues &keys) { |
109 | 111 | // TODO: is there a more efficient implementation of this? |
110 | | - for (auto &key: keys_) { |
| 112 | + for (auto &key : keys_) { |
111 | 113 | for (auto &kv : keys) { |
112 | 114 | if (key.Name() == kv.name) { |
113 | 115 | // 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 | + } |
115 | 147 | break; |
116 | 148 | } |
117 | 149 | } |
|
0 commit comments