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

RDART-852: Allow private fields on models #1635

Merged
merged 2 commits into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* Add better hint to error message, if opening native library fails. (Issue [#1595](https://github.com/realm/realm-dart/issues/1595))
* Added support for specifying schema version on `Configuration.flexibleSync`. This allows you to take advantage of an upcoming server-side feature that will allow schema migrations for synchronized Realms. (Issue [#1599](https://github.com/realm/realm-dart/issues/1599))
* The default base url in `AppConfiguration` has been updated to point to `services.cloud.mongodb.com`. See https://www.mongodb.com/docs/atlas/app-services/domain-migration/ for more information. (Issue [#1549](https://github.com/realm/realm-dart/issues/1549))
* Don't ignore private fields on realm models. (Issue [#1367](https://github.com/realm/realm-dart/issues/1367))

### Fixed
* Using valid const, but non-literal expressions, such as negation of numbers, as an initializer would fail. (Issue [#1606](https://github.com/realm/realm-dart/issues/1606))
Expand Down
4 changes: 2 additions & 2 deletions packages/realm_generator/lib/src/field_element_ex.dart
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ extension FieldElementEx on FieldElement {
return null;
}

if (ignoredInfo != null || isPrivate) {
// skip ignored and private fields
if (ignoredInfo != null) {
// skip ignored fields
return null;
}

Expand Down
1 change: 1 addition & 0 deletions packages/realm_generator/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ dependencies:
realm_common: ^2.0.0
source_gen: ^1.1.0
source_span: ^1.8.0
collection: ^1.18.0

dev_dependencies:
build_runner: ^2.1.0
Expand Down
11 changes: 11 additions & 0 deletions packages/realm_generator/test/good_test_data/private_fields.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// ignore_for_file: unused_element, prefer_final_fields, unused_field

import 'package:realm_common/realm_common.dart';

part 'private_fields.realm.dart';

@RealmModel()
class _WithPrivateFields {
late String _plain;
int _withDefault = 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// GENERATED CODE - DO NOT MODIFY BY HAND

part of 'private_fields.dart';

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

// ignore_for_file: type=lint
class WithPrivateFields extends _WithPrivateFields
with RealmEntity, RealmObjectBase, RealmObject {
static var _defaultsSet = false;

WithPrivateFields(
String _plain, {
int _withDefault = 0,
}) {
if (!_defaultsSet) {
_defaultsSet = RealmObjectBase.setDefaults<WithPrivateFields>({
'_withDefault': 0,
});
}
RealmObjectBase.set(this, '_plain', _plain);
RealmObjectBase.set(this, '_withDefault', _withDefault);
}

WithPrivateFields._();

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

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

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

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

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

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

static final schema = () {
RealmObjectBase.registerFactory(WithPrivateFields._);
register(_toEJson, _fromEJson);
return SchemaObject(
ObjectType.realmObject, WithPrivateFields, 'WithPrivateFields', [
SchemaProperty('_plain', RealmPropertyType.string),
SchemaProperty('_withDefault', RealmPropertyType.int),
]);
}();

@override
SchemaObject get objectSchema => RealmObjectBase.getSchema(this) ?? schema;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// MOCK FILE! This file exists to ensure the parent file is valid Dart.
// The parent will be used as input to the realm_generator in a test, and the
// output compared to the .expected file.

part of 'private_fields.dart';
Loading