From f4e567c0463699c33643ef641399996d5ac9ff27 Mon Sep 17 00:00:00 2001 From: Patrick Stewart Date: Thu, 26 Dec 2024 23:15:28 -0700 Subject: [PATCH] refactor: adding arrayaccess support --- .../container/lib/src/container.dart | 22 ++++++ .../container/test/array_access_test.dart | 77 +++++++++++++++++++ 2 files changed, 99 insertions(+) create mode 100644 incubation/container/container/test/array_access_test.dart diff --git a/incubation/container/container/lib/src/container.dart b/incubation/container/container/lib/src/container.dart index 607fb4d..c6bee4b 100644 --- a/incubation/container/container/lib/src/container.dart +++ b/incubation/container/container/lib/src/container.dart @@ -1011,6 +1011,28 @@ class Container { return null; } + /// Operator overload for array-style access to container bindings. + /// + /// This allows you to get instances from the container using array syntax: + /// ```dart + /// var logger = container[Logger]; + /// ``` + dynamic operator [](Type type) => make(type); + + /// Operator overload for array-style binding registration. + /// + /// This allows you to register bindings using array syntax: + /// ```dart + /// container[Logger] = ConsoleLogger(); + /// ``` + void operator []=(Type type, dynamic value) { + if (value is Function) { + registerFactory(value as dynamic Function(Container), as: type); + } else { + registerSingleton(value, as: type); + } + } + /// Check if we're in danger of a circular dependency. void _checkCircularDependency(Type type) { if (_buildStack.contains(type)) { diff --git a/incubation/container/container/test/array_access_test.dart b/incubation/container/container/test/array_access_test.dart new file mode 100644 index 0000000..451c78b --- /dev/null +++ b/incubation/container/container/test/array_access_test.dart @@ -0,0 +1,77 @@ +import 'package:platformed_container/container.dart'; +import 'package:test/test.dart'; + +class MockReflector extends Reflector { + @override + String? getName(Symbol symbol) => null; + + @override + ReflectedClass? reflectClass(Type clazz) => null; + + @override + ReflectedType? reflectType(Type type) => null; + + @override + ReflectedInstance? reflectInstance(Object? instance) => null; + + @override + ReflectedFunction? reflectFunction(Function function) => null; + + @override + ReflectedType reflectFutureOf(Type type) => throw UnimplementedError(); +} + +abstract class Logger { + void log(String message); +} + +class ConsoleLogger implements Logger { + @override + void log(String message) => print(message); +} + +void main() { + late Container container; + + setUp(() { + container = Container(MockReflector()); + }); + + group('Array Access Tests', () { + test('can get instance using array syntax', () { + container.registerSingleton(ConsoleLogger()); + var logger = container[Logger]; + expect(logger, isA()); + }); + + test('can register singleton using array syntax', () { + container[Logger] = ConsoleLogger(); + var logger = container.make(); + expect(logger, isA()); + }); + + test('can register factory using array syntax', () { + container[Logger] = (Container c) => ConsoleLogger(); + var logger = container.make(); + expect(logger, isA()); + }); + + test('array access works with parameter overrides', () { + container[Logger] = (Container c) { + var level = c.getParameterOverride('level') as String? ?? 'info'; + return ConsoleLogger(); + }; + + var logger = + container.withParameters({'level': 'debug'}, () => container[Logger]); + expect(logger, isA()); + }); + + test('array access works with child containers', () { + container[Logger] = ConsoleLogger(); + var child = container.createChild(); + var logger = child[Logger]; + expect(logger, isA()); + }); + }); +}