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

Method SetFieldByName

lib/base/object.cpp:129–152  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

127}
128
129void Object::SetFieldByName(const String& field, const Value& value, const DebugInfo& debugInfo)
130{
131 Type::Ptr type = GetReflectionType();
132
133 if (!type)
134 BOOST_THROW_EXCEPTION(ScriptError("Cannot set field on object.", debugInfo));
135
136 int fid = type->GetFieldId(field);
137
138 if (fid == -1)
139 BOOST_THROW_EXCEPTION(ScriptError("Attribute '" + field + "' does not exist.", debugInfo));
140
141 try {
142 SetField(fid, value);
143 } catch (const boost::bad_lexical_cast&) {
144 Field fieldInfo = type->GetFieldInfo(fid);
145 Type::Ptr ftype = Type::GetByName(fieldInfo.TypeName);
146 BOOST_THROW_EXCEPTION(ScriptError("Attribute '" + field + "' cannot be set to value of type '" + value.GetTypeName() + "', expected '" + ftype->GetName() + "'", debugInfo));
147 } catch (const std::bad_cast&) {
148 Field fieldInfo = type->GetFieldInfo(fid);
149 Type::Ptr ftype = Type::GetByName(fieldInfo.TypeName);
150 BOOST_THROW_EXCEPTION(ScriptError("Attribute '" + field + "' cannot be set to value of type '" + value.GetTypeName() + "', expected '" + ftype->GetName() + "'", debugInfo));
151 }
152}
153
154void Object::Validate([[maybe_unused]] int types, const ValidationUtils&)
155{

Callers

nothing calls this directly

Calls 5

ScriptErrorClass · 0.85
GetFieldIdMethod · 0.45
GetFieldInfoMethod · 0.45
GetTypeNameMethod · 0.45
GetNameMethod · 0.45

Tested by

no test coverage detected