Skip to content

Commit

Permalink
fix: fix policy expr type errors due to X.0 floating points in JSON b…
Browse files Browse the repository at this point in the history
…eing treated as ints
  • Loading branch information
j-lanson committed Sep 26, 2024
1 parent a3f9994 commit 9e72eab
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions hipcheck/src/policy_exprs/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,10 @@ fn partially_evaluate(fn_name: &'static str, arg: Expr) -> Result<Expr> {
Ok(lambda)
}

pub fn upcast(i: i64) -> Primitive {
Float(F64::new(i as f64).unwrap())
}

/// Define binary operations on primitives.
fn binary_primitive_op<F>(name: &'static str, env: &Env, args: &[Expr], op: F) -> Result<Expr>
where
Expand All @@ -165,6 +169,8 @@ where

let primitive = match (&arg_1, &arg_2) {
(Int(_), Int(_)) | (Float(_), Float(_)) | (Bool(_), Bool(_)) => op(arg_1, arg_2)?,
(Int(i), Float(_)) => op(upcast(*i), arg_2)?,
(Float(_), Int(i)) => op(arg_1, upcast(*i))?,
_ => return Err(Error::BadType(name)),
};

Expand Down Expand Up @@ -454,9 +460,9 @@ fn add(env: &Env, args: &[Expr]) -> Result<Expr> {
let op = |arg_1, arg_2| match (arg_1, arg_2) {
(Int(arg_1), Int(arg_2)) => Ok(Int(arg_1 + arg_2)),
(Float(arg_1), Float(arg_2)) => Ok(Float(arg_1 + arg_2)),
(DateTime(arg_1), Span(arg_2)) => Ok(DateTime(
arg_1
.checked_add(arg_2)
// Span or DateTime can come first
(DateTime(dt), Span(s)) | (Span(s), DateTime(dt)) => Ok(DateTime(
dt.checked_add(s)
.map_err(|err| Error::Datetime(err.to_string()))?,
)),
(Span(arg_1), Span(arg_2)) => Ok(Span(
Expand Down

0 comments on commit 9e72eab

Please sign in to comment.