mirror of
https://github.com/zeek/zeek.git
synced 2025-10-02 06:38:20 +00:00
Add enum_names() BIF to return names of an enum type's values
This commit is contained in:
parent
77514f234f
commit
f147c47271
3 changed files with 49 additions and 0 deletions
26
src/zeek.bif
26
src/zeek.bif
|
@ -1946,6 +1946,32 @@ function type_aliases%(x: any%): string_set
|
|||
return rval;
|
||||
%}
|
||||
|
||||
## Returns all value names associated with an enum type.
|
||||
##
|
||||
## et: An enum type.
|
||||
##
|
||||
## Returns: All enum value names associated with enum type *et*.
|
||||
## If *et* is not an enum type, an empty set is returned.
|
||||
function enum_names%(et: any%): string_set
|
||||
%{
|
||||
auto rval = make_intrusive<zeek::TableVal>(zeek::id::string_set);
|
||||
|
||||
if ( et->GetType()->Tag() != TYPE_TYPE )
|
||||
return rval;
|
||||
|
||||
const auto& t = et->GetType()->AsTypeType()->GetType();
|
||||
|
||||
if ( t->Tag() != TYPE_ENUM )
|
||||
return rval;
|
||||
|
||||
auto enum_type = t->AsEnumType();
|
||||
|
||||
for ( const auto& [name, i] : enum_type->Names() )
|
||||
rval->Assign(make_intrusive<zeek::StringVal>(name), nullptr);
|
||||
|
||||
return rval;
|
||||
%}
|
||||
|
||||
## Returns: list of command-line arguments (``argv``) used to run Zeek.
|
||||
function zeek_args%(%): string_vec
|
||||
%{
|
||||
|
|
13
testing/btest/Baseline/bifs.enum_names/out
Normal file
13
testing/btest/Baseline/bifs.enum_names/out
Normal file
|
@ -0,0 +1,13 @@
|
|||
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63.
|
||||
{
|
||||
RED,
|
||||
GREEN,
|
||||
PURPLE,
|
||||
BLUE
|
||||
}
|
||||
{
|
||||
RED,
|
||||
GREEN,
|
||||
PURPLE,
|
||||
BLUE
|
||||
}
|
10
testing/btest/bifs/enum_names.zeek
Normal file
10
testing/btest/bifs/enum_names.zeek
Normal file
|
@ -0,0 +1,10 @@
|
|||
# @TEST-EXEC: zeek -b %INPUT >out
|
||||
# @TEST-EXEC: btest-diff out
|
||||
|
||||
type Color: enum { RED, GREEN, BLUE };
|
||||
type ColorAlias: Color;
|
||||
|
||||
redef enum Color += { PURPLE };
|
||||
|
||||
print enum_names(Color);
|
||||
print enum_names(ColorAlias);
|
Loading…
Add table
Add a link
Reference in a new issue