From 488ae40015d79d0a0c8593992e59790bba62fc44 Mon Sep 17 00:00:00 2001 From: Serge Shkurko Date: Sun, 29 Dec 2019 19:25:22 +0300 Subject: [PATCH] Added typeId property to HiveType (#163) --- example/lib/main.dart | 6 ++++-- example/lib/main.g.dart | 3 +++ lib/src/adapters/big_int_adapter.dart | 3 +++ lib/src/adapters/date_time_adapter.dart | 3 +++ lib/src/annotations/hive_type.dart | 4 +++- lib/src/hive_impl.dart | 4 ++-- lib/src/registry/type_adapter.dart | 3 +++ lib/src/registry/type_registry.dart | 2 +- lib/src/registry/type_registry_impl.dart | 16 ++++++++-------- test/integration/hive_list_test.dart | 3 +++ test/integration/hive_object_test.dart | 3 +++ test/tests/registry/type_registry_impl_test.dart | 12 ++++++++++-- 12 files changed, 46 insertions(+), 16 deletions(-) diff --git a/example/lib/main.dart b/example/lib/main.dart index 6eb2304a6..37a9073e4 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -4,7 +4,7 @@ import 'package:hive/hive.dart'; part 'main.g.dart'; -@HiveType() +@HiveType(typeId: 1) class Person { @HiveField(0) String name; @@ -23,7 +23,9 @@ class Person { void main() async { var path = Directory.current.path; - Hive.init(path); + Hive + ..init(path) + ..registerAdapter(PersonAdapter()); var box = await Hive.openBox('testBox'); diff --git a/example/lib/main.g.dart b/example/lib/main.g.dart index a0e69a645..a7c572dc9 100644 --- a/example/lib/main.g.dart +++ b/example/lib/main.g.dart @@ -7,6 +7,9 @@ part of 'main.dart'; // ************************************************************************** class PersonAdapter extends TypeAdapter { + @override + int get typeId => 1; + @override Person read(BinaryReader reader) { var numOfFields = reader.readByte(); diff --git a/lib/src/adapters/big_int_adapter.dart b/lib/src/adapters/big_int_adapter.dart index 0220d67a6..e71f1879f 100644 --- a/lib/src/adapters/big_int_adapter.dart +++ b/lib/src/adapters/big_int_adapter.dart @@ -1,6 +1,9 @@ import 'package:hive/hive.dart'; class BigIntAdapter extends TypeAdapter { + @override + int get typeId => 17; + @override BigInt read(BinaryReader reader) { var len = reader.readByte(); diff --git a/lib/src/adapters/date_time_adapter.dart b/lib/src/adapters/date_time_adapter.dart index 3da8966ae..1cce46049 100644 --- a/lib/src/adapters/date_time_adapter.dart +++ b/lib/src/adapters/date_time_adapter.dart @@ -1,6 +1,9 @@ import 'package:hive/hive.dart'; class DateTimeAdapter extends TypeAdapter { + @override + int get typeId => 16; + @override DateTime read(BinaryReader reader) { var micros = reader.readInt(); diff --git a/lib/src/annotations/hive_type.dart b/lib/src/annotations/hive_type.dart index 85bc5d8ac..72ef21fd0 100644 --- a/lib/src/annotations/hive_type.dart +++ b/lib/src/annotations/hive_type.dart @@ -2,9 +2,11 @@ part of hive; /// Annotate classes with [HiveType] to generate a `TypeAdapter`. class HiveType { + final int typeId; + /// The name of the generated adapter. final String adapterName; /// If [adapterName] is not set, it'll be `"YourClass" + "Adapter"`. - const HiveType({this.adapterName}); + const HiveType({@required this.typeId, this.adapterName}); } diff --git a/lib/src/hive_impl.dart b/lib/src/hive_impl.dart index 56c6d6573..a9ef2e10b 100644 --- a/lib/src/hive_impl.dart +++ b/lib/src/hive_impl.dart @@ -32,8 +32,8 @@ class HiveImpl extends TypeRegistryImpl implements HiveInterface { } void _registerDefaultAdapters() { - registerInternal(DateTimeAdapter(), 16); - registerInternal(BigIntAdapter(), 17); + registerInternal(DateTimeAdapter()); + registerInternal(BigIntAdapter()); } @override diff --git a/lib/src/registry/type_adapter.dart b/lib/src/registry/type_adapter.dart index e08d2c417..0af4416d7 100644 --- a/lib/src/registry/type_adapter.dart +++ b/lib/src/registry/type_adapter.dart @@ -3,6 +3,9 @@ part of hive; /// Type adapters can be implemented to support non primitive values. @immutable abstract class TypeAdapter { + /// Called for type registration + int get typeId; + /// Is called when a value has to be decoded. T read(BinaryReader reader); diff --git a/lib/src/registry/type_registry.dart b/lib/src/registry/type_registry.dart index b6aad4136..56fbb21b9 100644 --- a/lib/src/registry/type_registry.dart +++ b/lib/src/registry/type_registry.dart @@ -7,5 +7,5 @@ abstract class TypeRegistry { /// Register a [TypeAdapter] to announce it to Hive. /// /// This is a necessary step before custom adapter can be used by Hive. - void registerAdapter(TypeAdapter adapter, int typeId); + void registerAdapter(TypeAdapter adapter, [int typeId]); } diff --git a/lib/src/registry/type_registry_impl.dart b/lib/src/registry/type_registry_impl.dart index 6fa5278fa..3dfa33831 100644 --- a/lib/src/registry/type_registry_impl.dart +++ b/lib/src/registry/type_registry_impl.dart @@ -28,23 +28,23 @@ class TypeRegistryImpl implements TypeRegistry { } @override - void registerAdapter(TypeAdapter adapter, int typeId) { - if (typeId < 0 || typeId > 223) { - throw HiveError('TypeId $typeId not allowed.'); + void registerAdapter(TypeAdapter adapter, [int typeId]) { + if (adapter.typeId < 0 || adapter.typeId > 223) { + throw HiveError('TypeId ${adapter.typeId} not allowed.'); } - var updatedTypeId = typeId + reservedTypeIds; + var updatedTypeId = adapter.typeId + reservedTypeIds; if (findAdapterForTypeId(updatedTypeId) != null) { throw HiveError('There is already a TypeAdapter for typeId $typeId.'); } - registerInternal(adapter, updatedTypeId); + registerInternal(adapter); } - void registerInternal(TypeAdapter adapter, int typeId) { - var resolved = _ResolvedAdapter(adapter, typeId); - _typeAdapters[typeId] = resolved; + void registerInternal(TypeAdapter adapter) { + var resolved = _ResolvedAdapter(adapter, adapter.typeId); + _typeAdapters[adapter.typeId] = resolved; } void resetAdapters() { diff --git a/test/integration/hive_list_test.dart b/test/integration/hive_list_test.dart index 349b1024b..b61321904 100644 --- a/test/integration/hive_list_test.dart +++ b/test/integration/hive_list_test.dart @@ -18,6 +18,9 @@ class _TestObject extends HiveObject { } class _TestObjectAdapter extends TypeAdapter<_TestObject> { + @override + int get typeId => 0; + @override _TestObject read(BinaryReader reader) { return _TestObject(reader.read() as String) diff --git a/test/integration/hive_object_test.dart b/test/integration/hive_object_test.dart index fd63304f5..26ad60f5f 100644 --- a/test/integration/hive_object_test.dart +++ b/test/integration/hive_object_test.dart @@ -14,6 +14,9 @@ class _TestObject extends HiveObject { } class _TestObjectAdapter extends TypeAdapter<_TestObject> { + @override + int get typeId => 0; + @override _TestObject read(BinaryReader reader) { return _TestObject(reader.readString()); diff --git a/test/tests/registry/type_registry_impl_test.dart b/test/tests/registry/type_registry_impl_test.dart index fa127f61b..6ab506cad 100644 --- a/test/tests/registry/type_registry_impl_test.dart +++ b/test/tests/registry/type_registry_impl_test.dart @@ -5,6 +5,11 @@ import 'package:test/test.dart'; import '../common.dart'; class TestAdapter extends TypeAdapter { + TestAdapter([this.typeId = 0]); + + @override + final int typeId; + @override int read(BinaryReader reader) { return 5; @@ -15,6 +20,9 @@ class TestAdapter extends TypeAdapter { } class TestAdapter2 extends TypeAdapter { + @override + int get typeId => 1; + @override int read(BinaryReader reader) { return 5; @@ -39,9 +47,9 @@ void main() { test('unsupported typeId', () { var registry = TypeRegistryImpl(); - expect(() => registry.registerAdapter(TestAdapter(), -1), + expect(() => registry.registerAdapter(TestAdapter(-1), -1), throwsHiveError('not allowed')); - expect(() => registry.registerAdapter(TestAdapter(), 224), + expect(() => registry.registerAdapter(TestAdapter(224), 224), throwsHiveError('not allowed')); });