From 8aad6869043ad5255ce06af7aee6f21ac78e9d36 Mon Sep 17 00:00:00 2001 From: Misir Jafarov Date: Fri, 26 Mar 2021 17:08:06 +0400 Subject: [PATCH] feat(hive): create HiveObjectMixin (#613) --- hive/lib/hive.dart | 2 +- hive/lib/src/object/hive_collection.dart | 4 +- .../lib/src/object/hive_collection_mixin.dart | 2 +- hive/lib/src/object/hive_list.dart | 6 +-- hive/lib/src/object/hive_list_impl.dart | 4 +- hive/lib/src/object/hive_object.dart | 4 +- hive/lib/src/object/hive_object_internal.dart | 2 +- hive/test/tests/mocks.mocks.dart | 41 ++++++++++--------- 8 files changed, 35 insertions(+), 30 deletions(-) diff --git a/hive/lib/hive.dart b/hive/lib/hive.dart index 61dfa2c1b..66769b5f6 100644 --- a/hive/lib/hive.dart +++ b/hive/lib/hive.dart @@ -18,7 +18,7 @@ import 'package:hive/src/object/hive_object.dart'; import 'package:hive/src/util/extensions.dart'; import 'package:meta/meta.dart'; -export 'src/object/hive_object.dart' show HiveObject; +export 'src/object/hive_object.dart' show HiveObject, HiveObjectMixin; part 'src/annotations/hive_field.dart'; part 'src/annotations/hive_type.dart'; diff --git a/hive/lib/src/object/hive_collection.dart b/hive/lib/src/object/hive_collection.dart index 6bcb9e68e..8595b748e 100644 --- a/hive/lib/src/object/hive_collection.dart +++ b/hive/lib/src/object/hive_collection.dart @@ -1,7 +1,7 @@ part of hive; -/// List containing [HiveObject]s. -abstract class HiveCollection implements List { +/// List containing [HiveObjectMixin]s. +abstract class HiveCollection implements List { /// The box which contains all the objects in this collection BoxBase get box; diff --git a/hive/lib/src/object/hive_collection_mixin.dart b/hive/lib/src/object/hive_collection_mixin.dart index 455a0c4d2..86890f016 100644 --- a/hive/lib/src/object/hive_collection_mixin.dart +++ b/hive/lib/src/object/hive_collection_mixin.dart @@ -1,7 +1,7 @@ import 'package:hive/hive.dart'; /// Implemetation of [HiveCollection]. -abstract class HiveCollectionMixin +abstract class HiveCollectionMixin implements HiveCollection { @override Iterable get keys sync* { diff --git a/hive/lib/src/object/hive_list.dart b/hive/lib/src/object/hive_list.dart index 72561f01b..772915060 100644 --- a/hive/lib/src/object/hive_list.dart +++ b/hive/lib/src/object/hive_list.dart @@ -1,8 +1,8 @@ part of hive; -/// Allows defining references to other [HiveObject]s. +/// Allows defining references to other [HiveObjectMixin]s. @experimental -abstract class HiveList extends HiveCollection +abstract class HiveList extends HiveCollection implements List { /// Create a new HiveList which can contain HiveObjects from [box]. factory HiveList(Box box, {List? objects}) => @@ -13,5 +13,5 @@ abstract class HiveList extends HiveCollection void dispose(); /// Casts the list to a new HiveList. - HiveList castHiveList(); + HiveList castHiveList(); } diff --git a/hive/lib/src/object/hive_list_impl.dart b/hive/lib/src/object/hive_list_impl.dart index baa6a0649..d5ef34cf6 100644 --- a/hive/lib/src/object/hive_list_impl.dart +++ b/hive/lib/src/object/hive_list_impl.dart @@ -8,7 +8,7 @@ import 'package:hive/src/util/delegating_list_view_mixin.dart'; import 'package:meta/meta.dart'; /// Not part of public API -class HiveListImpl +class HiveListImpl with HiveCollectionMixin, ListMixin, DelegatingListViewMixin implements HiveList { /// Not part of public API @@ -159,7 +159,7 @@ class HiveListImpl } @override - HiveList castHiveList() { + HiveList castHiveList() { if (_delegate != null) { return HiveListImpl(box, objects: _delegate!.cast()); } else { diff --git a/hive/lib/src/object/hive_object.dart b/hive/lib/src/object/hive_object.dart index 8bd86d9f3..e35faf08c 100644 --- a/hive/lib/src/object/hive_object.dart +++ b/hive/lib/src/object/hive_object.dart @@ -8,7 +8,7 @@ part 'hive_object_internal.dart'; /// Extend `HiveObject` to add useful methods to the objects you want to store /// in Hive -abstract class HiveObject { +mixin HiveObjectMixin { BoxBase? _box; dynamic _key; @@ -57,3 +57,5 @@ abstract class HiveObject { return false; } } + +abstract class HiveObject with HiveObjectMixin {} diff --git a/hive/lib/src/object/hive_object_internal.dart b/hive/lib/src/object/hive_object_internal.dart index c4d5c0e12..d3f322c14 100644 --- a/hive/lib/src/object/hive_object_internal.dart +++ b/hive/lib/src/object/hive_object_internal.dart @@ -1,7 +1,7 @@ part of hive_object_internal; /// Not part of public API -extension HiveObjectInternal on HiveObject { +extension HiveObjectInternal on HiveObjectMixin { /// Not part of public API @pragma('vm:prefer-inline') @pragma('dart2js:tryInline') diff --git a/hive/test/tests/mocks.mocks.dart b/hive/test/tests/mocks.mocks.dart index 9a545963d..aea296179 100644 --- a/hive/test/tests/mocks.mocks.dart +++ b/hive/test/tests/mocks.mocks.dart @@ -1,21 +1,22 @@ -import 'package:hive/src/box/default_compaction_strategy.dart'; -import 'package:hive/src/box/default_key_comparator.dart'; -import 'package:mockito/mockito.dart' as _i1; +import 'dart:async' as _i7; import 'dart:collection' as _i2; -import 'package:hive/hive.dart' as _i3; -import 'package:hive/src/object/hive_object.dart' as _i4; -import 'dart:typed_data' as _i5; +import 'dart:convert' as _i15; import 'dart:io' as _i6; -import 'dart:async' as _i7; -import 'package:hive/src/box/change_notifier.dart' as _i8; -import 'package:hive/src/binary/frame.dart' as _i9; +import 'dart:typed_data' as _i5; + +import 'package:hive/hive.dart' as _i3; import 'package:hive/src/backend/storage_backend.dart' as _i10; +import 'package:hive/src/binary/frame.dart' as _i9; +import 'package:hive/src/box/change_notifier.dart' as _i8; +import 'package:hive/src/box/default_compaction_strategy.dart'; +import 'package:hive/src/box/default_key_comparator.dart'; import 'package:hive/src/box/keystore.dart' as _i11; import 'package:hive/src/hive_impl.dart' as _i12; -import 'package:hive/src/registry/type_registry_impl.dart' as _i13; -import 'package:hive/src/object/hive_list_impl.dart' as _i14; -import 'dart:convert' as _i15; import 'package:hive/src/io/frame_io_helper.dart' as _i16; +import 'package:hive/src/object/hive_list_impl.dart' as _i14; +import 'package:hive/src/object/hive_object.dart' as _i4; +import 'package:hive/src/registry/type_registry_impl.dart' as _i13; +import 'package:mockito/mockito.dart' as _i1; class _FakeType extends _i1.Fake implements Type {} @@ -27,7 +28,7 @@ class _FakeLazyBox extends _i1.Fake implements _i3.LazyBox {} class _FakeBoxBase extends _i1.Fake implements _i3.BoxBase {} -class _FakeHiveList extends _i1.Fake +class _FakeHiveList extends _i1.Fake implements _i3.HiveList {} class _FakeIterator extends _i1.Fake implements Iterator {} @@ -162,9 +163,11 @@ class MockKeystore extends _i1.Mock implements _i11.Keystore { _i7.Stream<_i3.BoxEvent> watch({dynamic key}) => (super.noSuchMethod( Invocation.method(#watch, [], {#key: key}), Stream<_i3.BoxEvent>.empty()) as _i7.Stream<_i3.BoxEvent>); - _i9.Frame? insert(_i9.Frame? frame, {bool? notify = true, bool lazy = false}) => - (super.noSuchMethod(Invocation.method(#insert, [frame], {#notify: notify})) - as _i9.Frame?); + _i9.Frame? insert(_i9.Frame? frame, + {bool? notify = true, bool lazy = false}) => + (super.noSuchMethod( + Invocation.method(#insert, [frame], {#notify: notify})) + as _i9.Frame?); bool beginTransaction(List<_i9.Frame>? newFrames) => (super.noSuchMethod( Invocation.method(#beginTransaction, [newFrames]), false) as bool); int clear() => (super.noSuchMethod(Invocation.method(#clear, []), 0) as int); @@ -277,7 +280,7 @@ class MockHiveList extends _i1.Mock as _i3.BoxBase); Iterable get keys => (super.noSuchMethod(Invocation.getter(#keys), []) as Iterable); - _i3.HiveList castHiveList() => + _i3.HiveList castHiveList() => (super.noSuchMethod( Invocation.method(#castHiveList, []), _FakeHiveList()) as _i3.HiveList); @@ -336,7 +339,7 @@ class MockHiveListImpl extends _i1.Mock super.noSuchMethod(Invocation.method(#add, [element])); void addAll(Iterable? iterable) => super.noSuchMethod(Invocation.method(#addAll, [iterable])); - _i3.HiveList castHiveList() => + _i3.HiveList castHiveList() => (super.noSuchMethod( Invocation.method(#castHiveList, []), _FakeHiveList()) as _i3.HiveList); @@ -672,7 +675,7 @@ class MockBinaryWriter extends _i1.Mock implements _i3.BinaryWriter { void writeMap(Map? map, {bool? writeLength = true}) => super.noSuchMethod( Invocation.method(#writeMap, [map], {#writeLength: writeLength})); - void writeHiveList(_i3.HiveList<_i4.HiveObject>? list, + void writeHiveList(_i3.HiveList<_i4.HiveObjectMixin>? list, {bool? writeLength = true}) => super.noSuchMethod(Invocation.method( #writeHiveList, [list], {#writeLength: writeLength}));