mirror of
https://github.com/fluencelabs/aquavm
synced 2025-06-28 06:01:33 +00:00
Make iterator in fold resolvable (#23)
This commit is contained in:
@ -24,9 +24,9 @@ use crate::AquamarineError;
|
||||
use crate::JValue;
|
||||
use crate::Result;
|
||||
|
||||
use air_parser::ast::{CallOutput, Value};
|
||||
use air_parser::ast::CallOutput;
|
||||
|
||||
use std::{borrow::Cow, cell::RefCell, rc::Rc};
|
||||
use std::{cell::RefCell, rc::Rc};
|
||||
|
||||
/// Writes result of a local `Call` instruction to `ExecutionCtx` at `output`
|
||||
pub(super) fn set_local_call_result<'i>(
|
||||
@ -79,97 +79,3 @@ pub(super) fn set_remote_call_result<'i>(
|
||||
);
|
||||
call_ctx.new_path.push_back(new_evidence_state);
|
||||
}
|
||||
|
||||
/// Applies `json_path` to `jvalue`
|
||||
pub(super) fn find_by_json_path<'jvalue, 'json_path>(
|
||||
jvalue: &'jvalue JValue,
|
||||
json_path: &'json_path str,
|
||||
) -> Result<Vec<&'jvalue JValue>> {
|
||||
use AquamarineError::VariableNotInJsonPath as JsonPathError;
|
||||
|
||||
jsonpath_lib::select(jvalue, json_path).map_err(|e| JsonPathError(jvalue.clone(), String::from(json_path), e))
|
||||
}
|
||||
|
||||
/// Takes variable's value from `ExecutionCtx::data_cache`
|
||||
/// TODO: maybe return &'i JValue?
|
||||
pub(super) fn resolve_variable<'exec_ctx, 'i>(variable: &'i str, ctx: &'exec_ctx ExecutionCtx<'i>) -> Result<JValue> {
|
||||
use AquamarineError::VariableNotFound;
|
||||
|
||||
let value = ctx
|
||||
.data_cache
|
||||
.get(variable)
|
||||
.ok_or_else(|| VariableNotFound(variable.to_string()))?;
|
||||
|
||||
match value {
|
||||
AValue::JValueFoldCursor(fold_state) => {
|
||||
if let JValue::Array(array) = fold_state.iterable.as_ref() {
|
||||
Ok(array[fold_state.cursor].clone())
|
||||
} else {
|
||||
unreachable!("fold state must be well-formed because it is changed only by stepper")
|
||||
}
|
||||
}
|
||||
AValue::JValueRef(value) => Ok(value.as_ref().clone()),
|
||||
AValue::JValueAccumulatorRef(acc) => {
|
||||
let owned_acc = acc.borrow().iter().map(|v| v.as_ref()).cloned().collect::<Vec<_>>();
|
||||
Ok(JValue::Array(owned_acc))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn apply_json_path<'i>(jvalue: JValue, json_path: &'i str) -> Result<JValue> {
|
||||
let values = find_by_json_path(&jvalue, json_path)?;
|
||||
if values.is_empty() {
|
||||
return Err(AquamarineError::VariableNotFound(json_path.to_string()));
|
||||
}
|
||||
|
||||
if values.len() != 1 {
|
||||
return Err(AquamarineError::MultipleValuesInJsonPath(json_path.to_string()));
|
||||
}
|
||||
|
||||
// TODO: sure need this clone?
|
||||
Ok(values[0].clone())
|
||||
}
|
||||
|
||||
pub(super) fn require_string(value: JValue) -> Result<String> {
|
||||
if let JValue::String(s) = value {
|
||||
Ok(s)
|
||||
} else {
|
||||
Err(AquamarineError::IncompatibleJValueType(value, "string".to_string()))
|
||||
}
|
||||
}
|
||||
|
||||
/// Resolve value to string by either resolving variable from `ExecutionCtx`, taking literal value, or etc
|
||||
pub(super) fn resolve_value<'i, 'a: 'i>(value: &'a Value<'i>, ctx: &'a ExecutionCtx<'i>) -> Result<Cow<'i, str>> {
|
||||
let resolved = match value {
|
||||
Value::CurrentPeerId => Cow::Borrowed(ctx.current_peer_id.as_str()),
|
||||
Value::Literal(value) => Cow::Borrowed(*value),
|
||||
Value::Variable(name) => {
|
||||
let resolved = resolve_variable(name, ctx)?;
|
||||
let resolved = require_string(resolved)?;
|
||||
Cow::Owned(resolved)
|
||||
}
|
||||
Value::JsonPath { variable, path } => {
|
||||
let resolved = resolve_variable(variable, ctx)?;
|
||||
let resolved = apply_json_path(resolved, path)?;
|
||||
let resolved = require_string(resolved)?;
|
||||
Cow::Owned(resolved)
|
||||
}
|
||||
};
|
||||
|
||||
Ok(resolved)
|
||||
}
|
||||
|
||||
/// Resolve value to JValue, similar to `resolve_value`
|
||||
pub(super) fn resolve_jvalue<'i>(value: &Value<'i>, ctx: &ExecutionCtx<'i>) -> Result<JValue> {
|
||||
let value = match value {
|
||||
Value::CurrentPeerId => JValue::String(ctx.current_peer_id.clone()),
|
||||
Value::Literal(value) => JValue::String(value.to_string()),
|
||||
Value::Variable(name) => resolve_variable(name, ctx)?,
|
||||
Value::JsonPath { variable, path } => {
|
||||
let value = resolve_variable(variable, ctx)?;
|
||||
apply_json_path(value, path)?
|
||||
}
|
||||
};
|
||||
|
||||
Ok(value)
|
||||
}
|
||||
|
Reference in New Issue
Block a user