Skip to content

Commit

Permalink
When receiving ZADD with INCR modifier, convert result to double.
Browse files Browse the repository at this point in the history
  • Loading branch information
nihohit committed Jan 15, 2024
1 parent 523a089 commit d48c00b
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions glide-core/src/client/value_conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ pub(crate) fn convert_to_expected_type(
}

pub(crate) fn expected_type_for_cmd(cmd: &Cmd) -> Option<ExpectedReturnType> {
if cmd.arg_idx(0) == Some(b"ZADD") {
return if cmd.position(b"INCR").is_some() {
Some(ExpectedReturnType::Double)
} else {
None
};
}

let command = cmd.command()?;

match command.as_slice() {
Expand All @@ -73,3 +81,28 @@ pub(crate) fn expected_type_for_cmd(cmd: &Cmd) -> Option<ExpectedReturnType> {
_ => None,
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn convert_zadd_only_if_incr_is_included() {
assert!(matches!(
expected_type_for_cmd(
redis::cmd("ZADD")
.arg("XT")
.arg("CH")
.arg("INCR")
.arg("0.6")
.arg("foo")
),
Some(ExpectedReturnType::Double)
));

assert!(expected_type_for_cmd(
redis::cmd("ZADD").arg("XT").arg("CH").arg("0.6").arg("foo")
)
.is_none());
}
}

0 comments on commit d48c00b

Please sign in to comment.