Skip to content

Commit ed58e9e

Browse files
authored
Update Readme with Builder (#8)
1 parent fea262f commit ed58e9e

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed

README.md

+25-2
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,20 @@ If we have to load a 'user' record from a database, we wouldn't want to send all
2828
This is where DTO Mapper Library comes handy.
2929

3030
# Installation
31+
**_dto_mapper_** library depends on **_derive_builder_** to implement builder pattern for dto objects resulted from the dto mappers.
32+
By default, it generate builder for the dtos.
3133
You can use this instruction to install the latest version of **dto_mapper** library to your project
3234
```shell
35+
cargo add derive_builder
3336
cargo add dto_mapper
3437
```
3538
And import it to the rust source file you need to use it:
3639
```rust
3740
use dto_mapper::DtoMapper;
3841
```
3942

43+
More details on how to use derive_builder crate here: https://crates.io/crates/derive_builder
44+
4045
# Example
4146
Let's say we want to create 3 special entities for our application
4247
- LoginDto that will contain only 2 fields from **User** such as _**username**_ and _**password**_. we would like to rename _**username**_ to _**login**_ in LoginDto
@@ -48,10 +53,16 @@ It takes only those lines below to get this work done. And the conversion are be
4853
```rust
4954
use dto_mapper::DtoMapper;
5055

56+
/*** Use this declaration below in lib.rs if you're using a library crate , or in main.rs if you're using a binary crate.
57+
if your crate has lib.rs and main.rs. Use it instead inside your lib.rs.
58+
***/
59+
#[macro_use]
60+
extern crate derive_builder;
61+
5162
#[derive(DtoMapper,Default,Clone)]
5263
#[mapper( dto="LoginDto" , map=[ ("username:login",true) , ("password",true)] , derive=(Debug, Clone, PartialEq) )]
5364
#[mapper( dto="ProfileDto" , ignore=["password"] , derive=(Debug, Clone, PartialEq) )]
54-
#[mapper( dto="PersonDto" , map=[ ("firstname",true), ("lastname",true), ("email",false) ] )]
65+
#[mapper( dto="PersonDto" , no_builder=true, map=[ ("firstname",true), ("lastname",true), ("email",false) ] )] //no_builder=true will not create default builder for that dto
5566
struct User{
5667
username: String,
5768
password: String,
@@ -97,6 +108,17 @@ Let's consider now we have a **PersonDto** and we'd like to convert it back part
97108
let user_from_person: User = person.into();
98109
```
99110

111+
Let's consider building LoginDto with the builder pattern object generated by **dto_mapper**:
112+
```rust
113+
let mut login_dto_builder = LoginDtoBuilder::default();
114+
let login_dto = login_dto_builder
115+
.login("capois-lamort".into())
116+
.password("hello123".into())
117+
.build()
118+
.expect("Failed to build login dto");
119+
println!("LoginDto built with a builder: {:?}", login_dto);
120+
```
121+
100122
Here is how **vscode** prints the code generated by DTO mapper for the **LoginDto** and **ProfileDto**
101123
```Rust
102124
pub struct LoginDto {
@@ -138,6 +160,7 @@ struct SourceStruct{ }
138160
if `required_flag` is set to true, the destination dto field will be exactly of the same type with the source one in the struct.
139161
- **Optional fields**
140162
- **ignore** : an array of fieldnames not to include in the destination dtos. `ignore=["field1", "field1"]`
141-
142163
if **ignore** is present , then **map** field becomes optional. Except if needed rename destination fields for the dto
143164
- **derive** : a list of of macro to derive from. `derive=(Debug,Clone)`
165+
- **no_builder**: a boolean flag to turn on or off builders for the dto. Default value is **_false_**. If the Dto name is "MyDto" , the builder will create a struct named "MyDtoBuilder" that can be used to build "MyDto" struct.
166+

0 commit comments

Comments
 (0)