Skip to content

Commit

Permalink
Make @optional, @optional(), fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
asvetliakov committed Jun 3, 2017
1 parent 589f473 commit 01c834d
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 28 deletions.
3 changes: 3 additions & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ module.exports = function(grunt) {
},

ts: {
options: {
compiler: "./node_modules/typescript/bin/tsc"
},
default: {
src: ['src/**/*.ts'],
tsconfig: './tsconfig.json'
Expand Down
14 changes: 7 additions & 7 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ This DI container supports:
* Property and constructor dependencies autowiring
* Simple API, just 3 methods and few decorators to go
* Optional injection (from version 1.2)
* Injecting container as factory (**new** 1.3 version)
* Auto-Creating object factories! (**new** 1.3 version)
* Injecting container as factory (from 1.3 version)
* Auto-Creating object factories! (from 1.3 version)

## Todo
* Lazy injection
Expand Down Expand Up @@ -234,7 +234,7 @@ You can specify dependencies by using decorators:
@ConstructorInject(literal: string, method?: FactoryMethod)
// Use before class property/constructor argument with either @Inject or @ConstructorInject. Specified optional (non-strict) resolution
// If dependency wasn't found then leave original value (for property injection) or pass null (for constructor injection)
@Optional
@Optional()
// Used for creating auto factories (See below)
@Factory
```
Expand Down Expand Up @@ -286,8 +286,8 @@ import {Optional} from 'huject';
class Test {
public constructor(
service: MyService,
@Optional @ConstructorInject('token') param1: string,
@Optional @ConstructorInject('seed') param2: number)
@Optional() @ConstructorInject('token') param1: string,
@Optional() @ConstructorInject('seed') param2: number)
{
if (param1 !== null) {
....
Expand All @@ -304,11 +304,11 @@ import {Optional, Inject} from 'huject';
container.register('classToken', 'mytoken');

class Test {
@Optional
@Optional()
@Inject('classToken')
public classParam1: string = "default string";

@Optional
@Optional()
@Inject('servicePort')
public port: number = 80;
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "huject",
"version": "1.3.4",
"version": "1.4.0",
"description": "Typescript dependency injection container for humans",
"main": "./src/index.js",
"scripts": {
Expand Down
20 changes: 6 additions & 14 deletions src/decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,23 +103,15 @@ export function Inject(targetOrFactoryMethodOrLiteral: any, propertyKeyOrFactory
* @constructor
*/
export function Optional(...args: any[]): any {
let target;
switch (args.length) {
// Property @Optional
case 2:
target = args[0];
let propertyKey = args[1];
return function (target: Object, propertyKey: string, parameterIndex: number) {
if (typeof parameterIndex === "undefined") {
// Property @Optional()
Reflect.defineMetadata("inject:property:optional", true, target, propertyKey);
break;
// Constructor @Optional
case 3:
target = args[0];
let parameterIndex = args[2];
} else {
// Constructor @Optional()
let metadataName = 'inject:constructor:param' + parameterIndex + ':optional';
Reflect.defineMetadata(metadataName, true, target);
break;
default:
throw new Error("@Optional decorator is not allowed here");
}
}
}

Expand Down
10 changes: 4 additions & 6 deletions tests/decorators.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,8 @@ class ConstructorInjectionOptional {
public num: number;

public constructor(
@ConstructorInject(FactoryMethod.SINGLETON) @Optional service: TestInterface,
@Optional @ConstructorInject('nonexist') num: number
@ConstructorInject(FactoryMethod.SINGLETON) @Optional() service: TestInterface,
@Optional() @ConstructorInject('nonexist') num: number
) {
this.test = service;
this.num = num;
Expand All @@ -152,11 +152,11 @@ class ConstructorInjectionOptional {

class PropertyInjectionOptional {
@Inject('nonexiststring')
@Optional
@Optional()
public str: string = "default";

@Inject('nonexistnumber')
@Optional
@Optional()
public num: number = 25;
}

Expand Down Expand Up @@ -264,10 +264,8 @@ describe("Testing container's autowiring by using decorators", function () {
});

describe("When property injection has @Optional decorator", () => {
let containerSpy;
beforeEach(() => {
container.register(PropertyInjectionOptional);
containerSpy = sinon.spy(container, 'resolve');
});
it("Should leave property original specified value", () => {
let impl: PropertyInjectionOptional = container.resolve(PropertyInjectionOptional);
Expand Down

0 comments on commit 01c834d

Please sign in to comment.