Merge remote-tracking branch 'origin/topic/awelzel/enum-names-with-strings'

* origin/topic/awelzel/enum-names-with-strings:
  enum_names: Support naming types with a string
This commit is contained in:
Robin Sommer 2022-10-25 12:37:16 +02:00
commit 5aa7d80e88
No known key found for this signature in database
GPG key ID: D8187293B3FFE5D0
6 changed files with 56 additions and 7 deletions

View file

@ -1,3 +1,11 @@
5.2.0-dev.142 | 2022-10-25 12:37:16 +0200
* enum_names: Support naming types with a string (Arne Welzel, Corelight)
Add support to enum_names() to transparently lookup the type if a string
is provided. This is similar in how record_fields() behaves when being
passed a string.
5.2.0-dev.140 | 2022-10-25 12:34:39 +0200 5.2.0-dev.140 | 2022-10-25 12:34:39 +0200
* scripts: Migrate table iteration to blank identifiers (Arne Welzel, Corelight) * scripts: Migrate table iteration to blank identifiers (Arne Welzel, Corelight)

3
NEWS
View file

@ -77,6 +77,9 @@ Changed Functionality
will be raised once only. Further, analyzer confirmations are not raised will be raised once only. Further, analyzer confirmations are not raised
after a violation. after a violation.
- The parameter given to ``enum_names()`` can now be a string naming the
enum type, rather than the type itself.
Deprecated Functionality Deprecated Functionality
------------------------ ------------------------

View file

@ -1 +1 @@
5.2.0-dev.140 5.2.0-dev.142

View file

@ -1938,20 +1938,26 @@ function type_aliases%(x: any%): string_set
## Returns all value names associated with an enum type. ## Returns all value names associated with an enum type.
## ##
## et: An enum type. ## et: An enum type or a string naming one.
## ##
## Returns: All enum value names associated with enum type *et*. ## Returns: All enum value names associated with enum type *et*.
## If *et* is not an enum type, an empty set is returned. ## If *et* is not an enum type or does not name one, an empty set is returned.
function enum_names%(et: any%): string_set function enum_names%(et: any%): string_set
%{ %{
auto rval = make_intrusive<zeek::TableVal>(zeek::id::string_set); auto rval = make_intrusive<zeek::TableVal>(zeek::id::string_set);
if ( et->GetType()->Tag() != TYPE_TYPE ) zeek::TypePtr t = zeek::Type::nil;
return rval;
const auto& t = et->GetType()->AsTypeType()->GetType(); if ( et->GetType()->Tag() == TYPE_STRING )
{
const auto& id = zeek::detail::global_scope()->Find(et->AsStringVal()->ToStdString());
if ( id && id->IsType() )
t = id->GetType();
}
else if ( et->GetType()->Tag() == TYPE_TYPE )
t = et->GetType()->AsTypeType()->GetType();
if ( t->Tag() != TYPE_ENUM ) if ( ! t || t->Tag() != TYPE_ENUM )
return rval; return rval;
auto enum_type = t->AsEnumType(); auto enum_type = t->AsEnumType();

View file

@ -1,4 +1,20 @@
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63. ### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63.
with types
{
Monochrome::WHITE,
Monochrome::BLACK
}
{
RED,
GREEN,
PURPLE,
BLUE
}
{
Monochrome::WHITE,
Monochrome::BLACK
}
with strings
{ {
RED, RED,
GREEN, GREEN,
@ -11,3 +27,7 @@ GREEN,
PURPLE, PURPLE,
BLUE BLUE
} }
{
Monochrome::WHITE,
Monochrome::BLACK
}

View file

@ -6,5 +6,17 @@ type ColorAlias: Color;
redef enum Color += { PURPLE }; redef enum Color += { PURPLE };
module Monochrome;
type Color: enum { WHITE, BLACK };
print "with types";
print enum_names(Color); print enum_names(Color);
print enum_names(ColorAlias); print enum_names(ColorAlias);
print enum_names(Monochrome::Color);
print "with strings";
print enum_names("Color");
print enum_names("ColorAlias");
print enum_names("Monochrome::Color");