zengen 0.2.4

  • README.md
  • CHANGELOG.md
  • Installing
  • Versions
  • --

ZenGen

This project provides a pub transformer to generate boilerplate code.

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

Warning

Dart file modifications are not yet well integrated in the Dart editor.

If you run a web app use pub serve and launch dartium with localhost:8080 instead of Run in Dartium on your original file.

If you run a server app launch the built version of your dart file after a pub build instead of Run on your original file.

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 be transformed to :

@ToString()
class A {
  final a;
  final int b;
  A(this.a, this.b);
  @generated @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 be transformed to :

@EqualsAndHashCode()
class A {
  final a;
  final int b;
  A(this.a, this.b);
  @generated @override int get hashCode => hashObjects([a, b]);
  @generated @override bool operator ==(o) => identical(this, o) || o.runtimeType == runtimeType && o.a == a && o.b == 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 a class with @DefaultConstructor() will generate a default constructor with uninitialized final fields as required parameters and mutable fields as optional named parameters. If the class contains only final fields the generated constructor will be a const constructor.

For instance :

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

will be transformed to :

@DefaultConstructor()
class B {
  var a;
  final b;
  @generated B(this.b, {this.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;
  final List d;
}

will be transformed to :

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

Note that you can customize @EqualsAndHashCode() and @ToString() by using the annotation with the custom parameters.

@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 :

import 'package:zengen/zengen.dart';
abstract class A {
  m1();
}
class B {
  @Delegate() A _a;
}

will be transformed to :

import 'package:zengen/zengen.dart';
abstract class A {
  m1();
}
class B {
  @Delegate() A _a;
  @generated dynamic 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 :

import 'package:zengen/zengen.dart';
class A {
  @Lazy() var a = "String";
}

will be transformed to :

import 'package:zengen/zengen.dart';
class A {
  @generated dynamic get a => _lazyFields.putIfAbsent(#a, () => "String");
  @generated set a(dynamic v) => _lazyFields[#a] = v;
  @generated final _lazyFields = <Symbol, dynamic>{};
}

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

@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 :

import 'package:zengen/zengen.dart';
class A {
  m1();
  String get g;
  void set s(String s);
  @Implementation() _noSuchMethod(i) => print(i);
}

will be transformed to :

import 'package:zengen/zengen.dart';
class A {
  @generated dynamic m1() => _noSuchMethod(new StringInvocation('m1', isMethod: true));
  @generated String get g => _noSuchMethod(new StringInvocation('g', isGetter: true));
  @generated void set s(String s) { _noSuchMethod(new StringInvocation('s', isSetter: true, positionalArguments: [s])); }
  @Implementation() _noSuchMethod(i) => print(i);
}

Usage

To use this library in your code :

  • add a dependency in your pubspec.yaml :
dependencies:
  zengen: any
  • add the transformer in your pubspec.yaml :
transformers:
- zengen
  • add import in your dart code :
import 'package:zengen/zengen.dart';

License

Apache 2.0

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.2.4"

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...

Analysis

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