You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: README.md
+25-2
Original file line number
Diff line number
Diff line change
@@ -28,15 +28,20 @@ If we have to load a 'user' record from a database, we wouldn't want to send all
28
28
This is where DTO Mapper Library comes handy.
29
29
30
30
# 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.
31
33
You can use this instruction to install the latest version of **dto_mapper** library to your project
32
34
```shell
35
+
cargo add derive_builder
33
36
cargo add dto_mapper
34
37
```
35
38
And import it to the rust source file you need to use it:
36
39
```rust
37
40
usedto_mapper::DtoMapper;
38
41
```
39
42
43
+
More details on how to use derive_builder crate here: https://crates.io/crates/derive_builder
44
+
40
45
# Example
41
46
Let's say we want to create 3 special entities for our application
42
47
- 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
48
53
```rust
49
54
usedto_mapper::DtoMapper;
50
55
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.
#[mapper( dto="PersonDto" , no_builder=true, map=[ ("firstname",true), ("lastname",true), ("email",false) ] )]//no_builder=true will not create default builder for that dto
55
66
structUser{
56
67
username:String,
57
68
password:String,
@@ -97,6 +108,17 @@ Let's consider now we have a **PersonDto** and we'd like to convert it back part
97
108
letuser_from_person:User=person.into();
98
109
```
99
110
111
+
Let's consider building LoginDto with the builder pattern object generated by **dto_mapper**:
println!("LoginDto built with a builder: {:?}", login_dto);
120
+
```
121
+
100
122
Here is how **vscode** prints the code generated by DTO mapper for the **LoginDto** and **ProfileDto**
101
123
```Rust
102
124
pubstructLoginDto {
@@ -138,6 +160,7 @@ struct SourceStruct{ }
138
160
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.
139
161
-**Optional fields**
140
162
-**ignore** : an array of fieldnames not to include in the destination dtos. `ignore=["field1", "field1"]`
141
-
142
163
if **ignore** is present , then **map** field becomes optional. Except if needed rename destination fields for the dto
143
164
-**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.
0 commit comments