2020-12-22 21:05:04 +03:00
|
|
|
/*
|
|
|
|
* Copyright 2020 Fluence Labs Limited
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2021-01-15 00:38:58 +03:00
|
|
|
use super::*;
|
2021-02-11 15:39:37 +03:00
|
|
|
use crate::exec_err;
|
2020-12-22 21:05:04 +03:00
|
|
|
use crate::JValue;
|
2020-12-25 16:20:25 +03:00
|
|
|
use crate::ResolvedTriplet;
|
2020-12-22 21:05:04 +03:00
|
|
|
use crate::SecurityTetraplet;
|
|
|
|
|
2021-01-22 18:54:05 +03:00
|
|
|
use air_parser::ast;
|
2020-12-25 16:20:25 +03:00
|
|
|
use jsonpath_lib::select;
|
|
|
|
use jsonpath_lib::select_with_iter;
|
2020-12-22 21:05:04 +03:00
|
|
|
|
|
|
|
use std::ops::Deref;
|
2020-12-25 16:20:25 +03:00
|
|
|
use std::rc::Rc;
|
2020-12-22 21:05:04 +03:00
|
|
|
|
2020-12-25 16:20:25 +03:00
|
|
|
pub(super) type IterableValue = Box<dyn for<'ctx> Iterable<'ctx, Item = IterableItem<'ctx>>>;
|
2020-12-22 21:05:04 +03:00
|
|
|
|
|
|
|
/// Constructs iterable value for given instruction value,
|
|
|
|
/// return Some if iterable isn't empty and None otherwise.
|
|
|
|
pub(super) fn construct_iterable_value<'ctx>(
|
2021-01-22 18:54:05 +03:00
|
|
|
ast_iterable: &ast::IterableValue<'ctx>,
|
2020-12-22 21:05:04 +03:00
|
|
|
exec_ctx: &ExecutionCtx<'ctx>,
|
2021-01-15 00:38:58 +03:00
|
|
|
) -> ExecutionResult<Option<IterableValue>> {
|
2021-01-22 18:54:05 +03:00
|
|
|
match ast_iterable {
|
|
|
|
ast::IterableValue::Variable(name) => handle_instruction_variable(exec_ctx, name),
|
2021-02-18 17:30:14 +03:00
|
|
|
ast::IterableValue::JsonPath {
|
|
|
|
variable,
|
|
|
|
path,
|
|
|
|
should_flatten,
|
|
|
|
} => handle_instruction_json_path(exec_ctx, variable, path, *should_flatten),
|
2020-12-22 21:05:04 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn handle_instruction_variable<'ctx>(
|
|
|
|
exec_ctx: &ExecutionCtx<'ctx>,
|
|
|
|
variable_name: &str,
|
2021-01-15 00:38:58 +03:00
|
|
|
) -> ExecutionResult<Option<IterableValue>> {
|
2020-12-25 16:20:25 +03:00
|
|
|
let iterable: Option<IterableValue> = match exec_ctx.data_cache.get(variable_name) {
|
|
|
|
Some(AValue::JValueRef(call_result)) => from_call_result(call_result.clone())?,
|
2020-12-22 21:05:04 +03:00
|
|
|
Some(AValue::JValueAccumulatorRef(acc)) => {
|
|
|
|
let acc = acc.borrow();
|
|
|
|
if acc.is_empty() {
|
|
|
|
return Ok(None);
|
|
|
|
}
|
|
|
|
|
|
|
|
let call_results = acc.iter().cloned().collect::<Vec<_>>();
|
|
|
|
let foldable = IterableVecResolvedCall::init(call_results);
|
2020-12-25 16:20:25 +03:00
|
|
|
Some(Box::new(foldable))
|
2020-12-22 21:05:04 +03:00
|
|
|
}
|
2020-12-25 16:20:25 +03:00
|
|
|
Some(AValue::JValueFoldCursor(fold_state)) => {
|
|
|
|
let iterable_value = fold_state.iterable.peek().unwrap();
|
|
|
|
let jvalue = iterable_value.as_jvalue();
|
|
|
|
let result = Rc::new(jvalue.into_owned());
|
|
|
|
let triplet = as_triplet(&iterable_value);
|
|
|
|
|
|
|
|
let call_result = ResolvedCallResult { result, triplet };
|
|
|
|
from_call_result(call_result)?
|
2020-12-22 21:05:04 +03:00
|
|
|
}
|
2021-02-11 15:39:37 +03:00
|
|
|
_ => return exec_err!(ExecutionError::VariableNotFound(variable_name.to_string())),
|
2020-12-22 21:05:04 +03:00
|
|
|
};
|
|
|
|
|
2020-12-25 16:20:25 +03:00
|
|
|
Ok(iterable)
|
2020-12-22 21:05:04 +03:00
|
|
|
}
|
|
|
|
|
2020-12-25 16:20:25 +03:00
|
|
|
/// Constructs iterable value from resolved call result.
|
2021-01-15 00:38:58 +03:00
|
|
|
fn from_call_result(call_result: ResolvedCallResult) -> ExecutionResult<Option<IterableValue>> {
|
|
|
|
use ExecutionError::IncompatibleJValueType;
|
2020-12-22 21:05:04 +03:00
|
|
|
|
2020-12-25 16:20:25 +03:00
|
|
|
let len = match &call_result.result.deref() {
|
|
|
|
JValue::Array(array) => {
|
|
|
|
if array.is_empty() {
|
|
|
|
// skip fold if array is empty
|
2020-12-22 21:05:04 +03:00
|
|
|
return Ok(None);
|
|
|
|
}
|
2020-12-25 16:20:25 +03:00
|
|
|
array.len()
|
|
|
|
}
|
2021-02-11 15:39:37 +03:00
|
|
|
v => return exec_err!(IncompatibleJValueType((*v).clone(), "array")),
|
2020-12-25 16:20:25 +03:00
|
|
|
};
|
2020-12-22 21:05:04 +03:00
|
|
|
|
2020-12-25 16:20:25 +03:00
|
|
|
let foldable = IterableResolvedCall::init(call_result, len);
|
|
|
|
let foldable = Box::new(foldable);
|
2020-12-22 21:05:04 +03:00
|
|
|
|
2020-12-25 16:20:25 +03:00
|
|
|
Ok(Some(foldable))
|
|
|
|
}
|
2020-12-22 21:05:04 +03:00
|
|
|
|
2020-12-25 16:20:25 +03:00
|
|
|
fn handle_instruction_json_path<'ctx>(
|
|
|
|
exec_ctx: &ExecutionCtx<'ctx>,
|
|
|
|
variable_name: &str,
|
|
|
|
json_path: &str,
|
2021-02-18 17:30:14 +03:00
|
|
|
should_flatten: bool,
|
2021-01-15 00:38:58 +03:00
|
|
|
) -> ExecutionResult<Option<IterableValue>> {
|
|
|
|
use ExecutionError::JValueAccJsonPathError;
|
2020-12-25 16:20:25 +03:00
|
|
|
|
2021-02-18 17:30:14 +03:00
|
|
|
match exec_ctx.data_cache.get(variable_name) {
|
2020-12-25 16:20:25 +03:00
|
|
|
Some(AValue::JValueRef(variable)) => {
|
|
|
|
let jvalues = apply_json_path(&variable.result, json_path)?;
|
2021-02-18 17:30:14 +03:00
|
|
|
from_jvalues(jvalues, variable.triplet.clone(), json_path, should_flatten)
|
2020-12-22 21:05:04 +03:00
|
|
|
}
|
|
|
|
Some(AValue::JValueAccumulatorRef(acc)) => {
|
|
|
|
let acc = acc.borrow();
|
|
|
|
if acc.is_empty() {
|
|
|
|
return Ok(None);
|
|
|
|
}
|
|
|
|
|
2020-12-23 19:26:03 +03:00
|
|
|
let acc_iter = acc.iter().map(|v| v.result.deref());
|
2020-12-25 16:20:25 +03:00
|
|
|
let (jvalues, tetraplet_indices) = select_with_iter(acc_iter, &json_path)
|
|
|
|
.map_err(|e| JValueAccJsonPathError(acc.clone(), json_path.to_string(), e))?;
|
2020-12-23 19:26:03 +03:00
|
|
|
|
2021-02-18 17:30:14 +03:00
|
|
|
let jvalues = construct_iterable_jvalues(jvalues, should_flatten)?;
|
2020-12-22 21:05:04 +03:00
|
|
|
let tetraplets = tetraplet_indices
|
2020-12-23 19:26:03 +03:00
|
|
|
.into_iter()
|
|
|
|
.map(|id| SecurityTetraplet {
|
|
|
|
triplet: acc[id].triplet.clone(),
|
2020-12-25 16:20:25 +03:00
|
|
|
json_path: json_path.to_string(),
|
2020-12-22 21:05:04 +03:00
|
|
|
})
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
|
|
|
let foldable = IterableVecJsonPathResult::init(jvalues, tetraplets);
|
2021-02-18 17:30:14 +03:00
|
|
|
Ok(Some(Box::new(foldable)))
|
2020-12-22 21:05:04 +03:00
|
|
|
}
|
2020-12-25 16:20:25 +03:00
|
|
|
Some(AValue::JValueFoldCursor(fold_state)) => {
|
|
|
|
let iterable_value = fold_state.iterable.peek().unwrap();
|
|
|
|
let jvalues = iterable_value.apply_json_path(json_path)?;
|
|
|
|
let triplet = as_triplet(&iterable_value);
|
|
|
|
|
2021-02-18 17:30:14 +03:00
|
|
|
from_jvalues(jvalues, triplet, json_path, should_flatten)
|
2020-12-22 21:05:04 +03:00
|
|
|
}
|
2021-02-11 15:39:37 +03:00
|
|
|
_ => return exec_err!(ExecutionError::VariableNotFound(variable_name.to_string())),
|
2021-02-18 17:30:14 +03:00
|
|
|
}
|
2020-12-25 16:20:25 +03:00
|
|
|
}
|
|
|
|
|
2021-01-15 00:38:58 +03:00
|
|
|
fn apply_json_path<'jvalue, 'str>(
|
|
|
|
jvalue: &'jvalue JValue,
|
|
|
|
json_path: &'str str,
|
|
|
|
) -> ExecutionResult<Vec<&'jvalue JValue>> {
|
|
|
|
use ExecutionError::JValueJsonPathError;
|
2020-12-25 16:20:25 +03:00
|
|
|
|
2021-02-11 15:39:37 +03:00
|
|
|
select(jvalue, json_path).map_err(|e| Rc::new(JValueJsonPathError(jvalue.clone(), json_path.to_string(), e)))
|
2020-12-25 16:20:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Applies json_path to provided jvalues and construct IterableValue from the result and given triplet.
|
2021-02-18 17:30:14 +03:00
|
|
|
fn from_jvalues(
|
|
|
|
jvalues: Vec<&JValue>,
|
|
|
|
triplet: Rc<ResolvedTriplet>,
|
|
|
|
json_path: &str,
|
|
|
|
should_flatten: bool,
|
|
|
|
) -> ExecutionResult<Option<IterableValue>> {
|
2020-12-25 16:20:25 +03:00
|
|
|
if jvalues.is_empty() {
|
2021-02-18 17:30:14 +03:00
|
|
|
return Ok(None);
|
2020-12-25 16:20:25 +03:00
|
|
|
}
|
|
|
|
|
2021-02-18 17:30:14 +03:00
|
|
|
let jvalues = construct_iterable_jvalues(jvalues, should_flatten)?;
|
2020-12-25 16:20:25 +03:00
|
|
|
|
|
|
|
let tetraplet = SecurityTetraplet {
|
|
|
|
triplet,
|
|
|
|
json_path: json_path.to_string(),
|
|
|
|
};
|
|
|
|
|
|
|
|
let foldable = IterableJsonPathResult::init(jvalues, tetraplet);
|
2021-02-18 17:30:14 +03:00
|
|
|
Ok(Some(Box::new(foldable)))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn construct_iterable_jvalues(jvalues: Vec<&JValue>, should_flatten: bool) -> ExecutionResult<Vec<JValue>> {
|
|
|
|
if !should_flatten {
|
|
|
|
let jvalues = jvalues.into_iter().cloned().collect();
|
|
|
|
return Ok(jvalues);
|
|
|
|
}
|
|
|
|
|
|
|
|
if jvalues.len() != 1 {
|
|
|
|
let jvalues = jvalues.into_iter().cloned().collect();
|
|
|
|
let jvalue = JValue::Array(jvalues);
|
|
|
|
return exec_err!(ExecutionError::FlatteningError(jvalue));
|
|
|
|
}
|
|
|
|
|
|
|
|
match jvalues[0] {
|
2021-02-25 16:35:44 +03:00
|
|
|
JValue::Array(values) => Ok(values.clone()),
|
2021-02-18 17:30:14 +03:00
|
|
|
_ => {
|
|
|
|
let jvalues = jvalues.into_iter().cloned().collect();
|
|
|
|
let jvalue = JValue::Array(jvalues);
|
|
|
|
exec_err!(ExecutionError::FlatteningError(jvalue))
|
|
|
|
}
|
|
|
|
}
|
2020-12-25 16:20:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn as_triplet(iterable: &IterableItem<'_>) -> Rc<ResolvedTriplet> {
|
|
|
|
use IterableItem::*;
|
|
|
|
|
|
|
|
let tetraplet = match iterable {
|
|
|
|
RefRef((_, tetraplet)) => tetraplet,
|
|
|
|
RefValue((_, tetraplet)) => tetraplet,
|
|
|
|
RcValue((_, tetraplet)) => tetraplet,
|
2020-12-22 21:05:04 +03:00
|
|
|
};
|
|
|
|
|
2020-12-25 16:20:25 +03:00
|
|
|
// clone is cheap here, because triplet is under Rc
|
|
|
|
Rc::clone(&tetraplet.triplet)
|
2020-12-22 21:05:04 +03:00
|
|
|
}
|