custom_mapper_annotation 0.0.4 copy "custom_mapper_annotation: ^0.0.4" to clipboard
custom_mapper_annotation: ^0.0.4 copied to clipboard

Annotations for custom_mapper code generation package.

example/example.dart

import 'package:custom_mapper_annotation/custom_mapper_annotation.dart';

/// Example domain model representing a user.
class User {
  final String name;
  final int age;
  final String? email;
  final DateTime createdAt;

  const User({
    required this.name,
    required this.age,
    this.email,
    required this.createdAt,
  });

  @override
  String toString() {
    return 'User(name: $name, age: $age, email: $email, createdAt: $createdAt)';
  }
}

/// Example DTO class with mapper annotations.
///
/// This demonstrates how to use the custom_mapper_annotation package
/// to mark a class for automatic code generation.
@Mapper(domain: User, toDomain: true, toData: true)
class UserDto {
  final String name;

  @DefaultIfNull(0)
  final int age;

  final String? email;

  @IgnoreField()
  final String internalId;

  final String createdAtISO;

  const UserDto({
    required this.name,
    required this.age,
    this.email,
    required this.internalId,
    required this.createdAtISO,
  });

  @override
  String toString() {
    return 'UserDto(name: $name, age: $age, email: $email, internalId: $internalId, createdAtISO: $createdAtISO)';
  }
}

/// Example showing different annotation usages.
@Mapper(domain: User, toDomain: true) // Only generate toDomain method
class SimpleUserDto {
  final String name;

  @DefaultIfNull('Unknown')
  final String displayName;

  @IgnoreField()
  final bool isActive;

  const SimpleUserDto({
    required this.name,
    required this.displayName,
    required this.isActive,
  });
}

void main() {
  // This example shows how the annotations would be used.
  // The actual mapping methods would be generated by running:
  // dart run build_runner build

  const userDto = UserDto(
    name: 'John Doe',
    age: 25,
    email: 'john@example.com',
    internalId: 'internal_123',
    createdAtISO: '2023-01-01T00:00:00.000Z',
  );

  print('UserDto: $userDto');

  const simpleDto = SimpleUserDto(
    name: 'Jane Doe',
    displayName: 'Jane',
    isActive: true,
  );

  print('SimpleUserDto: $simpleDto');

  print('To see the generated mapping methods, run:');
  print('dart run build_runner build');
}
0
likes
160
points
0
downloads

Publisher

unverified uploader

Weekly Downloads

Annotations for custom_mapper code generation package.

Repository (GitHub)
View/report issues

Topics

#annotation #codegen #mapper

Documentation

API reference

License

MIT (license)

More

Packages that depend on custom_mapper_annotation