Skip to content

Commit

Permalink
RDART-852: Allow private fields on models (#1635)
Browse files Browse the repository at this point in the history
* Allow private fields on models

* Update CHANGELOG
  • Loading branch information
nielsenko authored Apr 17, 2024
1 parent f22f129 commit 642f2a6
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 2 deletions.
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';

0 comments on commit 642f2a6

Please sign in to comment.