From 0f47000a9b500a0648258aa37ae85cb2396b5d2f Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Wed, 6 May 2020 19:10:09 -0400 Subject: [PATCH 1/3] Add support for the exists tag. --- js/test_cases/exists/README.md | 1 + js/test_cases/exists/input1.json | 1 + js/test_cases/exists/input10.json | 1 + js/test_cases/exists/input11.json | 1 + js/test_cases/exists/input2.json | 1 + js/test_cases/exists/input3.json | 1 + js/test_cases/exists/input4.json | 1 + js/test_cases/exists/input5.json | 1 + js/test_cases/exists/input6.json | 1 + js/test_cases/exists/input7.json | 1 + js/test_cases/exists/input8.json | 1 + js/test_cases/exists/input9.json | 1 + js/test_cases/exists/main.dust | 5 +++++ src/renderer/renderer.rs | 18 +++++++++++++++--- 14 files changed, 32 insertions(+), 3 deletions(-) create mode 100644 js/test_cases/exists/README.md create mode 100644 js/test_cases/exists/input1.json create mode 100644 js/test_cases/exists/input10.json create mode 100644 js/test_cases/exists/input11.json create mode 100644 js/test_cases/exists/input2.json create mode 100644 js/test_cases/exists/input3.json create mode 100644 js/test_cases/exists/input4.json create mode 100644 js/test_cases/exists/input5.json create mode 100644 js/test_cases/exists/input6.json create mode 100644 js/test_cases/exists/input7.json create mode 100644 js/test_cases/exists/input8.json create mode 100644 js/test_cases/exists/input9.json create mode 100644 js/test_cases/exists/main.dust diff --git a/js/test_cases/exists/README.md b/js/test_cases/exists/README.md new file mode 100644 index 0000000..3da699b --- /dev/null +++ b/js/test_cases/exists/README.md @@ -0,0 +1 @@ +Exists appears to follow the same truthiness rules that sections follow, rather than merely checking if a value exists. diff --git a/js/test_cases/exists/input1.json b/js/test_cases/exists/input1.json new file mode 100644 index 0000000..69197e0 --- /dev/null +++ b/js/test_cases/exists/input1.json @@ -0,0 +1 @@ +{"things": ["Alice", "Bob", "Chris"]} diff --git a/js/test_cases/exists/input10.json b/js/test_cases/exists/input10.json new file mode 100644 index 0000000..08f519d --- /dev/null +++ b/js/test_cases/exists/input10.json @@ -0,0 +1 @@ +{"things": {}} diff --git a/js/test_cases/exists/input11.json b/js/test_cases/exists/input11.json new file mode 100644 index 0000000..b9ee8db --- /dev/null +++ b/js/test_cases/exists/input11.json @@ -0,0 +1 @@ +["cat", "dog"] diff --git a/js/test_cases/exists/input2.json b/js/test_cases/exists/input2.json new file mode 100644 index 0000000..4743329 --- /dev/null +++ b/js/test_cases/exists/input2.json @@ -0,0 +1 @@ +{"things": {"name": "Alice", "keyboard": "K-Type"}} diff --git a/js/test_cases/exists/input3.json b/js/test_cases/exists/input3.json new file mode 100644 index 0000000..8040d63 --- /dev/null +++ b/js/test_cases/exists/input3.json @@ -0,0 +1 @@ +{"there_are_no_things": 4} diff --git a/js/test_cases/exists/input4.json b/js/test_cases/exists/input4.json new file mode 100644 index 0000000..03d1e8a --- /dev/null +++ b/js/test_cases/exists/input4.json @@ -0,0 +1 @@ +{"things": "just a string"} diff --git a/js/test_cases/exists/input5.json b/js/test_cases/exists/input5.json new file mode 100644 index 0000000..4ef7571 --- /dev/null +++ b/js/test_cases/exists/input5.json @@ -0,0 +1 @@ +{"things": false} diff --git a/js/test_cases/exists/input6.json b/js/test_cases/exists/input6.json new file mode 100644 index 0000000..feb65c6 --- /dev/null +++ b/js/test_cases/exists/input6.json @@ -0,0 +1 @@ +{"things": null} diff --git a/js/test_cases/exists/input7.json b/js/test_cases/exists/input7.json new file mode 100644 index 0000000..d9fc4f8 --- /dev/null +++ b/js/test_cases/exists/input7.json @@ -0,0 +1 @@ +{"things": 0} diff --git a/js/test_cases/exists/input8.json b/js/test_cases/exists/input8.json new file mode 100644 index 0000000..1486a8a --- /dev/null +++ b/js/test_cases/exists/input8.json @@ -0,0 +1 @@ +{"things": ""} diff --git a/js/test_cases/exists/input9.json b/js/test_cases/exists/input9.json new file mode 100644 index 0000000..93babc6 --- /dev/null +++ b/js/test_cases/exists/input9.json @@ -0,0 +1 @@ +{"things": []} diff --git a/js/test_cases/exists/main.dust b/js/test_cases/exists/main.dust new file mode 100644 index 0000000..22d0d2c --- /dev/null +++ b/js/test_cases/exists/main.dust @@ -0,0 +1,5 @@ +{?things} +Thing: {things} +{:else} +No things {.} +{/things} diff --git a/src/renderer/renderer.rs b/src/renderer/renderer.rs index 4a0b1eb..e17b037 100644 --- a/src/renderer/renderer.rs +++ b/src/renderer/renderer.rs @@ -114,9 +114,6 @@ impl<'a> DustRenderer<'a> { // an empty array or null), Dust uses the // original context before walking the path as // the context for rendering the else block - // - // TODO: do filters apply? I don't think so - // but I should test return match &container.else_contents { Some(body) => self.render_body(&body, breadcrumbs), None => Ok("".to_owned()), @@ -139,6 +136,21 @@ impl<'a> DustRenderer<'a> { } } } + DustTag::DTExists(container) => { + let val = walk_path(breadcrumbs, &container.path.keys); + let loop_elements: Vec<&dyn ContextElement> = self.get_loop_elements(val)?; + if loop_elements.is_empty() { + return match &container.else_contents { + Some(body) => self.render_body(&body, breadcrumbs), + None => Ok("".to_owned()), + }; + } else { + return match &container.contents { + None => Ok("".to_owned()), + Some(body) => self.render_body(&body, breadcrumbs), + }; + } + } _ => (), // TODO: Implement the rest } Ok("".to_owned()) From b45688351e72bcf97abaca32e2cee553d33a57ca Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Wed, 6 May 2020 20:13:33 -0400 Subject: [PATCH 2/3] Rendering a reference appears to do a truthiness check because false renders an empty string despite rendering as "false" in an array element. --- js/test_cases/not_exists/input1.json | 1 + js/test_cases/not_exists/input10.json | 1 + js/test_cases/not_exists/input11.json | 1 + js/test_cases/not_exists/input2.json | 1 + js/test_cases/not_exists/input3.json | 1 + js/test_cases/not_exists/input4.json | 1 + js/test_cases/not_exists/input5.json | 1 + js/test_cases/not_exists/input6.json | 1 + js/test_cases/not_exists/input7.json | 1 + js/test_cases/not_exists/input8.json | 1 + js/test_cases/not_exists/input9.json | 1 + js/test_cases/not_exists/main.dust | 5 +++++ .../render_unusual_types/input6.json | 1 + .../render_unusual_types/input7.json | 1 + .../render_unusual_types/input8.json | 1 + src/renderer/renderer.rs | 19 ++++++++++++++++++- 16 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 js/test_cases/not_exists/input1.json create mode 100644 js/test_cases/not_exists/input10.json create mode 100644 js/test_cases/not_exists/input11.json create mode 100644 js/test_cases/not_exists/input2.json create mode 100644 js/test_cases/not_exists/input3.json create mode 100644 js/test_cases/not_exists/input4.json create mode 100644 js/test_cases/not_exists/input5.json create mode 100644 js/test_cases/not_exists/input6.json create mode 100644 js/test_cases/not_exists/input7.json create mode 100644 js/test_cases/not_exists/input8.json create mode 100644 js/test_cases/not_exists/input9.json create mode 100644 js/test_cases/not_exists/main.dust create mode 100644 js/test_cases/render_unusual_types/input6.json create mode 100644 js/test_cases/render_unusual_types/input7.json create mode 100644 js/test_cases/render_unusual_types/input8.json diff --git a/js/test_cases/not_exists/input1.json b/js/test_cases/not_exists/input1.json new file mode 100644 index 0000000..69197e0 --- /dev/null +++ b/js/test_cases/not_exists/input1.json @@ -0,0 +1 @@ +{"things": ["Alice", "Bob", "Chris"]} diff --git a/js/test_cases/not_exists/input10.json b/js/test_cases/not_exists/input10.json new file mode 100644 index 0000000..08f519d --- /dev/null +++ b/js/test_cases/not_exists/input10.json @@ -0,0 +1 @@ +{"things": {}} diff --git a/js/test_cases/not_exists/input11.json b/js/test_cases/not_exists/input11.json new file mode 100644 index 0000000..b9ee8db --- /dev/null +++ b/js/test_cases/not_exists/input11.json @@ -0,0 +1 @@ +["cat", "dog"] diff --git a/js/test_cases/not_exists/input2.json b/js/test_cases/not_exists/input2.json new file mode 100644 index 0000000..4743329 --- /dev/null +++ b/js/test_cases/not_exists/input2.json @@ -0,0 +1 @@ +{"things": {"name": "Alice", "keyboard": "K-Type"}} diff --git a/js/test_cases/not_exists/input3.json b/js/test_cases/not_exists/input3.json new file mode 100644 index 0000000..8040d63 --- /dev/null +++ b/js/test_cases/not_exists/input3.json @@ -0,0 +1 @@ +{"there_are_no_things": 4} diff --git a/js/test_cases/not_exists/input4.json b/js/test_cases/not_exists/input4.json new file mode 100644 index 0000000..03d1e8a --- /dev/null +++ b/js/test_cases/not_exists/input4.json @@ -0,0 +1 @@ +{"things": "just a string"} diff --git a/js/test_cases/not_exists/input5.json b/js/test_cases/not_exists/input5.json new file mode 100644 index 0000000..4ef7571 --- /dev/null +++ b/js/test_cases/not_exists/input5.json @@ -0,0 +1 @@ +{"things": false} diff --git a/js/test_cases/not_exists/input6.json b/js/test_cases/not_exists/input6.json new file mode 100644 index 0000000..feb65c6 --- /dev/null +++ b/js/test_cases/not_exists/input6.json @@ -0,0 +1 @@ +{"things": null} diff --git a/js/test_cases/not_exists/input7.json b/js/test_cases/not_exists/input7.json new file mode 100644 index 0000000..d9fc4f8 --- /dev/null +++ b/js/test_cases/not_exists/input7.json @@ -0,0 +1 @@ +{"things": 0} diff --git a/js/test_cases/not_exists/input8.json b/js/test_cases/not_exists/input8.json new file mode 100644 index 0000000..1486a8a --- /dev/null +++ b/js/test_cases/not_exists/input8.json @@ -0,0 +1 @@ +{"things": ""} diff --git a/js/test_cases/not_exists/input9.json b/js/test_cases/not_exists/input9.json new file mode 100644 index 0000000..93babc6 --- /dev/null +++ b/js/test_cases/not_exists/input9.json @@ -0,0 +1 @@ +{"things": []} diff --git a/js/test_cases/not_exists/main.dust b/js/test_cases/not_exists/main.dust new file mode 100644 index 0000000..bea33c4 --- /dev/null +++ b/js/test_cases/not_exists/main.dust @@ -0,0 +1,5 @@ +{^things} +Thing: {things} +{:else} +No things {.} +{/things} diff --git a/js/test_cases/render_unusual_types/input6.json b/js/test_cases/render_unusual_types/input6.json new file mode 100644 index 0000000..329afc2 --- /dev/null +++ b/js/test_cases/render_unusual_types/input6.json @@ -0,0 +1 @@ +{"name": false} diff --git a/js/test_cases/render_unusual_types/input7.json b/js/test_cases/render_unusual_types/input7.json new file mode 100644 index 0000000..0ab5773 --- /dev/null +++ b/js/test_cases/render_unusual_types/input7.json @@ -0,0 +1 @@ +{"name": true} diff --git a/js/test_cases/render_unusual_types/input8.json b/js/test_cases/render_unusual_types/input8.json new file mode 100644 index 0000000..c0b4ef3 --- /dev/null +++ b/js/test_cases/render_unusual_types/input8.json @@ -0,0 +1 @@ +{"name": [[], {}, true, false, ""]} diff --git a/src/renderer/renderer.rs b/src/renderer/renderer.rs index e17b037..13a8b29 100644 --- a/src/renderer/renderer.rs +++ b/src/renderer/renderer.rs @@ -99,7 +99,9 @@ impl<'a> DustRenderer<'a> { } DustTag::DTReference(reference) => { let val = walk_path(breadcrumbs, &reference.path.keys); - if let Err(RenderError::NotFound { .. }) = val { + let loop_elements: Vec<&dyn ContextElement> = self.get_loop_elements(val)?; + if loop_elements.is_empty() { + // if let Err(RenderError::NotFound { .. }) = val { // If reference does not exist in the context, it becomes an empty string return Ok("".to_owned()); } else { @@ -151,6 +153,21 @@ impl<'a> DustRenderer<'a> { }; } } + DustTag::DTNotExists(container) => { + let val = walk_path(breadcrumbs, &container.path.keys); + let loop_elements: Vec<&dyn ContextElement> = self.get_loop_elements(val)?; + if !loop_elements.is_empty() { + return match &container.else_contents { + Some(body) => self.render_body(&body, breadcrumbs), + None => Ok("".to_owned()), + }; + } else { + return match &container.contents { + None => Ok("".to_owned()), + Some(body) => self.render_body(&body, breadcrumbs), + }; + } + } _ => (), // TODO: Implement the rest } Ok("".to_owned()) From dedfa796304e7f991dd9f407b17d69f91372c010 Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Wed, 6 May 2020 20:30:03 -0400 Subject: [PATCH 3/3] Do a truthiness check on references before printing them. --- src/renderer/renderer.rs | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/renderer/renderer.rs b/src/renderer/renderer.rs index 13a8b29..f4b3edd 100644 --- a/src/renderer/renderer.rs +++ b/src/renderer/renderer.rs @@ -99,13 +99,17 @@ impl<'a> DustRenderer<'a> { } DustTag::DTReference(reference) => { let val = walk_path(breadcrumbs, &reference.path.keys); - let loop_elements: Vec<&dyn ContextElement> = self.get_loop_elements(val)?; - if loop_elements.is_empty() { - // if let Err(RenderError::NotFound { .. }) = val { - // If reference does not exist in the context, it becomes an empty string - return Ok("".to_owned()); - } else { - return val?.render(&reference.filters); + match val { + Err(RenderError::NotFound { .. }) => return Ok("".to_owned()), + Ok(final_val) => { + let loop_elements = final_val.get_loop_elements()?; + if loop_elements.is_empty() { + return Ok("".to_owned()); + } else { + return final_val.render(&reference.filters); + } + } + Err(render_error) => return Err(render_error), } } DustTag::DTSection(container) => {