Giter Site home page Giter Site logo

data_classes's Introduction

data_classes's People

Contributors

marcelgarus avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

factisresearch

data_classes's Issues

Name annotation more intuitively

Marking the non data class with @DataClass is somewhat counter-intuitive. Rename the annotation to something of the like @GenerateDataClassFor.
This issue is open for discussion, suggestions are welcome.

Add support for generating getters for an enum field

In a few of my projects, I have an enum that represents a class's "type" or kind (not in the Dart Type kind of way). It would be nice to let a package generate the value getters.
Take the following class:

enum Color { red, green, yellow, blue }
enum FruitShape { round, curved }

@GenerateDataClassFor()
class MutableFruit {
  @GenerateValueGetters()
  Color color;

  @GenerateValueGetters(usePrefix=true)
  FruitShape shape;

  Fruit(this.color, this.shape);
}

class Banana extends Fruit {
  Banana() : super(Color.yellow, FruitShape.curved);
}
class Blueberry extends Fruit {
  Blueberry() : super(Color.blue, FruitShape.round);
}

It would be cool if the Fruit had some getters like the following:

class Fruit {
  ...
  bool get isRed => color == Color.red;
  bool get isGreen => color == Color.green;
  ...
  bool get isShapeRound => shape == FruitShape.round;
  bool get isShapeCurved => shape == FruitShape.curved;
}

Why? Because then we could use some cool stuff like Banana().isYellow.

Feedback about this proposal is very welcome.

The future of this package

For a new version of the package, I consider completely overhauling the API of this package.

Fundamental problems of this package

  • The mutable class is often not needed, people often only care about the actual immutable class.
  • The class that's actually used by all the other code is the one that the developer has the least influence over, causing friction when trying to customize it (see #2 for adding annotations to the generated class).
  • Especially with the last proposal in #3 this package becomes more and more similar to the built_value package, not offering any unique benefits.

So, what's the future of this package?
The Dart team actively works on bringing static extension methods and non-nullability by default (NNBD) to Dart. That would allow this package to be even more lightweight and enable a workflow like the following:

Mark your class with an @DataClass() annotation:

@DataClass()
class User {
  final String firstName;
  final String lastName;
  final String? photoUrl;

  const User({
    this.firstName,
    this.lastName,
    this.photoUrl,
  });
}

Because of NNBD the constructor is equivalent to the current one; @required annotations and non-null assertions are no longer needed.
The package could then generate the copy, ==, hashCode and toString methods as static extension methods on the User type.

Benefits

  • You have full control of the class actually being used by other parts of your code, so you can easily add annotations or doc comments as well as add custom methods right inside the class.
  • The mutable version of the class is less present as it's only used in the copy method, causing less confusion like #1.
  • You could easily customize which methods get generated by implementing them yourself -- the package will only generate methods that are not implemented yet.

Downsides

  • You need to add a constructor manually.

I'm welcoming any opinions on whether this is a worthy tradeoff.

[Feature Request] Add json serialization

Thanks for sharing this library! I have definitely been wanting to use something like this.

Is it possible to add json serialization to the generated data class?

My specific use-case is that I'd like to use json_serializable. To use it, I'd need to add some boilerplate (toJson and fromJson) to the generated data class; however, the generated data class can't be edited by hand.

`copyWith` probably handles nulls incorrectly

hi,
I'm looking into your example and I guess nulls do not work properly(I didn't run it though).

  var me = const User(firstName: 'Marcel', lastName: 'Garus', photoUrl: 'http://example.com');
  var mySister = me.copyWith(firstName: 'Yvonne', photoUrl: null);

photoUrl is updated as follows photoUrl: photoUrl ?? this.photoUrl, which leads to mySister.photoUrl == 'http://example.com' instead of null.

Generated code is not compilable

I have this class:

part 'ReminderArgs.g.dart';

@DataClass()
class ReminderArgs {
	@nullable String assetId;
	@nullable Reminder reminder;
	bool showAsset = false;
}

and this is what data_classes generates:

// GENERATED CODE - DO NOT MODIFY BY HAND

part of 'ReminderArgs.dart';

// **************************************************************************
// DataClassGenerator
// **************************************************************************

/// This class is the immutable pendant of the MutablerArgs class.
@immutable
class rArgs {
  final String assetId;
  final Reminder reminder;
  final bool showAsset;

  /// Default constructor that creates a rArgs.
  const rArgs({
    this.assetId,
    this.reminder,
    @required this.showAsset,
  }) : assert(showAsset != null);

  /// Creates a rArgs from a MutablerArgs.
  factory rArgs.fromMutable(MutablerArgs mutable) {
    return rArgs(
      assetId: mutable.assetId,
      reminder: mutable.reminder,
      showAsset: mutable.showAsset,
    );
  }

  /// Turns this rArgs into a MutablerArgs.
  MutablerArgs toMutable() {
    return MutablerArgs()
      ..assetId = assetId
      ..reminder = reminder
      ..showAsset = showAsset;
  }

  /// Checks if this rArgs is equal to the other one.
  bool operator ==(Object other) {
    return other is rArgs &&
        assetId == other.assetId &&
        reminder == other.reminder &&
        showAsset == other.showAsset;
  }

  int get hashCode => hashList([
        assetId,
        reminder,
        showAsset,
      ]);

  rArgs copyWith({
    String assetId,
    Reminder reminder,
    bool showAsset,
  }) {
    return rArgs(
      assetId: assetId ?? this.assetId,
      reminder: reminder ?? this.reminder,
      showAsset: showAsset ?? this.showAsset,
    );
  }

  String toString() {
    return 'rArgs\n'
        '  assetId: $assetId\n'
        '  reminder: $reminder\n'
        '  showAsset: $showAsset\n'
        ')';
  }
}

pubspec.yaml:

dependencies:
data_classes: ^1.1.0
flutter:
sdk: flutter
flutter_localizations:
sdk: flutter
flutter_mobx: ^0.3.1+1
mobx: ^0.3.7
package_info: ^0.4.0+6

dev_dependencies:
build_runner: ^1.6.7
data_classes_generator: ^1.1.0
mobx_codegen: ^0.3.6

Optional named constructor parameters

Would it be possible to add support for optional named constructor parameters by specifying a default value in the base Mutable class?

For example this class:

@GenerateDataClass()
class MutableState {
    String a = "";
    String b;
}

Would generate the this constructor:

const ProfileConfirmState({
    this.a = "",
    @required this.b
  }) : assert(a != null),
       assert(b != null);

Exclude fields from generated copyWith or == methods

Would it be possible to add support for excluding fields from the generated copyWith, hashCode/== methods?

For example:

class MutableState({
    Exclude(copyWith == true, equals == true) String a;
    String b;
})

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.