Deprecate RecordVal::Lookup(const char*, bool)

Replace with GetField(const char*) and GetFieldOrDefault(const char*).
This commit is contained in:
Jon Siwek 2020-05-19 21:09:40 -07:00
parent 2b4d80c849
commit 5bf2ed02d7
14 changed files with 128 additions and 80 deletions

3
NEWS
View file

@ -202,6 +202,9 @@ Deprecated Functionality
- ``RecordVal::LookupWithDefault(int)`` is deprecated, use
``RecordVal::GetFieldOrDefault(int)``.
- ``RecordVal::Lookup(const char*, bool)`` is deprecated, use either
``RecordVal::GetField()`` or ``RecordVal::GetFieldOrDefault()``.
Zeek 3.1.0
==========

View file

@ -2781,14 +2781,24 @@ void RecordVal::DoneParsing()
parse_time_records.clear();
}
IntrusivePtr<Val> RecordVal::Lookup(const char* field, bool with_default) const
const IntrusivePtr<Val>& RecordVal::GetField(const char* field) const
{
int idx = GetType()->AsRecordType()->FieldOffset(field);
if ( idx < 0 )
reporter->InternalError("missing record field: %s", field);
return with_default ? GetFieldOrDefault(idx) : GetField(idx);
return GetField(idx);
}
IntrusivePtr<Val> RecordVal::GetFieldOrDefault(const char* field) const
{
int idx = GetType()->AsRecordType()->FieldOffset(field);
if ( idx < 0 )
reporter->InternalError("missing record field: %s", field);
return GetFieldOrDefault(idx);
}
IntrusivePtr<RecordVal> RecordVal::CoerceTo(IntrusivePtr<RecordType> t,

View file

@ -1000,6 +1000,46 @@ public:
Val* LookupWithDefault(int field) const
{ return GetFieldOrDefault(field).release(); }
/**
* Returns the value of a given field name.
* @param field The name of a field to retrieve.
* @return The value of the given field. If no such field name exists,
* a fatal error occurs.
*/
const IntrusivePtr<Val>& GetField(const char* field) const;
/**
* Returns the value of a given field name as cast to type @c T.
* @param field The name of a field to retrieve.
* @return The value of the given field cast to type @c T. If no such
* field name exists, a fatal error occurs.
*/
template <class T>
IntrusivePtr<T> GetField(const char* field) const
{ return cast_intrusive<T>(GetField(field)); }
/**
* Returns the value of a given field name if it's previously been
* assigned, or else returns the value created from evaluating the record
* fields' &default expression.
* @param field The name of a field to retrieve.
* @return The value of the given field. or the default value
* if the field hasn't been assigned yet. If no such field name exists,
* a fatal error occurs.
*/
IntrusivePtr<Val> GetFieldOrDefault(const char* field) const;
/**
* Returns the value of a given field name or its default value
* as cast to type @c T.
* @param field The name of a field to retrieve.
* @return The value of the given field or its default value cast to
* type @c T. If no such field name exists, a fatal error occurs.
*/
template <class T>
IntrusivePtr<T> GetFieldOrDefault(const char* field) const
{ return cast_intrusive<T>(GetField(field)); }
/**
* Looks up the value of a field by field name. If the field doesn't
* exist in the record type, it's an internal error: abort.
@ -1008,7 +1048,9 @@ public:
* the field has yet to be initialized.
* @return the value in field \a field.
*/
IntrusivePtr<Val> Lookup(const char* field, bool with_default = false) const;
[[deprecated("Remove in v4.1. Use GetField() or GetFieldOrDefault().")]]
Val* Lookup(const char* field, bool with_default = false) const
{ return with_default ? GetFieldOrDefault(field).release() : GetField(field).get(); }
void Describe(ODesc* d) const override;

View file

@ -170,8 +170,8 @@ void ConnSize_Analyzer::SetDurationThreshold(double duration)
void ConnSize_Analyzer::UpdateConnVal(RecordVal *conn_val)
{
// RecordType *connection_type is decleared in NetVar.h
RecordVal *orig_endp = conn_val->Lookup("orig")->AsRecordVal();
RecordVal *resp_endp = conn_val->Lookup("resp")->AsRecordVal();
RecordVal* orig_endp = conn_val->GetField("orig")->AsRecordVal();
RecordVal* resp_endp = conn_val->GetField("resp")->AsRecordVal();
// endpoint is the RecordType from NetVar.h
int pktidx = zeek::id::endpoint->FieldOffset("num_pkts");

View file

@ -459,22 +459,22 @@ void ICMP_Analyzer::Describe(ODesc* d) const
void ICMP_Analyzer::UpdateConnVal(RecordVal *conn_val)
{
auto orig_endp = conn_val->Lookup("orig");
auto resp_endp = conn_val->Lookup("resp");
const auto& orig_endp = conn_val->GetField("orig");
const auto& resp_endp = conn_val->GetField("resp");
UpdateEndpointVal(&orig_endp, true);
UpdateEndpointVal(&resp_endp, false);
UpdateEndpointVal(orig_endp, true);
UpdateEndpointVal(resp_endp, false);
// Call children's UpdateConnVal
Analyzer::UpdateConnVal(conn_val);
}
void ICMP_Analyzer::UpdateEndpointVal(IntrusivePtr<Val>* endp_arg, bool is_orig)
void ICMP_Analyzer::UpdateEndpointVal(const IntrusivePtr<Val>& endp_arg, bool is_orig)
{
Conn()->EnableStatusUpdateTimer();
int size = is_orig ? request_len : reply_len;
auto endp = (*endp_arg)->AsRecordVal();
auto endp = endp_arg->AsRecordVal();
if ( size < 0 )
{

View file

@ -84,7 +84,7 @@ protected:
RuleMatcherState matcher_state;
private:
void UpdateEndpointVal(IntrusivePtr<Val>* endp, bool is_orig);
void UpdateEndpointVal(const IntrusivePtr<Val>& endp, bool is_orig);
};
// Returns the counterpart type to the given type (e.g., the counterpart

View file

@ -1287,8 +1287,8 @@ void TCP_Analyzer::FlipRoles()
void TCP_Analyzer::UpdateConnVal(RecordVal *conn_val)
{
RecordVal *orig_endp_val = conn_val->Lookup("orig")->AsRecordVal();
RecordVal *resp_endp_val = conn_val->Lookup("resp")->AsRecordVal();
RecordVal* orig_endp_val = conn_val->GetField("orig")->AsRecordVal();
RecordVal* resp_endp_val = conn_val->GetField("resp")->AsRecordVal();
orig_endp_val->Assign(0, val_mgr->Count(orig->Size()));
orig_endp_val->Assign(1, val_mgr->Count(int(orig->state)));

View file

@ -216,8 +216,8 @@ void UDP_Analyzer::DeliverPacket(int len, const u_char* data, bool is_orig,
void UDP_Analyzer::UpdateConnVal(RecordVal *conn_val)
{
RecordVal *orig_endp = conn_val->Lookup("orig")->AsRecordVal();
RecordVal *resp_endp = conn_val->Lookup("resp")->AsRecordVal();
RecordVal* orig_endp = conn_val->GetField("orig")->AsRecordVal();
RecordVal* resp_endp = conn_val->GetField("resp")->AsRecordVal();
UpdateEndpointVal(orig_endp, true);
UpdateEndpointVal(resp_endp, false);

View file

@ -21,8 +21,8 @@ DataEvent::DataEvent(RecordVal* args, File* file,
file_analysis::Analyzer* DataEvent::Instantiate(RecordVal* args, File* file)
{
auto chunk_val = args->Lookup("chunk_event");
auto stream_val = args->Lookup("stream_event");
const auto& chunk_val = args->GetField("chunk_event");
const auto& stream_val = args->GetField("stream_event");
if ( ! chunk_val && ! stream_val ) return nullptr;

View file

@ -32,9 +32,9 @@ Extract::~Extract()
safe_close(fd);
}
static IntrusivePtr<Val> get_extract_field_val(RecordVal* args, const char* name)
static const IntrusivePtr<Val>& get_extract_field_val(RecordVal* args, const char* name)
{
auto rval = args->Lookup(name);
const auto& rval = args->GetField(name);
if ( ! rval )
reporter->Error("File extraction analyzer missing arg field: %s", name);
@ -44,8 +44,8 @@ static IntrusivePtr<Val> get_extract_field_val(RecordVal* args, const char* name
file_analysis::Analyzer* Extract::Instantiate(RecordVal* args, File* file)
{
auto fname = get_extract_field_val(args, "extract_filename");
auto limit = get_extract_field_val(args, "extract_limit");
const auto& fname = get_extract_field_val(args, "extract_filename");
const auto& limit = get_extract_field_val(args, "extract_limit");
if ( ! fname || ! limit )
return nullptr;

View file

@ -32,16 +32,6 @@ using namespace file_analysis;
#define OCSP_STRING_BUF_SIZE 2048
static IntrusivePtr<Val> get_ocsp_type(RecordVal* args, const char* name)
{
auto rval = args->Lookup(name);
if ( ! rval )
reporter->Error("File extraction analyzer missing arg field: %s", name);
return rval;
}
static bool OCSP_RESPID_bio(OCSP_BASICRESP* basic_resp, BIO* bio)
{
#if ( OPENSSL_VERSION_NUMBER < 0x10100000L ) || defined(LIBRESSL_VERSION_NUMBER)

View file

@ -233,7 +233,7 @@ bool Manager::CreateStream(Stream* info, RecordVal* description)
return false;
}
string name = description->Lookup("name", true)->AsString()->CheckString();
string name = description->GetFieldOrDefault("name")->AsString()->CheckString();
if ( Stream *i = FindStream(name) )
{
@ -242,17 +242,19 @@ bool Manager::CreateStream(Stream* info, RecordVal* description)
return false;
}
auto reader = description->Lookup("reader", true);
auto reader = description->GetFieldOrDefault("reader");
// get the source ...
const BroString* bsource = description->Lookup("source", true)->AsString();
auto source_val = description->GetFieldOrDefault("source");
const BroString* bsource = source_val->AsString();
string source((const char*) bsource->Bytes(), bsource->Len());
ReaderBackend::ReaderInfo rinfo;
rinfo.source = copy_string(source.c_str());
rinfo.name = copy_string(name.c_str());
auto mode = description->Lookup("mode", true)->AsEnumVal();
auto mode_val = description->GetFieldOrDefault("mode");
auto mode = mode_val->AsEnumVal();
switch ( mode->InternalInt() )
{
case 0:
@ -272,7 +274,7 @@ bool Manager::CreateStream(Stream* info, RecordVal* description)
return false;
}
auto config = description->Lookup("config", true);
auto config = description->GetFieldOrDefault("config");
info->config = config.release()->AsTableVal();
{
@ -317,14 +319,15 @@ bool Manager::CreateEventStream(RecordVal* fval)
return false;
}
string stream_name = fval->Lookup("name", true)->AsString()->CheckString();
string stream_name = fval->GetFieldOrDefault("name")->AsString()->CheckString();
auto fields_val = fval->Lookup("fields", true);
auto fields_val = fval->GetFieldOrDefault("fields");
RecordType* fields = fields_val->AsType()->AsTypeType()->Type()->AsRecordType();
auto want_record = fval->Lookup("want_record", true);
auto want_record = fval->GetFieldOrDefault("want_record");
Func* event = fval->Lookup("ev", true)->AsFunc();
auto ev_val = fval->GetFieldOrDefault("ev");
Func* event = ev_val->AsFunc();
const auto& etype = event->GetType();
@ -412,7 +415,7 @@ bool Manager::CreateEventStream(RecordVal* fval)
else
assert(false);
auto error_event_val = fval->Lookup("error_ev", true);
auto error_event_val = fval->GetFieldOrDefault("error_ev");
Func* error_event = error_event_val ? error_event_val->AsFunc() : nullptr;
if ( ! CheckErrorEventTypes(stream_name, error_event, false) )
@ -470,19 +473,19 @@ bool Manager::CreateTableStream(RecordVal* fval)
return false;
}
string stream_name = fval->Lookup("name", true)->AsString()->CheckString();
string stream_name = fval->GetFieldOrDefault("name")->AsString()->CheckString();
auto pred = fval->Lookup("pred", true);
auto idx_val = fval->Lookup("idx", true);
auto pred = fval->GetFieldOrDefault("pred");
auto idx_val = fval->GetFieldOrDefault("idx");
RecordType* idx = idx_val->AsType()->AsTypeType()->Type()->AsRecordType();
IntrusivePtr<RecordType> val;
auto val_val = fval->Lookup("val", true);
auto val_val = fval->GetFieldOrDefault("val");
if ( val_val )
val = {NewRef{}, val_val->AsType()->AsTypeType()->Type()->AsRecordType()};
auto dst = fval->Lookup("destination", true);
auto dst = fval->GetFieldOrDefault("destination");
// check if index fields match table description
int num = idx->NumFields();
@ -518,7 +521,7 @@ bool Manager::CreateTableStream(RecordVal* fval)
return false;
}
auto want_record = fval->Lookup("want_record", true);
auto want_record = fval->GetFieldOrDefault("want_record");
if ( val )
{
@ -551,7 +554,7 @@ bool Manager::CreateTableStream(RecordVal* fval)
}
}
auto event_val = fval->Lookup("ev", true);
auto event_val = fval->GetFieldOrDefault("ev");
Func* event = event_val ? event_val->AsFunc() : nullptr;
if ( event )
@ -624,7 +627,7 @@ bool Manager::CreateTableStream(RecordVal* fval)
assert(want_record->InternalInt() == 1 || want_record->InternalInt() == 0);
}
auto error_event_val = fval->Lookup("error_ev", true);
auto error_event_val = fval->GetFieldOrDefault("error_ev");
Func* error_event = error_event_val ? error_event_val->AsFunc() : nullptr;
if ( ! CheckErrorEventTypes(stream_name, error_event, true) )

View file

@ -237,7 +237,7 @@ bool Manager::CreateStream(EnumVal* id, RecordVal* sval)
return false;
}
RecordType* columns = sval->Lookup("columns")
RecordType* columns = sval->GetField("columns")
->AsType()->AsTypeType()->Type()->AsRecordType();
bool log_attr_present = false;
@ -264,7 +264,7 @@ bool Manager::CreateStream(EnumVal* id, RecordVal* sval)
return false;
}
auto event_val = sval->Lookup("ev");
const auto& event_val = sval->GetField("ev");
Func* event = event_val ? event_val->AsFunc() : nullptr;
if ( event )
@ -545,22 +545,22 @@ bool Manager::AddFilter(EnumVal* id, RecordVal* fval)
return false;
// Find the right writer type.
EnumVal* writer = fval->Lookup("writer", true)->AsEnumVal();
auto writer = fval->GetFieldOrDefault<EnumVal>("writer");
// Create a new Filter instance.
auto name = fval->Lookup("name", true);
auto pred = fval->Lookup("pred", true);
auto path_func = fval->Lookup("path_func", true);
auto log_local = fval->Lookup("log_local", true);
auto log_remote = fval->Lookup("log_remote", true);
auto interv = fval->Lookup("interv", true);
auto postprocessor = fval->Lookup("postprocessor", true);
auto config = fval->Lookup("config", true);
auto field_name_map = fval->Lookup("field_name_map", true);
auto scope_sep = fval->Lookup("scope_sep", true);
auto ext_prefix = fval->Lookup("ext_prefix", true);
auto ext_func = fval->Lookup("ext_func", true);
auto name = fval->GetFieldOrDefault("name");
auto pred = fval->GetFieldOrDefault("pred");
auto path_func = fval->GetFieldOrDefault("path_func");
auto log_local = fval->GetFieldOrDefault("log_local");
auto log_remote = fval->GetFieldOrDefault("log_remote");
auto interv = fval->GetFieldOrDefault("interv");
auto postprocessor = fval->GetFieldOrDefault("postprocessor");
auto config = fval->GetFieldOrDefault("config");
auto field_name_map = fval->GetFieldOrDefault("field_name_map");
auto scope_sep = fval->GetFieldOrDefault("scope_sep");
auto ext_prefix = fval->GetFieldOrDefault("ext_prefix");
auto ext_func = fval->GetFieldOrDefault("ext_func");
Filter* filter = new Filter;
filter->fval = fval->Ref();
@ -581,8 +581,8 @@ bool Manager::AddFilter(EnumVal* id, RecordVal* fval)
// Build the list of fields that the filter wants included, including
// potentially rolling out fields.
auto include = fval->Lookup("include");
auto exclude = fval->Lookup("exclude");
const auto& include = fval->GetField("include");
const auto& exclude = fval->GetField("exclude");
filter->num_ext_fields = 0;
if ( filter->ext_func )
@ -616,7 +616,7 @@ bool Manager::AddFilter(EnumVal* id, RecordVal* fval)
}
// Get the path for the filter.
auto path_val = fval->Lookup("path");
auto path_val = fval->GetField("path");
if ( path_val )
{

View file

@ -982,33 +982,33 @@ static BifEnum::Supervisor::ClusterRole role_str_to_enum(std::string_view r)
Supervisor::NodeConfig Supervisor::NodeConfig::FromRecord(const RecordVal* node)
{
Supervisor::NodeConfig rval;
rval.name = node->Lookup("name")->AsString()->CheckString();
auto iface_val = node->Lookup("interface");
rval.name = node->GetField("name")->AsString()->CheckString();
const auto& iface_val = node->GetField("interface");
if ( iface_val )
rval.interface = iface_val->AsString()->CheckString();
auto directory_val = node->Lookup("directory");
const auto& directory_val = node->GetField("directory");
if ( directory_val )
rval.directory = directory_val->AsString()->CheckString();
auto stdout_val = node->Lookup("stdout_file");
const auto& stdout_val = node->GetField("stdout_file");
if ( stdout_val )
rval.stdout_file = stdout_val->AsString()->CheckString();
auto stderr_val = node->Lookup("stderr_file");
const auto& stderr_val = node->GetField("stderr_file");
if ( stderr_val )
rval.stderr_file = stderr_val->AsString()->CheckString();
auto affinity_val = node->Lookup("cpu_affinity");
const auto& affinity_val = node->GetField("cpu_affinity");
if ( affinity_val )
rval.cpu_affinity = affinity_val->AsInt();
auto scripts_val = node->Lookup("scripts")->AsVectorVal();
auto scripts_val = node->GetField("scripts")->AsVectorVal();
for ( auto i = 0u; i < scripts_val->Size(); ++i )
{
@ -1016,7 +1016,7 @@ Supervisor::NodeConfig Supervisor::NodeConfig::FromRecord(const RecordVal* node)
rval.scripts.emplace_back(std::move(script));
}
auto cluster_table_val = node->Lookup("cluster")->AsTableVal();
auto cluster_table_val = node->GetField("cluster")->AsTableVal();
auto cluster_table = cluster_table_val->AsTable();
auto c = cluster_table->InitForIteration();
HashKey* k;
@ -1030,11 +1030,11 @@ Supervisor::NodeConfig Supervisor::NodeConfig::FromRecord(const RecordVal* node)
auto rv = v->GetVal()->AsRecordVal();
Supervisor::ClusterEndpoint ep;
ep.role = static_cast<BifEnum::Supervisor::ClusterRole>(rv->Lookup("role")->AsEnum());
ep.host = rv->Lookup("host")->AsAddr().AsString();
ep.port = rv->Lookup("p")->AsPortVal()->Port();
ep.role = static_cast<BifEnum::Supervisor::ClusterRole>(rv->GetField("role")->AsEnum());
ep.host = rv->GetField("host")->AsAddr().AsString();
ep.port = rv->GetField("p")->AsPortVal()->Port();
auto iface = rv->Lookup("interface");
const auto& iface = rv->GetField("interface");
if ( iface )
ep.interface = iface->AsStringVal()->ToStdString();