dart_mapper 1.0.1
dart_mapper: ^1.0.1 copied to clipboard
MapStruct-style object mapping for Dart. Define type-safe mappings declaratively with annotations and get generated implementations at compile time via build_runner.
/*
* Copyright (c) 2026 MyLittleSuite
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
import 'package:dart_mapper/dart_mapper.dart';
class UserDto {
final String name;
final String email;
const UserDto({required this.name, required this.email});
}
class UserEntity {
final String fullName;
final String emailAddress;
const UserEntity({required this.fullName, required this.emailAddress});
}
// Mapper definition — run `dart run build_runner build` to generate the
// concrete implementation (_$UserMapperImpl).
@Mapper()
abstract class UserMapper {
@Mapping(target: 'fullName', source: 'name')
@Mapping(target: 'emailAddress', source: 'email')
UserEntity toEntity(UserDto dto);
}
void main() {
// dart_mapper generates implementations at compile time via build_runner.
// After running `dart run build_runner build`, use:
// final mapper = UserMapper();
// final entity = mapper.toEntity(UserDto(name: 'Alice', email: 'alice@example.com'));
print('dart_mapper example — see README for full usage.');
}