Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Possibility to have immutable model [enhancement] #42

Closed
jaumard opened this issue Jul 24, 2018 · 4 comments
Closed

Possibility to have immutable model [enhancement] #42

jaumard opened this issue Jul 24, 2018 · 4 comments
Milestone

Comments

@jaumard
Copy link
Contributor

jaumard commented Jul 24, 2018

I'm used to work with immutable object to avoid side effect problems. Would be nice if the generator can adapt if the class is immutable :)

Example let's consider this class:

@immutable
class CartItem {
  final int id;
  final double amount;
  final String product;
  final int quantity;

  const CartItem({this.amount, this.product, this.quantity, this.id});

  String get amountLabel => kNumberFormat.format(amount);

  @override
  String toString() => 'amount=$amount, quantity=$quantity, product=$product';

  CartItem copy({int id, int quantity, String product, double amount}) {
    return CartItem(
      amount ?? this.amount,
      product ?? this.product,
      quantity ?? this.quantity,
      id: id ?? this.id,
    );
  }

  @override
  bool operator ==(Object other) =>
      identical(this, other) ||
          other is CartItem &&
              runtimeType == other.runtimeType &&
              amount == other.amount &&
              product == other.product &&
              quantity == other.quantity;

  @override
  int get hashCode =>
      amount.hashCode ^
      product.hashCode ^
      quantity.hashCode;
}

Then the generated fromMap look like:

CartItemDb fromMap(Map map) {
    CartItemDb model = new CartItemDb();

    model.id = adapter.parseValue(map['id']);
    model.amount = adapter.parseValue(map['amount']);
    model.product = adapter.parseValue(map['product']);
    model.quantity = adapter.parseValue(map['quantity']);
    model.cartId = adapter.parseValue(map['cart_id']);

    return model;
  }

Where the correct way would be:

CartItemDb fromMap(Map map) {
    CartItemDb model = new CartItemDb(
      id: adapter.parseValue(map['id']),
      amount: adapter.parseValue(map['amount']),
      product: adapter.parseValue(map['product']),
      quantity: adapter.parseValue(map['quantity']),
      cartId: adapter.parseValue(map['cart_id']),
    );
    return model;
  }

Might have some changes to do around association too as it directly change the object:

void associateCartDb(CartItemDb child, CartDb parent) {
    child.cartId = parent.id;
  }

When I understand better how this generation works I might take a look at this :)

@tejainece
Copy link
Member

Very important. Low prio. Will implement this in a week's time.

@jaumard
Copy link
Contributor Author

jaumard commented Sep 18, 2018

Hey @tejainece what about this ? :) would be nice to have it like it is in jaguar_serializer ^^

@tejainece
Copy link
Member

Will work on this next week.

@tejainece tejainece added this to the 2.2.x+ milestone Sep 25, 2018
@tejainece
Copy link
Member

This si fixed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants