enum_names: Support naming types with a string

In his ZeekWeek 2022 presentation, @stevesmoot mentioned that he had a
difficult time looking up enum names when all he had was a string
naming the type.

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.
This commit is contained in:
Arne Welzel 2022-10-21 20:03:54 +02:00
parent 7f7c77ab07
commit a00cef9920
4 changed files with 47 additions and 6 deletions

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.
with types
{
Monochrome::WHITE,
Monochrome::BLACK
}
{
RED,
GREEN,
PURPLE,
BLUE
}
{
Monochrome::WHITE,
Monochrome::BLACK
}
with strings
{
RED,
GREEN,
@ -11,3 +27,7 @@ GREEN,
PURPLE,
BLUE
}
{
Monochrome::WHITE,
Monochrome::BLACK
}

View file

@ -6,5 +6,17 @@ type ColorAlias: Color;
redef enum Color += { PURPLE };
module Monochrome;
type Color: enum { WHITE, BLACK };
print "with types";
print enum_names(Color);
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");