Skip to content

Commit 844a704

Browse files
Update README.md
1 parent 2b3a227 commit 844a704

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

README.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,58 @@
11
# dartstruct
22
An annotation processor inspired by mapstruct for generating type-safe mappers
3+
4+
## What is dartstruct?
5+
6+
dartstruct is a Dart annotation processor, inspired by Java MapStruct, for the generation of type-safe and performant mappers for dart classes. It saves you from writing mapping code by hand, which is a tedious and error-prone task.
7+
8+
9+
dartstruct offers the following advantages:
10+
11+
- Fast execution by using plain method invocations
12+
- Compile-time type safety. Only objects and attributes mapping to each other can be mapped, so there's no accidental mapping.
13+
- Self-contained code: no runtime dependencies
14+
- Clear error reports at build time if:
15+
- mappings are incomplete (not all target properties are mapped)
16+
- mappings are incorrect (cannot find a proper mapping method or type conversion)
17+
- Easily debuggable mapping code (or editable by hand—e.g. in case of a bug in the generator)
18+
19+
To create a mapping between two types, declare a mapper class like this:
20+
21+
```dart
22+
23+
@Mapper()
24+
abstract class CarMapper {
25+
26+
static final CarMapper INSTANCE = CarMapperImpl();
27+
28+
CarDto carToCarDto(Car car);
29+
30+
}
31+
32+
```
33+
34+
The generated implementation uses plain Dart method invocations for mapping between source and target objects. Properties are mapped if they have the same name in source and target.
35+
36+
# Using dartstruct
37+
38+
Add the generator to your dev dependencies
39+
40+
```yaml
41+
42+
dependencies:
43+
dartstruct: any
44+
45+
dev_dependencies:
46+
dartstruct_generator: any
47+
build_runner: any
48+
49+
```
50+
51+
then run generator
52+
53+
```bash
54+
55+
pub run build_runner build # Dart SDK
56+
flutter pub run build_runner build # Flutter SDK
57+
58+
```

0 commit comments

Comments
 (0)