Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update RpcClient::invoke_method() to return a UMessage in support of symmetry / flexibility #27

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/rpc/rpcclient.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
use async_trait::async_trait;

use crate::rpc::rpcmapper::RpcMapperError;
use crate::uprotocol::{UAttributes, UPayload, UUri};
use crate::uprotocol::{UAttributes, UMessage, UPayload, UUri};

pub type RpcClientResult = Result<UPayload, RpcMapperError>;
pub type RpcClientResult = Result<UMessage, RpcMapperError>;

/// `RpcClient` serves as an interface to be used by code generators for uProtocol services defined in
/// `.proto` files, such as the core uProtocol services found in
Expand Down
65 changes: 46 additions & 19 deletions src/rpc/rpcmapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use crate::uprotocol::{Data, UCode, UPayload, UPayloadFormat, UStatus};

pub type RpcPayloadResult = Result<RpcPayload, RpcMapperError>;

#[derive(Clone)]
#[derive(Clone, Debug)]
pub struct RpcPayload {
pub status: UStatus,
pub payload: Option<UPayload>,
Expand Down Expand Up @@ -82,8 +82,13 @@ impl RpcMapper {
where
T: MessageFull + Default,
{
let payload = response?; // Directly returns in case of error
Any::try_from(payload)
let message = response?; // Directly returns in case of error
let Some(payload) = message.payload.as_ref() else {
return Err(RpcMapperError::InvalidPayload(
PLeVasseur marked this conversation as resolved.
Show resolved Hide resolved
"Payload is empty".to_string(),
));
};
Any::try_from(*payload)
.map_err(|_e| {
RpcMapperError::UnknownType("Couldn't decode payload into Any".to_string())
})
Expand Down Expand Up @@ -122,10 +127,17 @@ impl RpcMapper {
///
// TODO This entire thing feels klunky and kludgy; this needs to be revisited...
pub fn map_response_to_result(response: RpcClientResult) -> RpcPayloadResult {
let payload = response?; // Directly returns in case of error
Any::try_from(payload)
.map_err(|_e| {
RpcMapperError::UnknownType("Couldn't decode payload into Any".to_string())
let message = response?; // Directly returns in case of error
let Some(payload) = message.payload.as_ref() else {
return Err(RpcMapperError::InvalidPayload(
"Payload is empty".to_string(),
));
};
Any::try_from(*payload)
.map_err(|e| {
RpcMapperError::UnknownType(
format!("Couldn't decode payload into Any: {:?}", e).to_string(),
)
})
.and_then(|any| {
match Self::unpack_any::<UStatus>(&any) {
Expand Down Expand Up @@ -298,20 +310,29 @@ mod tests {
use cloudevents::{Event, EventBuilder, EventBuilderV10};

use crate::cloudevents::CloudEvent as CloudEventProto;
use crate::uprotocol::UMessageType;
use crate::uprotocol::{UMessage, UMessageType};

fn build_status_response(code: UCode, msg: &str) -> RpcClientResult {
let status = UStatus::fail_with_code(code, msg);
let any = RpcMapper::pack_any(&status)?;
Ok(any.try_into().unwrap())
let message = UMessage {
payload: Some(any.try_into().unwrap()).into(),
..Default::default()
};

Ok(message)
}

fn build_empty_payload_response() -> RpcClientResult {
let payload = UPayload {
data: Some(Data::Value(vec![])),
..Default::default()
};
Ok(payload)
let message = UMessage {
payload: Some(payload).into(),
..Default::default()
};
Ok(message)
}

fn build_number_response(number: i32) -> RpcClientResult {
Expand All @@ -320,7 +341,11 @@ mod tests {
value: number.to_be_bytes().into(),
..Default::default()
};
Ok(any.try_into().unwrap())
let message = UMessage {
payload: Some(any.try_into().unwrap()).into(),
..Default::default()
};
Ok(message)
}

fn build_cloud_event_for_test() -> Event {
Expand All @@ -332,12 +357,15 @@ mod tests {
.unwrap()
}

fn build_cloudevent_upayload_for_test() -> UPayload {
fn build_cloudevent_umessage_for_test() -> UMessage {
let event = build_cloud_event_for_test();
let proto_event = CloudEventProto::from(event);
let any = RpcMapper::pack_any(&proto_event).unwrap();

any.try_into().unwrap()
UMessage {
payload: Some(any.try_into().unwrap()).into(),
..Default::default()
}
}

#[test]
Expand All @@ -356,7 +384,6 @@ mod tests {
#[test]
fn test_compose_that_returns_status() {
let response = build_status_response(UCode::INVALID_ARGUMENT, "boom");

let result = RpcMapper::map_response_to_result(response).unwrap();

assert!(result.status.is_failed());
Expand All @@ -380,11 +407,11 @@ mod tests {

#[test]
fn test_success_invoke_method_happy_flow_using_map_response_to_rpc_response() {
let response_payload = build_cloudevent_upayload_for_test();
let response_message = build_cloudevent_umessage_for_test();

let result = RpcMapper::map_response_to_result(Ok(response_payload.clone())).unwrap();
let result = RpcMapper::map_response_to_result(Ok(response_message.clone())).unwrap();
assert!(result.status.is_failed());
assert_eq!(result.payload.unwrap(), response_payload);
assert_eq!(result.payload.unwrap(), response_message.payload.unwrap());
}

#[test]
Expand Down Expand Up @@ -417,8 +444,8 @@ mod tests {

#[test]
fn test_success_invoke_method_happy_flow_using_map_response() {
let response_payload = build_cloudevent_upayload_for_test();
let e = RpcMapper::map_response::<CloudEventProto>(Ok(response_payload)).unwrap();
let response_message = build_cloudevent_umessage_for_test();
let e = RpcMapper::map_response::<CloudEventProto>(Ok(response_message)).unwrap();
let event = Event::from(e);

assert_eq!(event, build_cloud_event_for_test());
Expand Down
Loading