Skip to content
Merged
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
48 changes: 40 additions & 8 deletions auto-engine-core/src/types/conditions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,25 @@ pub struct Conditions {
pub not_exist: Option<String>,
}

#[derive(Serialize, Deserialize, Clone, Debug, Default)]
pub struct ConditionResult {
pub pass: bool,
#[serde(skip_serializing_if = "Option::is_none")]
pub reason: Option<String>,
}

impl Conditions {
pub async fn check(&self, ctx: &Context) -> Result<bool, String> {
pub async fn check(&self, ctx: &Context) -> Result<ConditionResult, String> {
if let Some(key) = &self.exist
&& key != ""
{
let values = ctx.string_value.read().await;
if !values.contains_key(key) {
log::info!("{} does not exist, {:?}", key, values);
return Ok(false);
return Ok(ConditionResult {
pass: false,
reason: Some(format!("{} does not exist", key)),
});
}
}

Expand All @@ -30,25 +40,47 @@ impl Conditions {
let values = ctx.string_value.read().await;
if values.contains_key(key) {
log::info!("{} does not exist, {:?}", key, values);
return Ok(false);
return Ok(ConditionResult {
pass: false,
reason: Some(format!("{} does not exist", key)),
});
}
}

if let Some(condition) = &self.condition
&& condition != ""
{
let condition = utils::parse_variables(ctx, condition).await;
if condition == "" {
return Ok(false);
let result = utils::try_parse_variables(ctx, condition).await;
let condition = match result {
Ok(v) => v,
Err(err) => {
log::error!("{}", err);
return Ok(ConditionResult {
pass: false,
reason: Some(err.to_string()),
});
}
};
if condition.trim() == "" {
return Ok(ConditionResult {
pass: false,
reason: Some(condition.clone()),
});
}
let result = evalexpr::eval_boolean(&condition)
.map_err(|err| format!("{} is not boolean", err))?;
if !result {
log::info!("{} does not pass condition", condition);
return Ok(false);
return Ok(ConditionResult {
pass: false,
reason: Some(condition.clone()),
});
}
}
Ok(true)
Ok(ConditionResult {
pass: false,
reason: None,
})
}
}

Expand Down
30 changes: 30 additions & 0 deletions auto-engine-core/src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,36 @@ pub async fn parse_variables(context: &Context, input: &str) -> String {
.into_owned()
}

pub async fn try_parse_variables(context: &Context, input: &str) -> Result<String, String> {
let ctx = context.string_value.read().await;
let mut err: Option<String> = None;

let result = REGEX_PARSE_VARIABLES.replace_all(input, |caps: &regex::Captures| {
let var_name = &caps[1];

let variable = match ctx.get(var_name) {
Some(value) => serde_json::to_string(value)
.unwrap_or_default()
.trim_matches('"')
.to_string(),
None => {
err = Some(format!("variable `{}` not found", var_name));
String::new()
}
};
if variable == "" {
err = Some(format!("variable `{}` is empty", var_name));
}
variable
});

if let Some(e) = err {
Err(e)
} else {
Ok(result.into_owned())
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
5 changes: 3 additions & 2 deletions auto-engine-core/src/workflow/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,11 +257,12 @@ fn handle_nod(
}

if let Some(condition) = node_schema.metadata.conditions {
if !condition.check(&ctx).await? {
let result = condition.check(&ctx).await?;
if !result.pass {
emitter
.emit(
NODE_EVENT,
NodeEventPayload::skip::<String>(node_id.clone(), None),
NodeEventPayload::skip(node_id.clone(), Some(result)),
)
.unwrap_or_default();
return Ok(None);
Expand Down
Loading