https://github.com/kernelpanic92/old_dartstruct
An annotation processor inspired by mapstruct for generating type-safe mappers
https://github.com/kernelpanic92/old_dartstruct
annotation-processor build-runner dart dartstruct mapping mapstruct object-mapper source-gen
Last synced: 9 months ago
JSON representation
An annotation processor inspired by mapstruct for generating type-safe mappers
- Host: GitHub
- URL: https://github.com/kernelpanic92/old_dartstruct
- Owner: KernelPanic92
- License: mit
- Created: 2020-04-20T18:13:53.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2024-12-21T05:37:48.000Z (about 1 year ago)
- Last Synced: 2025-03-24T16:54:37.024Z (9 months ago)
- Topics: annotation-processor, build-runner, dart, dartstruct, mapping, mapstruct, object-mapper, source-gen
- Language: Dart
- Size: 33.2 KB
- Stars: 5
- Watchers: 2
- Forks: 1
- Open Issues: 13
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# dartstruct
An annotation processor inspired by mapstruct for generating type-safe mappers

## What is dartstruct?
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.
dartstruct offers the following advantages:
- Fast execution by using plain method invocations
- Compile-time type safety. Only objects and attributes mapping to each other can be mapped, so there's no accidental mapping.
- Self-contained code: no runtime dependencies
- Clear error reports at build time if:
- mappings are incomplete (not all target properties are mapped)
- mappings are incorrect (cannot find a proper mapping method or type conversion)
- Easily debuggable mapping code (or editable by hand—e.g. in case of a bug in the generator)
To create a mapping between two types, declare a mapper class like this:
```dart
@Mapper()
abstract class CarMapper {
static CarMapper get INSTANCE => CarMapperImpl();
CarDto carToCarDto(Car car);
}
```
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.
# Using dartstruct
Add the generator to your dev dependencies
```yaml
dependencies:
dartstruct: any
dev_dependencies:
dartstruct_generator: any
build_runner: any
```
then run generator
```bash
pub run build_runner build # Dart SDK
flutter pub run build_runner build # Flutter SDK
```