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

Support adapter type inheritance #927

Merged
merged 1 commit into from
Mar 11, 2022
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
14 changes: 11 additions & 3 deletions hive/lib/src/registry/type_registry_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ class ResolvedAdapter<T> {

ResolvedAdapter(this.adapter, this.typeId);

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

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

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

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

/// Not part of public API
Expand Down
58 changes: 58 additions & 0 deletions hive/test/tests/registry/type_registry_impl_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,40 @@ 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 @@ -99,6 +133,30 @@ 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