simple_mapper 1.0.1

  • Readme
  • Changelog
  • Example
  • Installing
  • 50

Maps objects of different types to other objects using configured expressions. The goal of this library is to simplify and reduce code needed to map objects.

Usage #

Simple 1-to-1 mapping #

Map configurations can be added by passing a callback that takes the source object and calling mapper as arguments and returns the destination object.

var mapper = Mapper()
  .addMap<CompanyB, CompanyA>((source, mapper) => CompanyB(
      id: source.id,
      name: source.name
    ));
  
var companyA = CompanyA(
  id: 1,
  name: 'ABC'
);

var companyB = mapper.map<CompanyB, CompanyA>(companyA);

Mapping children #

The mapper can be used in the callback to map children.

var mapper = Mapper()
  .addMap<CompanyB, CompanyA>((source, mapper) => CompanyB(
    id: source.id,
    name: source.name,
    employees: source.employees
      ?.map((employee) => mapper.map<EmployeeB, EmployeeA>(employee))
      ?.toList()
  ))
  .addMap<EmployeeB, EmployeeA>((source, mapper) => EmployeeB(
    id: source.id,
    name: source.name,
    startDate: source.startDate,
    timeEmployed: source.startDate == null || source.endDate == null ?
      null :
      source.endDate.difference(source.startDate),
    company: mapper.map<CompanyB, CompanyA>(source.company)
  ));

Mapping null #

The callback is only executed if the source is not null; so null checking in the expression is unnecessary. When null is passed to the map method, null is returned.

var company = mapper.map<CompanyB, CompanyA>(null);
print(company == null); // true

1.0.1 #

  • Made some changes to documentation.

1.0.0 #

  • Initial version

example/simple_mapper_example.dart

import 'package:simple_mapper/simple_mapper.dart';

import 'models/company_a.dart';
import 'models/company_b.dart';
import 'models/employee_a.dart';
import 'models/employee_b.dart';

main() {
  var now = DateTime.now();
  var mapper = Mapper()
    .addMap<CompanyB, CompanyA>((source, mapper) => CompanyB(
      id: source.id,
      name: source.name,
      employees: source.employees
        ?.map((employee) => mapper.map<EmployeeB, EmployeeA>(employee))
        ?.toList()
    ))
    .addMap<EmployeeB, EmployeeA>((source, mapper) => EmployeeB(
      id: source.id,
      name: source.name,
      startDate: source.startDate,
      timeEmployed: source.startDate == null || source.endDate == null ?
        null :
        source.endDate.difference(source.startDate),
      company: mapper.map<CompanyB, CompanyA>(source.company)
    ));
  var companyA = CompanyA(
    id: 1,
    name: 'ABC',
    employees: <EmployeeA>[
      EmployeeA(
        id: 1,
        name: 'John Smith',
        startDate: now.subtract(Duration(days: 30)),
        endDate: now
      )
    ]
  );

  print(companyA);
  print('mapped to');
  print(mapper.map<CompanyB, CompanyA>(companyA));
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  simple_mapper: ^1.0.1

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:simple_mapper/simple_mapper.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
0
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
50
Learn more about scoring.

We analyzed this package on Jul 11, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.8.4
  • pana: 0.13.15

Health suggestions

Format lib/src/map_configuration.dart.

Run dartfmt to format lib/src/map_configuration.dart.

Format lib/src/map_exception.dart.

Run dartfmt to format lib/src/map_exception.dart.

Format lib/src/map_expression.dart.

Run dartfmt to format lib/src/map_expression.dart.

Format lib/src/mapper.dart.

Run dartfmt to format lib/src/mapper.dart.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.2.0 <3.0.0
Dev dependencies
pedantic ^1.0.0
test ^1.0.0