Skip to content

Commit

Permalink
fix: some type-errors in function calls caused panics (#141)
Browse files Browse the repository at this point in the history
* test: test cases for type errors

* fix: don't panic when arg to fromMillis is not a number

* fix: compatibility with the reference implementation requires a non-string picture string to error

* fix: don't panic if args to toMillis are the wrong type
  • Loading branch information
tommy authored Nov 15, 2024
1 parent 1269f28 commit c41c841
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 21 deletions.
45 changes: 24 additions & 21 deletions src/evaluator/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1002,30 +1002,28 @@ pub fn from_millis<'a>(
}

max_args!(context, args, 3);
assert_arg!(args[0].is_number(), context, 1);

let millis = args[0].as_f64() as i64;

let timestamp = Utc
.timestamp_millis_opt(millis)
.single()
.ok_or_else(|| Error::T0410ArgumentNotValid(0, 1, context.name.to_string()))?;
let Some(timestamp) = Utc.timestamp_millis_opt(millis).single() else {
bad_arg!(context, 1);
};

let (picture, timezone) = match args {
[_, picture, timezone] => (
if picture.is_string() {
picture.as_str()
} else {
Cow::Borrowed("") // Treat non-strings (like ()) as empty strings
},
timezone.as_str(),
),
[_, picture] => (
if picture.is_string() {
picture.as_str()
} else {
Cow::Borrowed("")
},
Cow::Borrowed(""),
),
[_, picture, timezone] if picture.is_undefined() => {
assert_arg!(timezone.is_string(), context, 3);
(Cow::Borrowed(""), timezone.as_str())
}
[_, picture, timezone] => {
assert_arg!(picture.is_string(), context, 2);
assert_arg!(timezone.is_string(), context, 3);
(picture.as_str(), timezone.as_str())
}
[_, picture] => {
assert_arg!(picture.is_string(), context, 2);
(picture.as_str(), Cow::Borrowed(""))
}
_ => (Cow::Borrowed(""), Cow::Borrowed("")),
};

Expand Down Expand Up @@ -1106,6 +1104,7 @@ pub fn to_millis<'a>(
}

max_args!(context, args, 2);
assert_arg!(args[0].is_string(), context, 1);

// Extract the timestamp string
let timestamp_str = args[0].as_str();
Expand All @@ -1115,7 +1114,11 @@ pub fn to_millis<'a>(

// Extract the optional picture string
let picture = match args {
[_, picture] => picture.as_str(),
[_, picture] if picture.is_undefined() => Cow::Borrowed(""),
[_, picture] => {
assert_arg!(picture.is_string(), context, 2);
picture.as_str()
}
_ => Cow::Borrowed(""),
};

Expand Down
17 changes: 17 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -758,4 +758,21 @@ mod tests {
));
assert_eq!(result_invalid, empty_array);
}

#[test]
fn evaluate_expect_type_errors() {
for expr in [
"$fromMillis('foo')",
"$fromMillis(1, 1)",
"$fromMillis(1, '', 1)",
"$toMillis(1)",
"$toMillis('1970-01-01T00:00:00.000Z', 1)",
] {
let arena = Bump::new();
let jsonata = JsonAta::new(expr, &arena).unwrap();
let err = jsonata.evaluate(None, None).unwrap_err();

assert_eq!(err.code(), "T0410", "Expected type error from {expr}");
}
}
}

0 comments on commit c41c841

Please sign in to comment.