realm / realm-dart

Realm is a mobile database: a replacement for SQLite & ORMs.
Apache License 2.0
763 stars 87 forks source link

Generated RealmObject classes have incorrect names for embedded objects when using two leading underscores #1684

Open akshdeep-singh opened 5 months ago

akshdeep-singh commented 5 months ago

What happened?

I require some of my generated realm classes to be private, so I am using two underscores and expect the generated classes to have single leading underscore. This is working for ObjectType.realmObject, but not for ObjectType.embeddedObject.

Inside generated file, the embedded realm class doesn't have expected leading underscore inside the parent class.

Repro steps

  1. Define data model classes (both realmObject and embeddedObject) having name with two leading underscores.
  2. Generate realm classes dart run realm generate

Version

3.19.3 Flutter SDK, 3.3.1 Dart SDK

What Atlas Services are you using?

Local Database only

What type of application is this?

Flutter Application

Client OS and version

Pixel_3a_API_34_extension_level_7_x86_64

Code snippets

import 'package:realm/realm.dart';

part 'test.realm.dart';

@RealmModel(ObjectType.embeddedObject)
class __B {
  late String text;
}

@RealmModel()
class __A {
  @PrimaryKey()
  late String id;
  late List<__B> items;
}
// GENERATED CODE - DO NOT MODIFY BY HAND

part of 'test.dart';

// **************************************************************************
// RealmObjectGenerator
// **************************************************************************

// ignore_for_file: type=lint
class _B extends __B with RealmEntity, RealmObjectBase, EmbeddedObject {
  _B(
    String text,
  ) {
    RealmObjectBase.set(this, 'text', text);
  }

  _B._();

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

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

  @override
  Stream<RealmObjectChanges<_B>> changesFor([List<String>? keyPaths]) =>
      RealmObjectBase.getChangesFor<_B>(this, keyPaths);

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

  EJsonValue toEJson() {
    return <String, dynamic>{
      'text': text.toEJson(),
    };
  }

  static EJsonValue _toEJson(_B value) => value.toEJson();
  static _B _fromEJson(EJsonValue ejson) {
    return switch (ejson) {
      {
        'text': EJsonValue text,
      } =>
        _B(
          fromEJson(text),
        ),
      _ => raiseInvalidEJson(ejson),
    };
  }

  static final schema = () {
    RealmObjectBase.registerFactory(_B._);
    register(_toEJson, _fromEJson);
    return SchemaObject(ObjectType.embeddedObject, _B, '_B', [
      SchemaProperty('text', RealmPropertyType.string),
    ]);
  }();

  @override
  SchemaObject get objectSchema => RealmObjectBase.getSchema(this) ?? schema;
}

class _A extends __A with RealmEntity, RealmObjectBase, RealmObject {
  _A(
    String id, {
    Iterable<B> items = const [],
  }) {
    RealmObjectBase.set(this, 'id', id);
    RealmObjectBase.set<RealmList<B>>(this, 'items', RealmList<B>(items));
  }

  _A._();

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

  @override
  RealmList<B> get items =>
      RealmObjectBase.get<B>(this, 'items') as RealmList<B>;
  @override
  set items(covariant RealmList<B> value) => throw RealmUnsupportedSetError();

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

  @override
  Stream<RealmObjectChanges<_A>> changesFor([List<String>? keyPaths]) =>
      RealmObjectBase.getChangesFor<_A>(this, keyPaths);

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

  EJsonValue toEJson() {
    return <String, dynamic>{
      'id': id.toEJson(),
      'items': items.toEJson(),
    };
  }

  static EJsonValue _toEJson(_A value) => value.toEJson();
  static _A _fromEJson(EJsonValue ejson) {
    return switch (ejson) {
      {
        'id': EJsonValue id,
        'items': EJsonValue items,
      } =>
        _A(
          fromEJson(id),
          items: fromEJson(items),
        ),
      _ => raiseInvalidEJson(ejson),
    };
  }

  static final schema = () {
    RealmObjectBase.registerFactory(_A._);
    register(_toEJson, _fromEJson);
    return SchemaObject(ObjectType.realmObject, _A, '_A', [
      SchemaProperty('id', RealmPropertyType.string, primaryKey: true),
      SchemaProperty('items', RealmPropertyType.object,
          linkTarget: 'B', collectionType: RealmCollectionType.list),
    ]);
  }();

  @override
  SchemaObject get objectSchema => RealmObjectBase.getSchema(this) ?? schema;
}

Stacktrace of the exception/crash you're getting

No response

Relevant log output

No response

sync-by-unito[bot] commented 5 months ago

➤ PM Bot commented:

Jira ticket: RDART-1033