From 74a0d60721297b0975292164e6a0559b8334404f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20Bajto=C5=A1?= Date: Mon, 8 Apr 2019 13:36:01 +0200 Subject: [PATCH] feat(metadata): support symbols used as property/method keys --- packages/metadata/src/decorator-factory.ts | 37 +++++++++++++--------- packages/metadata/src/reflect.ts | 23 ++++++++++++-- 2 files changed, 42 insertions(+), 18 deletions(-) diff --git a/packages/metadata/src/decorator-factory.ts b/packages/metadata/src/decorator-factory.ts index 019238267f87..4b4b9c488003 100644 --- a/packages/metadata/src/decorator-factory.ts +++ b/packages/metadata/src/decorator-factory.ts @@ -3,10 +3,10 @@ // This file is licensed under the MIT License. // License text available at https://opensource.org/licenses/MIT -import {Reflector} from './reflect'; -import * as _ from 'lodash'; import * as debugModule from 'debug'; -import {MetadataMap, DecoratorType, MetadataKey} from './types'; +import * as _ from 'lodash'; +import {Reflector} from './reflect'; +import {DecoratorType, MetadataKey, MetadataMap} from './types'; const debug = debugModule('loopback:metadata:decorator'); // tslint:disable:no-any @@ -124,7 +124,7 @@ export class DecoratorFactory< */ static getTargetName( target: Object, - member?: string, + member?: string | symbol, descriptorOrIndex?: TypedPropertyDescriptor | number, ) { let name = @@ -135,13 +135,17 @@ export class DecoratorFactory< return `class ${name}`; } if (member == null) member = 'constructor'; + + const memberAccessor = + typeof member === 'symbol' ? '[' + member.toString() + ']' : '.' + member; + if (typeof descriptorOrIndex === 'number') { // Parameter - name = `${name}.${member}[${descriptorOrIndex}]`; + name = `${name}${memberAccessor}[${descriptorOrIndex}]`; } else if (descriptorOrIndex != null) { - name = `${name}.${member}()`; + name = `${name}${memberAccessor}()`; } else { - name = `${name}.${member}`; + name = `${name}${memberAccessor}`; } return name; } @@ -210,7 +214,7 @@ export class DecoratorFactory< protected mergeWithInherited( inheritedMetadata: M, target: Object, - member?: string, + member?: string | symbol, descriptorOrIndex?: TypedPropertyDescriptor | number, ): M { throw new Error('mergeWithInherited() is not implemented'); @@ -232,7 +236,7 @@ export class DecoratorFactory< protected mergeWithOwn( ownMetadata: M, target: Object, - member?: string, + member?: string | symbol, descriptorOrIndex?: TypedPropertyDescriptor | number, ): M { throw new Error('mergeWithOwn() is not implemented'); @@ -254,7 +258,7 @@ export class DecoratorFactory< */ protected decorate( target: Object, - member?: string, + member?: string | symbol, descriptorOrIndex?: TypedPropertyDescriptor | number, ) { const targetName = DecoratorFactory.getTargetName( @@ -430,7 +434,7 @@ export class PropertyDecoratorFactory extends DecoratorFactory< } create(): PropertyDecorator { - return (target: Object, propertyName: string) => + return (target: Object, propertyName: string | symbol) => this.decorate(target, propertyName); } @@ -498,7 +502,7 @@ export class MethodDecoratorFactory extends DecoratorFactory< create(): MethodDecorator { return ( target: Object, - methodName: string, + methodName: string | symbol, descriptor: TypedPropertyDescriptor, ) => this.decorate(target, methodName, descriptor); } @@ -592,8 +596,11 @@ export class ParameterDecoratorFactory extends DecoratorFactory< } create(): ParameterDecorator { - return (target: Object, methodName: string, parameterIndex: number) => - this.decorate(target, methodName, parameterIndex); + return ( + target: Object, + methodName: string | symbol, + parameterIndex: number, + ) => this.decorate(target, methodName, parameterIndex); } /** @@ -723,7 +730,7 @@ export class MethodParameterDecoratorFactory extends DecoratorFactory< create(): MethodDecorator { return ( target: Object, - methodName: string, + methodName: string | symbol, descriptor: TypedPropertyDescriptor, ) => this.decorate(target, methodName, descriptor); } diff --git a/packages/metadata/src/reflect.ts b/packages/metadata/src/reflect.ts index d7baf94dcd16..2c7c56ce1b4c 100644 --- a/packages/metadata/src/reflect.ts +++ b/packages/metadata/src/reflect.ts @@ -148,14 +148,31 @@ export class NamespacedReflect { return metaKeys; } + decorate( + decorators: (PropertyDecorator | MethodDecorator)[], + target: Object, + targetKey?: string | symbol, + descriptor?: PropertyDescriptor, + ): PropertyDescriptor | Function; + + decorate( + decorators: ClassDecorator[], + target: Object, + ): PropertyDescriptor | Function; + decorate( decorators: (PropertyDecorator | MethodDecorator)[] | ClassDecorator[], target: Object, - targetKey?: string, + targetKey?: string | symbol, descriptor?: PropertyDescriptor, ): PropertyDescriptor | Function { if (targetKey) { - return Reflect.decorate(decorators, target, targetKey, descriptor); + return Reflect.decorate( + <(PropertyDecorator | MethodDecorator)[]>decorators, + target, + targetKey, + descriptor, + ); } else { return Reflect.decorate(decorators, target); } @@ -167,7 +184,7 @@ export class NamespacedReflect { metadataValue: any, ): { (target: Function): void; - (target: Object, targetKey: string): void; + (target: Object, targetKey: string | symbol): void; } { metadataKey = this.getMetadataKey(metadataKey); return Reflect.metadata(metadataKey, metadataValue);