auto_model 1.0.0 copy "auto_model: ^1.0.0" to clipboard
auto_model: ^1.0.0 copied to clipboard

A library that generate some standard functionality and utility functions for Model(similar to Data Class in Kotlin).

A library that generate some standard functionality and utility functions for Model(similar to Data Class in Kotlin)

annotations #

  • @Json() / @json : An annotation that add an extended json field to the model to convert the current model instance into standard JSON.

  • @SerializedName('name'') : An annotation that indicates this member should be serialized to JSON with the provided name value as its field name(similar to SerializedName in Gson).

  • @Copy() / @copy : An annotation that add an extended function to the model to copy an object altering some of its properties.

  • @ToString() / @toString : An annotation that add an extended function (named toString2) to generate a string with form like "User(name=John, age=42)".

Usage #

  • step 1: Define a model class and add one or more of @Json() / @json || @Copy() / @copy || @ToString() / @toString to the model class.

class.dart:

import 'package:auto_model/auto_model.dart';

import 'student.dart';
import 'teacher.dart';

part 'class.g.dart';

@json
@toString
@copy
class Class {
  String name;
  List<Student> students;
  Teacher headTeacher;

  int get studentCount => students?.length;

  /// required empty ctr if use @toString
  Class();

  Class.require(this.name, this.headTeacher, {this.students});
}

  • step 2: In module directory, run:
$ pub get
...
$ pub run build_runner build
  • When the command finishes executing, a dart standard source code file with a suffix of *.g.dart will be generated. The code will generate an extension class that contains the corresponding function or field, as follows:

class.g.dart

// GENERATED CODE - DO NOT MODIFY BY HAND

part of 'class.dart';

// **************************************************************************
// CopyGenerator
// **************************************************************************

extension $CopyClassExtension on Class {
  /// Used for copy an object altering some of its properties,
  /// but keeping the rest unchanged.
  Class copy({String name, List<Student> students, Teacher headTeacher}) {
    var copied = Class(); // ignore: prefer_final_locals
    if (name != null) {
      copied.name = name;
    } else {
      copied.name = this.name;
    }

    if (students != null) {
      copied.students = students;
    } else {
      copied.students = this.students;
    }

    if (headTeacher != null) {
      copied.headTeacher = headTeacher;
    } else {
      copied.headTeacher = this.headTeacher;
    }

    return copied;
  }
}

// **************************************************************************
// JsonGenerator
// **************************************************************************

extension $JsonClassExtension on Class {
  /// import 'package:auto_model/auto_model.dart';
  String get json => toJson(this);
}

// **************************************************************************
// ToStringGenerator
// **************************************************************************

extension $ToStringClassExtension on Class {
  /// Used for generate a string with all available fields form like
  /// "User(name=John, age=42)".
  String toString2() {
    return 'Class(name=$name, students=$students, headTeacher=$headTeacher, studentCount=$studentCount)';
  }
}


Later plans #

Plan to merge these annotations together and generate them in an extension class

Features and bugs #

Please file feature requests and bugs at the issue tracker.

1
likes
30
pub points
0%
popularity

Publisher

verified publishernikeo.cn

A library that generate some standard functionality and utility functions for Model(similar to Data Class in Kotlin).

Homepage
Repository (GitHub)
View/report issues

License

BSD-3-Clause (LICENSE)

Dependencies

analyzer, build, source_gen

More

Packages that depend on auto_model