From 982d99a180de7706dcc59575f366a3f250c26e14 Mon Sep 17 00:00:00 2001 From: bokuweb Date: Wed, 8 Jul 2020 15:08:37 +0900 Subject: [PATCH] fix: update return values (#37) --- raiden-derive/src/lib.rs | 1 + raiden-derive/src/ops/query.rs | 2 -- raiden-derive/src/ops/update.rs | 29 ++++++++++++++++------------- raiden/examples/update.rs | 6 +++--- raiden/src/errors/mod.rs | 30 ++++++++++++++++++++++++++++++ raiden/src/ops/mod.rs | 1 + raiden/src/ops/update.rs | 9 +++++++++ 7 files changed, 60 insertions(+), 18 deletions(-) create mode 100644 raiden/src/ops/update.rs diff --git a/raiden-derive/src/lib.rs b/raiden-derive/src/lib.rs index e54518d0..606abe0e 100644 --- a/raiden-derive/src/lib.rs +++ b/raiden-derive/src/lib.rs @@ -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, diff --git a/raiden-derive/src/ops/query.rs b/raiden-derive/src/ops/query.rs index 1a00692d..a2bb3edb 100644 --- a/raiden-derive/src/ops/query.rs +++ b/raiden-derive/src/ops/query.rs @@ -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); diff --git a/raiden-derive/src/ops/update.rs b/raiden-derive/src/ops/update.rs index 36495b6e..2198dc84 100644 --- a/raiden-derive/src/ops/update.rs +++ b/raiden-derive/src/ops/update.rs @@ -3,6 +3,7 @@ use quote::*; pub(crate) fn expand_update_item( partition_key: &proc_macro2::Ident, sort_key: &Option, + fields: &syn::FieldsNamed, attr_enum_name: &proc_macro2::Ident, struct_name: &proc_macro2::Ident, rename_all_type: crate::rename::RenameAllType, @@ -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)] @@ -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, @@ -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(); @@ -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" diff --git a/raiden/examples/update.rs b/raiden/examples/update.rs index 5b722973..1bc1fb42 100644 --- a/raiden/examples/update.rs +++ b/raiden/examples/update.rs @@ -1,6 +1,6 @@ use raiden::*; -#[derive(Raiden)] +#[derive(Raiden, Debug)] #[raiden(table_name = "UpdateTestData0")] pub struct Example { #[raiden(partition_key)] @@ -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()); } diff --git a/raiden/src/errors/mod.rs b/raiden/src/errors/mod.rs index be73f142..3fce7c1b 100644 --- a/raiden/src/errors/mod.rs +++ b/raiden/src/errors/mod.rs @@ -121,6 +121,36 @@ impl From> for RaidenError { } } +impl From> for RaidenError { + fn from(error: RusotoError) -> 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> for RaidenError { fn from(error: RusotoError) -> Self { match error { diff --git a/raiden/src/ops/mod.rs b/raiden/src/ops/mod.rs index 85eead13..dbcfffd7 100644 --- a/raiden/src/ops/mod.rs +++ b/raiden/src/ops/mod.rs @@ -1,5 +1,6 @@ pub mod get; pub mod put; +pub mod update; pub mod query; pub mod transact_write; diff --git a/raiden/src/ops/update.rs b/raiden/src/ops/update.rs new file mode 100644 index 00000000..c3725513 --- /dev/null +++ b/raiden/src/ops/update.rs @@ -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 { + pub consumed_capacity: Option, + pub item: T, + pub item_collection_metrics: Option, +}