value_t_generator 0.4.0

value_t #

Generates code to create a simple type with a copywith class and implements all the getter fields in any extended or implemented classes.

Advantages over coding it yourself #

Less code to write and less code to read back over too. Refactoring can be less because you don't need to change all the names in the copyWith definitions, constructors and where a class has been extended multiple times. A free toString implementation (I find this useful when comparing objects for tests)

Downsides over coding it yourself #

####Go to definition will take you to the generated class...two work arounds

  • Need to define a const constructor in the base type
  • Go to definition then on the $MyClass go to definition again...not much of an impedement
  • name the file the same as your class and instead of pressing f12 cmd + p and type the filename. This sounds like more work but because of the conciseness of your classes you can fit whole class heirarchies and related interfaces in a single file and still view all the definitions.
  • Also, the generated file will have the fields names so you can is it as a reference anyway

Renaming classes #

you must rename both your defition and the generated name $PersonClass and PersonClass. This one is more fiddly, press f2 on PersonClass and rename, then change the name of $PersonClass...this order will result in just two changes.

Usage

  var a = MarkingIncorrect("my answer", <String>["blah", "blah"], "fullWord");

  print("a is MarkingState:" + (a is Marking).toString());

  var d = a.copyWith(answer: "blah");

Definition

  Person a = WindowCleaner(pet: Pet(type: "cat"), name: "Bob", employeeId: 5, wetness: "wet");

  if (a is Employee) {
    print('I am an employee');
    print(a.employeeId);
  }

  if (a is Person) {
    print('I am a Person');
    print(a.name);
  }

  var b = a.copyWith(name: "bobby");
  print(b.name);

  if (b is OftenWet) {
    print("windowCleaners are OftenWet");
  }
}

When defining your class put a dollar infront of the name and make it abstract #

abstract class $MyClass {

When defining the fields make them non-final getters #

    String get name;
}

When using your class in code remove the dollar #

var a = MyClass("Adriaaan");

See example folder for usage #

Inspirations and alternatives #

built_value and meta_types

0.4.0 #

  • allow constants (so that we can allow constant default values)
  • override toString

0.3.0 #

  • removed the null checks if there is a default on a field
  • allow abstract classes

0.1.0 #

  • First published version, supports extends and implements

Use this package as a library

1. Depend on it

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


dependencies:
  value_t_generator: ^0.4.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:value_t_generator/value_t_generator.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
0
Health:
Code health derived from static analysis. [more]
98
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
80
Overall:
Weighted score of the above. [more]
45
Learn more about scoring.

We analyzed this package on Jul 17, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.4.0
  • pana: 0.12.19

Platforms

Detected platforms: other

Primary library: package:value_t_generator/value_t_generator.dart with components: io, build, mirrors.

Health issues and suggestions

Document public APIs. (-1 points)

2 out of 2 API elements have no dartdoc comment.Providing good documentation for libraries, classes, functions, and other API elements improves code readability and helps developers find and use your API.

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

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

line 5 col 7: Use contains instead of indexOf

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

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

line 61 col 19: Use isNotEmpty instead of length

Format lib/src/elementSuperTypeToString.dart.

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

Maintenance issues and suggestions

Support latest dependencies. (-10 points)

The version constraint in pubspec.yaml does not support the latest published versions for 1 dependency (analyzer).

Maintain an example. (-10 points)

Create a short demo in the example/ directory to show how to use this package.

Common filename patterns include main.dart, example.dart, and value_t_generator.dart. Packages with multiple examples should provide example/README.md.

For more information see the pub package layout conventions.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0 <3.0.0
analyzer >=0.35.4 <0.36.0 0.35.4 0.37.0
build ^1.1.1 1.1.5
collection ^1.14.11 1.14.11
quiver ^2.0.1 2.0.3
source_gen ^0.9.4+1 0.9.4+2
value_t_annotation ^0.4.0 0.4.0
Transitive dependencies
args 1.5.2
async 2.3.0
charcode 1.1.2
convert 2.1.1
crypto 2.0.6
dart_style 1.2.4 1.2.9
front_end 0.1.14 0.1.20
glob 1.1.7
kernel 0.3.14 0.3.20
logging 0.11.3+2
matcher 0.12.5
meta 1.1.7
package_config 1.0.5
path 1.6.2
pedantic 1.8.0+1
pub_semver 1.4.2
source_span 1.5.5
stack_trace 1.9.3
string_scanner 1.0.4
term_glyph 1.1.0
typed_data 1.1.6
watcher 0.9.7+12
yaml 2.1.16
Dev dependencies
build_runner ^1.0.0
test ^1.0.0

Admin