Skip to content

Commit

Permalink
feat(metadata): support symbols used as property/method keys
Browse files Browse the repository at this point in the history
  • Loading branch information
bajtos committed Apr 8, 2019
1 parent bfc4c04 commit 8b69a8a
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 18 deletions.
37 changes: 22 additions & 15 deletions packages/metadata/src/decorator-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -124,7 +124,7 @@ export class DecoratorFactory<
*/
static getTargetName(
target: Object,
member?: string,
member?: string | symbol,
descriptorOrIndex?: TypedPropertyDescriptor<any> | number,
) {
let name =
Expand All @@ -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;
}
Expand Down Expand Up @@ -210,7 +214,7 @@ export class DecoratorFactory<
protected mergeWithInherited(
inheritedMetadata: M,
target: Object,
member?: string,
member?: string | symbol,
descriptorOrIndex?: TypedPropertyDescriptor<any> | number,
): M {
throw new Error('mergeWithInherited() is not implemented');
Expand All @@ -232,7 +236,7 @@ export class DecoratorFactory<
protected mergeWithOwn(
ownMetadata: M,
target: Object,
member?: string,
member?: string | symbol,
descriptorOrIndex?: TypedPropertyDescriptor<any> | number,
): M {
throw new Error('mergeWithOwn() is not implemented');
Expand All @@ -254,7 +258,7 @@ export class DecoratorFactory<
*/
protected decorate(
target: Object,
member?: string,
member?: string | symbol,
descriptorOrIndex?: TypedPropertyDescriptor<any> | number,
) {
const targetName = DecoratorFactory.getTargetName(
Expand Down Expand Up @@ -430,7 +434,7 @@ export class PropertyDecoratorFactory<T> extends DecoratorFactory<
}

create(): PropertyDecorator {
return (target: Object, propertyName: string) =>
return (target: Object, propertyName: string | symbol) =>
this.decorate(target, propertyName);
}

Expand Down Expand Up @@ -498,7 +502,7 @@ export class MethodDecoratorFactory<T> extends DecoratorFactory<
create(): MethodDecorator {
return (
target: Object,
methodName: string,
methodName: string | symbol,
descriptor: TypedPropertyDescriptor<any>,
) => this.decorate(target, methodName, descriptor);
}
Expand Down Expand Up @@ -592,8 +596,11 @@ export class ParameterDecoratorFactory<T> 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);
}

/**
Expand Down Expand Up @@ -723,7 +730,7 @@ export class MethodParameterDecoratorFactory<T> extends DecoratorFactory<
create(): MethodDecorator {
return (
target: Object,
methodName: string,
methodName: string | symbol,
descriptor: TypedPropertyDescriptor<any>,
) => this.decorate(target, methodName, descriptor);
}
Expand Down
23 changes: 20 additions & 3 deletions packages/metadata/src/reflect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(<ClassDecorator[]>decorators, <Function>target);
}
Expand All @@ -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);
Expand Down

0 comments on commit 8b69a8a

Please sign in to comment.