From c8b4496f382fa725e5f3b93acabcd8cff15ad5d8 Mon Sep 17 00:00:00 2001 From: rr_reddy Date: Thu, 21 Mar 2024 11:27:23 +0530 Subject: [PATCH 1/2] feat: ignoreReassignmentError configuration --- lib/get_it.dart | 4 +++ lib/get_it_impl.dart | 21 ++++++++++++-- test/ignore_error_test.dart | 57 +++++++++++++++++++++++++++++++++++++ 3 files changed, 80 insertions(+), 2 deletions(-) create mode 100644 test/ignore_error_test.dart diff --git a/lib/get_it.dart b/lib/get_it.dart index 77ca466..e76c9e2 100644 --- a/lib/get_it.dart +++ b/lib/get_it.dart @@ -151,6 +151,10 @@ abstract class GetIt { /// If you really need to you can disable the asserts by setting[allowReassignment]= true bool allowReassignment = false; + /// By default it's throws error when [allowReassignment]= false. and trying to register same type + /// If you really need, you can disable the Asserts / Errror by setting[ignoreReassignmentError]= true + bool ignoreReassignmentError = false; + /// Till V7.6.7 GetIt didn't allow to register multiple instances of the same type. /// if you want to register multiple instances of the same type you can enable this /// and use `getAll()` to retrieve all instances of that parent type diff --git a/lib/get_it_impl.dart b/lib/get_it_impl.dart index 3e3295b..9cdb79f 100644 --- a/lib/get_it_impl.dart +++ b/lib/get_it_impl.dart @@ -375,6 +375,11 @@ class _GetItImplementation implements GetIt { @override bool allowReassignment = false; + /// By default it's throws error when [allowReassignment]= false. and trying to register same type + /// If you really need, you can disable the Asserts / Errror by setting[ignoreReassignmentError]= true + @override + bool ignoreReassignmentError = false; + /// Is used by several other functions to retrieve the correct [_ServiceFactory] _ServiceFactory? _findFirstFactoryByNameAndTypeOrNull( @@ -1027,19 +1032,31 @@ class _GetItImplementation implements GetIt { if (instanceName != null) { throwIf( existingTypeRegistration.namedFactories.containsKey(instanceName) && - !allowReassignment, + !allowReassignment && + !ignoreReassignmentError, ArgumentError( 'Object/factory with name $instanceName and ' 'type $T is already registered inside GetIt. ', ), ); + + /// skip registration + if (ignoreReassignmentError && !allowReassignment) { + return; + } } else { if (existingTypeRegistration.factories.isNotEmpty) { throwIfNot( allowReassignment || - GetIt.allowRegisterMultipleImplementationsOfoneType, + GetIt.allowRegisterMultipleImplementationsOfoneType || + ignoreReassignmentError, ArgumentError('Type $T is already registered inside GetIt. '), ); + + /// skip registration + if (ignoreReassignmentError && !allowReassignment) { + return; + } } } } diff --git a/test/ignore_error_test.dart b/test/ignore_error_test.dart new file mode 100644 index 0000000..8c79728 --- /dev/null +++ b/test/ignore_error_test.dart @@ -0,0 +1,57 @@ +import 'package:get_it/get_it.dart'; +import 'package:test/test.dart'; + +void main() { + test(' Throws ArgumentError ', () async { + final getIt = GetIt.instance; + getIt.allowReassignment = false; + getIt.ignoreReassignmentError = false; + getIt.reset(); + getIt.registerSingleton(MockDataStore()); + + expect( + () => getIt.registerSingleton(RemoteDataStore()), + throwsArgumentError, + ); + }); + + test(' replaces dependency safely ', () async { + final getIt = GetIt.instance; + getIt.reset(); + getIt.allowReassignment = true; + getIt.ignoreReassignmentError = false; + getIt.registerSingleton(MockDataStore()); + getIt.registerSingleton(RemoteDataStore()); + + expect(getIt(), isA()); + }); + + test(' Ignores ReassignmentError ', () async { + final getIt = GetIt.instance; + getIt.reset(); + getIt.allowReassignment = false; + getIt.ignoreReassignmentError = true; + getIt.registerSingleton(MockDataStore()); + final remoteDataStore = RemoteDataStore(); + getIt.registerSingleton(remoteDataStore); + + expect(getIt(), isA()); + }); + + test(' does not care about [ignoreReassignmentError] varibale ', () async { + final getIt = GetIt.instance; + getIt.reset(); + getIt.allowReassignment = true; + getIt.ignoreReassignmentError = true; + getIt.registerSingleton(MockDataStore()); + getIt.registerSingleton(RemoteDataStore()); + + expect(getIt(), isA()); + }); +} + +abstract class DataStore {} + +class RemoteDataStore implements DataStore {} + +class MockDataStore implements DataStore {} From df3a12e894e1f7ea102bd39a1015cd646ca728d3 Mon Sep 17 00:00:00 2001 From: rr_reddy Date: Sat, 23 Mar 2024 19:14:34 +0530 Subject: [PATCH 2/2] refactor: variable name change and updated readme --- README.md | 7 ++++++- lib/get_it.dart | 6 ++++-- lib/get_it_impl.dart | 17 +++++++++-------- pubspec.yaml | 1 + ....dart => skip_double_registration_test.dart} | 12 ++++++------ 5 files changed, 26 insertions(+), 17 deletions(-) rename test/{ignore_error_test.dart => skip_double_registration_test.dart} (82%) diff --git a/README.md b/README.md index 2c6fba1..ad25d18 100644 --- a/README.md +++ b/README.md @@ -154,7 +154,12 @@ You have to pass a factory function `func` that returns an instance of an implem ### Overwriting registrations If you try to register a type more than once you will fail with an assertion in debug mode because normally this is not needed and probably a bug. -If you really have to overwrite a registration, then you can by setting the property `allowReassignment==true`. +If you really have to overwrite a registration, then you can by setting the property `allowReassignment = true`. + +### Skip Double registrations + +If you try to register a type more than once and when `allowReassignment = false` you will fail with an assertion in debug mode. +If you want to just skip this double registration silently without an error, then you can by setting the property `skipDoubleRegistration = true`. ### Testing if a Singleton is already registered diff --git a/lib/get_it.dart b/lib/get_it.dart index e76c9e2..4bc3fa7 100644 --- a/lib/get_it.dart +++ b/lib/get_it.dart @@ -7,6 +7,7 @@ import 'dart:collection'; import 'package:async/async.dart'; import 'package:collection/collection.dart' show IterableExtension; +import 'package:meta/meta.dart'; part 'get_it_impl.dart'; @@ -152,8 +153,9 @@ abstract class GetIt { bool allowReassignment = false; /// By default it's throws error when [allowReassignment]= false. and trying to register same type - /// If you really need, you can disable the Asserts / Errror by setting[ignoreReassignmentError]= true - bool ignoreReassignmentError = false; + /// If you really need, you can disable the Asserts / Errror by setting[skipDoubleRegistration]= true + @visibleForTesting + bool skipDoubleRegistration = false; /// Till V7.6.7 GetIt didn't allow to register multiple instances of the same type. /// if you want to register multiple instances of the same type you can enable this diff --git a/lib/get_it_impl.dart b/lib/get_it_impl.dart index 9cdb79f..8e48011 100644 --- a/lib/get_it_impl.dart +++ b/lib/get_it_impl.dart @@ -376,9 +376,10 @@ class _GetItImplementation implements GetIt { bool allowReassignment = false; /// By default it's throws error when [allowReassignment]= false. and trying to register same type - /// If you really need, you can disable the Asserts / Errror by setting[ignoreReassignmentError]= true + /// If you really need, you can disable the Asserts / Errror by setting[skipDoubleRegistration]= true + @visibleForTesting @override - bool ignoreReassignmentError = false; + bool skipDoubleRegistration = false; /// Is used by several other functions to retrieve the correct [_ServiceFactory] _ServiceFactory? @@ -1033,15 +1034,15 @@ class _GetItImplementation implements GetIt { throwIf( existingTypeRegistration.namedFactories.containsKey(instanceName) && !allowReassignment && - !ignoreReassignmentError, + !skipDoubleRegistration, ArgumentError( 'Object/factory with name $instanceName and ' 'type $T is already registered inside GetIt. ', ), ); - /// skip registration - if (ignoreReassignmentError && !allowReassignment) { + /// skip double registration + if (skipDoubleRegistration && !allowReassignment) { return; } } else { @@ -1049,12 +1050,12 @@ class _GetItImplementation implements GetIt { throwIfNot( allowReassignment || GetIt.allowRegisterMultipleImplementationsOfoneType || - ignoreReassignmentError, + skipDoubleRegistration, ArgumentError('Type $T is already registered inside GetIt. '), ); - /// skip registration - if (ignoreReassignmentError && !allowReassignment) { + /// skip double registration + if (skipDoubleRegistration && !allowReassignment) { return; } } diff --git a/pubspec.yaml b/pubspec.yaml index 9b732e1..ea98530 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -10,6 +10,7 @@ environment: dependencies: async: ^2.11.0 collection: ^1.17.1 + meta: ^1.11.0 dev_dependencies: lint: ^2.1.2 diff --git a/test/ignore_error_test.dart b/test/skip_double_registration_test.dart similarity index 82% rename from test/ignore_error_test.dart rename to test/skip_double_registration_test.dart index 8c79728..4413f17 100644 --- a/test/ignore_error_test.dart +++ b/test/skip_double_registration_test.dart @@ -5,7 +5,7 @@ void main() { test(' Throws ArgumentError ', () async { final getIt = GetIt.instance; getIt.allowReassignment = false; - getIt.ignoreReassignmentError = false; + getIt.skipDoubleRegistration = false; getIt.reset(); getIt.registerSingleton(MockDataStore()); @@ -19,18 +19,18 @@ void main() { final getIt = GetIt.instance; getIt.reset(); getIt.allowReassignment = true; - getIt.ignoreReassignmentError = false; + getIt.skipDoubleRegistration = false; getIt.registerSingleton(MockDataStore()); getIt.registerSingleton(RemoteDataStore()); expect(getIt(), isA()); }); - test(' Ignores ReassignmentError ', () async { + test(' Ignores Double registration error ', () async { final getIt = GetIt.instance; getIt.reset(); getIt.allowReassignment = false; - getIt.ignoreReassignmentError = true; + getIt.skipDoubleRegistration = true; getIt.registerSingleton(MockDataStore()); final remoteDataStore = RemoteDataStore(); getIt.registerSingleton(remoteDataStore); @@ -38,11 +38,11 @@ void main() { expect(getIt(), isA()); }); - test(' does not care about [ignoreReassignmentError] varibale ', () async { + test(' does not care about [skipDoubleRegistration] varibale ', () async { final getIt = GetIt.instance; getIt.reset(); getIt.allowReassignment = true; - getIt.ignoreReassignmentError = true; + getIt.skipDoubleRegistration = true; getIt.registerSingleton(MockDataStore()); getIt.registerSingleton(RemoteDataStore());