diff --git a/Cargo.lock b/Cargo.lock index 2a6f952c..476b1012 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -266,66 +266,6 @@ dependencies = [ "syn", ] -[[package]] -name = "darling" -version = "0.10.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0d706e75d87e35569db781a9b5e2416cff1236a47ed380831f959382ccd5f858" -dependencies = [ - "darling_core", - "darling_macro", -] - -[[package]] -name = "darling_core" -version = "0.10.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f0c960ae2da4de88a91b2d920c2a7233b400bc33cb28453a2987822d8392519b" -dependencies = [ - "fnv", - "ident_case", - "proc-macro2", - "quote", - "strsim", - "syn", -] - -[[package]] -name = "darling_macro" -version = "0.10.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d9b5a2f4ac4969822c62224815d069952656cadc7084fdca9751e6d959189b72" -dependencies = [ - "darling_core", - "quote", - "syn", -] - -[[package]] -name = "derive_builder" -version = "0.9.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a2658621297f2cf68762a6f7dc0bb7e1ff2cfd6583daef8ee0fed6f7ec468ec0" -dependencies = [ - "darling", - "derive_builder_core", - "proc-macro2", - "quote", - "syn", -] - -[[package]] -name = "derive_builder_core" -version = "0.9.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2791ea3e372c8495c0bc2033991d76b512cd799d07491fbd6890124db9458bef" -dependencies = [ - "darling", - "proc-macro2", - "quote", - "syn", -] - [[package]] name = "difference" version = "2.0.0" @@ -1057,7 +997,6 @@ version = "0.1.0" dependencies = [ "async-trait", "base64 0.12.1", - "derive_builder", "insta", "pretty_assertions", "raiden-derive", @@ -1065,6 +1004,7 @@ dependencies = [ "rusoto_credential", "rusoto_dynamodb", "rust-crypto", + "safe-builder", "serde", "serde_derive", "serde_json", @@ -1078,11 +1018,11 @@ name = "raiden-derive" version = "0.1.0" dependencies = [ "convert_case", - "derive_builder", "ident_case", "proc-macro2", "proc-quote", "quote", + "safe-builder", "syn", ] @@ -1393,6 +1333,26 @@ version = "1.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "535622e6be132bccd223f4bb2b8ac8d53cda3c7a6394944d3b2b33fb974f9d76" +[[package]] +name = "safe-builder" +version = "0.1.0" +source = "git+https://github.com/raiden-rs/safe-builder.git?tag=0.0.3#415b64c88718515d2c3c3b65c4520353421c92d3" +dependencies = [ + "safe-builder-derive", +] + +[[package]] +name = "safe-builder-derive" +version = "0.1.0" +source = "git+https://github.com/raiden-rs/safe-builder.git?tag=0.0.3#415b64c88718515d2c3c3b65c4520353421c92d3" +dependencies = [ + "convert_case", + "proc-macro2", + "proc-quote", + "quote", + "syn", +] + [[package]] name = "schannel" version = "0.1.18" @@ -1610,12 +1570,6 @@ version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "213701ba3370744dcd1a12960caa4843b3d68b4d1c0a5d575e0d65b2ee9d16c0" -[[package]] -name = "strsim" -version = "0.9.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6446ced80d6c486436db5c078dde11a9f73d42b57fb273121e160b84f63d894c" - [[package]] name = "subtle" version = "1.0.0" diff --git a/README.md b/README.md index efe9470d..30d170b3 100644 --- a/README.md +++ b/README.md @@ -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()); diff --git a/raiden-derive/Cargo.toml b/raiden-derive/Cargo.toml index 3182b01b..862cd35c 100644 --- a/raiden-derive/Cargo.toml +++ b/raiden-derive/Cargo.toml @@ -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" } + diff --git a/raiden-derive/src/finder/mod.rs b/raiden-derive/src/finder/mod.rs index 5f134f1b..23e592d0 100644 --- a/raiden-derive/src/finder/mod.rs +++ b/raiden-derive/src/finder/mod.rs @@ -110,3 +110,15 @@ pub(crate) fn find_sort_key_field(fields: &syn::FieldsNamed) -> Option bool { + match ty { + syn::Type::Path(syn::TypePath { + path: syn::Path { segments, .. }, + .. + }) => { + segments.iter().any(|s| s.ident == "Option") + } + _ => false, + } +} diff --git a/raiden-derive/src/lib.rs b/raiden-derive/src/lib.rs index 6cb787e3..4ff615c8 100644 --- a/raiden-derive/src/lib.rs +++ b/raiden-derive/src/lib.rs @@ -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, - } -} diff --git a/raiden-derive/src/ops/mod.rs b/raiden-derive/src/ops/mod.rs index 4b37ced7..7e67257b 100644 --- a/raiden-derive/src/ops/mod.rs +++ b/raiden-derive/src/ops/mod.rs @@ -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::*; \ No newline at end of file +pub(crate) use update::*; + diff --git a/raiden-derive/src/ops/put.rs b/raiden-derive/src/ops/put.rs index 9e7c8b75..25085a53 100644 --- a/raiden-derive/src/ops/put.rs +++ b/raiden-derive/src/ops/put.rs @@ -1,5 +1,6 @@ use crate::rename::*; use quote::*; +use syn::*; pub(crate) fn expand_put_item( _partition_key: &proc_macro2::Ident, @@ -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 = 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)* @@ -231,3 +241,12 @@ pub struct PutItemInput { } */ + +#[allow(clippy::ptr::arg)] +fn expand_default_type_variables( + idents: &Vec, +) -> impl Iterator { + idents.clone().into_iter().map(|_ident| { + quote! { (), } + }) +} diff --git a/raiden-derive/src/ops/shared.rs b/raiden-derive/src/ops/shared.rs index 2842f9a5..66fd80e8 100644 --- a/raiden-derive/src/ops/shared.rs +++ b/raiden-derive/src/ops/shared.rs @@ -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); diff --git a/raiden/Cargo.toml b/raiden/Cargo.toml index f2478e3b..58f62934 100644 --- a/raiden/Cargo.toml +++ b/raiden/Cargo.toml @@ -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" diff --git a/raiden/examples/put.rs b/raiden/examples/put.rs index f3beaed7..fca86799 100644 --- a/raiden/examples/put.rs +++ b/raiden/examples/put.rs @@ -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); } diff --git a/raiden/examples/transact_write.rs b/raiden/examples/transact_write.rs index 4cf43dfb..a8de2481 100644 --- a/raiden/examples/transact_write.rs +++ b/raiden/examples/transact_write.rs @@ -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() diff --git a/raiden/src/lib.rs b/raiden/src/lib.rs index 88fc9bd1..51802f8d 100644 --- a/raiden/src/lib.rs +++ b/raiden/src/lib.rs @@ -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 { diff --git a/raiden/tests/all/put.rs b/raiden/tests/all/put.rs index 64a597aa..73c18159 100644 --- a/raiden/tests/all/put.rs +++ b/raiden/tests/all/put.rs @@ -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); } @@ -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); } @@ -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); } @@ -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); } @@ -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); } @@ -349,10 +344,7 @@ mod tests { }); let set: std::collections::HashSet = 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); @@ -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); } @@ -404,10 +395,9 @@ mod tests { let set: std::collections::HashSet = std::collections::HashSet::new(); let expected_set: std::collections::HashSet = 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; diff --git a/raiden/tests/all/transact_write.rs b/raiden/tests/all/transact_write.rs index 5a74d5af..beaa7dc0 100644 --- a/raiden/tests/all/transact_write.rs +++ b/raiden/tests/all/transact_write.rs @@ -26,13 +26,11 @@ mod tests { 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(); assert_eq!( tx.put(User::put(input).condition(cond)) .put(User::put(input2)) @@ -56,8 +54,7 @@ mod tests { let input = User::put_item_builder() .id("testId".to_owned()) .name("bokuweb".to_owned()) - .build() - .unwrap(); + .build(); let set_expression = User::update_expression() .set(User::name()) .value("updated!!"); @@ -83,8 +80,7 @@ mod tests { let input = User::put_item_builder() .id("testId".to_owned()) .name("bokuweb".to_owned()) - .build() - .unwrap(); + .build(); assert_eq!( tx.put( User::put(input)