From 054480b79723700defe4e9c130c257153a653aac Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Sat, 16 May 2020 21:30:51 -0400 Subject: [PATCH] First stab at fixing the error, not a full success. --- js/test_cases/helpers_gt/main.dust | 8 +++-- js/test_cases/helpers_gte/main.dust | 8 +++-- js/test_cases/helpers_lt/main.dust | 8 +++-- js/test_cases/helpers_lte/main.dust | 8 +++-- src/bin.rs | 50 ++++++++++++++++++++++++++--- src/renderer/renderer.rs | 5 +++ 6 files changed, 70 insertions(+), 17 deletions(-) diff --git a/js/test_cases/helpers_gt/main.dust b/js/test_cases/helpers_gt/main.dust index 0bd9fca..2bc9370 100644 --- a/js/test_cases/helpers_gt/main.dust +++ b/js/test_cases/helpers_gt/main.dust @@ -21,9 +21,9 @@ beta is {beta}{~n} {@gt key="a" value="A"}"a" is greater than "A"{:else}"a" is less than or equal to "A"{/gt}{~n} {@gt key="a" value="}"}"a" is greater than "}"{:else}"a" is less than or equal to "}"{/gt}{~n} {! -Commented out because unicode breaks nom -{@gt key="☃" value="☄"}"☃" is greater than "☄"{:else}"☃" is less than or equal to "☄"{/gt}{~n} -!} + Commented out because unicode breaks nom + {@gt key="☃" value="☄"}"☃" is greater than "☄"{:else}"☃" is less than or equal to "☄"{/gt}{~n} + !} {@gt key=true_value value=false_value}true is greater than false{:else}true is less than or equal to false{/gt}{~n} {@gt key=array_lower value=array_higher}[3,5,7] is greater than [8,9]{:else}[3,5,7] is less than or equal to [8,9]{/gt}{~n} {@gt key=array_higher value=array_lower}[8,9] is greater than [3,5,7]{:else}[8,9] is less than or equal to [3,5,7]{/gt}{~n} @@ -40,3 +40,5 @@ Commented out because unicode breaks nom {@gt key=array_higher value=array_higher_longer}array_higher is greater than array_higher_longer{:else}array_higher is less than or equal to array_higher_longer{/gt}{~n} {@gt key=array_lower value=array_lower_with_array}array_lower is greater than array_lower_with_array{:else}array_lower is less than or equal to array_lower_with_array{/gt}{~n} {@gt key=array_lower_with_array value=array_lower}array_lower_with_array is greater than array_lower{:else}array_lower_with_array is less than or equal to array_lower{/gt}{~n} +{@gt key=some_obj value=int}some_obj is greater than int{:else}some_obj is less than or equal to int{/gt}{~n} +{@gt key=some_obj value="[object Object]"}some_obj is greater than "[object Object]"{:else}some_obj is less than or equal to "[object Object]"{/gt}{~n} diff --git a/js/test_cases/helpers_gte/main.dust b/js/test_cases/helpers_gte/main.dust index b0ee031..750f75b 100644 --- a/js/test_cases/helpers_gte/main.dust +++ b/js/test_cases/helpers_gte/main.dust @@ -21,9 +21,9 @@ beta is {beta}{~n} {@gte key="a" value="A"}"a" is greater than or equal to "A"{:else}"a" is less than "A"{/gte}{~n} {@gte key="a" value="}"}"a" is greater than or equal to "}"{:else}"a" is less than "}"{/gte}{~n} {! -Commented out because unicode breaks nom -{@gte key="☃" value="☄"}"☃" is greater than or equal to "☄"{:else}"☃" is less than "☄"{/gte}{~n} -!} + Commented out because unicode breaks nom + {@gte key="☃" value="☄"}"☃" is greater than or equal to "☄"{:else}"☃" is less than "☄"{/gte}{~n} + !} {@gte key=true_value value=false_value}true is greater than or equal to false{:else}true is less than false{/gte}{~n} {@gte key=array_lower value=array_higher}[3,5,7] is greater than or equal to [8,9]{:else}[3,5,7] is less than [8,9]{/gte}{~n} {@gte key=array_higher value=array_lower}[8,9] is greater than or equal to [3,5,7]{:else}[8,9] is less than [3,5,7]{/gte}{~n} @@ -39,3 +39,5 @@ Commented out because unicode breaks nom {@gte key=array_higher value=array_higher_longer}array_higher is greater than or equal to array_higher_longer{:else}array_higher is less than array_higher_longer{/gte}{~n} {@gte key=array_lower value=array_lower_with_array}array_lower is greater than or equal to array_lower_with_array{:else}array_lower is less than array_lower_with_array{/gte}{~n} {@gte key=array_lower_with_array value=array_lower}array_lower_with_array is greater than or equal to array_lower{:else}array_lower_with_array is less than array_lower{/gte}{~n} +{@gte key=some_obj value=int}some_obj is greater than or equal to int{:else}some_obj is less than int{/gte}{~n} +{@gte key=some_obj value="[object Object]"}some_obj is greater than or equal to "[object Object]"{:else}some_obj is less than "[object Object]"{/gte}{~n} diff --git a/js/test_cases/helpers_lt/main.dust b/js/test_cases/helpers_lt/main.dust index f2b81d1..cbebe3f 100644 --- a/js/test_cases/helpers_lt/main.dust +++ b/js/test_cases/helpers_lt/main.dust @@ -21,9 +21,9 @@ beta is {beta}{~n} {@lt key="a" value="A"}"a" is less than "A"{:else}"a" is greater than or equal to "A"{/lt}{~n} {@lt key="a" value="}"}"a" is less than "}"{:else}"a" is greater than or equal to "}"{/lt}{~n} {! -Commented out because unicode breaks nom -{@lt key="☃" value="☄"}"☃" is less than "☄"{:else}"☃" is greater than or equal to "☄"{/lt}{~n} -!} + Commented out because unicode breaks nom + {@lt key="☃" value="☄"}"☃" is less than "☄"{:else}"☃" is greater than or equal to "☄"{/lt}{~n} + !} {@lt key=true_value value=false_value}true is less than false{:else}true is greater than or equal to false{/lt}{~n} {@lt key=array_lower value=array_higher}[3,5,7] is less than [8,9]{:else}[3,5,7] is greater than or equal to [8,9]{/lt}{~n} {@lt key=array_higher value=array_lower}[8,9] is less than [3,5,7]{:else}[8,9] is greater than or equal to [3,5,7]{/lt}{~n} @@ -39,3 +39,5 @@ Commented out because unicode breaks nom {@lt key=array_higher value=array_higher_longer}array_higher is less than array_higher_longer{:else}array_higher is greater than or equal to array_higher_longer{/lt}{~n} {@lt key=array_lower value=array_lower_with_array}array_lower is less than array_lower_with_array{:else}array_lower is greater than or equal to array_lower_with_array{/lt}{~n} {@lt key=array_lower_with_array value=array_lower}array_lower_with_array is less than array_lower{:else}array_lower_with_array is greater than or equal to array_lower{/lt}{~n} +{@lt key=some_obj value=int}some_obj is less than int{:else}some_obj is greater than or equal to int{/lt}{~n} +{@lt key=some_obj value="[object Object]"}some_obj is less than "[object Object]"{:else}some_obj is greater than or equal to "[object Object]"{/lt}{~n} diff --git a/js/test_cases/helpers_lte/main.dust b/js/test_cases/helpers_lte/main.dust index 72ee337..98939e4 100644 --- a/js/test_cases/helpers_lte/main.dust +++ b/js/test_cases/helpers_lte/main.dust @@ -21,9 +21,9 @@ beta is {beta}{~n} {@lte key="a" value="A"}"a" is less than or equal to "A"{:else}"a" is greater than "A"{/lte}{~n} {@lte key="a" value="}"}"a" is less than or equal to "}"{:else}"a" is greater than "}"{/lte}{~n} {! -Commented out because unicode breaks nom -{@lte key="☃" value="☄"}"☃" is less than or equal to "☄"{:else}"☃" is greater than "☄"{/lte}{~n} -!} + Commented out because unicode breaks nom + {@lte key="☃" value="☄"}"☃" is less than or equal to "☄"{:else}"☃" is greater than "☄"{/lte}{~n} + !} {@lte key=true_value value=false_value}true is less than or equal to false{:else}true is greater than false{/lte}{~n} {@lte key=array_lower value=array_higher}[3,5,7] is less than or equal to [8,9]{:else}[3,5,7] is greater than [8,9]{/lte}{~n} {@lte key=array_higher value=array_lower}[8,9] is less than or equal to [3,5,7]{:else}[8,9] is greater than [3,5,7]{/lte}{~n} @@ -39,3 +39,5 @@ Commented out because unicode breaks nom {@lte key=array_higher value=array_higher_longer}array_higher is less than or equal to array_higher_longer{:else}array_higher is greater than array_higher_longer{/lte}{~n} {@lte key=array_lower value=array_lower_with_array}array_lower is less than or equal to array_lower_with_array{:else}array_lower is greater than array_lower_with_array{/lte}{~n} {@lte key=array_lower_with_array value=array_lower}array_lower_with_array is less than or equal to array_lower{:else}array_lower_with_array is greater than array_lower{/lte}{~n} +{@lte key=some_obj value=int}some_obj is less than or equal to int{:else}some_obj is greater than int{/lte}{~n} +{@lte key=some_obj value="[object Object]"}some_obj is less than or equal to "[object Object]"{:else}some_obj is greater than "[object Object]"{/lte}{~n} diff --git a/src/bin.rs b/src/bin.rs index 4274501..964658d 100644 --- a/src/bin.rs +++ b/src/bin.rs @@ -169,6 +169,14 @@ impl CompareContextElement for serde_json::Value { } fn partial_compare(&self, other: &dyn ContextElement) -> Option { + // Handle type coerced objects + + // When doing a greater than or less than comparison, + // javascript coerces objects into "[object Object]". + if let serde_json::Value::Object(_) = self { + return "[object Object]".to_owned().partial_compare(other); + } + // Handle other serde_json::Value match other.to_any().downcast_ref::() { None => (), @@ -228,11 +236,30 @@ impl CompareContextElement for serde_json::Value { // Handle string literals match other.to_any().downcast_ref::() { None => (), - Some(other_string) => { - return self - .as_str() - .map_or(None, |s| s.partial_cmp(other_string.as_str())) - } + Some(other_string) => match self { + serde_json::Value::String(self_string) => { + return self_string.partial_cmp(other_string) + } + serde_json::Value::Number(self_number) => { + match ( + self_number.as_u64(), + self_number.as_i64(), + self_number.as_f64(), + ) { + (Some(self_uint), _, _) => { + return self_uint.to_string().partial_cmp(other_string) + } + (_, Some(self_int), _) => { + return self_int.to_string().partial_cmp(other_string) + } + (_, _, Some(self_float)) => { + return self_float.to_string().partial_cmp(other_string) + } + (None, None, None) => panic!("This should be impossible since u64 and i64 can both be converted to floats") + } + } + _ => (), + }, } // Handle numeric literals match other.to_any().downcast_ref::() { @@ -256,3 +283,16 @@ impl CompareContextElement for serde_json::Value { fn convert_vec_to_context_element(array: &Vec) -> Vec<&dyn ContextElement> { array.iter().map(|v| v as _).collect() } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_type_coercion_object_int() { + let x: serde_json::Value = + serde_json::from_str(r#"{"name": "cat"}"#).expect("Failed to parse json"); + let y: u64 = 7; + assert_eq!(x.partial_compare(&y), Some(Ordering::Greater)); + } +} diff --git a/src/renderer/renderer.rs b/src/renderer/renderer.rs index f44022c..5ba55d4 100644 --- a/src/renderer/renderer.rs +++ b/src/renderer/renderer.rs @@ -520,6 +520,11 @@ mod tests { fn equals(&self, other: &dyn ContextElement) -> bool { false } + + fn partial_compare(&self, other: &dyn ContextElement) -> Option { + // TODO: Implement + None + } } #[test]