Skip to content

Commit

Permalink
Add warning for registering multiple adapters for the same type
Browse files Browse the repository at this point in the history
  • Loading branch information
Rexios80 committed Jul 5, 2024
1 parent 3b0d0dc commit a714ed8
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 11 deletions.
52 changes: 42 additions & 10 deletions hive/lib/src/registry/type_registry_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,23 @@ import 'package:meta/meta.dart';
/// Needed to codegen the TypeRegistry mock
@visibleForTesting
class ResolvedAdapter<T> {
/// TODO: Document this!
/// The [TypeAdapter] for type [T]
final TypeAdapter adapter;

/// TODO: Document this!
/// The [adapter]'s [typeId]
final int typeId;

/// TODO: Document this!
/// A wrapper for a [TypeAdapter] and its [typeId]
ResolvedAdapter(this.adapter, this.typeId);

/// TODO: Document this!
/// Checks if the given value's [runtimeType] is of type [T]
bool matchesRuntimeType(dynamic value) => value.runtimeType == T;

/// TODO: Document this!
/// Checks if the given value is of type [T]
bool matchesType(dynamic value) => value is T;

/// Checks if the given type is of type [T]
bool isForType<U>() => T == U;
}

class _NullTypeRegistry implements TypeRegistryImpl {
Expand All @@ -35,6 +38,9 @@ class _NullTypeRegistry implements TypeRegistryImpl {
@override
Never findAdapterForValue(value) => throw UnimplementedError();

@override
ResolvedAdapter? findAdapterForType<T>() => throw UnimplementedError();

@override
Never ignoreTypeId<T>(int typeId) => throw UnimplementedError();

Expand Down Expand Up @@ -84,6 +90,16 @@ class TypeRegistryImpl implements TypeRegistry {
return _typeAdapters[typeId];
}

/// Not part of public API
ResolvedAdapter? findAdapterForType<T>() {
for (final adapter in _typeAdapters.values) {
if (adapter.isForType<T>()) {
return adapter;
}
}
return null;
}

@override
void registerAdapter<T>(
TypeAdapter<T> adapter, {
Expand All @@ -103,17 +119,21 @@ class TypeRegistryImpl implements TypeRegistry {
var typeId = adapter.typeId;
if (!internal) {
if (typeId < 0 || typeId > 223) {
throw HiveError('TypeId $typeId not allowed.');
throw HiveError('TypeId $typeId not allowed. Type ids must be in the '
'range 0 <= typeId <= 223.');
}
typeId = typeId + reservedTypeIds;

final oldAdapter = findAdapterForTypeId(typeId);
final oldAdapter = findAdapterForTypeId(typeId)?.adapter;
if (oldAdapter != null) {
if (override) {
final oldAdapterType = oldAdapter.runtimeType;
final newAdapterType = adapter.runtimeType;
final typeId = adapter.typeId;
print(
'You are trying to override ${oldAdapter.runtimeType.toString()}'
'with ${adapter.runtimeType.toString()} for typeId: '
'${adapter.typeId}. Please note that overriding adapters might '
'You are trying to override $oldAdapterType '
'with $newAdapterType for typeId: $typeId. '
'Please note that overriding adapters might '
'cause weird errors. Try to avoid overriding adapters unless not '
'required.',
);
Expand All @@ -122,6 +142,18 @@ class TypeRegistryImpl implements TypeRegistry {
'typeId ${typeId - reservedTypeIds}.');
}
}

final adapterForSameType = findAdapterForType<T>()?.adapter;
if (adapterForSameType != null) {
final adapterType = adapter.runtimeType;
final existingAdapterType = adapterForSameType.runtimeType;
print(
'WARNING: You are trying to register $adapterType for '
'type $T but there is already a TypeAdapter for this '
'type: $existingAdapterType. Note that $adapterType will '
'have no effect as $existingAdapterType takes precedence.',
);
}
}

final resolved = ResolvedAdapter<T>(adapter, typeId);
Expand Down
2 changes: 1 addition & 1 deletion hive_flutter/lib/src/hive_extensions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ extension HiveX on HiveInterface {
///
/// You can provide a [subDir] where the boxes should be stored.
///
/// Also registers the flutter type adapters
/// Also registers the flutter type adapters.
Future<void> initFlutter([
String? subDir,
HiveStorageBackendPreference backendPreference =
Expand Down

0 comments on commit a714ed8

Please sign in to comment.