mirror of
https://github.com/zeek/zeek.git
synced 2025-10-02 14:48:21 +00:00
Fix conversion with record types
This commit is contained in:
parent
af9e852c28
commit
fe9926e538
3 changed files with 47 additions and 9 deletions
24
src/Val.cc
24
src/Val.cc
|
@ -4268,17 +4268,23 @@ ValPtr cast_value_to_type(Val* v, Type* t)
|
||||||
auto set_type = v->GetType<SetType>();
|
auto set_type = v->GetType<SetType>();
|
||||||
auto indices = set_type->GetIndices();
|
auto indices = set_type->GetIndices();
|
||||||
|
|
||||||
if ( indices->GetTypes().size() > 1 || ! indices->IsPure() )
|
if ( indices->GetTypes().size() > 1 )
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
|
||||||
auto ret_type = IntrusivePtr<VectorType>{NewRef{}, t->AsVectorType()};
|
auto ret_type = IntrusivePtr<VectorType>{NewRef{}, t->AsVectorType()};
|
||||||
auto ret = make_intrusive<VectorVal>(ret_type);
|
auto ret = make_intrusive<VectorVal>(ret_type);
|
||||||
|
|
||||||
auto lv = v->AsTableVal()->ToPureListVal();
|
auto* table = v->AsTable();
|
||||||
auto n = lv->Length();
|
auto* tval = v->AsTableVal();
|
||||||
|
int index = 0;
|
||||||
for ( int i = 0; i < n; ++i )
|
for ( const auto& te : *table )
|
||||||
ret->Assign(i, lv->Idx(i));
|
{
|
||||||
|
auto k = te.GetHashKey();
|
||||||
|
auto lv = tval->RecreateIndex(*k);
|
||||||
|
ValPtr entry_key = lv->Length() == 1 ? lv->Idx(0) : lv;
|
||||||
|
ret->Assign(index, entry_key);
|
||||||
|
index++;
|
||||||
|
}
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
@ -4320,11 +4326,11 @@ static bool can_cast_set_and_vector(const Type* t1, const Type* t2)
|
||||||
|
|
||||||
if ( st && vt )
|
if ( st && vt )
|
||||||
{
|
{
|
||||||
auto set_indices = st->GetIndices();
|
auto set_indices = st->GetIndices()->GetTypes();
|
||||||
if ( set_indices->GetTypes().size() > 1 || ! set_indices->IsPure() )
|
if ( set_indices.size() > 1 )
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
return same_type(set_indices->GetPureType(), vt->Yield());
|
return same_type(set_indices[0], vt->Yield());
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
|
|
|
@ -23,3 +23,17 @@ set to vector (port)
|
||||||
21/tcp
|
21/tcp
|
||||||
}
|
}
|
||||||
[23/tcp, 21/tcp]
|
[23/tcp, 21/tcp]
|
||||||
|
|
||||||
|
set to vector (record)
|
||||||
|
{
|
||||||
|
[a=a, b=b],
|
||||||
|
[a=a1, b=b1]
|
||||||
|
}
|
||||||
|
{
|
||||||
|
[a=a, b=b],
|
||||||
|
[a=a1, b=b1]
|
||||||
|
}
|
||||||
|
|
||||||
|
vector to set (record)
|
||||||
|
[[a=a, b=b], [a=a1, b=b1]]
|
||||||
|
[[a=a, b=b], [a=a1, b=b1]]
|
||||||
|
|
|
@ -3,6 +3,11 @@
|
||||||
# @TEST-EXEC: btest-diff out
|
# @TEST-EXEC: btest-diff out
|
||||||
# @TEST-EXEC: btest-diff .stderr
|
# @TEST-EXEC: btest-diff .stderr
|
||||||
|
|
||||||
|
type r: record {
|
||||||
|
a: string;
|
||||||
|
b: string;
|
||||||
|
};
|
||||||
|
|
||||||
print("vector to set");
|
print("vector to set");
|
||||||
local v1 = vector(1, 1, 1, 2, 2, 3, 3, 4);
|
local v1 = vector(1, 1, 1, 2, 2, 3, 3, 4);
|
||||||
local s1 = v1 as set[count];
|
local s1 = v1 as set[count];
|
||||||
|
@ -22,3 +27,16 @@ local s3 = set(21/tcp, 23/tcp);
|
||||||
local v3 = s3 as vector of port;
|
local v3 = s3 as vector of port;
|
||||||
print(s3);
|
print(s3);
|
||||||
print(v3);
|
print(v3);
|
||||||
|
|
||||||
|
local s: set[r] = set([$a="a", $b="b"], [$a="a1", $b="b1"]);
|
||||||
|
local v: vector of r = vector([$a="a", $b="b"], [$a="a1", $b="b1"]);
|
||||||
|
|
||||||
|
print("");
|
||||||
|
print("set to vector (record)");
|
||||||
|
print s;
|
||||||
|
print v as set[r];
|
||||||
|
|
||||||
|
print("");
|
||||||
|
print("vector to set (record)");
|
||||||
|
print v;
|
||||||
|
print s as vector of r;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue