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

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

Open
akshdeep-singh opened this issue May 22, 2024 · 1 comment

Comments

@akshdeep-singh
Copy link

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

Copy link

sync-by-unito bot commented May 22, 2024

➤ PM Bot commented:

Jira ticket: RDART-1033

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

No branches or pull requests

1 participant