-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented abstraction for HGET command
- Loading branch information
1 parent
74e26af
commit fd6459d
Showing
6 changed files
with
226 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
use crate::commands::auth::AuthCommand; | ||
use crate::commands::builder::{CommandBuilder, IsNullFrame, ToStringBytes}; | ||
use crate::commands::get::GetResponse; | ||
use crate::commands::hello::HelloCommand; | ||
use crate::commands::{Command, ResponseTypeError}; | ||
use crate::network::protocol::Protocol; | ||
use crate::network::{Client, CommandErrors, Future}; | ||
use bytes::Bytes; | ||
use embedded_nal::TcpClientStack; | ||
use embedded_time::Clock; | ||
|
||
/// Abstraction for HGET command | ||
pub struct HashGetCommand { | ||
/// Hash key | ||
key: Bytes, | ||
|
||
/// Hash field to receive | ||
field: Bytes, | ||
} | ||
|
||
impl HashGetCommand { | ||
pub fn new<K, F>(key: K, field: F) -> Self | ||
where | ||
Bytes: From<K>, | ||
Bytes: From<F>, | ||
{ | ||
Self { | ||
key: key.into(), | ||
field: field.into(), | ||
} | ||
} | ||
} | ||
|
||
impl<F> Command<F> for HashGetCommand | ||
where | ||
F: From<CommandBuilder> + IsNullFrame + ToStringBytes, | ||
{ | ||
type Response = Option<GetResponse>; | ||
|
||
fn encode(&self) -> F { | ||
CommandBuilder::new("HGET").arg(&self.key).arg(&self.field).into() | ||
} | ||
|
||
fn eval_response(&self, frame: F) -> Result<Self::Response, ResponseTypeError> { | ||
GetResponse::from_frame(frame) | ||
} | ||
} | ||
|
||
impl<'a, N: TcpClientStack, C: Clock, P: Protocol> Client<'a, N, C, P> | ||
where | ||
AuthCommand: Command<<P as Protocol>::FrameType>, | ||
HelloCommand: Command<<P as Protocol>::FrameType>, | ||
{ | ||
/// Shorthand for [GetCommand] | ||
pub fn hget<K, F>( | ||
&'a self, | ||
key: K, | ||
field: F, | ||
) -> Result<Future<'a, N, C, P, HashGetCommand>, CommandErrors> | ||
where | ||
<P as Protocol>::FrameType: ToStringBytes, | ||
<P as Protocol>::FrameType: IsNullFrame, | ||
<P as Protocol>::FrameType: From<CommandBuilder>, | ||
Bytes: From<K>, | ||
Bytes: From<F>, | ||
{ | ||
self.send(HashGetCommand::new(key, field)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
use crate::commands::helpers::CmdStr; | ||
use crate::commands::hget::HashGetCommand; | ||
use crate::commands::Command; | ||
use redis_protocol::resp2::types::Frame as Resp2Frame; | ||
use redis_protocol::resp3::types::Frame as Resp3Frame; | ||
|
||
#[test] | ||
fn test_encode_resp2() { | ||
let frame: Resp2Frame = HashGetCommand::new("my_hash", "color").encode(); | ||
|
||
assert!(frame.is_array()); | ||
if let Resp2Frame::Array(array) = frame { | ||
assert_eq!(3, array.len()); | ||
assert_eq!("HGET", array[0].to_string().unwrap()); | ||
assert_eq!("my_hash", array[1].to_string().unwrap()); | ||
assert_eq!("color", array[2].to_string().unwrap()); | ||
} | ||
} | ||
|
||
#[test] | ||
fn test_encode_resp3() { | ||
let frame: Resp3Frame = HashGetCommand::new("my_hash", "color").encode(); | ||
|
||
assert!(frame.is_array()); | ||
if let Resp3Frame::Array { data, attributes: _ } = frame { | ||
assert_eq!(3, data.len()); | ||
assert_eq!("HGET", data[0].to_string().unwrap()); | ||
assert_eq!("my_hash", data[1].to_string().unwrap()); | ||
assert_eq!("color", data[2].to_string().unwrap()); | ||
} | ||
} | ||
|
||
#[test] | ||
fn test_eval_response_resp2_key_existing() { | ||
let response = HashGetCommand::new("my_hash", "color") | ||
.eval_response(CmdStr::new("correct response1").to_bulk()) | ||
.unwrap(); | ||
|
||
assert_eq!("correct response1", response.unwrap().as_str().unwrap()); | ||
} | ||
|
||
#[test] | ||
fn test_eval_response_resp3_key_existing() { | ||
let response = HashGetCommand::new("my_hash", "color") | ||
.eval_response(CmdStr::new("correct").to_blob()) | ||
.unwrap(); | ||
|
||
assert_eq!("correct", response.unwrap().as_str().unwrap()); | ||
} | ||
|
||
#[test] | ||
fn test_eval_response_resp2_key_missing() { | ||
let response = HashGetCommand::new("my_hash", "color").eval_response(Resp2Frame::Null).unwrap(); | ||
|
||
assert!(response.is_none()); | ||
} | ||
|
||
#[test] | ||
fn test_eval_response_resp3_key_missing() { | ||
let response = HashGetCommand::new("my_hash", "color").eval_response(Resp3Frame::Null).unwrap(); | ||
|
||
assert!(response.is_none()); | ||
} | ||
|
||
#[test] | ||
fn test_eval_response_resp2_invalid_response() { | ||
let response = HashGetCommand::new("my_hash", "color").eval_response(Resp2Frame::Array(vec![])); | ||
|
||
assert!(response.is_err()); | ||
} | ||
|
||
#[test] | ||
fn test_eval_response_resp3_invalid_response() { | ||
let response = HashGetCommand::new("my_hash", "color").eval_response(Resp3Frame::Array { | ||
data: vec![], | ||
attributes: None, | ||
}); | ||
|
||
assert!(response.is_err()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ mod bgsave; | |
mod custom; | ||
mod get; | ||
pub(crate) mod hello; | ||
mod hget; | ||
mod hset; | ||
mod ping; | ||
mod publish; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters