Skip to content

Commit

Permalink
Fix some key values issue + serde_json change
Browse files Browse the repository at this point in the history
  • Loading branch information
Keats committed Sep 19, 2022
1 parent 468f146 commit 124de5e
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 4 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
@@ -1,5 +1,10 @@
# Changelog

## 1.17.1 (unreleased)

- Make `get_random` use isize instead of i32 and bad error message
- Fix variables lookup when the evaluated key has a `.` or quotes

## 1.17.0 (2022-08-14)

- Fix bug where operands in `in` operation were escaped before comparison
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "tera"
version = "1.17.0"
version = "1.17.1"
authors = ["Vincent Prouillet <hello@prouilletvincent.com>"]
license = "MIT"
readme = "README.md"
Expand Down
12 changes: 11 additions & 1 deletion src/context.rs
Expand Up @@ -144,7 +144,17 @@ impl ValueRender for Value {
fn render(&self, write: &mut impl Write) -> std::io::Result<()> {
match *self {
Value::String(ref s) => write!(write, "{}", s),
Value::Number(ref i) => write!(write, "{}", i),
Value::Number(ref i) => {
if let Some(v) = i.as_i64() {
write!(write, "{}", v)
} else if let Some(v) = i.as_u64() {
write!(write, "{}", v)
} else if let Some(v) = i.as_f64() {
write!(write, "{}", v)
} else {
unreachable!()
}
}
Value::Bool(i) => write!(write, "{}", i),
Value::Null => Ok(()),
Value::Array(ref a) => {
Expand Down
8 changes: 6 additions & 2 deletions src/renderer/processor.rs
Expand Up @@ -37,7 +37,7 @@ fn evaluate_sub_variables<'a>(key: &str, call_stack: &CallStack<'a>) -> Result<S
}
Ok(post_var) => {
let post_var_as_str = match *post_var {
Value::String(ref s) => s.to_string(),
Value::String(ref s) => format!(r#""{}""#, s),
Value::Number(ref n) => n.to_string(),
_ => {
return Err(Error::msg(format!(
Expand Down Expand Up @@ -764,7 +764,11 @@ impl<'a> Processor<'a> {
if res.is_nan() {
None
} else {
Number::from_f64(res)
if res.round() == res && res.is_finite() {
Some(Number::from(res as i64))
} else {
Number::from_f64(res)
}
}
}
MathOperator::Add => {
Expand Down
20 changes: 20 additions & 0 deletions src/renderer/tests/square_brackets.rs
Expand Up @@ -92,3 +92,23 @@ fn var_access_by_loop_index_with_set() {
);
assert!(res.is_ok());
}


// https://github.com/Keats/tera/issues/754
#[test]
fn can_get_value_if_key_contains_period() {
let mut context = Context::new();
context.insert("name", "Mt. Robson Provincial Park");
let mut map = HashMap::new();
map.insert("Mt. Robson Provincial Park".to_string(), "hello".to_string());
context.insert("tag_info", &map);

let res = Tera::one_off(
r#"{{ tag_info[name] }}"#,
&context,
true,
);
assert!(res.is_ok());
let res = res.unwrap();
assert_eq!(res, "hello");
}

0 comments on commit 124de5e

Please sign in to comment.