Skip to content

Commit

Permalink
Refacto eval_expression returning a Value rather than a String
Browse files Browse the repository at this point in the history
  • Loading branch information
fabricereix committed Oct 16, 2023
1 parent 8ca0ca3 commit 089dd46
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 19 deletions.
6 changes: 3 additions & 3 deletions packages/hurl/src/runner/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use hurl_core::ast::{JsonListElement, JsonObjectElement, JsonValue, Template, Te
use hurl_core::parser::{parse_json_boolean, parse_json_null, parse_json_number, Reader};

use crate::runner::core::{Error, RunnerError};
use crate::runner::template::eval_expression;
use crate::runner::template::render_expression;
use crate::runner::value::Value;

/// Evaluates a JSON value to a string given a set of `variables`.
Expand Down Expand Up @@ -65,7 +65,7 @@ pub fn eval_json_value(
}
}
JsonValue::Expression(exp) => {
let s = eval_expression(exp, variables)?;
let s = render_expression(exp, variables)?;

// The String can only be null, a bool, a number
// It will be easier when your variables value have a type
Expand Down Expand Up @@ -156,7 +156,7 @@ fn eval_json_template_element(
match template_element {
TemplateElement::String { encoded, .. } => Ok(encoded.clone()),
TemplateElement::Expression(expr) => {
let s = eval_expression(expr, variables)?;
let s = render_expression(expr, variables)?;
Ok(encode_json_string(&s))
}
}
Expand Down
62 changes: 46 additions & 16 deletions packages/hurl/src/runner/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,28 +44,33 @@ fn eval_template_element(
) -> Result<String, Error> {
match template_element {
TemplateElement::String { value, .. } => Ok(value.clone()),
TemplateElement::Expression(expr) => eval_expression(expr, variables),
TemplateElement::Expression(expr) => render_expression(expr, variables),
}
}

pub fn eval_expression(expr: &Expr, variables: &HashMap<String, Value>) -> Result<String, Error> {
pub fn render_expression(expr: &Expr, variables: &HashMap<String, Value>) -> Result<String, Error> {
let source_info = &expr.variable.source_info;
let name = &expr.variable.name;
let value = eval_expression(expr, variables)?;
if value.is_renderable() {
Ok(value.clone().to_string())
} else {
Err(Error {
source_info: source_info.clone(),
inner: RunnerError::UnrenderableVariable {
name: name.to_string(),
value: value.to_string(),
},
assert: false,
})
}
}

pub fn eval_expression(expr: &Expr, variables: &HashMap<String, Value>) -> Result<Value, Error> {
let source_info = &expr.variable.source_info;
let name = &expr.variable.name;
match variables.get(name.as_str()) {
Some(value) => {
if value.is_renderable() {
Ok(value.clone().to_string())
} else {
Err(Error {
source_info: source_info.clone(),
inner: RunnerError::UnrenderableVariable {
name: name.to_string(),
value: value.to_string(),
},
assert: false,
})
}
}
Some(value) => Ok(value.clone()),
_ => Err(Error {
source_info: source_info.clone(),
inner: RunnerError::TemplateVariableNotDefined { name: name.clone() },
Expand Down Expand Up @@ -149,4 +154,29 @@ mod tests {
}
);
}

#[test]
fn test_render_expression() {
let mut variables = HashMap::new();
variables.insert("status".to_string(), Value::Bool(true));
let expr = Expr {
space0: Whitespace {
value: "".to_string(),
source_info: SourceInfo::new(0, 0, 0, 0),
},
variable: Variable {
name: "status".to_string(),
source_info: SourceInfo::new(0, 0, 0, 0),
},
space1: Whitespace {
value: "".to_string(),
source_info: SourceInfo::new(0, 0, 0, 0),
},
};
assert_eq!(
eval_expression(&expr, &variables).unwrap(),
Value::Bool(true)
);
assert_eq!(render_expression(&expr, &variables).unwrap(), "true");
}
}

0 comments on commit 089dd46

Please sign in to comment.