Skip to content

Commit

Permalink
revert isar#927 see if it fixes HiveError: Duplicate field number
Browse files Browse the repository at this point in the history
  • Loading branch information
roubachof committed Apr 30, 2022
1 parent 0abee88 commit dd9a4a9
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 70 deletions.
14 changes: 3 additions & 11 deletions hive/lib/src/registry/type_registry_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ class ResolvedAdapter<T> {

ResolvedAdapter(this.adapter, this.typeId);

bool matchesRuntimeType(dynamic value) => value.runtimeType == T;

bool matchesType(dynamic value) => value is T;
bool matches(dynamic value) => value is T;
}

class _NullTypeRegistry implements TypeRegistryImpl {
Expand Down Expand Up @@ -58,16 +56,10 @@ class TypeRegistryImpl implements TypeRegistry {

/// Not part of public API
ResolvedAdapter? findAdapterForValue(dynamic value) {
ResolvedAdapter? match;
for (var adapter in _typeAdapters.values) {
if (adapter.matchesRuntimeType(value)) {
return adapter;
}
if (adapter.matchesType(value) && match == null) {
match = adapter;
}
if (adapter.matches(value)) return adapter;
}
return match;
return null;
}

/// Not part of public API
Expand Down
58 changes: 0 additions & 58 deletions hive/test/tests/registry/type_registry_impl_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,40 +33,6 @@ class TestAdapter2 extends TypeAdapter<int> {
void write(BinaryWriter writer, obj) {}
}

class Parent {}

class Child extends Parent {}

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

@override
final int typeId;

@override
Parent read(BinaryReader reader) {
return Parent();
}

@override
void write(BinaryWriter writer, Parent obj) {}
}

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

@override
final int typeId;

@override
Child read(BinaryReader reader) {
return Child();
}

@override
void write(BinaryWriter writer, Child obj) {}
}

void main() {
group('TypeRegistryImpl', () {
group('.registerAdapter()', () {
Expand Down Expand Up @@ -133,30 +99,6 @@ void main() {
expect(resolvedAdapter.typeId, 32);
expect(resolvedAdapter.adapter, adapter1);
});

test(
'returns adapter if exact runtime type of value matches ignoring '
'registration order', () {
final registry = TypeRegistryImpl();
final parentAdapter = ParentAdapter(0);
final childAdapter = ChildAdapter(1);
registry.registerAdapter(parentAdapter);
registry.registerAdapter(childAdapter);

final resolvedAdapter = registry.findAdapterForValue(Child());
expect(resolvedAdapter?.typeId, 33);
expect(resolvedAdapter?.adapter, childAdapter);
});

test('returns super type adapter for subtype', () {
final registry = TypeRegistryImpl();
final parentAdapter = ParentAdapter(0);
registry.registerAdapter(parentAdapter);

final resolvedAdapter = registry.findAdapterForValue(Child());
expect(resolvedAdapter?.typeId, 32);
expect(resolvedAdapter?.adapter, parentAdapter);
});
});

test('.resetAdapters()', () {
Expand Down
2 changes: 1 addition & 1 deletion hive_generator/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: hive_generator
description: Extension for Hive. Automatically generates TypeAdapters to store any class.
version: 1.1.2+3
version: 1.1.2+4
homepage: https://github.com/hivedb/hive/tree/master/hive_generator
documentation: https://docs.hivedb.dev/

Expand Down

0 comments on commit dd9a4a9

Please sign in to comment.