Merge remote-tracking branch 'origin/topic/awelzel/make-record-fields-ordered'

* origin/topic/awelzel/make-record-fields-ordered:
  Type/RecordType: Make table returned by GetRecordFieldsVal() ordered
This commit is contained in:
Arne Welzel 2025-07-23 13:37:56 +02:00
commit 84cbd3784f
11 changed files with 63 additions and 44 deletions

View file

@ -1213,7 +1213,12 @@ static string container_type_name(const Type* ft) {
TableValPtr RecordType::GetRecordFieldsVal(const RecordVal* rv) const {
static auto record_field = id::find_type<RecordType>("record_field");
static auto record_field_table = id::find_type<TableType>("record_field_table");
auto rval = make_intrusive<TableVal>(record_field_table);
auto attrs = zeek::make_intrusive<detail::Attributes>(record_field_table,
/*in_record=*/false,
/*is_global=*/false);
attrs->AddAttr(make_intrusive<detail::Attr>(detail::ATTR_ORDERED));
auto rval = make_intrusive<TableVal>(record_field_table, std::move(attrs));
for ( int i = 0; i < NumFields(); ++i ) {
const auto& ft = GetFieldType(i);

View file

@ -697,7 +697,10 @@ public:
/**
* Returns a "record_field_table" value for introspection purposes.
* @param rv an optional record value, if given the values of
* all fields will be provided in the returned table.
* all fields will be provided in the returned table. The table
* is initialized with the &ordered attribute such that iterating over
* the table will yield entries for fields in the same order as they
* are kept in the record type types list.
*/
TableValPtr GetRecordFieldsVal(const RecordVal* rv = nullptr) const;

View file

@ -2403,7 +2403,10 @@ function lookup_ID%(id: string%) : any
##
## rec: The record value or type to inspect.
##
## Returns: A table that describes the fields of a record.
## Returns: A table that describes the fields of a record. The returned table has
## the :zeek:attr:`&ordered` attribute set. Iterating over the table will
## yield entries for the fields in the same order as the fields were
## declared.
function record_fields%(rec: any%): record_field_table
%{
static auto record_field_table = zeek::id::find_type<zeek::TableType>("record_field_table");