First stab at fixing the error, not a full success.

This commit is contained in:
Tom Alexander 2020-05-16 21:30:51 -04:00
parent 974ce96f14
commit 054480b797
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
6 changed files with 70 additions and 17 deletions

View File

@ -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_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 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=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}

View File

@ -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_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 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=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}

View File

@ -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_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 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=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}

View File

@ -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_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 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=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}

View File

@ -169,6 +169,14 @@ impl CompareContextElement for serde_json::Value {
} }
fn partial_compare(&self, other: &dyn ContextElement) -> Option<Ordering> { fn partial_compare(&self, other: &dyn ContextElement) -> Option<Ordering> {
// 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 // Handle other serde_json::Value
match other.to_any().downcast_ref::<Self>() { match other.to_any().downcast_ref::<Self>() {
None => (), None => (),
@ -228,11 +236,30 @@ impl CompareContextElement for serde_json::Value {
// Handle string literals // Handle string literals
match other.to_any().downcast_ref::<String>() { match other.to_any().downcast_ref::<String>() {
None => (), None => (),
Some(other_string) => { Some(other_string) => match self {
return self serde_json::Value::String(self_string) => {
.as_str() return self_string.partial_cmp(other_string)
.map_or(None, |s| s.partial_cmp(other_string.as_str()))
} }
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 // Handle numeric literals
match other.to_any().downcast_ref::<u64>() { match other.to_any().downcast_ref::<u64>() {
@ -256,3 +283,16 @@ impl CompareContextElement for serde_json::Value {
fn convert_vec_to_context_element(array: &Vec<serde_json::Value>) -> Vec<&dyn ContextElement> { fn convert_vec_to_context_element(array: &Vec<serde_json::Value>) -> Vec<&dyn ContextElement> {
array.iter().map(|v| v as _).collect() 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));
}
}

View File

@ -520,6 +520,11 @@ mod tests {
fn equals(&self, other: &dyn ContextElement) -> bool { fn equals(&self, other: &dyn ContextElement) -> bool {
false false
} }
fn partial_compare(&self, other: &dyn ContextElement) -> Option<Ordering> {
// TODO: Implement
None
}
} }
#[test] #[test]