| 211 | } |
| 212 | |
| 213 | void 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("."); |
no test coverage detected