nomirrorsmap 1.1.2

  • README.md
  • Installing
  • Versions
  • 12

NoMirrorsMap Build Status Coverage Status

Information

NoMirrorsMap allows you to map objects in one format to objects in another format. For example, you can use it to serialise and deserialise native dart objects to and from Json, or to map an object of one type to an object of another type with the same property names.

NoMirrorsMap does this without using Mirrors (hence the name...). It uses a transformer to create a map file for all the objects that you may need to map, and uses that map file at runtime to avoid the use of Mirrors and to significantly improve the mapping performance.

Usage

Before you can use NoMirrorsMap, you need to add a transformer entry to your pubspec.yaml file and you need to tell NoMirrorsMap which types you are going to be mapping so the transformer knows which types it needs to generate mapping data for. You can do this in two ways:

  1. You can decorate any types that need to be mapped with the @Mappable() attribute.
  2. You can add an entry for a library under the transformer declaration in your pubspec.yaml file, which will cause NoMirrorsMap to generate mapping data for ALL the types in that library.

NoMirrorsMap works via a two step process; first the object to be mapped from one format to another is mapped into an intermediate format, and then that intermediate format is mapped to the final target format. While the object is in its intermediate format, you have the opportunity to manipulate the object. In the current codebase, NoMirrorsMap contains manipulators which can change the casing of properties to PascalCase, or to camelCase, or to change the target type.

When you call the convert method, you supply the object to be mapped, the converter to convert the object to the intermediate format, the converter to convert the intermediate format to the final format, and an optional list of manipulators to modify the intermediate format before it is run through the target format converter.

So for example, to convert an object to Json, you would do this:

var json = new NoMirrorsMap().convert(objectToMap, new ClassConverter(), new JsonConverter());

To convert an object to Json, changing the casing of the properties to PascalCase in the process, you would do this:

var json = new NoMirrorsMap().convert(objectToMap, new ClassConverter(), new JsonConverter(), [ new PascalCaseManipulator() ]);

Other manipulators include:

  • PascalCaseManipulator
  • CamelCaseManipulator
  • TypeToTypeManipulator

We recommend creating static methods for doing common mappings. For example you might create the following static methods for encoding and decoding json:

class Json {
	static String encode( dynamic obj ) {
    	return new NoMirrorsMap().convert( obj, new ClassConverter(), new JsonConverter() );
    }

    static String decode( string json, Type type ) {
    	return new NoMirrorsMap().convert( json, new JsonConverter(), new ClassConverter( startType: type ) );
    }
}

NoMirrorsMap has a few converters within the codebase by default, but you can easily write your own if you need to map to or from any other formats you may need (XML, CSV etc). Feel free to submit these in a pull request!

Basic Example Using The @Mappable() Attribute

pubspec.yaml

dependencies:
	nomirrorsmap: any
transformers:
	- nomirrorsmap

main.dart

import 'package:nomirrorsmap/nomirrorsmap.dart';

main(){
	var obj = new EncodableObject()..id = 1;
	var json = new NoMirrorsMap().convert(obj, new ClassConverter(), new JsonConverter());
}

@Mappable()
class EncodableObject {
	int id;
}

Basic Example Using Library Declaration in pubspec.yaml

pubspec.yaml

dependencies:
	nomirrorsmap: any
transformers:
	- nomirrorsmap:
		library_names:
        	- "my_library"

main.dart

import 'package:nomirrorsmap/nomirrorsmap.dart';
import 'my_library.dart';

main(){
	var obj1 = new FirstEncodableObject()..id = 1;
    var obj2 = new SecondEncodableObject()..id = 2;

    var mapper = new NoMirrorsMap();
	var json1 = mapper.convert(obj1, new ClassConverter(), new JsonConverter());
    var json2 = mapper.convert(obj2, new ClassConverter(), new JsonConverter());

}

my_library.dart

library my_library;

class FirstEncodableObject {
	int id;
}

class SecondEncodableObject {
	int id;
}

Bitdeli Badge

Use this package as a library

1. Depend on it

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


dependencies:
  nomirrorsmap: ^1.1.2

2. Install it

You can install packages from the command line:

with pub:


$ pub get

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

3. Import it

Now in your Dart code, you can use:


import 'package:nomirrorsmap/nomirrorsmap.dart';
  
Version Uploaded Documentation Archive
1.1.2 Jul 27, 2017 Go to the documentation of nomirrorsmap 1.1.2 Download nomirrorsmap 1.1.2 archive
1.1.1 Feb 20, 2017 Go to the documentation of nomirrorsmap 1.1.1 Download nomirrorsmap 1.1.1 archive
1.1.0 Sep 5, 2016 Go to the documentation of nomirrorsmap 1.1.0 Download nomirrorsmap 1.1.0 archive
1.0.27 Aug 26, 2016 Go to the documentation of nomirrorsmap 1.0.27 Download nomirrorsmap 1.0.27 archive
1.0.26 Aug 3, 2016 Go to the documentation of nomirrorsmap 1.0.26 Download nomirrorsmap 1.0.26 archive
1.0.25 Jul 29, 2016 Go to the documentation of nomirrorsmap 1.0.25 Download nomirrorsmap 1.0.25 archive
1.0.24 Apr 28, 2016 Go to the documentation of nomirrorsmap 1.0.24 Download nomirrorsmap 1.0.24 archive
1.0.23 Dec 4, 2015 Go to the documentation of nomirrorsmap 1.0.23 Download nomirrorsmap 1.0.23 archive
1.0.22 Dec 3, 2015 Go to the documentation of nomirrorsmap 1.0.22 Download nomirrorsmap 1.0.22 archive
1.0.21 Dec 3, 2015 Go to the documentation of nomirrorsmap 1.0.21 Download nomirrorsmap 1.0.21 archive

All 48 versions...

Popularity:
Describes how popular the package is relative to other packages. [more]
24
Health:
Code health derived from static analysis. [more]
0
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
0
Overall:
Weighted score of the above. [more]
12
Learn more about scoring.

The package version is not analyzed, because it does not support Dart 2. Until this is resolved, the package will receive a health and maintenance score of 0.

Analysis issues and suggestions

Support Dart 2 in pubspec.yaml.

The SDK constraint in pubspec.yaml doesn't allow the Dart 2.0.0 release. For information about upgrading it to be Dart 2 compatible, please see https://www.dartlang.org/dart-2#migration.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=1.9.0 <2.0.0