mapper 0.2.0

  • README.md
  • CHANGELOG.md
  • Example
  • Installing
  • Versions
  • new62

Mapper

Simple library to convert maps to objects and objects to maps

import 'package:mapper/mapper.dart';

@Entity(fullMatch: true) // will map all fields
class Simple {
  String strProp;
  int intProp;
  bool boolProp;
  double doubleProp;
}

const Map<String, dynamic> simple = const {
  "strProp": "val",
  "intProp": 13,
  "boolProp": true,
  "doubleProp": 12.4,
};

main() {
    Simple obj = decode<Simple>(simple);

    obj.strProp; // val
    obj.intProp; // 13
    obj.boolProp; // true
    obj.doubleProp; 12,4

    Map<String, dynamic> simple2 = encode(obj);

    simple2['strProp'] == simple['strProp']; // true
    simple2['intProp'] == simple['intProp']; // true 
    simple2['boolProp'] == simple['boolProp']; // true
    simple2['doubleProp'] == simple['doubleProp']; // true
}

Basic features:

  • can convert simple types (String, int, boolean, double)
  • can convert Lists and Maps (only with simple types inside)
  • can be added external converters
  • mirror fields are cache after first use to increase speed
  • add meta to control convertation process

Roadmap:

  • add ability to convert complex Maps and Lists with included classes
  • add ability to convert classes with constructors with arguments

Custom converter example:

import 'package:mapper/mapper.dart';

class BoolParser extends Parser {
  bool decode(val) {
    if (val is int) {
      return val == 1;
    }
    return null;
  }

  int encode(val) {
    if (val is bool) {
      return val == true ? 1 : 0;
    }
    return null;
  }
}

@Entity(fullMatch: true)
class Simple {
  @Property(name: 'str') // change field name
  String strProp;
  bool boolProp;
  @Property(ignore: true) // ignore this field
  int intProp;
}

main() {

    addParser('bool', new BoolParser());

    Simple obj = new Simple();
    obj.strProp = "string";
    obj.boolProp = true;
    obj.intProp = 3;

    Map<String, dynamic> simple = encode(obj);

    simple['str'] == 'string'; // true
    simple.containsKey('strProp'); // false, key name was changed
    simple['boolProp'] == 1; // true
    simple.containsKey('intProp'); // false, field was ignored
}

0.2.0

  • added meta to control conversion process

0.1.0

  • added basic conversion features

example/main.dart

import 'package:mapper/mapper.dart';

class BoolParser extends Parser {
  bool decode(val) {
    if (val is int) {
      return val == 1;
    }
    return null;
  }

  int encode(val) {
    if (val is bool) {
      return val == true ? 1 : 0;
    }
    return null;
  }
}

@Entity(fullMatch: true)
class Simple {
  String strProp;
  int intProp;
  bool boolProp;
  double doubleProp;
}

const Map<String, dynamic> simple = const {
  "strProp": "val",
  "intProp": 13,
  "boolProp": 1,
  "doubleProp": 12.4,
};

main() {
  addParser('bool', new BoolParser());

  Simple obj = decode<Simple>(simple);

  obj.strProp; // val
  obj.intProp; // 13
  obj.boolProp; // true, was 1
  obj.doubleProp; // 12,4

  Map<String, dynamic> simple2 = encode(obj);

  simple2['strProp'] == simple['strProp']; // true
  simple2['intProp'] == simple['intProp']; // true
  simple2['boolProp'] == simple['boolProp']; // true
  simple2['doubleProp'] == simple['doubleProp']; // true
}

Use this package as a library

1. Depend on it

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


dependencies:
  mapper: ^0.2.0

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:mapper/mapper.dart';
  
Version Uploaded Documentation Archive
0.2.0 Nov 23, 2018 Go to the documentation of mapper 0.2.0 Download mapper 0.2.0 archive
0.1.3 Nov 22, 2018 Go to the documentation of mapper 0.1.3 Download mapper 0.1.3 archive
0.1.2 Nov 22, 2018 Go to the documentation of mapper 0.1.2 Download mapper 0.1.2 archive
0.1.1 Nov 22, 2018 Go to the documentation of mapper 0.1.1 Download mapper 0.1.1 archive
0.1.0 Nov 22, 2018 Go to the documentation of mapper 0.1.0 Download mapper 0.1.0 archive
Popularity:
Describes how popular the package is relative to other packages. [more]
30
Health:
Code health derived from static analysis. [more]
89
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
62
Learn more about scoring.

We analyzed this package on Dec 5, 2018, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.1.0
  • pana: 0.12.7

Platforms

Detected platforms: web, other

Primary library: package:mapper/mapper.dart with components: mirrors.

Health issues and suggestions

Document public APIs (-10 points)

20 out of 20 API elements (library, class, field or method) have no adequate dartdoc content. Good documentation improves code readability and discoverability through search.

Fix lib/src/decoder.dart. (-0.50 points)

Analysis of lib/src/decoder.dart reported 1 hint:

line 18 col 10: The value of the local variable 'className' isn't used.

Fix lib/src/encoder.dart. (-0.50 points)

Analysis of lib/src/encoder.dart reported 1 hint:

line 25 col 11: The value of the local variable 'name' isn't used.

Format lib/src/meta.dart.

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

Format lib/src/mirrorcache.dart.

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

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0 <3.0.0
Dev dependencies
test 1.5.0