Skip to content

Commit

Permalink
fix: use put_item_builder
Browse files Browse the repository at this point in the history
  • Loading branch information
bokuweb committed Jul 25, 2020
1 parent 35f5ff2 commit d539997
Show file tree
Hide file tree
Showing 14 changed files with 88 additions and 129 deletions.
90 changes: 22 additions & 68 deletions Cargo.lock

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

3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ fn main() {
let input = User::put_item_builder()
.id("mock_id".to_owned())
.name("bokuweb".to_owned())
.build()
.unwrap();
.build();
let res = client.put(&input).run().await;
}
rt.block_on(example());
Expand Down
3 changes: 2 additions & 1 deletion raiden-derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ proc-quote = "0.3.2"
syn = "^1.0"
proc-macro2 = "^1.0"
ident_case = "^1.0"
derive_builder = "^0.9"
convert_case = "^0.4.0"
safe-builder = { tag = "0.0.3", git = "https://github.com/raiden-rs/safe-builder.git" }

12 changes: 12 additions & 0 deletions raiden-derive/src/finder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,15 @@ pub(crate) fn find_sort_key_field(fields: &syn::FieldsNamed) -> Option<syn::Fiel
}
fields.get(0).cloned()
}

pub(crate) fn is_option(ty: &syn::Type) -> bool {
match ty {
syn::Type::Path(syn::TypePath {
path: syn::Path { segments, .. },
..
}) => {
segments.iter().any(|s| s.ident == "Option")
}
_ => false,
}
}
10 changes: 0 additions & 10 deletions raiden-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,13 +238,3 @@ pub fn derive_raiden(input: TokenStream) -> TokenStream {
// _ => return None,
// };
// }

fn is_option(ty: &Type) -> bool {
match ty {
Type::Path(syn::TypePath {
path: syn::Path { segments, .. },
..
}) if segments[0].ident == "Option" => true,
_ => false,
}
}
3 changes: 2 additions & 1 deletion raiden-derive/src/ops/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ pub(crate) use scan::*;
pub(crate) use shared::*;
pub(crate) use batch_get::*;
pub(crate) use transact_write::*;
pub(crate) use update::*;
pub(crate) use update::*;

33 changes: 26 additions & 7 deletions raiden-derive/src/ops/put.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::rename::*;
use quote::*;
use syn::*;

pub(crate) fn expand_put_item(
_partition_key: &proc_macro2::Ident,
Expand Down Expand Up @@ -86,19 +87,28 @@ pub(crate) fn expand_put_item(
}
};

quote! {
impl #struct_name {
pub fn put_item_builder() -> #item_input_builder_name {
#item_input_builder_name::default()
}
}
// Create default type variables for PutItemBuilder, i.e. XXXPutItemBuilder<(), (), ()>
let required_field_idents: Vec<Ident> = fields
.named
.iter()
.filter(|f| !crate::finder::include_unary_attr(&f.attrs, "uuid"))
.filter(|f| !crate::finder::is_option(&f.ty))
.map(|f| f.ident.clone().unwrap())
.collect();
let default_types = expand_default_type_variables(&required_field_idents);

quote! {
#[derive(Debug, Clone, PartialEq, Builder)]
#[builder(setter(into))]
pub struct #item_input_name {
#(#input_fields)*
}

impl #struct_name {
pub fn put_item_builder() -> #item_input_builder_name<#(#default_types)*> {
#item_input_name::builder()
}
}

#[derive(Debug, Clone, PartialEq)]
pub struct #item_output_name {
#(#output_fields)*
Expand Down Expand Up @@ -231,3 +241,12 @@ pub struct PutItemInput {
}
*/

#[allow(clippy::ptr::arg)]
fn expand_default_type_variables(
idents: &Vec<Ident>,
) -> impl Iterator<Item = proc_macro2::TokenStream> {
idents.clone().into_iter().map(|_ident| {
quote! { (), }
})
}
2 changes: 1 addition & 1 deletion raiden-derive/src/ops/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub(crate) fn expand_attr_to_item(
} else {
ident.to_string()
};
if crate::is_option(&f.ty) {
if crate::finder::is_option(&f.ty) {
quote! {
#ident: {
let item = #item_ident.get(#attr_key);
Expand Down
2 changes: 1 addition & 1 deletion raiden/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ serde_json = "^1"
serde_derive = "^1"
base64 = "^0.12"
thiserror = "^1"
derive_builder = "^0.9"
safe-builder = { tag = "0.0.3", git = "https://github.com/raiden-rs/safe-builder.git" }

[dev-dependencies]
pretty_assertions = "0.6.1"
Expand Down
3 changes: 1 addition & 2 deletions raiden/examples/put.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ fn main() {
let input = User::put_item_builder()
.id("testId".to_owned())
.name("bokuweb".to_owned())
.build()
.unwrap();
.build();
let res = client.put(input).run().await;
dbg!(res);
}
Expand Down
6 changes: 2 additions & 4 deletions raiden/examples/transact_write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,11 @@ fn main() {
let input = User::put_item_builder()
.id("testId".to_owned())
.name("bokuweb".to_owned())
.build()
.unwrap();
.build();
let input2 = User::put_item_builder()
.id("testId2".to_owned())
.name("bokuweb".to_owned())
.build()
.unwrap();
.build();
tx.put(User::put(input).condition(cond))
.put(User::put(input2))
.run()
Expand Down
2 changes: 1 addition & 1 deletion raiden/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub use rusoto_core_rustls::*;

pub type Placeholder = String;

pub use derive_builder::Builder;
pub use safe_builder::Builder;

#[derive(Debug, Clone, PartialEq)]
pub enum AttributeType {
Expand Down
36 changes: 13 additions & 23 deletions raiden/tests/all/put.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,7 @@ mod tests {
let user = User::put_item_builder()
.id("mock_id".to_owned())
.name("bokuweb".to_owned())
.build()
.unwrap();
.build();
let res = client.put(user).run().await;
dbg!(res);
}
Expand Down Expand Up @@ -209,9 +208,8 @@ mod tests {
name: "ap-northeast-1".into(),
});
let item = UserWithUuid::put_item_builder()
.name("bokuweb")
.build()
.unwrap();
.name("bokuweb".to_owned())
.build();
let res = client.put(item).run().await;
assert_eq!(res.is_ok(), true);
}
Expand All @@ -238,10 +236,9 @@ mod tests {
name: "ap-northeast-1".into(),
});
let item = UserVecTest::put_item_builder()
.name("bokuweb")
.name("bokuweb".to_owned())
.nums(vec![0, 1, 2])
.build()
.unwrap();
.build();
let res = client.put(item).run().await;
assert_eq!(res.is_ok(), true);
}
Expand Down Expand Up @@ -271,10 +268,9 @@ mod tests {
nums.insert(1);

let item = UserSetTest::put_item_builder()
.name("bokuweb")
.name("bokuweb".to_owned())
.nums(nums)
.build()
.unwrap();
.build();
let res = client.put(item).run().await;
assert_eq!(res.is_ok(), true);
}
Expand Down Expand Up @@ -319,10 +315,9 @@ mod tests {
nums.insert(1);

let item = UserSetTest::put_item_builder()
.name("bokuweb")
.name("bokuweb".to_owned())
.nums(nums)
.build()
.unwrap();
.build();
let res = client.put(item).run().await;
assert_eq!(res.is_ok(), true);
}
Expand All @@ -349,10 +344,7 @@ mod tests {
});
let set: std::collections::HashSet<String> = std::collections::HashSet::new();

let item = UserEmptySetTest::put_item_builder()
.set(set)
.build()
.unwrap();
let item = UserEmptySetTest::put_item_builder().set(set).build();
let res = client.put(item).run().await;
dbg!(&res);
assert_eq!(res.is_ok(), true);
Expand All @@ -378,8 +370,7 @@ mod tests {
});
let item = EmptyStringTestData0::put_item_builder()
.name("".to_owned())
.build()
.unwrap();
.build();
let res = client.put(item).run().await;
assert_eq!(res.is_ok(), true);
}
Expand All @@ -404,10 +395,9 @@ mod tests {
let set: std::collections::HashSet<String> = std::collections::HashSet::new();
let expected_set: std::collections::HashSet<String> = std::collections::HashSet::new();
let item = EmptyPutTestData0::put_item_builder()
.id("testid")
.id("testid".to_owned())
.sset(set)
.build()
.unwrap();
.build();
let res = client.put(item).run().await;
assert_eq!(res.is_ok(), true);
let res = client.get(res.unwrap().item.id).run().await;
Expand Down
Loading

0 comments on commit d539997

Please sign in to comment.