Skip to content

Commit

Permalink
Merge pull request #1 from jrote1/master
Browse files Browse the repository at this point in the history
pull from origin
  • Loading branch information
adeel41 committed Dec 4, 2015
2 parents 9689b8a + 9f8f424 commit 61aeeeb
Show file tree
Hide file tree
Showing 74 changed files with 3,484 additions and 2,032 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ packages/

# Include when developing application packages.
pubspec.lock
*.packages
43 changes: 43 additions & 0 deletions .packages
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Generated by pub on 2015-12-03 09:20:10.458.
analyzer:file:///C:/Users/Jake/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/analyzer-0.26.3/lib/
args:file:///C:/Users/Jake/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/args-0.13.2/lib/
async:file:///C:/Users/Jake/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/async-1.4.0/lib/
barback:file:///C:/Users/Jake/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/barback-0.15.2+7/lib/
charcode:file:///C:/Users/Jake/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/charcode-1.1.0/lib/
cli_util:file:///C:/Users/Jake/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/cli_util-0.0.1+2/lib/
code_transformers:file:///C:/Users/Jake/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/code_transformers-0.2.9+4/lib/
collection:file:///C:/Users/Jake/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/collection-1.2.0/lib/
convert:file:///C:/Users/Jake/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/convert-1.0.1/lib/
crypto:file:///C:/Users/Jake/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/crypto-0.9.1/lib/
csslib:file:///C:/Users/Jake/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/csslib-0.12.2/lib/
dart_style:file:///C:/Users/Jake/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/dart_style-0.2.0/lib/
glob:file:///C:/Users/Jake/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/glob-1.0.5/lib/
html:file:///C:/Users/Jake/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/html-0.12.2/lib/
http_multi_server:file:///C:/Users/Jake/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/http_multi_server-1.3.2/lib/
http_parser:file:///C:/Users/Jake/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/http_parser-1.1.0/lib/
logging:file:///C:/Users/Jake/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/logging-0.11.2/lib/
matcher:file:///C:/Users/Jake/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/matcher-0.12.0+1/lib/
mime:file:///C:/Users/Jake/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/mime-0.9.3/lib/
package_config:file:///C:/Users/Jake/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/package_config-0.1.3/lib/
path:file:///C:/Users/Jake/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/path-1.3.9/lib/
plugin:file:///C:/Users/Jake/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/plugin-0.1.0/lib/
pool:file:///C:/Users/Jake/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/pool-1.2.1/lib/
pub_semver:file:///C:/Users/Jake/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/pub_semver-1.2.3/lib/
reflective:file:///C:/Users/Jake/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/reflective-0.0.23/lib/
shelf:file:///C:/Users/Jake/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/shelf-0.6.4+2/lib/
shelf_static:file:///C:/Users/Jake/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/shelf_static-0.2.3+1/lib/
shelf_web_socket:file:///C:/Users/Jake/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/shelf_web_socket-0.0.1+4/lib/
source_map_stack_trace:file:///C:/Users/Jake/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/source_map_stack_trace-1.0.4/lib/
source_maps:file:///C:/Users/Jake/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/source_maps-0.10.1/lib/
source_span:file:///C:/Users/Jake/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/source_span-1.2.1/lib/
stack_trace:file:///C:/Users/Jake/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/stack_trace-1.5.0/lib/
string_scanner:file:///C:/Users/Jake/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/string_scanner-0.1.4/lib/
test:file:///C:/Users/Jake/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/test-0.12.4+9/lib/
typed_data:file:///C:/Users/Jake/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/typed_data-1.1.1/lib/
unittest:file:///C:/Users/Jake/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/unittest-0.12.4/lib/
utf:file:///C:/Users/Jake/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/utf-0.9.0+2/lib/
watcher:file:///C:/Users/Jake/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/watcher-0.9.7/lib/
when:file:///C:/Users/Jake/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/when-0.2.0/lib/
which:file:///C:/Users/Jake/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/which-0.1.3/lib/
yaml:file:///C:/Users/Jake/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/yaml-2.1.7/lib/
nomirrorsmap:lib/
8 changes: 8 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
language: dart
dart:
- stable
sudo: false
before_script:
- chmod +x travis.sh
- chmod +x ensure_dartfmt.sh
script: ./travis.sh
117 changes: 115 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,115 @@
# nomirrorsmap.dart
Maps from any source to any destination without using mirrors
## NoMirrorsMap [![Build Status](https://travis-ci.org/jrote1/nomirrorsmap.dart.svg)](https://travis-ci.org/jrote1/nomirrorsmap.dart) [![Coverage Status](https://coveralls.io/repos/jrote1/nomirrorsmap.dart/badge.svg?branch=master&service=github)](https://coveralls.io/github/jrote1/nomirrorsmap.dart?branch=master)
### 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
```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
```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](https://d2weczhvl823v0.cloudfront.net/jrote1/nomirrorsmap.dart/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

15 changes: 15 additions & 0 deletions ensure_dartfmt.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/sh

dart_files=$(git ls-tree --name-only --full-tree -r HEAD | grep '.dart$')
[ -z "$dart_files" ] && exit 0

unformatted=$(dartfmt -n $dart_files)
[ -z "$unformatted" ] && exit 0

# Some files are not dartfmt'd. Print message and fail.
echo >&2 "dart files must be formatted with dartfmt. Please run:"
for fn in $unformatted; do
echo >&2 " dartfmt -w $PWD/$fn"
done

exit 1
19 changes: 0 additions & 19 deletions lib/main_transformer.dart

This file was deleted.

34 changes: 15 additions & 19 deletions lib/nomirrorsmap.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,19 @@ export 'src/shared/shared.dart';
import 'src/converters/converters.dart';
import 'src/manipulators/manipulators.dart';

class NoMirrorsMap
{
dynamic convert( dynamic value, Converter sourceConverter, Converter destinationConverter, [List<BaseObjectDataManipulator> manipulators] )
{
var convertedSource = sourceConverter.toBaseObjectData( value );
if(manipulators != null)
manipulators.forEach((m) => m.manipulate(convertedSource));
return destinationConverter.fromBaseObjectData( convertedSource );
}

}

class MapType{
const MapType();
class NoMirrorsMap {
NoMirrorsMap() {
if (TypeInformationRetrieverLocator.instance ==
null) TypeInformationRetrieverLocator
.setInstance(new NoMirrorsMapStore());
}

dynamic convert(
dynamic value, Converter sourceConverter, Converter destinationConverter,
[List<BaseObjectDataManipulator> manipulators]) {
var convertedSource = sourceConverter.toBaseIntermediateObject(value);
if (manipulators != null) manipulators
.forEach((m) => m.manipulate(convertedSource));
return destinationConverter.fromBaseIntermediateObject(convertedSource);
}
}





62 changes: 62 additions & 0 deletions lib/nomirrorsmap_mirrors.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
library nomirrorsmap.mirrors;

import 'src/converters/converters.dart';
import 'package:reflective/reflective.dart' as reflective;

void useMirrors() {
TypeInformationRetrieverLocator
.setInstance(new MirrorsTypeInformationRetriever());
}

class MirrorsTypeInformationRetriever implements TypeInformationRetriever {
@override
bool containsEnumGeneratedMap(Type type) {
return new reflective.TypeReflection(type).isEnum;
}

@override
ClassMapping getClassGeneratedMap(Type type) {
var result = new ClassMapping()..fields = [];
reflective.TypeReflection reflectiveType = reflective.type(type);
for (var fieldName in reflectiveType.fields.keys) {
var field = reflectiveType.field(fieldName);
var fieldMapping = new FieldMapping()
..name = field.name
..setter = field.set
..getter = field.value;

result.fields.add(new ClassField()
..type = field.type.rawType
..fieldMapping = fieldMapping);
}
result.type = type;
result.fullName = reflectiveType.fullName;
result.instantiate = () => reflectiveType.construct();

return result;
}

@override
ClassMapping getClassGeneratedMapByListType(Type type) {
var classType =
new reflective.TypeReflection(type).typeArguments.first.rawType;
return getClassGeneratedMap(classType);
}

@override
ClassMapping getClassGeneratedMapByQualifiedName(String qualifiedName) {
var classType =
new reflective.TypeReflection.fromFullName(qualifiedName).rawType;
return getClassGeneratedMap(classType);
}

@override
ClassMapping getClassGeneratedMapWithNoCheck(Type type) {
return getClassGeneratedMap(type);
}

@override
List getEnumGeneratedMap(Type type) {
return new reflective.TypeReflection(type).enumValues;
}
}
5 changes: 5 additions & 0 deletions lib/src/conversion_objects/base_intermediate_object.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
part of nomirrorsmap.conversion_objects;

class BaseIntermediateObject {
Type objectType;
}
9 changes: 0 additions & 9 deletions lib/src/conversion_objects/base_object_data.dart

This file was deleted.

7 changes: 7 additions & 0 deletions lib/src/conversion_objects/class_intermediate_object.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
part of nomirrorsmap.conversion_objects;

class ClassIntermediateObject extends BaseIntermediateObject {
String previousHashCode;

Map<String, BaseIntermediateObject> properties;
}
11 changes: 0 additions & 11 deletions lib/src/conversion_objects/class_object_data.dart

This file was deleted.

8 changes: 4 additions & 4 deletions lib/src/conversion_objects/conversion_objects.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
library nomirrorsmap.conversion_objects;

part 'class_object_data.dart';
part 'native_object_data.dart';
part 'list_object_data.dart';
part 'base_object_data.dart';
part 'class_intermediate_object.dart';
part 'native_intermediate_object.dart';
part 'list_intermediate_object.dart';
part 'base_intermediate_object.dart';
5 changes: 5 additions & 0 deletions lib/src/conversion_objects/list_intermediate_object.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
part of nomirrorsmap.conversion_objects;

class ListIntermediateObject extends BaseIntermediateObject {
List<BaseIntermediateObject> values;
}
10 changes: 0 additions & 10 deletions lib/src/conversion_objects/list_object_data.dart

This file was deleted.

5 changes: 5 additions & 0 deletions lib/src/conversion_objects/native_intermediate_object.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
part of nomirrorsmap.conversion_objects;

class NativeIntermediateObject extends BaseIntermediateObject {
dynamic value;
}
Loading

0 comments on commit 61aeeeb

Please sign in to comment.