Turns out I was wrong, the @size helper attempts to cast to a number regardless of the input and it uses that. Fixed the test.

This commit is contained in:
Tom Alexander 2020-06-14 15:20:54 -04:00
parent e70f397545
commit 648ae5dfdb
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
4 changed files with 30 additions and 32 deletions

View File

@ -1,3 +1,3 @@
{
"val": "Alice"
"val": "7.99"
}

View File

@ -353,6 +353,10 @@ impl Castable for serde_json::Value {
})
.ok(),
(serde_json::Value::Number(_), "number") => Some(IceResult::from_borrowed(self)),
(serde_json::Value::Null, "number") => None,
(serde_json::Value::Bool(_), "number") => None,
(serde_json::Value::Array(_), "number") => None,
(serde_json::Value::Object(_), "number") => None,
(_, _) => panic!("Unimplemented cast"),
}
}

View File

@ -141,32 +141,6 @@ impl<'a> IntoContextElement for RValue<'a> {
.map(|ice| ice.into_context_element(renderer, breadcrumbs))
.ok()
.flatten(),
// Special case: if the template is only a reference to a
// single variable without any additional text, then
// simply resolve that variable.
//
// TODO: What if the reference has filters?
// RValue::RVTemplate(template)
// if template.len() == 1
// && template.iter().all(|pne| match pne {
// PartialNameElement::PNReference { .. } => true,
// PartialNameElement::PNSpan { .. } => false,
// }) =>
// {
// let path = match template
// .first()
// .expect("Match guard proves theres one element")
// {
// PartialNameElement::PNReference { path, .. } => path,
// PartialNameElement::PNSpan { .. } => {
// panic!("Match guard prevents spans from hitting this code")
// }
// };
// walk_path(breadcrumbs, &path)
// .map(|ice| ice.into_context_element(renderer, breadcrumbs))
// .ok()
// .flatten()
// }
RValue::RVTemplate(template) => renderer
.render_partial_name(template, breadcrumbs)
.map(|rendered| OwnedLiteral::LString(rendered))

View File

@ -730,17 +730,37 @@ impl<'a> DustRenderer<'a> {
maybe_ice
.as_ref()
.map(|ice| ice.get_context_element_reference())
.map(|ce| ce.get_size())
// .map(|ce| ce.get_size())
});
match value_ce {
// If we found the key but could not get the size from it, render nothing.
Some(Ok(None)) => return Ok("".to_owned()),
// If "key" is not on the @size tag at all, render 0.
None => return Ok("0".to_owned()),
// If the key value could not be found in the context, render 0.
Some(Err(_)) => return Ok("0".to_owned()),
Some(Ok(Some(ce_size))) => {
return ce_size.get_context_element_reference().render(&Vec::new())
Some(Ok(ce)) => {
// The @size helper attempts to cast values to
// numbers, and if that succeeds it uses the
// number, otherwise we'll get the size of the
// original type.
match ce.cast_to_type("number") {
Some(ice) => {
return ice
.get_context_element_reference()
.get_size()
.map(|ce_size| {
ce_size.get_context_element_reference().render(&Vec::new())
})
.unwrap_or(Ok("".to_owned()))
}
None => {
return ce
.get_size()
.map(|ce_size| {
ce_size.get_context_element_reference().render(&Vec::new())
})
.unwrap_or(Ok("".to_owned()))
}
}
}
}
}