Skip to content

Commit

Permalink
fix: update return values (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
bokuweb authored Jul 8, 2020
1 parent 3f75d48 commit 982d99a
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 18 deletions.
1 change: 1 addition & 0 deletions raiden-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ pub fn derive_raiden(input: TokenStream) -> TokenStream {
let update_item = ops::expand_update_item(
&partition_key,
&sort_key,
&fields,
&attr_enum_name,
&struct_name,
rename_all_type,
Expand Down
2 changes: 0 additions & 2 deletions raiden-derive/src/ops/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,6 @@ pub(crate) fn expand_query(

let mut items: Vec<#struct_name> = vec![];

dbg!(&self.input);

loop {
if let Some(limit) = self.limit {
self.input.limit = Some(limit);
Expand Down
29 changes: 16 additions & 13 deletions raiden-derive/src/ops/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use quote::*;
pub(crate) fn expand_update_item(
partition_key: &proc_macro2::Ident,
sort_key: &Option<proc_macro2::Ident>,
fields: &syn::FieldsNamed,
attr_enum_name: &proc_macro2::Ident,
struct_name: &proc_macro2::Ident,
rename_all_type: crate::rename::RenameAllType,
Expand All @@ -12,6 +13,7 @@ pub(crate) fn expand_update_item(
let update_expression_name = format_ident!("{}UpdateExpression", struct_name);
let client_name = format_ident!("{}Client", struct_name);
let builder_name = format_ident!("{}UpdateItemBuilder", struct_name);
let from_item = super::expand_attr_to_item(&format_ident!("res_item"), fields, rename_all_type);

quote! {
#[derive(Debug, Clone, PartialEq)]
Expand Down Expand Up @@ -45,6 +47,7 @@ pub(crate) fn expand_update_item(
key.insert(stringify!(#partition_key).to_owned(), key_attr);
input.key = key;
input.table_name = self.table_name();
input.return_values = Some("ALL_NEW".to_owned());
#builder_name {
client: &self.client,
input,
Expand Down Expand Up @@ -104,12 +107,6 @@ pub(crate) fn expand_update_item(
self
}

// INFO: raiden supports only none, all_old and all_new to map response to struct.
pub fn return_all_new(mut self) -> Self {
self.input.return_values = Some("ALL_NEW".to_owned());
self
}

fn build_expression(&mut self) -> (String, ::raiden::AttributeNames , ::raiden::AttributeValues) {
let mut attr_names: ::raiden::AttributeNames = std::collections::HashMap::new();
let mut attr_values: ::raiden::AttributeValues = std::collections::HashMap::new();
Expand Down Expand Up @@ -169,18 +166,24 @@ pub(crate) fn expand_update_item(
}


pub async fn run(mut self) -> Result<(), ()> {
pub async fn run(mut self) -> Result<::raiden::update::UpdateOutput<#struct_name>, ::raiden::RaidenError> {
let (expression, names, values) = self.build_expression();
self.input.expression_attribute_names = Some(names);
self.input.expression_attribute_values = Some(values);
self.input.update_expression = Some(expression);
self.input.return_values = Some("ALL_NEW".to_owned());
let res = self.client.update_item(self.input).await;
dbg!(&res);
Ok(())
let res = self.client.update_item(self.input).await?;

let res_item = &res.attributes.unwrap();
let item = #struct_name {
#(#from_item)*
};

Ok(::raiden::update::UpdateOutput {
item,
consumed_capacity: res.consumed_capacity,
item_collection_metrics: res.item_collection_metrics,
})
}
}
}
}

// updateExpression := " ADD ids :ids SET updatedAt = :date, #version = #version + :inc"
6 changes: 3 additions & 3 deletions raiden/examples/update.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use raiden::*;

#[derive(Raiden)]
#[derive(Raiden, Debug)]
#[raiden(table_name = "UpdateTestData0")]
pub struct Example {
#[raiden(partition_key)]
Expand All @@ -19,8 +19,8 @@ fn main() {
let set_expression = Example::update_expression()
.set(Example::name())
.value("updated!!");
let res = client.update("id0").set(set_expression).run().await;
dbg!(res);
let res = client.update("id0").set(set_expression).run().await.unwrap();
dbg!(res.item);
}
rt.block_on(example());
}
30 changes: 30 additions & 0 deletions raiden/src/errors/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,36 @@ impl From<RusotoError<PutItemError>> for RaidenError {
}
}

impl From<RusotoError<UpdateItemError>> for RaidenError {
fn from(error: RusotoError<UpdateItemError>) -> Self {
match error {
RusotoError::Service(error) => match error {
UpdateItemError::InternalServerError(msg) => RaidenError::InternalServerError(msg),
UpdateItemError::ProvisionedThroughputExceeded(msg) => {
RaidenError::ProvisionedThroughputExceeded(msg)
}
UpdateItemError::RequestLimitExceeded(msg) => {
RaidenError::RequestLimitExceeded(msg)
}
UpdateItemError::ResourceNotFound(msg) => RaidenError::ResourceNotFound(msg),
UpdateItemError::ConditionalCheckFailed(msg) => {
RaidenError::ConditionalCheckFailed(msg)
}
UpdateItemError::ItemCollectionSizeLimitExceeded(msg) => {
RaidenError::ItemCollectionSizeLimitExceeded(msg)
}
UpdateItemError::TransactionConflict(msg) => RaidenError::TransactionConflict(msg),
},
RusotoError::HttpDispatch(e) => RaidenError::HttpDispatch(e),
RusotoError::Credentials(e) => RaidenError::Credentials(e),
RusotoError::Validation(msg) => RaidenError::Validation(msg),
RusotoError::ParseError(msg) => RaidenError::ParseError(msg),
RusotoError::Unknown(res) => RaidenError::Unknown(res),
RusotoError::Blocking => RaidenError::Blocking,
}
}
}

impl From<RusotoError<TransactWriteItemsError>> for RaidenError {
fn from(error: RusotoError<TransactWriteItemsError>) -> Self {
match error {
Expand Down
1 change: 1 addition & 0 deletions raiden/src/ops/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub mod get;
pub mod put;
pub mod update;
pub mod query;
pub mod transact_write;

Expand Down
9 changes: 9 additions & 0 deletions raiden/src/ops/update.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use serde::{Deserialize, Serialize};

// See. https://github.com/rusoto/rusoto/blob/cf22a4348ae717a20760bb9934cfd118ddb4437e/rusoto/services/dynamodb/src/generated.rs#L2971
#[derive(Default, Debug, Clone, PartialEq, Deserialize, Serialize)]
pub struct UpdateOutput<T> {
pub consumed_capacity: Option<crate::ConsumedCapacity>,
pub item: T,
pub item_collection_metrics: Option<crate::ItemCollectionMetrics>,
}

0 comments on commit 982d99a

Please sign in to comment.