Skip to content

Commit e6dd8c4

Browse files
committed
Replace calls to LhsValue::get_nested by LhsValue::extract_nested
This will allow further experimention with making the execution context an interface to make it agnostic about how the underlying data is stored.
1 parent 124b16d commit e6dd8c4

2 files changed

Lines changed: 63 additions & 73 deletions

File tree

engine/src/ast/index_expr.rs

Lines changed: 63 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,6 @@ pub struct IndexExpr {
3131
pub indexes: Vec<FieldIndex>,
3232
}
3333

34-
#[allow(clippy::manual_ok_err)]
35-
#[inline]
36-
pub fn ok_ref<T, E>(result: &Result<T, E>) -> Option<&T> {
37-
match result {
38-
Ok(x) => Some(x),
39-
Err(_) => None,
40-
}
41-
}
42-
4334
impl ValueExpr for IndexExpr {
4435
#[inline]
4536
fn walk<'a, V: Visitor<'a>>(&'a self, visitor: &mut V) {
@@ -64,7 +55,7 @@ impl ValueExpr for IndexExpr {
6455
let map_each_count = self.map_each_count();
6556
let Self {
6657
identifier,
67-
indexes,
58+
mut indexes,
6859
} = self;
6960

7061
let last = match map_each_count {
@@ -86,20 +77,20 @@ impl ValueExpr for IndexExpr {
8677
IdentifierExpr::FunctionCallExpr(call) => compiler.compile_function_call_expr(call),
8778
}
8879
} else if let Some(last) = last {
80+
indexes.truncate(last);
8981
// Average path
9082
match identifier {
9183
IdentifierExpr::Field(f) => CompiledValueExpr::new(move |ctx| {
9284
ctx.get_field_value_unchecked(&f)
93-
.and_then(|value| value.get_nested(&indexes[..last]))
94-
.map(LhsValue::as_ref)
85+
.and_then(|value| value.as_ref().extract_nested(&indexes))
9586
.ok_or(ty)
9687
}),
9788
IdentifierExpr::FunctionCallExpr(call) => {
9889
let call = compiler.compile_function_call_expr(call);
9990
CompiledValueExpr::new(move |ctx| {
10091
call.execute(ctx)
10192
.ok()
102-
.and_then(|val| val.extract_nested(&indexes[..last]))
93+
.and_then(|val| val.extract_nested(&indexes))
10394
.ok_or(ty)
10495
})
10596
}
@@ -168,12 +159,13 @@ impl IndexExpr {
168159
})
169160
} else {
170161
CompiledOneExpr::new(move |ctx| {
171-
ok_ref(&call.execute(ctx))
172-
.and_then(|val| val.get_nested(&indexes))
162+
call.execute(ctx)
163+
.ok()
164+
.and_then(|val| val.extract_nested(&indexes))
173165
.map_or(
174166
default,
175167
#[inline]
176-
|val| comp.compare(val, ctx),
168+
|val| comp.compare(&val, ctx),
177169
)
178170
})
179171
}
@@ -188,11 +180,11 @@ impl IndexExpr {
188180
} else {
189181
CompiledOneExpr::new(move |ctx| {
190182
ctx.get_field_value_unchecked(&f)
191-
.and_then(|value| value.get_nested(&indexes))
183+
.and_then(|value| value.as_ref().extract_nested(&indexes))
192184
.map_or(
193185
default,
194186
#[inline]
195-
|val| comp.compare(val, ctx),
187+
|val| comp.compare(&val, ctx),
196188
)
197189
})
198190
}
@@ -213,35 +205,68 @@ impl IndexExpr {
213205
match identifier {
214206
IdentifierExpr::FunctionCallExpr(call) => {
215207
let call = compiler.compile_function_call_expr(call);
216-
CompiledVecExpr::new(move |ctx| {
217-
let comp = &comp;
218-
ok_ref(&call.execute(ctx))
219-
.and_then(|val| val.get_nested(&indexes))
220-
.map_or(
208+
if indexes.is_empty() {
209+
CompiledVecExpr::new(move |ctx| {
210+
let comp = &comp;
211+
call.execute(ctx).map_or(
221212
BOOL_ARRAY,
222213
#[inline]
223-
|val: &LhsValue<'_>| {
214+
|val: LhsValue<'_>| {
224215
TypedArray::from_iter(
225216
val.iter().unwrap().map(|item| comp.compare(item, ctx)),
226217
)
227218
},
228219
)
229-
})
220+
})
221+
} else {
222+
CompiledVecExpr::new(move |ctx| {
223+
let comp = &comp;
224+
call.execute(ctx)
225+
.ok()
226+
.and_then(|val| val.extract_nested(&indexes))
227+
.map_or(
228+
BOOL_ARRAY,
229+
#[inline]
230+
|val: LhsValue<'_>| {
231+
TypedArray::from_iter(
232+
val.iter().unwrap().map(|item| comp.compare(item, ctx)),
233+
)
234+
},
235+
)
236+
})
237+
}
230238
}
231-
IdentifierExpr::Field(f) => CompiledVecExpr::new(move |ctx| {
232-
let comp = &comp;
233-
ctx.get_field_value_unchecked(&f)
234-
.and_then(|value| value.get_nested(&indexes))
235-
.map_or(
236-
BOOL_ARRAY,
237-
#[inline]
238-
|val: &LhsValue<'_>| {
239-
TypedArray::from_iter(
240-
val.iter().unwrap().map(|item| comp.compare(item, ctx)),
239+
IdentifierExpr::Field(f) => {
240+
if indexes.is_empty() {
241+
CompiledVecExpr::new(move |ctx| {
242+
let comp = &comp;
243+
ctx.get_field_value_unchecked(&f).map_or(
244+
BOOL_ARRAY,
245+
#[inline]
246+
|val: &LhsValue<'_>| {
247+
TypedArray::from_iter(
248+
val.iter().unwrap().map(|item| comp.compare(item, ctx)),
249+
)
250+
},
251+
)
252+
})
253+
} else {
254+
CompiledVecExpr::new(move |ctx| {
255+
let comp = &comp;
256+
ctx.get_field_value_unchecked(&f)
257+
.and_then(|value| value.as_ref().extract_nested(&indexes))
258+
.map_or(
259+
BOOL_ARRAY,
260+
#[inline]
261+
|val: LhsValue<'_>| {
262+
TypedArray::from_iter(
263+
val.iter().unwrap().map(|item| comp.compare(item, ctx)),
264+
)
265+
},
241266
)
242-
},
243-
)
244-
}),
267+
})
268+
}
269+
}
245270
}
246271
}
247272

engine/src/types.rs

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -701,41 +701,6 @@ impl<'a> LhsValue<'a> {
701701
}
702702
}
703703

704-
/// Retrieve an element from an LhsValue given a path item and a specified
705-
/// type.
706-
/// Returns a TypeMismatchError error if current type does not support it
707-
/// nested element.
708-
///
709-
/// Both LhsValue::Array and LhsValue::Map support nested elements.
710-
pub(crate) fn get(
711-
&'a self,
712-
item: &FieldIndex,
713-
) -> Result<Option<&'a LhsValue<'a>>, IndexAccessError> {
714-
match (self, item) {
715-
(LhsValue::Array(arr), FieldIndex::ArrayIndex(idx)) => Ok(arr.get(*idx as usize)),
716-
(_, FieldIndex::ArrayIndex(_)) => Err(IndexAccessError {
717-
index: item.clone(),
718-
actual: self.get_type(),
719-
}),
720-
(LhsValue::Map(map), FieldIndex::MapKey(key)) => Ok(map.get(key.as_bytes())),
721-
(_, FieldIndex::MapKey(_)) => Err(IndexAccessError {
722-
index: item.clone(),
723-
actual: self.get_type(),
724-
}),
725-
(_, FieldIndex::MapEach) => Err(IndexAccessError {
726-
index: item.clone(),
727-
actual: self.get_type(),
728-
}),
729-
}
730-
}
731-
732-
#[inline]
733-
pub(crate) fn get_nested(&'a self, indexes: &[FieldIndex]) -> Option<&'a LhsValue<'a>> {
734-
indexes
735-
.iter()
736-
.try_fold(self, |value, idx| value.get(idx).unwrap())
737-
}
738-
739704
pub(crate) fn extract(
740705
self,
741706
item: &FieldIndex,

0 commit comments

Comments
 (0)