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

Added typeId property to HiveType (#163) #164

Merged
merged 1 commit into from
Dec 31, 2019
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
6 changes: 4 additions & 2 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import 'package:hive/hive.dart';

part 'main.g.dart';

@HiveType()
@HiveType(typeId: 1)
class Person {
@HiveField(0)
String name;
Expand All @@ -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');

Expand Down
3 changes: 3 additions & 0 deletions example/lib/main.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions lib/src/adapters/big_int_adapter.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import 'package:hive/hive.dart';

class BigIntAdapter extends TypeAdapter<BigInt> {
@override
int get typeId => 17;

@override
BigInt read(BinaryReader reader) {
var len = reader.readByte();
Expand Down
3 changes: 3 additions & 0 deletions lib/src/adapters/date_time_adapter.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import 'package:hive/hive.dart';

class DateTimeAdapter extends TypeAdapter<DateTime> {
@override
int get typeId => 16;

@override
DateTime read(BinaryReader reader) {
var micros = reader.readInt();
Expand Down
4 changes: 3 additions & 1 deletion lib/src/annotations/hive_type.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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});
}
4 changes: 2 additions & 2 deletions lib/src/hive_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ class HiveImpl extends TypeRegistryImpl implements HiveInterface {
}

void _registerDefaultAdapters() {
registerInternal(DateTimeAdapter(), 16);
registerInternal(BigIntAdapter(), 17);
registerInternal(DateTimeAdapter());
registerInternal(BigIntAdapter());
}

@override
Expand Down
3 changes: 3 additions & 0 deletions lib/src/registry/type_adapter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ part of hive;
/// Type adapters can be implemented to support non primitive values.
@immutable
abstract class TypeAdapter<T> {
/// Called for type registration
int get typeId;

/// Is called when a value has to be decoded.
T read(BinaryReader reader);

Expand Down
2 changes: 1 addition & 1 deletion lib/src/registry/type_registry.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>(TypeAdapter<T> adapter, int typeId);
void registerAdapter<T>(TypeAdapter<T> adapter, [int typeId]);
}
16 changes: 8 additions & 8 deletions lib/src/registry/type_registry_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,23 @@ class TypeRegistryImpl implements TypeRegistry {
}

@override
void registerAdapter<T>(TypeAdapter<T> adapter, int typeId) {
if (typeId < 0 || typeId > 223) {
throw HiveError('TypeId $typeId not allowed.');
void registerAdapter<T>(TypeAdapter<T> 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<T>(TypeAdapter<T> adapter, int typeId) {
var resolved = _ResolvedAdapter<T>(adapter, typeId);
_typeAdapters[typeId] = resolved;
void registerInternal<T>(TypeAdapter<T> adapter) {
var resolved = _ResolvedAdapter<T>(adapter, adapter.typeId);
_typeAdapters[adapter.typeId] = resolved;
}

void resetAdapters() {
Expand Down
3 changes: 3 additions & 0 deletions test/integration/hive_list_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions test/integration/hive_object_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
12 changes: 10 additions & 2 deletions test/tests/registry/type_registry_impl_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ import 'package:test/test.dart';
import '../common.dart';

class TestAdapter extends TypeAdapter<int> {
TestAdapter([this.typeId = 0]);

@override
final int typeId;

@override
int read(BinaryReader reader) {
return 5;
Expand All @@ -15,6 +20,9 @@ class TestAdapter extends TypeAdapter<int> {
}

class TestAdapter2 extends TypeAdapter<int> {
@override
int get typeId => 1;

@override
int read(BinaryReader reader) {
return 5;
Expand All @@ -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'));
});

Expand Down