2015-04-29 16:22:38 -07:00
|
|
|
import {global} from 'angular2/src/facade/lang';
|
2015-04-28 18:17:00 -07:00
|
|
|
|
2015-05-04 11:11:22 -07:00
|
|
|
export function makeDecorator(annotationCls) {
|
2015-05-20 18:10:30 -07:00
|
|
|
return function(... args) {
|
2015-04-29 16:22:38 -07:00
|
|
|
var Reflect = global.Reflect;
|
|
|
|
if (!(Reflect && Reflect.getMetadata)) {
|
|
|
|
throw 'reflect-metadata shim is required when using class decorators';
|
|
|
|
}
|
2015-05-07 12:30:18 -07:00
|
|
|
var annotationInstance = Object.create(annotationCls.prototype);
|
|
|
|
annotationCls.apply(annotationInstance, args);
|
2015-04-28 18:17:00 -07:00
|
|
|
return function(cls) {
|
|
|
|
var annotations = Reflect.getMetadata('annotations', cls);
|
|
|
|
annotations = annotations || [];
|
|
|
|
annotations.push(annotationInstance);
|
|
|
|
Reflect.defineMetadata('annotations', annotations, cls);
|
|
|
|
return cls;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-20 18:10:30 -07:00
|
|
|
export function makeParamDecorator(annotationCls): any {
|
2015-05-18 11:57:20 -07:00
|
|
|
return function(... args) {
|
2015-04-29 16:22:38 -07:00
|
|
|
var Reflect = global.Reflect;
|
|
|
|
if (!(Reflect && Reflect.getMetadata)) {
|
|
|
|
throw 'reflect-metadata shim is required when using parameter decorators';
|
|
|
|
}
|
2015-05-07 12:30:18 -07:00
|
|
|
var annotationInstance = Object.create(annotationCls.prototype);
|
|
|
|
annotationCls.apply(annotationInstance, args);
|
2015-04-29 16:22:38 -07:00
|
|
|
return function(cls, unusedKey, index) {
|
2015-05-20 18:10:30 -07:00
|
|
|
var parameters: Array<Array<any>> = Reflect.getMetadata('parameters', cls);
|
2015-04-29 16:22:38 -07:00
|
|
|
parameters = parameters || [];
|
2015-05-20 18:10:30 -07:00
|
|
|
|
2015-04-29 16:22:38 -07:00
|
|
|
// there might be gaps if some in between parameters do not have annotations.
|
|
|
|
// we pad with nulls.
|
|
|
|
while (parameters.length <= index) {
|
|
|
|
parameters.push(null);
|
|
|
|
}
|
2015-05-20 18:10:30 -07:00
|
|
|
|
|
|
|
parameters[index] = parameters[index] || [];
|
|
|
|
var annotationsForParam: Array<any> = parameters[index];
|
|
|
|
annotationsForParam.push(annotationInstance);
|
|
|
|
|
2015-04-29 16:22:38 -07:00
|
|
|
Reflect.defineMetadata('parameters', parameters, cls);
|
|
|
|
return cls;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|