refactor: strongly typed TSC ops (#20466)

Removes usage of `serde_json::Value` in several ops used in TSC, in
favor of using strongly typed structs. This will unblock more 
changes in https://github.com/denoland/deno/pull/20462.
This commit is contained in:
Bartek Iwańczuk 2023-09-12 02:55:57 +02:00 committed by GitHub
parent 950e0e9cd6
commit 82c2864065
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 83 additions and 57 deletions

View file

@ -733,12 +733,9 @@ struct RespondArgs {
}
#[op]
fn op_respond(state: &mut OpState, args: Value) -> Result<Value, AnyError> {
fn op_respond(state: &mut OpState, args: RespondArgs) {
let state = state.borrow_mut::<State>();
let v: RespondArgs = serde_json::from_value(args)
.context("Error converting the result for \"op_respond\".")?;
state.maybe_response = Some(v);
Ok(json!(true))
state.maybe_response = Some(args);
}
/// Execute a request on the supplied snapshot, returning a response which
@ -1160,21 +1157,18 @@ mod tests {
#[tokio::test]
async fn test_respond() {
let mut state = setup(None, None, None).await;
let actual = op_respond::call(
&mut state,
json!({
"diagnostics": [
{
"messageText": "Unknown compiler option 'invalid'.",
"category": 1,
"code": 5023
}
],
"stats": [["a", 12]]
}),
)
.expect("should have invoked op");
assert_eq!(actual, json!(true));
let args = serde_json::from_value(json!({
"diagnostics": [
{
"messageText": "Unknown compiler option 'invalid'.",
"category": 1,
"code": 5023
}
],
"stats": [["a", 12]]
}))
.unwrap();
op_respond::call(&mut state, args);
let state = state.borrow::<State>();
assert_eq!(
state.maybe_response,