Skip to content

Commit

Permalink
Render Date value
Browse files Browse the repository at this point in the history
  • Loading branch information
fabricereix committed Nov 23, 2024
1 parent 474dcc6 commit ce9d41c
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 13 deletions.
22 changes: 20 additions & 2 deletions packages/hurl/src/runner/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ pub fn eval(expr: &Expr, variables: &VariableSet) -> Result<Value, RunnerError>
pub fn render(expr: &Expr, variables: &VariableSet) -> Result<String, RunnerError> {
let source_info = expr.source_info;
let value = eval(expr, variables)?;
if value.is_renderable() {
Ok(value.to_string())
if let Some(s) = value.render() {
Ok(s)
} else {
let kind = RunnerErrorKind::UnrenderableExpression {
value: value.to_string(),
Expand All @@ -65,6 +65,7 @@ mod tests {
#[test]
fn test_render_expression() {
let mut variables = VariableSet::new();

variables.insert("status".to_string(), Value::Bool(true));
let expr = Expr {
kind: ExprKind::Variable(Variable {
Expand All @@ -75,5 +76,22 @@ mod tests {
};
assert_eq!(eval(&expr, &variables).unwrap(), Value::Bool(true));
assert_eq!(render(&expr, &variables).unwrap(), "true");

let data_chrono = chrono::DateTime::parse_from_rfc2822("Tue, 10 Jan 2023 08:29:52 GMT")
.unwrap()
.into();
variables.insert("now".to_string(), Value::Date(data_chrono));
let expr = Expr {
kind: ExprKind::Variable(Variable {
name: "now".to_string(),
source_info: SourceInfo::new(Pos::new(0, 0), Pos::new(0, 0)),
}),
source_info: SourceInfo::new(Pos::new(0, 0), Pos::new(0, 0)),
};
assert_eq!(eval(&expr, &variables).unwrap(), Value::Date(data_chrono));
assert_eq!(
render(&expr, &variables).unwrap(),
"2023-01-10T08:29:52+00:00"
);
}
}
12 changes: 1 addition & 11 deletions packages/hurl/src/runner/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
use hurl_core::ast::*;

use crate::runner::error::RunnerError;
use crate::runner::Value;
use crate::runner::{expr, VariableSet};

/// Renders to string a `template` given a map of variables.
Expand All @@ -44,22 +43,13 @@ fn eval_template_element(
}
}

impl Value {
pub fn is_renderable(&self) -> bool {
matches!(
self,
Value::Number(_) | Value::Bool(_) | Value::String(_) | Value::Null
)
}
}

#[cfg(test)]
mod tests {
use hurl_core::ast::SourceInfo;
use hurl_core::reader::Pos;

use super::*;
use crate::runner::{Number, RunnerErrorKind};
use crate::runner::{Number, RunnerErrorKind, Value};

fn template_element_expression() -> TemplateElement {
// {{name}}
Expand Down
11 changes: 11 additions & 0 deletions packages/hurl/src/runner/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,17 @@ impl Value {
pub fn is_scalar(&self) -> bool {
!matches!(self, Value::Nodeset(_) | Value::List(_))
}

pub fn render(&self) -> Option<String> {
match self {
Value::Bool(v) => Some(v.to_string()),
Value::Date(d) => Some(d.to_rfc3339()),
Value::Null => Some("null".to_string()),
Value::Number(v) => Some(v.to_string()),
Value::String(s) => Some(s.clone()),
_ => None,
}
}
}

#[cfg(test)]
Expand Down

0 comments on commit ce9d41c

Please sign in to comment.