Add RecordVal::AssignField() and use it in supervisor code

This is a convenience method to assign a known record field value by
field name.  May also be useful to reduce warnings from static analysis
(e.g. Coverity) about not checking for negative return values before
assigning since that now flows through a [[noreturn]] error path.
This commit is contained in:
Jon Siwek 2021-03-26 18:57:42 -07:00
parent 7bf885b0b8
commit 2855df63ce
3 changed files with 33 additions and 22 deletions

View file

@ -1152,6 +1152,19 @@ public:
void Assign(int field, String* new_val)
{ Assign(field, new StringVal(new_val)); }
/**
* Assign a value of type @c T to a record field of the given name.
* A fatal error occurs if the no such field name exists.
*/
template <class T>
void AssignField(const char* field_name, T&& val)
{
int idx = GetType()->AsRecordType()->FieldOffset(field_name);
if ( idx < 0 )
reporter->InternalError("missing record field: %s", field_name);
Assign(idx, std::forward<T>(val));
}
/**
* Appends a value to the record's fields. The caller is responsible
* for ensuring that fields are appended in the correct order and