dmap 0.3.0

  • Readme
  • Changelog
  • Example
  • Installing
  • 50

Dmap #

dmap is a simple library inspired by mapstructure based on [mirrors] to allow simple (and not necessarly efficient) mapping of a Map<String, dynamic> into a object or class

Feature #

  • Decode Map<String, dynamic> into Class
  • Encode Class into Map<String, dynamic>
  • Zero values handling to avoid null in nested fields
  • Hooks to transform types on parsing
  • Allow renaming
  • Allow multiple binding of an object through multiple tags

Usage #

import "package:dmap/dmap.dart";

class User {
  String lastName;
  String firstName;
  int age;
}

class Job {
  User user;
  String title;
}

void main() {
	var job = decode<Job>({"title": "dev", "user": {"lastName": "doe", "firstName": "john"}});
	print(job.user.lastName); // "doe"
}

With hooks

import 'package:dmap/dmap.dart';

class User {
	@Tag(name: "creation_date")
	DateTime creationDate;
	
	@Tag(name: "last_name")
	String lastName;
	
	@Tag(name: "first_name")
	String firstName;
	
	int age;
	
	String toString() => "[$creationDate | $lastName | $firstName | $age]";
}

main() {
	var decoder = new Decoder(hook: (fromType, toType, val) {
		if (fromType == String && toType == DateTime) {
			return DateTime.parse(val);
		}
		return val;
	});
	var input = {
		"creation_date": "2019-03-17",
		"last_name": "Doe",
		"first_name": "John",
		"age": 42
	};
	var output = decoder.decode<User>(input);
	print(output); // [2019-03-17 00:00:00.000 | Doe | John | 42]
}

FAQ #

  • Why using mirros ? This library is oriented to be simple to use and mirrors allow a dynamisme which can offers nice things
  • On which plateform can it be used ? only on server and flutter

Authors #

0.3.0 #

  • Adding basic encoding capabilities through Map<String, dynamic> encode<T>(T data)
  • Refactoring package to share code between encode and decode

0.2.1 #

  • Fixing linters and improve descriptions

0.2.0 #

  • Adding hook to transform types and allow smarter parsing

0.1.0 #

  • Initial version

example/dmap_example.dart

import 'package:dmap/dmap.dart';

class User {
	@Tag(name: "creation_date")
	DateTime creationDate;
	
	@Tag(name: "last_name")
	String lastName;
	
	@Tag(name: "first_name")
	String firstName;
	
	int age;
	
	String toString() => "[$creationDate | $lastName | $firstName | $age]";
}

main() {
	var decoder = new Decoder(hook: (fromType, toType, val) {
		if (fromType == String && toType == DateTime) {
			return DateTime.parse(val);
		}
		return val;
	});
	var input = {
		"creation_date": "2019-03-17",
		"last_name": "Doe",
		"first_name": "John",
		"age": 42
	};
	var output = decoder.decode<User>(input);
	print(output); // [2019-03-17 00:00:00.000 | Doe | John | 42]
}

Use this package as a library

1. Depend on it

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


dependencies:
  dmap: ^0.3.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:dmap/dmap.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 Dec 5, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.6.1
  • pana: 0.12.21

Platforms

Detected platforms: web, other

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

Health suggestions

Fix lib/src/encode/src/encode_base.dart. (-0.50 points)

Analysis of lib/src/encode/src/encode_base.dart reported 1 hint:

line 40 col 16: Unnecessary new keyword.

Format lib/src/annotation/src/get_annotation.dart.

Run dartfmt to format lib/src/annotation/src/get_annotation.dart.

Format lib/src/decode/src/decode_base.dart.

Run dartfmt to format lib/src/decode/src/decode_base.dart.

Dependencies

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