jankuss / genq

Instant Data Class Generation for Dart
MIT License
133 stars 0 forks source link

Not Dart compatible #19

Closed JCKodel closed 4 months ago

JCKodel commented 4 months ago

Error when parsing: lib\features\auth\entities\app_theme.dart:13

final class AppTheme with _$AppTheme implements ISerializable, ICopyWith { ^ Unexpected token: FINAL (final). Expected CLASS. ✅ Generated 0 data classes

Dart is not JavaScript, we have final, abstract interface, abstract base and sealed classes.

jankuss commented 4 months ago

indeed, right now genq expects the class keyword right after annotation. We should support different class modifiers though, I'll implement a fix.

jankuss commented 4 months ago

We can extend the parser to support class modifiers. The analyzer would still not be happy with the generated output though.

Take this example:

@genq
final class Geo with _$Geo {
  factory Geo({
    required String lat,
    required String lng,
  }) = _Geo;
}

The generated _Geo constructor needs to implement from Geo:

abstract class _Geo implements Geo { /* ... */

If Geo is a final class, this is not valid dart code. We probably need to put this final modifier in the generated classes as well to avoid the error:

bin/json.genq.dart:130:16 • The type '_Geo' must be 'base', 'final' or 'sealed' because the supertype 'Geo' is 'final'
JCKodel commented 4 months ago

Don't bother.

Even equality isn't functional (the generated code only checks for reference equality, defeating the whole purpose of value equality).

jankuss commented 4 months ago

Don't bother.

Even equality isn't functional (the generated code only checks for reference equality, defeating the whole purpose of value equality).

@JCKodel this is not true - the generated equality operator checks for value equality for each field in the data class.

  final user1 = User(id: 1, name: 'JCKodel', address: Address(zip: '12345'));
  final user2 = User(id: 1, name: 'JCKodel', address: Address(zip: '12345'));

  print(user1 == user2); // This will print true

This even holds true for lists/sets/maps

  final user1 = User(id: 1, name: 'JCKodel', addresses: [Address(zip: '12345')]);
  final user2 = User(id: 1, name: 'JCKodel', addresses: [Address(zip: '12345')]);

  print(user1 == user2); // This will print true

Do you have an example where this breaks for you? Are you sure you are not accidentally using another class as a field which does not support value equality?