Skip to content
This repository has been archived by the owner on Dec 6, 2017. It is now read-only.

Commit

Permalink
fix(module): correctly handle null value binding
Browse files Browse the repository at this point in the history
Fixes #93
  • Loading branch information
pavelgj committed Apr 26, 2014
1 parent b9039e8 commit ada47b3
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
20 changes: 12 additions & 8 deletions lib/src/module.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
part of di;

_DEFAULT_VALUE(_) => null;

typedef dynamic FactoryFn(Injector injector);

/**
Expand Down Expand Up @@ -75,8 +77,9 @@ class Module {
* Up to one (0 or 1) of the following parameters can be specified at the
* same time: [toImplementation], [toFactory], [toValue].
*/
void bind(Type type, {dynamic toValue, FactoryFn toFactory,
Type toImplementation, Type withAnnotation, Visibility visibility}) {
void bind(Type type, {dynamic toValue: _DEFAULT_VALUE,
FactoryFn toFactory: _DEFAULT_VALUE, Type toImplementation,
Type withAnnotation, Visibility visibility}) {
bindByKey(new Key(type, withAnnotation), toValue: toValue,
toFactory: toFactory, toImplementation: toImplementation,
visibility: visibility);
Expand All @@ -86,13 +89,14 @@ class Module {
* Same as [bind] except it takes [Key] instead of
* [Type] [withAnnotation] combination.
*/
void bindByKey(Key key, {dynamic toValue, FactoryFn toFactory,
Type toImplementation, Visibility visibility}) {
void bindByKey(Key key, {dynamic toValue: _DEFAULT_VALUE,
FactoryFn toFactory: _DEFAULT_VALUE, Type toImplementation,
Visibility visibility}) {
_checkBindArgs(toValue, toFactory, toImplementation);
_dirty();
if (toValue != null) {
if (!identical(toValue, _DEFAULT_VALUE)) {
_providers[key.id] = new ValueProvider(key.type, toValue, visibility);
} else if (toFactory != null) {
} else if (!identical(toFactory, _DEFAULT_VALUE)) {
_providers[key.id] = new FactoryProvider(key.type, toFactory, visibility);
} else {
_providers[key.id] = new TypeProvider(
Expand All @@ -102,8 +106,8 @@ class Module {

_checkBindArgs(toValue, toFactory, toImplementation) {
int count = 0;
if (toValue != null) count++;
if (toFactory != null) count++;
if (!identical(toValue, _DEFAULT_VALUE)) count++;
if (!identical(toFactory, _DEFAULT_VALUE)) count++;
if (toImplementation != null) count++;
if (count > 1) {
throw 'Only one of following parameters can be specified: '
Expand Down
11 changes: 11 additions & 0 deletions test/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,17 @@ createInjectorSpec(String injectorName, InjectorFactory injectorFactory) {
});


it('should allow providing null values', () {
var module = new Module()
..bind(Engine, toValue: null);

var injector = injectorFactory([module]);
var engineInstance = injector.get(Engine);

expect(engineInstance, isNull);
});


it('should allow providing factory functions', () {
var module = new Module()..bind(Engine, toFactory: (Injector injector) {
return 'factory-product';
Expand Down

0 comments on commit ada47b3

Please sign in to comment.