Skip to content

Commit

Permalink
Merge pull request #30 from kristoferB/master
Browse files Browse the repository at this point in the history
Update to redis 0.26
  • Loading branch information
daniel7grant authored Aug 5, 2024
2 parents a0e635f + 6d5050b commit b1df4b2
Show file tree
Hide file tree
Showing 9 changed files with 117 additions and 37 deletions.
57 changes: 47 additions & 10 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ license = "MIT"
keywords = ["redis", "macro", "derive", "json"]

[dependencies]
redis = { version = "0.25", optional = true }
redis = { version = "0.26", optional = true }
redis-macros-derive = { version = "0.3.0", optional = true, path = "./redis-macros-derive" }
serde = { version = "1.0", features = ["derive"], optional = true }
serde_json = { version = "1.0", optional = true }
Expand All @@ -22,7 +22,7 @@ json = ["dep:redis", "dep:serde", "dep:serde_json"]
macros = ["dep:redis-macros-derive"]

[dev-dependencies]
deadpool-redis = "0.15"
redis = { version = "0.25", features = ["tokio-comp", "json"] }
deadpool-redis = { version = "0.16.0" }
redis = { version = "0.26.0", features = ["tokio-comp", "json"] }
serde_yaml = "0.9"
tokio = { version = "1.38", features = ["full"] }
59 changes: 51 additions & 8 deletions redis-macros-derive/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion redis-macros-derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ quote = "1.0"
syn = { version = "2.0" }

[dev-dependencies]
redis = { version = "0.25", features = ["tokio-comp", "json"] }
redis = { version = "0.26.0", features = ["tokio-comp", "json"] }
redis-macros = { path = ".." }
serde = { version = "1.0", features = ["derive"] }
serde_json = { version = "1.0" }
Expand Down
2 changes: 1 addition & 1 deletion redis-macros-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ pub fn from_redis_value_macro(input: TokenStream) -> TokenStream {
impl #impl_generics redis::FromRedisValue for #ident #ty_generics #where_with_serialize {
fn from_redis_value(v: &redis::Value) -> redis::RedisResult<Self> {
match *v {
redis::Value::Data(ref bytes) => {
redis::Value::BulkString(ref bytes) => {
if let Ok(s) = std::str::from_utf8(bytes) {
if let Ok(s) = #serializer::from_str(s) {
Ok(s)
Expand Down
2 changes: 1 addition & 1 deletion src/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ where
{
fn from_redis_value(v: &Value) -> RedisResult<Self> {
match *v {
Value::Data(ref bytes) => {
Value::BulkString(ref bytes) => {
if let Ok(s) = ::std::str::from_utf8(bytes) {
let mut ch = s.chars();
if ch.next() == Some('[') && ch.next_back() == Some(']') {
Expand Down
10 changes: 5 additions & 5 deletions tests/derive_from_redis_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub fn it_should_implement_the_from_redis_value_trait() {
],
};

let val = Value::Data("{\"id\":1,\"name\":\"Ziggy\",\"addresses\":[{\"Street\":\"Downing\"},{\"Road\":\"Abbey\"}]}".as_bytes().into());
let val = Value::BulkString("{\"id\":1,\"name\":\"Ziggy\",\"addresses\":[{\"Street\":\"Downing\"},{\"Road\":\"Abbey\"}]}".as_bytes().into());
let result = User::from_redis_value(&val);
assert_eq!(result, Ok(user));
}
Expand All @@ -42,25 +42,25 @@ pub fn it_should_also_deserialize_if_the_input_is_in_brackets() {
],
};

let val = Value::Data("[{\"id\":1,\"name\":\"Ziggy\",\"addresses\":[{\"Street\":\"Downing\"},{\"Road\":\"Abbey\"}]}]".as_bytes().into());
let val = Value::BulkString("[{\"id\":1,\"name\":\"Ziggy\",\"addresses\":[{\"Street\":\"Downing\"},{\"Road\":\"Abbey\"}]}]".as_bytes().into());
let result = User::from_redis_value(&val);
assert_eq!(result, Ok(user));
}

#[test]
pub fn it_should_fail_if_input_is_not_compatible_with_type() {
let val = Value::Data("{}".as_bytes().into());
let val = Value::BulkString("{}".as_bytes().into());
let result = User::from_redis_value(&val);
if let Err(err) = result {
assert_eq!(err.to_string(), "Response was of incompatible type - TypeError: Response type not deserializable to User with serde_json. (response was string-data('\"{}\"'))".to_string());
assert_eq!(err.to_string(), "Response was of incompatible type - TypeError: Response type not deserializable to User with serde_json. (response was bulk-string('\"{}\"'))".to_string());
} else {
panic!("Deserialization should fail.");
}
}

#[test]
pub fn it_should_fail_if_input_is_not_valid_utf8() {
let val = Value::Data(vec![0, 159, 146, 150]); // Some invalid utf8
let val = Value::BulkString(vec![0, 159, 146, 150]); // Some invalid utf8
let result = User::from_redis_value(&val);
if let Err(err) = result {
assert_eq!(err.to_string(), "Response was of incompatible type - TypeError: Response was not valid UTF-8 string. (response was binary-data([0, 159, 146, 150]))".to_string());
Expand Down
2 changes: 1 addition & 1 deletion tests/derive_from_redis_value_redis_yaml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub fn it_should_implement_the_from_redis_value_trait_with_redis_yaml() {
],
};

let val = Value::Data(
let val = Value::BulkString(
"id: 1
name: Ziggy
addresses:
Expand Down
14 changes: 7 additions & 7 deletions tests/json_wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub fn it_should_deserialize_json_results() {
],
};

let val = Value::Data("[{\"id\":1,\"name\":\"Ziggy\",\"addresses\":[{\"Street\":\"Downing\"},{\"Road\":\"Abbey\"}]}]".as_bytes().into());
let val = Value::BulkString("[{\"id\":1,\"name\":\"Ziggy\",\"addresses\":[{\"Street\":\"Downing\"},{\"Road\":\"Abbey\"}]}]".as_bytes().into());
let result = Json::<User>::from_redis_value(&val);
if let Ok(Json(parsed_user)) = result {
assert_eq!(parsed_user, user);
Expand All @@ -42,7 +42,7 @@ pub fn it_should_also_deserialize_json_wrappable_arguments() {
Address::Road("Abbey".to_string()),
];

let val = Value::Data(
let val = Value::BulkString(
"[[{\"Street\":\"Downing\"},{\"Road\":\"Abbey\"}]]"
.as_bytes()
.into(),
Expand All @@ -59,29 +59,29 @@ pub fn it_should_also_deserialize_json_wrappable_arguments() {
#[test]
pub fn it_should_fail_if_the_result_is_not_redis_json() {
// RedisJSON responses should have wrapping brackets (i.e. [{...}])
let val = Value::Data("{\"id\":1,\"name\":\"Ziggy\",\"addresses\":[{\"Street\":\"Downing\"},{\"Road\":\"Abbey\"}]}".as_bytes().into());
let val = Value::BulkString("{\"id\":1,\"name\":\"Ziggy\",\"addresses\":[{\"Street\":\"Downing\"},{\"Road\":\"Abbey\"}]}".as_bytes().into());
let result = Json::<User>::from_redis_value(&val);
if let Err(err) = result {
assert_eq!(err.to_string(), "Response was of incompatible type - TypeError: Response type was not JSON type. (response was string-data('\"{\\\"id\\\":1,\\\"name\\\":\\\"Ziggy\\\",\\\"addresses\\\":[{\\\"Street\\\":\\\"Downing\\\"},{\\\"Road\\\":\\\"Abbey\\\"}]}\"'))".to_string());
assert_eq!(err.to_string(), "Response was of incompatible type - TypeError: Response type was not JSON type. (response was bulk-string('\"{\\\"id\\\":1,\\\"name\\\":\\\"Ziggy\\\",\\\"addresses\\\":[{\\\"Street\\\":\\\"Downing\\\"},{\\\"Road\\\":\\\"Abbey\\\"}]}\"'))".to_string());
} else {
panic!("RedisJSON unwrapping should fail.");
}
}

#[test]
pub fn it_should_fail_if_input_is_not_compatible_with_type() {
let val = Value::Data("[{}]".as_bytes().into());
let val = Value::BulkString("[{}]".as_bytes().into());
let result = Json::<User>::from_redis_value(&val);
if let Err(err) = result {
assert_eq!(err.to_string(), "Response was of incompatible type - TypeError: Response type in JSON was not deserializable. (response was string-data('\"[{}]\"'))".to_string());
assert_eq!(err.to_string(), "Response was of incompatible type - TypeError: Response type in JSON was not deserializable. (response was bulk-string('\"[{}]\"'))".to_string());
} else {
panic!("Deserialization should fail.");
}
}

#[test]
pub fn it_should_fail_if_input_is_not_valid_utf8() {
let val = Value::Data(vec![0, 159, 146, 150]); // Some invalid utf8
let val = Value::BulkString(vec![0, 159, 146, 150]); // Some invalid utf8
let result = Json::<User>::from_redis_value(&val);
if let Err(err) = result {
assert_eq!(err.to_string(), "Response was of incompatible type - TypeError: Response was not valid UTF-8 string. (response was binary-data([0, 159, 146, 150]))".to_string());
Expand Down

0 comments on commit b1df4b2

Please sign in to comment.