Commit a127d504 authored by nanahira's avatar nanahira

add param decorator

parent 574166d7
......@@ -9,7 +9,7 @@ export type AllDecorators = MethodDecorator &
export class MetadataSetter<M extends StringDict, AM extends StringDict> {
private getMetadataInDecorator<
K extends MergeKey<M, AM>,
GM extends Record<MergeKey<M, AM>, any> = GenericMap<M, AM>
GM extends Record<MergeKey<M, AM>, any> = GenericMap<M, AM>,
>(metaKey: K, target: any, key?: any): GM[K] {
if (key) {
return Reflect.getMetadata(metaKey, target, key);
......@@ -20,7 +20,7 @@ export class MetadataSetter<M extends StringDict, AM extends StringDict> {
private setMetadataInDecorator<
K extends MergeKey<M, AM>,
GM extends Record<MergeKey<M, AM>, any> = GenericMap<M, AM>
GM extends Record<MergeKey<M, AM>, any> = GenericMap<M, AM>,
>(metaKey: K, value: GM[K], target: any, key?: any) {
if (key) {
return Reflect.defineMetadata(metaKey, value, target, key);
......@@ -32,23 +32,13 @@ export class MetadataSetter<M extends StringDict, AM extends StringDict> {
transform<
K extends MergeKey<M, AM>,
IK extends Key<AM>,
GM extends Record<MergeKey<M, AM>, any> = GenericMap<M, AM>
GM extends Record<MergeKey<M, AM>, any> = GenericMap<M, AM>,
>(
metadataKey: K,
metadataValueFun: (oldValue: GM[K]) => GM[K],
keysIndexMeta?: IK,
): AllDecorators {
return (target: any, key?: any, descriptorOrParamIndex?: number | any) => {
const descriptor =
typeof descriptorOrParamIndex === 'number'
? undefined
: descriptorOrParamIndex;
/*
const paramIndex =
typeof descriptorOrParamIndex === 'number'
? descriptorOrParamIndex
: undefined;
*/
return (target, key?) => {
const targetClass = getClass(target);
const oldValue = this.getMetadataInDecorator<K, GM>(
metadataKey,
......@@ -122,4 +112,23 @@ export class MetadataSetter<M extends StringDict, AM extends StringDict> {
keysIndexMeta,
);
}
param<K extends Key<AM>, IK extends Key<AM>>(
metadataKey: K,
metadataValue: AM[K],
keysIndexMeta?: IK,
): ParameterDecorator {
return (obj, key, i) => {
const dec = this.transform<K, IK, ArrayValueMap<AM>>(
metadataKey,
(arr) => {
const newArr = arr || [];
newArr[i] = metadataValue;
return newArr;
},
keysIndexMeta,
);
dec(obj, key);
};
}
}
......@@ -6,6 +6,7 @@ export function isClass(target: any) {
return proto === Function;
}
// eslint-disable-next-line @typescript-eslint/ban-types
export function getClass(target: any): Function {
return isClass(target) ? target : getClass(target.constructor);
}
......@@ -185,4 +185,31 @@ describe('Reflector', () => {
expect(reflector.get('keys', B)).toEqual(['a', 'b']);
expect(reflector.get('keys', b)).toEqual(['a', 'b']);
});
it('should work with parameter decorators', () => {
class A {
constructor(
@Metadata.param('bar', 1) a: string,
@Metadata.param('bar', 2) b: string,
) {}
method(
@Metadata.param('bar', 3) a: string,
@Metadata.param('bar', 4) b: string,
) {}
static staticMethod(
@Metadata.param('bar', 5) a: string,
@Metadata.param('bar', 6) b: string,
) {}
}
const a = new A('foo', 'bar');
expect(reflector.get('bar', A)).toStrictEqual([1, 2]);
expect(reflector.get('bar', a)).toStrictEqual([1, 2]);
expect(reflector.get('bar', A, 'method')).toStrictEqual([3, 4]);
expect(reflector.get('bar', a, 'method')).toStrictEqual([3, 4]);
expect(reflector.get('bar', A, 'staticMethod')).toStrictEqual([5, 6]);
expect(reflector.get('bar', a, 'staticMethod')).toStrictEqual([5, 6]);
});
});
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment