From f2160b8259053b7db5002ba21fd5805a58432649 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kasper=20Overg=C3=A5rd=20Nielsen?= Date: Fri, 9 Sep 2022 17:45:26 +0200 Subject: [PATCH 1/4] Add realm specific implementation for List.add and List.insert --- lib/src/list.dart | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/lib/src/list.dart b/lib/src/list.dart index 7f531e56b..e53a70a3c 100644 --- a/lib/src/list.dart +++ b/lib/src/list.dart @@ -63,7 +63,11 @@ class ManagedRealmList extends collection.ListBase with Re @override set length(int newLength) { var l = length; - if (newLength < l) removeRange(newLength, l); + if (newLength < l) { + removeRange(newLength, l); + } else { + throw RealmException('You cannot increase length on a realm list without adding elements'); + } } @override @@ -93,6 +97,16 @@ class ManagedRealmList extends collection.ListBase with Re } } + @override + void add(T element) { + RealmListInternal.setValue(handle, realm, length, element); + } + + @override + void insert(int index, T element) { + realmCore.listInsertElementAt(handle, index, element); + } + @override void operator []=(int index, T value) { RealmListInternal.setValue(handle, realm, index, value); From 014b32e91c665c8de3001a664277ebd175e03337 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kasper=20Overg=C3=A5rd=20Nielsen?= Date: Fri, 9 Sep 2022 17:58:01 +0200 Subject: [PATCH 2/4] Add test case --- test/list_test.dart | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/test/list_test.dart b/test/list_test.dart index fb02ab7ee..ad298050d 100644 --- a/test/list_test.dart +++ b/test/list_test.dart @@ -699,4 +699,15 @@ Future main([List? args]) async { expect(team.players, isNot(players)); expect(team.players, unorderedMatches(players)); }); + + test('ManagedRealmList.length= throws', () { + final config = Configuration.local([Team.schema, Person.schema]); + final realm = getRealm(config); + + final team = Team('sad team'); + + realm.write(() => realm.add(team)); + + expect(() => realm.write(() => team.players.length = 100), throws('You cannot increase length on a realm list without adding elements')); + }); } From 1221f77c982d1fc7df2a4953cd0c811d9d270bdb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kasper=20Overg=C3=A5rd=20Nielsen?= Date: Fri, 9 Sep 2022 18:01:36 +0200 Subject: [PATCH 3/4] Update CHANGELOG --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6071e241b..e2fdabcea 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ ### Fixed * Allow null arguments on query. ([#872](https://github.com/realm/realm-dart/pull/872)). Fixes [#871](https://github.com/realm/realm-dart/issues/871) * Previously removeAt did not truncate length. ([#884](https://github.com/realm/realm-dart/pull/884)). Fixes [#883](https://github.com/realm/realm-dart/issues/883) +* List.length= now throws, if you try to increase length, ([#894](https://github.com/realm/realm-dart/pull/894)). ### Compatibility * Realm Studio: 12.0.0 or later. From e5940ca0850c7e19ef61c8821fccaa73c5c2ccc5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kasper=20Overg=C3=A5rd=20Nielsen?= Date: Mon, 12 Sep 2022 11:49:12 +0200 Subject: [PATCH 4/4] Add test case for decreasing lenght --- test/list_test.dart | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/test/list_test.dart b/test/list_test.dart index ad298050d..25e561f8d 100644 --- a/test/list_test.dart +++ b/test/list_test.dart @@ -700,7 +700,7 @@ Future main([List? args]) async { expect(team.players, unorderedMatches(players)); }); - test('ManagedRealmList.length= throws', () { + test('ManagedRealmList.length= throws on increase', () { final config = Configuration.local([Team.schema, Person.schema]); final realm = getRealm(config); @@ -710,4 +710,18 @@ Future main([List? args]) async { expect(() => realm.write(() => team.players.length = 100), throws('You cannot increase length on a realm list without adding elements')); }); + + test('ManagedRealmList.length= truncates on decrease', () { + final config = Configuration.local([Team.schema, Person.schema]); + final realm = getRealm(config); + + final team = Team('sad team', players: [for (int i = 0; i < 100; ++i) Person('$i')]); + realm.write(() => realm.add(team)); + expect(team.players.length, 100); + expect(realm.all().length, 100); + + expect(realm.write(() => team.players.length = 10), 10); + expect(team.players.length, 10); + expect(realm.all().length, 100); + }); }