Skip to content

Commit

Permalink
feat: support read from design:type
Browse files Browse the repository at this point in the history
  • Loading branch information
wanghx committed Jul 1, 2022
1 parent e072e6c commit da341fd
Show file tree
Hide file tree
Showing 10 changed files with 78 additions and 21 deletions.
16 changes: 14 additions & 2 deletions core/core-decorator/src/decorator/Inject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,20 @@ export interface InjectParams {

export function Inject(param?: InjectParams | string) {
return function(target: any, propertyKey: PropertyKey) {
// params allow string, or object
const objName = typeof param === 'string' ? param : param?.name;
let objName: PropertyKey | undefined;
if (!param) {
// try to read design:type from proto
const proto = PrototypeUtil.getDesignType(target, propertyKey);
if (typeof proto === 'function' && proto !== Object) {
// if property type is function and not Object( means maybe proto class ), then try to read EggPrototypeInfo.name as obj name
const info = PrototypeUtil.getProperty(proto as EggProtoImplClass);
objName = info?.name;
}
} else {
// params allow string or object
objName = typeof param === 'string' ? param : param?.name;
}

const injectObject: InjectObjectInfo = {
refName: propertyKey,
objName: objName || propertyKey,
Expand Down
8 changes: 4 additions & 4 deletions core/core-decorator/src/util/MetadataUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ export class MetadataUtil {
return Reflect.getOwnMetadata(metadataKey, clazz);
}

static hasMetaData(metadataKey: MetaDataKey, clazz: EggProtoImplClass): boolean {
return Reflect.hasMetadata(metadataKey, clazz);
static hasMetaData(metadataKey: MetaDataKey, clazz: EggProtoImplClass, propKey?: PropertyKey): boolean {
return Reflect.hasMetadata(metadataKey, clazz, propKey as string);
}

static getMetaData<T>(metadataKey: MetaDataKey, clazz: EggProtoImplClass): T | undefined {
return Reflect.getMetadata(metadataKey, clazz);
static getMetaData<T>(metadataKey: MetaDataKey, clazz: EggProtoImplClass, propKey?: PropertyKey): T | undefined {
return Reflect.getMetadata(metadataKey, clazz, propKey as string);
}

static getBooleanMetaData(metadataKey: MetaDataKey, clazz: EggProtoImplClass): boolean {
Expand Down
4 changes: 4 additions & 0 deletions core/core-decorator/src/util/PrototypeUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,8 @@ export class PrototypeUtil {
static setClazzProto(clazz: EggProtoImplClass, proto: object) {
return MetadataUtil.defineMetaData(this.CLAZZ_PROTO, proto, clazz);
}

static getDesignType(clazz: EggProtoImplClass, propKey?: PropertyKey) {
return MetadataUtil.getMetaData('design:type', clazz, propKey);
}
}
9 changes: 9 additions & 0 deletions core/core-decorator/test/decorators.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,15 @@ describe('test/decorator.test.ts', () => {
}, {
refName: 'testService',
objName: 'testService',
}, {
objName: 'abcabc',
refName: 'testService2',
}, {
objName: 'testService3',
refName: 'otherService',
}, {
objName: 'testService4',
refName: 'testService4',
}];
assert.deepStrictEqual(PrototypeUtil.getInjectObjects(CacheService), expectInjectInfo);
});
Expand Down
23 changes: 20 additions & 3 deletions core/core-decorator/test/fixtures/decators/CacheService.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,40 @@
import { ContextProto, Inject } from '../../..';
import { ICache } from './ICache';
import { TestService, TestService2 } from './OtherService';

@ContextProto()
export class TestService {
export class TestService3 {
sayHi() {
console.info('hi');
}
}

@ContextProto()
export class TestService4 {
sayHi() {
console.info('hi');
}
}


@ContextProto()
export default class CacheService {
static fileName = __filename;

@Inject({
name: 'fooCache',
proto: 'ICache',
} as any)
})
cache: ICache;

@Inject('testService')
testService: TestService;

@Inject()
testService2: TestService2;

@Inject()
otherService: TestService3;

@Inject()
testService4: any;
}
15 changes: 15 additions & 0 deletions core/core-decorator/test/fixtures/decators/OtherService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { ContextProto } from '../../..';

@ContextProto()
export class TestService {
sayHi() {
console.info('hi');
}
}

@ContextProto({ name: 'abcabc' })
export class TestService2 {
sayHi() {
console.info('hi');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import { ICache } from './ICache';
export default class CacheService {
@Inject({
name: 'fooCache',
proto: 'ICache',
} as any)
})
@InitTypeQualifier(ObjectInitType.SINGLETON)
@ModuleQualifier('foo')
cache: ICache;
Expand Down
10 changes: 5 additions & 5 deletions core/metadata/test/fixtures/modules/extends-module/Base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ export class Base {
}

@ContextProto()
export class Bar extends Base {
@Inject()
foo: Foo;
export class Foo extends Base {

}

@ContextProto()
export class Foo extends Base {

export class Bar extends Base {
@Inject()
foo: Foo;
}
10 changes: 5 additions & 5 deletions core/runtime/test/fixtures/modules/extends-module/Base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ export class Base {
}

@ContextProto()
export class Bar extends Base {
@Inject()
foo: Foo;
export class Foo extends Base {

}

@ContextProto()
export class Foo extends Base {

export class Bar extends Base {
@Inject()
foo: Foo;
}
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"inlineSourceMap": true,
"declaration": true,
"resolveJsonModule": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true
}
}

0 comments on commit da341fd

Please sign in to comment.