zengen 0.3.0

  • README.md
  • CHANGELOG.md
  • Installing
  • Versions
  • 5

ZenGen

Build Status

This project provides a source_gen generator to generate boilerplate code.

This library is inspired from the project Lombok in the Java world.

Features

@ToString()

Annotating a class with @ToString() will generate an implementation of String toString() built by default with its public getters.

For instance :

@ToString()
class _A {
  final a;
  final int b;
  _A(this.a, this.b);
}

will generate :

@GeneratedFrom(_A)
class A {
  final a;
  final int b;
  A(this.a, this.b);
  @override String toString() => "A(a=$a, b=$b)";
}

The code generated can be customized with the following optional parameters:

  • callSuper: if set to true the result of toString will contains the result of super.toString().
  • exclude: a list of getter names can be exclude with this argument.
  • includePrivate: if set to true the generation will include private getters.

@EqualsAndHashCode()

Annotating a class with @EqualsAndHashCode() will generate an implementation of bool operator ==(o) and int get hashCode built by default with its public getters.

For instance :

@EqualsAndHashCode()
class _A {
  final a;
  final int b;
  _A(this.a, this.b);
}

will generate :

@GeneratedFrom(_A)
class A {
  final a;
  final int b;
  A(this.a, this.b);
  @override bool operator ==(o) => identical(this, o) ||
      o.runtimeType == runtimeType && o.a == a && o.b == b;
  @override int get hashCode => hashObjects([a, b]);
}

The code generated can be customize with the following optional parameters:

  • callSuper: if set to true the generated code will use additionnally super.hashCode and super == o.
  • exclude: a list of getter names can be exclude with this argument.
  • includePrivate: if set to true the generation will include private getters.

@DefaultConstructor()

Annotating an external constructor with @DefaultConstructor() will generate a default constructor with uninitialized final fields as required parameters and mutable fields as optional named parameters. You can use the useConst parameter to generate a const constructor.

For instance :

class _A {
  var a;
  final b;
  @DefaultConstructor() external _A();
}

will generate :

@GeneratedFrom(_A)
class A {
  var a;
  final b;
  A(this.b, {this.a});
}

The external _A(); is used to make the analyzer happy and will be removed in the generated A.

@Value()

Annotating a class with @Value() is the same as annotating the class with @DefaultConstructor(), @EqualsAndHashCode() and @ToString().

For instance :

@Value()
class _A {
  final int a;
  final b, c;
  external _A();
}

will generate :

@GeneratedFrom(_A)
class A {
  final int a;
  final b, c;
  A(this.a, this.b, this.c);
  @override bool operator ==(o) => identical(this, o) ||
      o.runtimeType == runtimeType && o.a == a && o.b == b && o.c == c;
  @override int get hashCode => hashObjects([a, b, c]);
  @override String toString() => "A(a=$a, b=$b, c=$c)";
}

Note that you can customize @EqualsAndHashCode() and @ToString() by using the annotation with the custom parameters. You can use the useConst parameter to generate a const constructor.

@Delegate()

Annotating a field/getter with @Delegate() will add to the enclosing class all the public methods available on the type of the field/getter.

For instance :

abstract class A {
  m1();
}
class _B {
  @Delegate() A _a;
}

will generate :

@GeneratedFrom(_B)
class B {
  @Delegate() A _a;
  m1() => _a.m1();
}

The code generated can be customize with the following optional parameters:

  • exclude: a list of members can be exclude with this argument.

@Lazy()

Annotating a field with @Lazy() will make it lazy computed.

For instance :

class _A {
  @Lazy() var a = "String";
}

will generate :

@GeneratedFrom(_A)
class A {
  dynamic get a => _lazyFields.putIfAbsent(#a, () => "String");
  set a(dynamic v) => _lazyFields[#a] = v;
  final _lazyFields = <Symbol, dynamic>{};
}

The lazy fields are stored into _lazyFields by field names. If the field is final no setter will be generated.

@Cached()

Annotating a method with @Cached() will make its result managed by a cache. By default the cache used is a forever cache that will compute the result once and keep it forever in memory.

For instance :

class _A {
  @Cached() int fib(int n) => (n < 2) ? n : fib(n - 1) + fib(n - 2);
}

will generate :

@GeneratedFrom(_A)
class A {
  int fib(int n) => _caches
      .putIfAbsent(
          #fib,
          () => _createCache(
              #fib, (int n) => (n < 2) ? n : fib(n - 1) + fib(n - 2)))
      .getValue([n]);
  final _caches = <Symbol, Cache>{};
  Cache _createCache(Symbol methodName, Function compute) => new Cache(compute);
}

The caches for each method are stored into _caches. You can implement your own Cache _createCache(Symbol methodName, Function compute) to customize the cache policy.

@Implementation()

Annotating a method with @Implementation() will make it the method called by all abstract members. The method annotated must have exactly one parameter of type StringInvocation. This type is the same as Invocation from dart:core except that the Symbol are replaced by String. This allows to avoid dart:mirrors.

For instance :

abstract class _A {
  m1();
  String get g;
  void set s(String s);
  @Implementation() _noSuchMethod(i) => print(i);
}

will generate :

@GeneratedFrom(_A)
class A {
  m1() => _noSuchMethod(new StringInvocation('m1', isMethod: true));
  String get g => _noSuchMethod(new StringInvocation('g', isGetter: true));
  void set s(String s) {
    _noSuchMethod(
        new StringInvocation('s', isSetter: true, positionalArguments: [s]));
  }

  _noSuchMethod(i) => print(i);
}

Usage

To use this library in your code :

  • add a dependency in your pubspec.yaml :
dependencies:
  zengen: any
  • add import in your dart code :
import 'package:zengen/zengen.dart';
  • create a script build.dart that run the generator
import 'package:zengen/generator.dart';
import 'package:source_gen/source_gen.dart' show build;

main(List<String> args) async {
  print(await build(args, [new ZengenGenerator()],
      librarySearchPaths: ['example/', 'lib/', 'web/', 'test/']));
}

You can use the build_system package to allow the generator to be run on every file changes.

License

Apache 2.0

v0.3.0 (2015-10-08)

Brand new implementation based on source_gen.

v0.2.8 (2014-12-10)

Bug fixes

  • Getting rid of export (#19).

v0.2.7 (2014-06-22)

Features

  • @Cached(): allows to cache results of methods (#14).

v0.2.6 (2014-06-17)

Features

  • @DefaultConstructor() and @Value() now accept a useConst to generate a const constructor (#17).

v0.2.5 (2014-06-16)

Bug Fixes

  • @DefaultConstructor() doesn't create const for now.

v0.2.4 (2014-06-14)

Bug Fixes

  • @Implementation() doesn't handle field on interface (#16).

v0.2.3 (2014-06-12)

Features

  • @Implementation(): implementation of all abstract methods.

v0.2.2 (2014-06-09)

Features

  • @DefaultConstructor(): generate a default constructor.
  • @Value(): simplify the creation of value class.

v0.2.1 (2014-06-07)

Features

  • @Lazy(): make field initialization lazy.

v0.2.0 (2014-06-04)

Now a resolved-AST is used. This has been enforced for the implementation of @Delegate

  • Add the optional named parameter includePrivate to @ToString() and @EqualsAndHashCode.
  • Implementation of operator==(o) has been changed to use identical and runtimeType.

Features

  • @Delegate: generate methods from a getter type.

v0.1.1 (2014-05-09)

  • Add the optional named parameter callSuper to @ToString() and @EqualsAndHashCode.
  • Add the optional named parameter exclude to @ToString() and @EqualsAndHashCode.

v0.1.0 (2014-05-08)

Initial release available for public testing with @ToString() and @EqualsAndHashCode.

Features

  • @ToString(): generate the implementation of String toString().
  • @EqualsAndHashCode(): generate the implementation of bool operator ==(o) and int get hashCode.

Semantic Version Conventions

http://semver.org/

  • Stable: All even numbered minor versions are considered API stable: i.e.: v1.0.x, v1.2.x, and so on.
  • Development: All odd numbered minor versions are considered API unstable: i.e.: v0.9.x, v1.1.x, and so on.

Use this package as a library

1. Depend on it

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


dependencies:
  zengen: ^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:zengen/zengen.dart';
  
Version Uploaded Documentation Archive
0.4.0 Nov 2, 2016 Go to the documentation of zengen 0.4.0 Download zengen 0.4.0 archive
0.3.2 Dec 22, 2015 Go to the documentation of zengen 0.3.2 Download zengen 0.3.2 archive
0.3.1 Dec 15, 2015 Go to the documentation of zengen 0.3.1 Download zengen 0.3.1 archive
0.3.0 Oct 8, 2015 Go to the documentation of zengen 0.3.0 Download zengen 0.3.0 archive
0.2.8 Dec 10, 2014 Go to the documentation of zengen 0.2.8 Download zengen 0.2.8 archive
0.2.7 Jun 22, 2014 Go to the documentation of zengen 0.2.7 Download zengen 0.2.7 archive
0.2.6 Jun 17, 2014 Go to the documentation of zengen 0.2.6 Download zengen 0.2.6 archive
0.2.5 Jun 16, 2014 Go to the documentation of zengen 0.2.5 Download zengen 0.2.5 archive
0.2.4 Jun 14, 2014 Go to the documentation of zengen 0.2.4 Download zengen 0.2.4 archive
0.2.3 Jun 12, 2014 Go to the documentation of zengen 0.2.3 Download zengen 0.2.3 archive

All 15 versions...

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

This package version is not analyzed, because it is more than two years old. Check the latest stable version for its analysis.

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.

Maintenance issues and suggestions

Running dartdoc failed. (-10 points)

Make sure dartdoc runs without any issues.

Dependencies

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