146 lines
4.8 KiB
Rust
Raw Normal View History

2020-11-10 17:30:49 +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 crate::contexts::execution::AValue;
use crate::contexts::execution::ExecutionCtx;
use crate::execution::boxed_value::JValuable;
use crate::execution::ExecutionError;
use crate::execution::ExecutionResult;
2020-11-10 17:30:49 +03:00
use crate::JValue;
2020-12-22 21:05:04 +03:00
use crate::SecurityTetraplet;
2020-11-10 17:30:49 +03:00
2021-02-11 15:39:37 +03:00
use air_parser::ast::CallInstrArgValue;
2020-11-10 17:30:49 +03:00
2020-12-22 21:05:04 +03:00
/// Resolve value to called function arguments.
pub(crate) fn resolve_to_args<'i>(
2021-02-11 15:39:37 +03:00
value: &CallInstrArgValue<'i>,
2020-12-22 21:05:04 +03:00
ctx: &ExecutionCtx<'i>,
2021-01-15 00:38:58 +03:00
) -> ExecutionResult<(JValue, Vec<SecurityTetraplet>)> {
2021-02-11 15:39:37 +03:00
match value {
2021-02-16 09:45:33 +03:00
CallInstrArgValue::InitPeerId => prepare_consts(ctx.init_peer_id.clone(), ctx),
2021-02-11 15:39:37 +03:00
CallInstrArgValue::LastError => prepare_last_error(ctx),
2021-02-16 09:45:33 +03:00
CallInstrArgValue::Literal(value) => prepare_consts(value.to_string(), ctx),
CallInstrArgValue::Boolean(value) => prepare_consts(*value, ctx),
CallInstrArgValue::Number(value) => prepare_consts(value, ctx),
CallInstrArgValue::Variable(variable) => {
let name = get_variable_name(variable);
prepare_variable(name, ctx)
}
2021-02-18 17:30:14 +03:00
CallInstrArgValue::JsonPath {
variable,
path,
should_flatten,
} => {
let name = get_variable_name(variable);
prepare_json_path(name, path, *should_flatten, ctx)
}
2020-12-22 21:05:04 +03:00
}
2021-02-11 15:39:37 +03:00
}
2020-12-22 21:05:04 +03:00
#[allow(clippy::unnecessary_wraps)]
fn prepare_consts(arg: impl Into<JValue>, ctx: &ExecutionCtx<'_>) -> ExecutionResult<(JValue, Vec<SecurityTetraplet>)> {
2021-02-16 09:45:33 +03:00
let jvalue = arg.into();
2021-02-11 15:39:37 +03:00
let tetraplet = SecurityTetraplet::literal_tetraplet(ctx.init_peer_id.clone());
Ok((jvalue, vec![tetraplet]))
}
#[allow(clippy::unnecessary_wraps)]
fn prepare_last_error(ctx: &ExecutionCtx<'_>) -> ExecutionResult<(JValue, Vec<SecurityTetraplet>)> {
2021-02-11 15:39:37 +03:00
let result = match &ctx.last_error {
Some(error) => {
let serialized_error = error.serialize();
let jvalue = JValue::String(serialized_error);
let tetraplets = error
.tetraplet
.clone()
.unwrap_or_else(|| SecurityTetraplet::literal_tetraplet(ctx.init_peer_id.clone()));
(jvalue, vec![tetraplets])
2020-11-10 17:30:49 +03:00
}
2021-02-11 15:39:37 +03:00
None => {
let jvalue = JValue::String(String::new());
let tetraplets = vec![];
2020-11-10 17:30:49 +03:00
2021-02-11 15:39:37 +03:00
(jvalue, tetraplets)
2020-12-22 21:05:04 +03:00
}
2021-02-11 15:39:37 +03:00
};
Ok(result)
}
fn prepare_variable<'i>(name: &str, ctx: &ExecutionCtx<'i>) -> ExecutionResult<(JValue, Vec<SecurityTetraplet>)> {
let resolved = resolve_to_jvaluable(name, ctx)?;
let tetraplets = resolved.as_tetraplets();
let jvalue = resolved.into_jvalue();
Ok((jvalue, tetraplets))
}
fn prepare_json_path<'i>(
name: &str,
json_path: &str,
2021-02-18 17:30:14 +03:00
should_flatten: bool,
2021-02-11 15:39:37 +03:00
ctx: &ExecutionCtx<'i>,
) -> ExecutionResult<(JValue, Vec<SecurityTetraplet>)> {
let resolved = resolve_to_jvaluable(name, ctx)?;
let (jvalue, tetraplets) = resolved.apply_json_path_with_tetraplets(json_path)?;
2021-02-18 17:30:14 +03:00
let jvalue = if should_flatten {
if jvalue.len() != 1 {
let jvalue = jvalue.into_iter().cloned().collect::<Vec<_>>();
return crate::exec_err!(ExecutionError::FlatteningError(JValue::Array(jvalue)));
}
jvalue[0].clone()
} else {
let jvalue = jvalue.into_iter().cloned().collect::<Vec<_>>();
JValue::Array(jvalue)
};
2021-02-11 15:39:37 +03:00
Ok((jvalue, tetraplets))
2020-11-10 17:30:49 +03:00
}
2020-12-22 21:05:04 +03:00
/// Constructs jvaluable result from `ExecutionCtx::data_cache` by name.
pub(crate) fn resolve_to_jvaluable<'name, 'i, 'ctx>(
name: &'name str,
ctx: &'ctx ExecutionCtx<'i>,
2021-01-15 00:38:58 +03:00
) -> ExecutionResult<Box<dyn JValuable + 'ctx>> {
use ExecutionError::VariableNotFound;
2020-11-10 17:30:49 +03:00
let value = ctx
.data_cache
2020-12-22 21:05:04 +03:00
.get(name)
.ok_or_else(|| VariableNotFound(name.to_string()))?;
2020-11-10 17:30:49 +03:00
match value {
2020-12-22 21:05:04 +03:00
AValue::JValueRef(value) => Ok(Box::new(value.clone())),
AValue::JValueStreamRef(stream) => Ok(Box::new(stream.borrow())),
2020-11-10 17:30:49 +03:00
AValue::JValueFoldCursor(fold_state) => {
2020-12-22 21:05:04 +03:00
let peeked_value = fold_state.iterable.peek().unwrap();
Ok(Box::new(peeked_value))
2020-11-10 17:30:49 +03:00
}
}
}
use air_parser::ast::Variable;
pub(crate) fn get_variable_name<'a>(variable: &'a Variable<'_>) -> &'a str {
match variable {
Variable::Scalar(name) => name,
Variable::Stream(name) => name,
}
}