google / json_serializable.dart

Generates utilities to aid in serializing to/from JSON.
https://pub.dev/packages/json_serializable
BSD 3-Clause "New" or "Revised" License
1.55k stars 397 forks source link

Adding support for combining this library with realm #1350

Open martinale14 opened 1 year ago

kevmoo commented 1 year ago

Uh...I'm excited to see the contribution.

Making something specific to one other framework is less than ideal, but I'm interested in iterating. What are you trying to do here? I don't see any tests or a changelog entry

martinale14 commented 1 year ago

Uh...I'm excited to see the contribution.

Making something specific to one other framework is less than ideal, but I'm interested in iterating. What are you trying to do here? I don't see any tests or a changelog entry

Actually im working on a better solution, im going to update this pr adding some test cases and the changelog, i tried to remove some code to change the core of the package and maintain old implementations

kevmoo commented 1 year ago

Uh...I'm excited to see the contribution. Making something specific to one other framework is less than ideal, but I'm interested in iterating. What are you trying to do here? I don't see any tests or a changelog entry

Actually im working on a better solution, im going to update this pr adding some test cases and the changelog, i tried to remove some code to change the core of the package and maintain old implementations

I don't like "support realm". That's a bit of a deal breaker. If you could explain what you're trying to do a bit more, I'm happy to discuss!

martinale14 commented 1 year ago

``> > > Uh...I'm excited to see the contribution.

Making something specific to one other framework is less than ideal, but I'm interested in iterating. What are you trying to do here? I don't see any tests or a changelog entry

Actually im working on a better solution, im going to update this pr adding some test cases and the changelog, i tried to remove some code to change the core of the package and maintain old implementations

I don't like "support realm". That's a bit of a deal breaker. If you could explain what you're trying to do a bit more, I'm happy to discuss!

realm is a solution for local and remote db, and also generates a file and it generates with this structure:

@RealmModel()
abstract class _TeacherModel {
  @JsonKey(name: '_id')
  late String id;
  @JsonKey(name: 'name')
  late String name;
  @JsonKey(name: 'last_name')
  late String lastName;
  @JsonKey(name: 'is_active')
  late bool isActive;
  @JsonKey(name: 'age')
  late int age;
  @JsonKey(name: 'is_old')
  late bool? isOld;
}

generates this code:

class TeacherModel extends _TeacherModel
    with RealmEntity, RealmObjectBase, RealmObject {
  TeacherModel(
    ObjectId id,
    String name,
    String lastName,
    bool isActive,
    int age, {
    bool? isOld,
  }) {
    RealmObjectBase.set(this, '_id', id);
    RealmObjectBase.set(this, 'name', name);
    RealmObjectBase.set(this, 'lastName', lastName);
    RealmObjectBase.set(this, 'isActive', isActive);
    RealmObjectBase.set(this, 'age', age);
    RealmObjectBase.set(this, 'isOld', isOld);
  }

  TeacherModel._();

  @override
  ObjectId get id => RealmObjectBase.get<ObjectId>(this, '_id') as ObjectId;
  @override
  set id(ObjectId value) => RealmObjectBase.set(this, '_id', value);

  @override
  String get name => RealmObjectBase.get<String>(this, 'name') as String;
  @override
  set name(String value) => RealmObjectBase.set(this, 'name', value);

  @override
  String get lastName =>
      RealmObjectBase.get<String>(this, 'lastName') as String;
  @override
  set lastName(String value) => RealmObjectBase.set(this, 'lastName', value);

  @override
  bool get isActive => RealmObjectBase.get<bool>(this, 'isActive') as bool;
  @override
  set isActive(bool value) => RealmObjectBase.set(this, 'isActive', value);

  @override
  int get age => RealmObjectBase.get<int>(this, 'age') as int;
  @override
  set age(int value) => RealmObjectBase.set(this, 'age', value);

  @override
  bool? get isOld => RealmObjectBase.get<bool>(this, 'isOld') as bool?;
  @override
  set isOld(bool? value) => RealmObjectBase.set(this, 'isOld', value);

  @override
  Stream<RealmObjectChanges<TeacherModel>> get changes =>
      RealmObjectBase.getChanges<TeacherModel>(this);

  @override
  TeacherModel freeze() => RealmObjectBase.freezeObject<TeacherModel>(this);

  static SchemaObject get schema => _schema ??= _initSchema();
  static SchemaObject? _schema;
  static SchemaObject _initSchema() {
    RealmObjectBase.registerFactory(TeacherModel._);
    return const SchemaObject(
        ObjectType.realmObject, TeacherModel, 'TeacherModel', [
      SchemaProperty('id', RealmPropertyType.objectid,
          mapTo: '_id', primaryKey: true),
      SchemaProperty('name', RealmPropertyType.string),
      SchemaProperty('lastName', RealmPropertyType.string),
      SchemaProperty('isActive', RealmPropertyType.bool),
      SchemaProperty('age', RealmPropertyType.int),
      SchemaProperty('isOld', RealmPropertyType.bool, optional: true),
    ]);
  }
}

but when combining with json serializable this causes an error its that the serializable generates converters for the _TeacherModelClass, so the idea of the pr is to create a flag that allow us to build depending on the realm generated model following the same rules