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

Drop final requirement on primary keys for now #240

Merged
merged 2 commits into from
Feb 7, 2022
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
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
vNext
==============================================================

### Changes
* Primary key annotation no longer requires field to be final.


0.2.0+alpha Release notes (2022-01-31)
==============================================================

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ The Realm Flutter package name is `realm`
@RealmModel()
class _Item {
@PrimaryKey()
late final int id;
late int id;

late String name;

Expand Down
14 changes: 14 additions & 0 deletions generator/lib/src/field_element_ex.dart
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,19 @@ extension FieldElementEx on FieldElement {
element: this,
));
}
// Since the setter of a dart late final public field without initializer is public,
// the error of setting a primary key after construction will be a runtime error no matter
// what we do. See:
//
// https://github.com/dart-lang/language/issues/1239
// https://github.com/dart-lang/language/issues/2068
//
// Hence we may as well lift the restriction that primary keys must be declared final.
//
// However, this may change in the future. Either as the dart language team change this
// blemish. Or perhaps we can avoid the late modifier, once static meta programming lands
// in dart. Therefor we keep the code outcommented for later.
/*
if (!isFinal) {
throw RealmInvalidGenerationSourceError(
'Primary key field is not final',
Expand All @@ -116,6 +129,7 @@ extension FieldElementEx on FieldElement {
element: this,
);
}
*/
}

// Validate indexes
Expand Down
40 changes: 0 additions & 40 deletions generator/test/generator_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -226,46 +226,6 @@ class _Bad {
);
});

test('primary key not final', () async {
await expectLater(
() async => await testBuilder(
generateRealmObjects(),
{
'pkg|lib/src/test.dart': r'''
import 'package:realm_common/realm_common.dart';

part 'test.g.dart';

@RealmModel()
class _Bad {
@PrimaryKey()
late int primaryKeyIsNotFinal;
}'''
},
reader: await PackageAssetReader.currentIsolate(),
),
throwsA(
isA<RealmInvalidGenerationSourceError>().having(
(e) => e.format(),
'format()',
'Primary key field is not final\n'
'\n'
'in: package:pkg/src/test.dart:8:12\n'
' ╷\n'
'5 │ @RealmModel()\n'
'6 │ class _Bad {\n'
' │ ━━━━ in realm model for \'Bad\'\n'
'7 │ @PrimaryKey()\n'
'8 │ late int primaryKeyIsNotFinal;\n'
' │ ^^^^^^^^^^^^^^^^^^^^ is not final\n'
' ╵\n'
'Add a final keyword to the definition of \'primaryKeyIsNotFinal\', or remove the @PrimaryKey annotation.\n'
'',
),
),
);
});

test('primary keys always indexed', () async {
final sb = StringBuffer();
var done = false;
Expand Down