MCPcopy Create free account
hub / github.com/Icinga/icinga2 / RestoreAttribute

Method RestoreAttribute

lib/base/configobject.cpp:213–324  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

211}
212
213void ConfigObject::RestoreAttribute(const String& attr, bool updateVersion)
214{
215 Type::Ptr type = GetReflectionType();
216
217 std::vector<String> tokens = attr.Split(".");
218
219 String fieldName = tokens[0];
220
221 int fid = type->GetFieldId(fieldName);
222
223 Value currentValue = GetField(fid);
224
225 Dictionary::Ptr original_attributes = GetOriginalAttributes();
226
227 if (!original_attributes)
228 return;
229
230 Value oldValue = original_attributes->Get(attr);
231 Value newValue;
232
233 if (tokens.size() > 1) {
234 newValue = currentValue.Clone();
235 Value current = newValue;
236
237 if (current.IsEmpty())
238 BOOST_THROW_EXCEPTION(std::invalid_argument("Cannot restore non-existent object attribute"));
239
240 String prefix = tokens[0];
241
242 for (std::vector<String>::size_type i = 1; i < tokens.size() - 1; i++) {
243 if (!current.IsObjectType<Dictionary>())
244 BOOST_THROW_EXCEPTION(std::invalid_argument("Value must be a dictionary."));
245
246 Dictionary::Ptr dict = current;
247
248 const String& key = tokens[i];
249 prefix += "." + key;
250
251 if (!dict->Contains(key))
252 BOOST_THROW_EXCEPTION(std::invalid_argument("Cannot restore non-existent object attribute"));
253
254 current = dict->Get(key);
255 }
256
257 if (!current.IsObjectType<Dictionary>())
258 BOOST_THROW_EXCEPTION(std::invalid_argument("Value must be a dictionary."));
259
260 Dictionary::Ptr dict = current;
261
262 const String& key = tokens[tokens.size() - 1];
263 prefix += "." + key;
264
265 std::vector<String> restoredAttrs;
266
267 {
268 ObjectLock olock(original_attributes);
269 for (const auto& kv : original_attributes) {
270 std::vector<String> originalTokens = String(kv.first).Split(".");

Callers 3

HandleRequestMethod · 0.80

Calls 11

SplitMethod · 0.80
push_backMethod · 0.80
StringClass · 0.70
GetFieldIdMethod · 0.45
GetMethod · 0.45
sizeMethod · 0.45
CloneMethod · 0.45
IsEmptyMethod · 0.45
ContainsMethod · 0.45
SetMethod · 0.45
RemoveMethod · 0.45

Tested by

no test coverage detected