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
Implement adding new field with custom expression as value (#10)
* Implement adding new field with custom expression as value
* code refactoring
* update readme and code refactoring
* update doc version reference for crate.io
Copy file name to clipboardexpand all lines: README.md
+57-2
Original file line number
Diff line number
Diff line change
@@ -28,12 +28,13 @@ 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.
31
+
**_dto_mapper_** library depends on **_unstringify_** and **_derive_builder_**which implements builder pattern for dto objects resulted from the dto mappers.
32
32
By default, it generate builder for the dtos.
33
33
You can use this instruction to install the latest version of **dto_mapper** library to your project
34
34
```shell
35
35
cargo add derive_builder
36
36
cargo add dto_mapper
37
+
cargo add unstringify
37
38
```
38
39
And import it to the rust source file you need to use it:
39
40
```rust
@@ -58,11 +59,23 @@ It takes only those lines below to get this work done. And the conversion are be
#[mapper( dto="PersonDto" , no_builder=true, map=[ ("firstname",true), ("lastname",true), ("email",false) ] )] //no_builder=true will not create default builder for that dto
74
+
//no_builder=true will not create default builder for that dto
Let's consider building CustomDto by adding a new field called `name` which will be initialized with concatenation of firstname and lastname fields from `User` struct with the help of **dto_mapper**:
160
+
```rust
161
+
letuser=User {
162
+
firstname:"Dessalines".into(),
163
+
lastname:"Jean Jacques".into(),
164
+
..User::default()
165
+
};
166
+
167
+
println!("{:?}", user);
168
+
letcustom_dto:CustomDto=user.into();
169
+
println!("{:?}", custom_dto);
170
+
```
171
+
172
+
Here is how **vscode** prints the code generated by DTO mapper for **CustomDTO** and the Into Trait implemented by **User** for it
You can install 'expand' binary crate and use ***cargo expand*** command in order to print out the DTO structures generated as shown above:
190
+
```shell
191
+
cargo install expand
192
+
cargo expand
193
+
```
144
194
# Description of macro derive and attributes for Dto Mapper
145
195
First, DTO Mapper library requires that the source struct implements Default traits as the library relies on it to implement Into Traits for conversion between DTO and Source struct.
146
196
If not it will result in error in your IDE. It is a must to to derive or implement Default for your source struct.
@@ -163,4 +213,9 @@ struct SourceStruct{ }
163
213
if **ignore** is present , then **map** field becomes optional. Except if needed rename destination fields for the dto
164
214
-**derive** : a list of of macro to derive from. `derive=(Debug,Clone)`
165
215
-**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.
216
+
-**new_fields** : an array of declaration of new field names to include to the resulted dto structure. `new_fields=[("fieldname:type"), ("initialize_expression") )]`.
217
+
`fieldname:type` will create a new field with the `fieldname` specified and the `type`. It is not mandatory to rename. you can have `map=[("fieldname",true)]`
218
+
`initialize_expression` is used an initialize value or expression to use when converting the original structure to the dto.
219
+
For instance `new_fields=[("name:String"), ("concat_str(self.firstname,self.lastname)") )]` will create a new field in the dto called `name` which will be initialized with the concatenation of the original struct `firstname` and `lastname` fields. See the example above.
220
+
**I would strongly suggest to use function as `initialize_expression` for more complex scenarios in case parsing is failing when writing complex inline expression directly. This will reduce code complexity!!**
0 commit comments