dartz_plus_generator 0.1.0
dartz_plus_generator: ^0.1.0 copied to clipboard
Code generator for dartz_plus AutoMapper.
Dartz Plus Generator #
A code generator for dartz_plus that automatically creates mapping extensions between your data transfer objects (DTOs) and domain entities.
Features #
- Automatic Mapper Generation: Generate bidirectional mapping extensions with a single annotation
- Type-Safe: Compile-time code generation ensures type safety
- Customizable: Control whether reverse mapping is generated
- Smart Field Resolution: Handles optional and nullable parameters intelligently
- Inheritance Support: Works with class hierarchies by collecting fields from superclasses
Installation #
Add dartz_plus_generator as a dev dependency and dartz_plus as a regular dependency in your pubspec.yaml:
dependencies:
dartz_plus: ^0.1.0
dev_dependencies:
build_runner: ^2.10.4
dartz_plus_generator: ^0.1.0
Then run:
flutter pub get
Usage #
Basic Example #
- Annotate your DTO class with
@Mapperand specify the target entity type:
import 'package:dartz_plus/dartz_plus.dart';
part 'user_model.g.dart'; // Include the generated file
@Mapper(UserEntity)
class UserDto {
final String name;
final int age;
UserDto({required this.name, required this.age});
}
class UserEntity {
final String name;
final int age;
final String? email;
const UserEntity(this.name, this.age, {this.email});
}
- Run the code generator:
dart run build_runner build
# or for watch mode
dart run build_runner watch
- The generator creates extension methods for mapping:
// Generated extension allows you to convert DTO to Entity
final dto = UserDto(name: 'John', age: 30);
final entity = dto.toUserEntity(); // UserEntity(name: 'John', age: 30)
// Reverse mapping is also generated by default
final backToDto = entity.toUserDto(); // UserDto(name: 'John', age: 30)
Controlling Reverse Mapping #
By default, the generator creates bidirectional mappings. You can disable reverse mapping:
@Mapper(UserEntity, reverse: false)
class UserDto {
// Only generates dto.toUserEntity()
// No reverse entity.toUserDto() method
}
How It Works #
The generator:
- Analyzes the
@Mapperannotation to identify the target type - Matches fields between source and target classes by name
- Generates extension methods with
to{TargetName}()methods - Handles optional and nullable parameters intelligently:
- Required parameters must have matching fields in the source
- Optional/nullable parameters are skipped if no matching field exists
Example with Optional Fields #
@Mapper(ProfileEntity)
class ProfileDto {
final String id;
final String name;
ProfileDto({required this.id, required this.name});
}
class ProfileEntity {
final String id;
final String name;
final DateTime? createdAt; // Optional field
ProfileEntity({
required this.id,
required this.name,
this.createdAt,
});
}
// Usage
final dto = ProfileDto(id: '123', name: 'Alice');
final entity = dto.toProfileEntity(); // createdAt will be null
Generated Code Example #
For the basic example above, the generator produces:
// user_model.g.dart
part of 'user_model.dart';
extension UserDtoToUserEntityMapper on UserDto {
UserEntity toUserEntity() {
return UserEntity(
name,
age,
);
}
}
extension UserEntityToUserDtoMapper on UserEntity {
UserDto toUserDto() {
return UserDto(
name: name,
age: age,
);
}
}
Requirements #
- Dart SDK: ^3.10.4
dartz_plus: Compatible versionbuild_runner: ^2.10.4 or higher
Build Configuration #
The generator uses build.yaml for configuration. The default configuration:
builders:
mapper:
import: "package:dartz_plus_generator/builder.dart"
builder_factories: ["mapperBuilder"]
build_extensions: { ".dart": [".g.dart"] }
auto_apply: dependents
build_to: cache
applies_builders: ["source_gen|combining_builder"]
Error Handling #
The generator will throw helpful errors at build time if:
@Mapperis applied to a non-class element- Target type is not a class
- Target class doesn't have a default (unnamed) constructor
- Required parameters in the target constructor don't have matching fields in the source
Contributing #
Contributions are welcome! Please feel free to submit a Pull Request.
License #
This project is licensed under the MIT License.
Links #
- dartz_plus - The main functional programming library
- GitHub Repository