compiled_mirrors 0.1.0

  • README.md
  • Installing
  • Versions
  • 37

Compiled Mirrors

Statically reflecting Dart classes.

Reflection in Dart through dart:mirrors is a powerful tool for building tests and assorted utilities. However, when compiled to JavaScript and run in a browser, mirrors can severely reduce code's performance. In Flutter, mirrors are disabled entirely.

Compiled Mirrors generates a reflection of Dart classes annotated with @compileMirrors. This allows you to do a number of interesting things.

Testing complex classes

My focus has been on using this as a testing utility for evaluating the fields between two complex objects. For example, suppose we have the following class:

class Person {
  final String firstName;
  final String lastName;
  final int age;
  final Location birthplace;
  final Location residence;
  final University almaMater;

  Person(this.firstName, this.lastName, this.gender, this.age,
      this.birthplace, this.residence, this.almaMater);

  factory Person.fromParents(Person mother, Person father) {
    // Make a new person with traits of both parents.
  }
}

Suppose we want to test the fromParents factory. We'd need to build an expected Person for the results of the test, and an actual Person:

test('fromParents Person factory', () {
  var expected = new Person(/* various fields */);
  var actual = new Person.fromParents(testMother, testFather);

Without an equality definition for this person, we need to test against every field in the Person object:

  expect(actual.firstName, expected.firstName);
  expect(actual.lastName, expected.lastName);
  // boilerplate continues.
});

Very verbose!

Also, if we decide later on that we want to add a birthDate field to Person, the test will pass even if the factory generates a birthDate we didn't expect! This is a bug waiting to happen.

Mirrored fields

We can avoid this problem with a mirror of the object's fields. If we compile a mirror for the Person object, we can generate an automatically-updating equality test:

test('fromParents Person factory', () {
  var expected = new Person(/* various fields */);
  var actual = new Person.fromParents(testMother, testFather);
  var expectedFields = new Person$CompiledMirror(expected).fields;
  var actualFields = new Person$CompiledMirror(actual).fields;
  for (var fieldName in expectedFields.keys) {
    expect(actualFields[fieldName](), expectedFields[fieldName]());
  }
});

Much simpler and much more reliable!

Other uses

There are other uses for mirrors. If there's a use case that you'd like a compiled mirror for, please file an issue.

Use this package as a library

1. Depend on it

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


dependencies:
  compiled_mirrors: ^0.1.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:compiled_mirrors/compiled_mirrors.dart';
  
Version Uploaded Documentation Archive
0.1.0 Mar 28, 2017 Go to the documentation of compiled_mirrors 0.1.0 Download compiled_mirrors 0.1.0 archive
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]
37
Overall:
Weighted score of the above. [more]
37
Learn more about scoring.

We analyzed this package on Aug 15, 2018, and provided a score, details, and suggestions below. Analysis was completed with status tool failures using:

  • Dart: 2.0.0
  • pana: 0.11.8

Platforms

Detected platforms: unsure

Error(s) prevent platform classification:

Fix dependencies in pubspec.yaml.

Issues and suggestions

Fix dependencies in pubspec.yaml.

Running pub upgrade failed with the following output:

ERR: The current Dart SDK version is 2.0.0.
 
 Because compiled_mirrors depends on quiver >=0.3.0 <=0.29.0+1 which requires SDK version <2.0.0, version solving failed.

Fix platform conflicts.

Error(s) prevent platform classification:

Fix dependencies in pubspec.yaml.

Maintain CHANGELOG.md.

Changelog entries help clients to follow the progress in your code.

Add SDK constraint in pubspec.yaml.

For information about setting SDK constraint, please see https://www.dartlang.org/tools/pub/pubspec#sdk-constraints.

Package is getting outdated.

The package was released 72 weeks ago.

The description is too short.

Add more detail about the package, what it does and what is its target use case. Try to write at least 60 characters.

Package is pre-v1 release.

While there is nothing inherently wrong with versions of 0.*.*, it usually means that the author is still experimenting with the general direction of the API.