Skip to content

Commit

Permalink
Implement core::ops::Mul for objects.
Browse files Browse the repository at this point in the history
  • Loading branch information
kklingenberg committed Nov 21, 2023
1 parent 854d22c commit d2e9e27
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
11 changes: 11 additions & 0 deletions jaq-interpret/src/val.rs
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,17 @@ impl core::ops::Mul for Val {
(Str(_), Int(_)) | (Int(_), Str(_)) => Ok(Null),
(Num(n), r) => Self::from_dec_str(&n) * r,
(l, Num(n)) => l * Self::from_dec_str(&n),
(Obj(mut l), Obj(r)) => {
let inner = Rc::make_mut(&mut l);
for (k, v) in r.iter() {
let merged = match (inner.get(k), v) {
(Some(nl @ Obj(_)), nr @ Obj(_)) => nl.clone() * nr.clone(),
_ => Ok(v.clone()),
}?;
inner.insert(k.clone(), merged);
}
Ok(Obj(l))
}
(l, r) => Err(Error::MathOp(l, MathOp::Mul, r)),
}
}
Expand Down
5 changes: 5 additions & 0 deletions jaq-interpret/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ fn mul() {

give(json!("Hello"), "0 * .", json!(null));
give(json!(-1), ". * \"Hello\"", json!(null));
give(
json!({"k": {"a": 1, "b": 2}}),
". * {k: {a: 0, c: 3}}",
json!({"k": {"a": 0, "b": 2, "c": 3}}),
);
}

#[test]
Expand Down

0 comments on commit d2e9e27

Please sign in to comment.