Skip to content

Commit

Permalink
Merge branch 'aws:main' into sort_python
Browse files Browse the repository at this point in the history
  • Loading branch information
GilboaAWS authored Jun 3, 2024
2 parents 1437f5f + e93129a commit 5a68de8
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 24 deletions.
11 changes: 5 additions & 6 deletions glide-core/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use std::ops::Deref;
use std::time::Duration;
pub use types::*;

use self::value_conversion::{convert_to_expected_type, expected_type_for_cmd};
use self::value_conversion::{convert_to_expected_type, expected_type_for_cmd, get_value_type};
mod reconnecting_connection;
mod standalone_client;
mod value_conversion;
Expand Down Expand Up @@ -135,10 +135,9 @@ fn parse_timeout_to_f64(cmd: &Cmd, timeout_idx: usize) -> RedisResult<f64> {
ErrorKind::ResponseError,
err_msg,
format!(
"Expected to find timeout value at index {:?} for command {:?}. Recieved command = {:?}",
"Expected to find timeout value at index {:?} for command {:?}.",
timeout_idx,
std::str::from_utf8(&cmd.command().unwrap_or_default()),
cmd
),
))
};
Expand Down Expand Up @@ -166,7 +165,7 @@ fn get_timeout_from_cmd_arg(
Err(RedisError::from((
ErrorKind::ResponseError,
"Timeout cannot be negative",
format!("Recieved timeout={:?}", timeout_secs),
format!("Received timeout = {:?}.", timeout_secs),
)))
} else if timeout_secs == 0.0 {
// `0` means we should set no timeout
Expand All @@ -177,7 +176,7 @@ fn get_timeout_from_cmd_arg(
Err(RedisError::from((
ErrorKind::ResponseError,
"Timeout is out of range, max timeout is 2^32 - 1 (u32::MAX)",
format!("Recieved timeout={:?}", timeout_secs),
format!("Received timeout = {:?}.", timeout_secs),
)))
} else {
// Extend the request timeout to ensure we don't timeout before receiving a response from the server.
Expand Down Expand Up @@ -262,7 +261,7 @@ impl Client {
return Err((
ErrorKind::ResponseError,
"Received non-array response for transaction",
format!("Response: `{value:?}`"),
format!("(response was {:?})", get_value_type(&value)),
)
.into());
}
Expand Down
56 changes: 38 additions & 18 deletions glide-core/src/client/value_conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,13 @@ pub(crate) fn convert_to_expected_type(
_ => Err((
ErrorKind::TypeError,
"Response couldn't be converted to map",
format!("(response was {:?})", value),
format!("(response was {:?})", get_value_type(&value)),
)
.into()),
},
ExpectedReturnType::MapOfStringToDouble => match value {
Value::Nil => Ok(value),
Value::Map(map) => {
let map_clone = map.clone();
let result = map
.into_iter()
.map(|(key, inner_value)| {
Expand All @@ -68,7 +67,7 @@ pub(crate) fn convert_to_expected_type(
_ => Err((
ErrorKind::TypeError,
"Response couldn't be converted to map of {string: double}",
format!("(response was {:?})", map_clone),
format!("(response was {:?})", get_value_type(&inner_value)),
)
.into()),
}
Expand All @@ -85,7 +84,7 @@ pub(crate) fn convert_to_expected_type(
_ => Err((
ErrorKind::TypeError,
"Response couldn't be converted to map of {string: double}",
format!("(response was {:?})", value),
format!("(response was {:?})", get_value_type(&value)),
)
.into()),
},
Expand All @@ -96,7 +95,7 @@ pub(crate) fn convert_to_expected_type(
_ => Err((
ErrorKind::TypeError,
"Response couldn't be converted to set",
format!("(response was {:?})", value),
format!("(response was {:?})", get_value_type(&value)),
)
.into()),
},
Expand Down Expand Up @@ -125,7 +124,7 @@ pub(crate) fn convert_to_expected_type(
_ => Err((
ErrorKind::TypeError,
"Response couldn't be converted to Array (ZRankResponseType)",
format!("(response was {:?})", value),
format!("(response was {:?})", get_value_type(&value)),
)
.into()),
},
Expand All @@ -143,7 +142,7 @@ pub(crate) fn convert_to_expected_type(
_ => Err((
ErrorKind::TypeError,
"Could not convert value to boolean",
format!("(value was {:?})", item),
format!("(response was {:?})", get_value_type(&item)),
)
.into()),
},
Expand All @@ -152,20 +151,20 @@ pub(crate) fn convert_to_expected_type(

converted_array.map(Value::Array)
}
Value::BulkString(bytes) => match std::str::from_utf8(&bytes) {
Value::BulkString(ref bytes) => match std::str::from_utf8(bytes) {
Ok("true") => Ok(Value::Boolean(true)),
Ok("false") => Ok(Value::Boolean(false)),
_ => Err((
ErrorKind::TypeError,
"Response couldn't be converted to boolean",
format!("(response was {:?})", bytes),
format!("(response was {:?})", get_value_type(&value)),
)
.into()),
},
_ => Err((
ErrorKind::TypeError,
"Response couldn't be converted to Json Toggle return type",
format!("(response was {:?})", value),
format!("(response was {:?})", get_value_type(&value)),
)
.into()),
},
Expand All @@ -174,7 +173,7 @@ pub(crate) fn convert_to_expected_type(
_ => Err((
ErrorKind::TypeError,
"Response couldn't be converted to an array of boolean",
format!("(response was {:?})", value),
format!("(response was {:?})", get_value_type(&value)),
)
.into()),
},
Expand All @@ -191,7 +190,7 @@ pub(crate) fn convert_to_expected_type(
_ => Err((
ErrorKind::TypeError,
"Response couldn't be converted to an array of doubles",
format!("(response was {:?})", value),
format!("(response was {:?})", get_value_type(&value)),
)
.into()),
},
Expand Down Expand Up @@ -223,7 +222,7 @@ pub(crate) fn convert_to_expected_type(
_ => Err((
ErrorKind::TypeError,
"Response couldn't be converted to ZMPOP return type",
format!("(response was {:?})", value),
format!("(response was {:?})", get_value_type(&value)),
)
.into()),
},
Expand Down Expand Up @@ -257,7 +256,7 @@ pub(crate) fn convert_to_expected_type(
_ => Err((
ErrorKind::TypeError,
"Response couldn't be converted to an array of array of double or null. Inner value of Array must be Array or Null",
format!("(Inner value was {:?})", item),
format!("(response was {:?})", get_value_type(&item)),
)
.into()),
})
Expand All @@ -268,7 +267,7 @@ pub(crate) fn convert_to_expected_type(
_ => Err((
ErrorKind::TypeError,
"Response couldn't be converted to an array of array of double or null",
format!("(response was {:?})", value),
format!("(response was {:?})", get_value_type(&value)),
)
.into()),
},
Expand Down Expand Up @@ -310,7 +309,7 @@ pub(crate) fn convert_to_expected_type(
_ => Err((
ErrorKind::TypeError,
"LOLWUT response couldn't be converted to a user-friendly format",
(&format!("(response was {:?}...)", value)[..100]).into(),
format!("(response was {:?})", get_value_type(&value)),
)
.into()),
}
Expand Down Expand Up @@ -375,7 +374,7 @@ pub(crate) fn convert_to_expected_type(
_ => Err((
ErrorKind::TypeError,
"Response couldn't be converted to an array containing a key, member, and score",
format!("(response was {:?})", value),
format!("(response was {:?})", get_value_type(&value)),
)
.into()),
},
Expand Down Expand Up @@ -490,7 +489,7 @@ fn convert_to_array_of_pairs(
_ => Err((
ErrorKind::TypeError,
"Response couldn't be converted to an array of key-value pairs",
format!("(response was {:?})", response),
format!("(response was {:?})", get_value_type(&response)),
)
.into()),
}
Expand Down Expand Up @@ -571,6 +570,27 @@ pub(crate) fn expected_type_for_cmd(cmd: &Cmd) -> Option<ExpectedReturnType> {
}
}

/// Gets the enum variant as a string for the `value` given.
pub(crate) fn get_value_type<'a>(value: &Value) -> &'a str {
match value {
Value::Nil => "Nil",
Value::Int(_) => "Int",
Value::BulkString(_) => "BulkString",
Value::Array(_) => "Array",
Value::SimpleString(_) => "SimpleString",
Value::Okay => "OK",
Value::Map(_) => "Map",
Value::Attribute { .. } => "Attribute",
Value::Set(_) => "Set",
Value::Double(_) => "Double",
Value::Boolean(_) => "Boolean",
Value::VerbatimString { .. } => "VerbatimString",
Value::BigNumber(_) => "BigNumber",
Value::Push { .. } => "Push",
// TODO Value::ServerError from https://github.com/redis-rs/redis-rs/pull/1093
}
}

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

0 comments on commit 5a68de8

Please sign in to comment.