Skip to content

allow missing (optional) fields #100

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion engine/src/ast/field_expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,10 @@ impl<'s> IdentifierExpr<'s> {
) -> CompiledValueExpr<'s, C::U> {
match self {
IdentifierExpr::Field(f) => {
CompiledValueExpr::new(move |ctx| Ok(ctx.get_field_value_unchecked(f).as_ref()))
CompiledValueExpr::new(move |ctx| match ctx.get_field_value(f) {
Some(value) => Ok(value.as_ref()),
None => Err(f.get_type()),
})
}
IdentifierExpr::FunctionCallExpr(call) => compiler.compile_function_call_expr(call),
}
Expand Down
31 changes: 17 additions & 14 deletions engine/src/ast/index_expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,10 @@ impl<'s> ValueExpr<'s> for IndexExpr<'s> {
// Average path
match identifier {
IdentifierExpr::Field(f) => CompiledValueExpr::new(move |ctx| {
let init_value = ctx.get_field_value(f).ok_or(ty)?;
indexes[..last]
.iter()
.try_fold(ctx.get_field_value_unchecked(f), |value, index| {
value.get(index).unwrap()
})
.try_fold(init_value, |value, index| value.get(index).unwrap())
.map(LhsValue::as_ref)
.ok_or(ty)
}),
Expand All @@ -130,7 +129,11 @@ impl<'s> ValueExpr<'s> for IndexExpr<'s> {
match identifier {
IdentifierExpr::Field(f) => CompiledValueExpr::new(move |ctx| {
let mut iter = MapEachIterator::from_indexes(&indexes[..]);
iter.reset(ctx.get_field_value_unchecked(f).as_ref());
let value = match ctx.get_field_value(f) {
Some(value) => value,
None => return Err(f.get_type()),
};
iter.reset(value.as_ref());
Ok(LhsValue::Array(Array::try_from_iter(ty, iter).unwrap()))
}),
IdentifierExpr::FunctionCallExpr(call) => {
Expand Down Expand Up @@ -189,16 +192,13 @@ impl<'s> IndexExpr<'s> {
}
IdentifierExpr::Field(f) => {
if indexes.is_empty() {
CompiledOneExpr::new(move |ctx| func(ctx.get_field_value_unchecked(f), ctx))
CompiledOneExpr::new(move |ctx| match ctx.get_field_value(f) {
Some(value) => func(value, ctx),
None => false,
})
} else {
CompiledOneExpr::new(move |ctx| {
index_access_one!(
indexes,
Some(ctx.get_field_value_unchecked(f)),
default,
ctx,
func
)
index_access_one!(indexes, ctx.get_field_value(f), default, ctx, func)
})
}
}
Expand Down Expand Up @@ -226,7 +226,7 @@ impl<'s> IndexExpr<'s> {
})
}
IdentifierExpr::Field(f) => CompiledVecExpr::new(move |ctx| {
index_access_vec!(indexes, Some(ctx.get_field_value_unchecked(f)), ctx, func)
index_access_vec!(indexes, ctx.get_field_value(f), ctx, func)
}),
}
}
Expand All @@ -246,7 +246,10 @@ impl<'s> IndexExpr<'s> {
match identifier {
IdentifierExpr::Field(f) => CompiledVecExpr::new(move |ctx| {
let mut iter = MapEachIterator::from_indexes(&indexes[..]);
iter.reset(ctx.get_field_value_unchecked(f).as_ref());
let Some(value) = ctx.get_field_value(f) else {
return TypedArray::default();
};
iter.reset(value.as_ref());
TypedArray::from_iter(iter.map(|item| func(&item, ctx)))
}),
IdentifierExpr::FunctionCallExpr(call) => {
Expand Down
18 changes: 0 additions & 18 deletions engine/src/execution_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,24 +99,6 @@ impl<'e, U> ExecutionContext<'e, U> {
}
}

#[inline]
pub(crate) fn get_field_value_unchecked(&self, field: Field<'_>) -> &LhsValue<'_> {
// This is safe because this code is reachable only from Filter::execute
// which already performs the scheme compatibility check, but check that
// invariant holds in the future at least in the debug mode.
debug_assert!(self.scheme() == field.scheme());

// For now we panic in this, but later we are going to align behaviour
// with wireshark: resolve all subexpressions that don't have RHS value
// to `false`.
self.values[field.index()].as_ref().unwrap_or_else(|| {
panic!(
"Field {} was registered but not given a value",
field.name()
);
})
}

/// Get the value of a field.
pub fn get_field_value(&self, field: Field<'_>) -> Option<&LhsValue<'_>> {
assert!(self.scheme() == field.scheme());
Expand Down