From f9c0ccea1d8b7bea3d7ff9863cf6077dc3434733 Mon Sep 17 00:00:00 2001 From: popomore Date: Tue, 19 Sep 2023 17:02:59 +0800 Subject: [PATCH 1/3] feat: add className property to EggPrototypeInfo --- .../src/decorator/MultiInstanceProto.ts | 3 +- .../core-decorator/src/decorator/Prototype.ts | 1 + .../model/EggMultiInstancePrototypeInfo.ts | 8 +++++ .../src/model/EggPrototypeInfo.ts | 4 +++ core/core-decorator/test/decorators.test.ts | 2 ++ .../src/factory/EggPrototypeCreatorFactory.ts | 1 + core/metadata/src/impl/EggPrototypeBuilder.ts | 3 ++ core/metadata/src/impl/EggPrototypeImpl.ts | 3 ++ core/metadata/src/model/EggPrototype.ts | 1 + core/metadata/test/LoadUnit.test.ts | 8 +++-- .../src/StandaloneInnerObjectProto.ts | 6 +++- standalone/standalone/test/index.test.ts | 35 +++++++++++++++++++ 12 files changed, 70 insertions(+), 5 deletions(-) diff --git a/core/core-decorator/src/decorator/MultiInstanceProto.ts b/core/core-decorator/src/decorator/MultiInstanceProto.ts index ec76110a..fc638af0 100644 --- a/core/core-decorator/src/decorator/MultiInstanceProto.ts +++ b/core/core-decorator/src/decorator/MultiInstanceProto.ts @@ -52,17 +52,18 @@ export function MultiInstanceProto(param: MultiInstancePrototypeParams) { const property: EggMultiInstancePrototypeInfo = { ...DEFAULT_PARAMS, ...param as MultiInstancePrototypeStaticParams, + className: clazz.name, }; PrototypeUtil.setMultiInstanceStaticProperty(clazz, property); } else if ((param as MultiInstancePrototypeCallbackParams).getObjects) { const property: EggMultiInstanceCallbackPrototypeInfo = { ...DEFAULT_PARAMS, ...param as MultiInstancePrototypeCallbackParams, + className: clazz.name, }; PrototypeUtil.setMultiInstanceCallbackProperty(clazz, property); } - // './tegg/core/common-util/src/StackUtil.ts', // './tegg/core/core-decorator/src/decorator/Prototype.ts', // './tegg/core/core-decorator/node_modules/_reflect-metadata@0.1.13@reflect-metadata/Reflect.js', diff --git a/core/core-decorator/src/decorator/Prototype.ts b/core/core-decorator/src/decorator/Prototype.ts index 7da72323..54c52d0d 100644 --- a/core/core-decorator/src/decorator/Prototype.ts +++ b/core/core-decorator/src/decorator/Prototype.ts @@ -26,6 +26,7 @@ export function Prototype(param?: PrototypeParams) { const property: Partial = { ...DEFAULT_PARAMS, ...param, + className: clazz.name, }; if (!property.name) { property.name = NameUtil.getClassName(clazz); diff --git a/core/core-decorator/src/model/EggMultiInstancePrototypeInfo.ts b/core/core-decorator/src/model/EggMultiInstancePrototypeInfo.ts index b3a1ea96..f329e3d0 100644 --- a/core/core-decorator/src/model/EggMultiInstancePrototypeInfo.ts +++ b/core/core-decorator/src/model/EggMultiInstancePrototypeInfo.ts @@ -13,6 +13,10 @@ export interface MultiInstancePrototypeGetObjectsContext { } export interface EggMultiInstancePrototypeInfo { + /** + * The class name of the object + */ + className?: string; /** * obj init type */ @@ -33,6 +37,10 @@ export interface EggMultiInstancePrototypeInfo { } export interface EggMultiInstanceCallbackPrototypeInfo { + /** + * The class name of the object + */ + className?: string; /** * obj init type */ diff --git a/core/core-decorator/src/model/EggPrototypeInfo.ts b/core/core-decorator/src/model/EggPrototypeInfo.ts index b5b5f806..f612897f 100644 --- a/core/core-decorator/src/model/EggPrototypeInfo.ts +++ b/core/core-decorator/src/model/EggPrototypeInfo.ts @@ -10,6 +10,10 @@ export interface EggPrototypeInfo { * egg object name */ name: EggPrototypeName; + /** + * The class name of the object + */ + className?: string; /** * obj init type */ diff --git a/core/core-decorator/test/decorators.test.ts b/core/core-decorator/test/decorators.test.ts index 1e905564..51f08625 100644 --- a/core/core-decorator/test/decorators.test.ts +++ b/core/core-decorator/test/decorators.test.ts @@ -28,6 +28,7 @@ describe('test/decorator.test.ts', () => { initType: ObjectInitType.CONTEXT, accessLevel: AccessLevel.PUBLIC, protoImplType: DEFAULT_PROTO_IMPL_TYPE, + className: 'ContextCache', }; assert.deepStrictEqual(PrototypeUtil.getProperty(ContextCache), expectObjectProperty); }); @@ -41,6 +42,7 @@ describe('test/decorator.test.ts', () => { initType: ObjectInitType.SINGLETON, accessLevel: AccessLevel.PUBLIC, protoImplType: DEFAULT_PROTO_IMPL_TYPE, + className: 'SingletonCache', }; assert.deepStrictEqual(PrototypeUtil.getProperty(SingletonCache), expectObjectProperty); }); diff --git a/core/metadata/src/factory/EggPrototypeCreatorFactory.ts b/core/metadata/src/factory/EggPrototypeCreatorFactory.ts index aa8760d1..a3efb42f 100644 --- a/core/metadata/src/factory/EggPrototypeCreatorFactory.ts +++ b/core/metadata/src/factory/EggPrototypeCreatorFactory.ts @@ -28,6 +28,7 @@ export class EggPrototypeCreatorFactory { initType: multiInstanceProtoInfo.initType, accessLevel: multiInstanceProtoInfo.accessLevel, qualifiers: obj.qualifiers, + className: multiInstanceProtoInfo.className, }); } } else { diff --git a/core/metadata/src/impl/EggPrototypeBuilder.ts b/core/metadata/src/impl/EggPrototypeBuilder.ts index 48e6a153..d10798bd 100644 --- a/core/metadata/src/impl/EggPrototypeBuilder.ts +++ b/core/metadata/src/impl/EggPrototypeBuilder.ts @@ -40,6 +40,7 @@ export class EggPrototypeBuilder { private injectObjects: Array = []; private loadUnit: LoadUnit; private qualifiers: QualifierInfo[] = []; + private className?: string; static create(ctx: EggPrototypeLifecycleContext): EggPrototype { const { clazz, loadUnit } = ctx; @@ -48,6 +49,7 @@ export class EggPrototypeBuilder { const builder = new EggPrototypeBuilder(); builder.clazz = clazz; builder.name = ctx.prototypeInfo.name; + builder.className = ctx.prototypeInfo.className; builder.initType = ctx.prototypeInfo.initType; builder.accessLevel = ctx.prototypeInfo.accessLevel; builder.filepath = filepath!; @@ -131,6 +133,7 @@ export class EggPrototypeBuilder { injectObjectProtos, this.loadUnit.id, this.qualifiers, + this.className, ); } } diff --git a/core/metadata/src/impl/EggPrototypeImpl.ts b/core/metadata/src/impl/EggPrototypeImpl.ts index 6d0d8fe5..5548c045 100644 --- a/core/metadata/src/impl/EggPrototypeImpl.ts +++ b/core/metadata/src/impl/EggPrototypeImpl.ts @@ -20,6 +20,7 @@ export class EggPrototypeImpl implements EggPrototype { readonly accessLevel: AccessLevel; readonly injectObjects: InjectObjectProto[]; readonly loadUnitId: Id; + readonly className?: string; constructor( id: string, @@ -31,6 +32,7 @@ export class EggPrototypeImpl implements EggPrototype { injectObjectMap: InjectObjectProto[], loadUnitId: Id, qualifiers: QualifierInfo[], + className?: string, ) { this.id = id; this.clazz = clazz; @@ -41,6 +43,7 @@ export class EggPrototypeImpl implements EggPrototype { this.injectObjects = injectObjectMap; this.loadUnitId = loadUnitId; this.qualifiers = qualifiers; + this.className = className; } verifyQualifiers(qualifiers: QualifierInfo[]): boolean { diff --git a/core/metadata/src/model/EggPrototype.ts b/core/metadata/src/model/EggPrototype.ts index 790f2aed..83a38ec0 100644 --- a/core/metadata/src/model/EggPrototype.ts +++ b/core/metadata/src/model/EggPrototype.ts @@ -45,6 +45,7 @@ export interface EggPrototype extends LifecycleObject { const appRepoProto = loadUnit.getEggPrototype('appRepo', [{ attribute: InitTypeQualifierAttribute, value: ObjectInitType.SINGLETON }]); const sprintRepoProto = loadUnit.getEggPrototype('sprintRepo', [{ attribute: InitTypeQualifierAttribute, value: ObjectInitType.SINGLETON }]); const userRepoProto = loadUnit.getEggPrototype('userRepo', [{ attribute: InitTypeQualifierAttribute, value: ObjectInitType.SINGLETON }]); - assert(appRepoProto); - assert(sprintRepoProto); - assert(userRepoProto); + assert.strictEqual(appRepoProto.length, 1); + assert.strictEqual(appRepoProto[0].className, 'AppRepo'); + assert.strictEqual(sprintRepoProto.length, 1); + assert.strictEqual(userRepoProto.length, 1); await LoadUnitFactory.destroyLoadUnit(loadUnit); }); @@ -92,6 +93,7 @@ describe('test/LoadUnit/LoadUnit.test.ts', () => { const foo2Prototype = loadUnit.getEggPrototype('foo', [{ attribute: FOO_ATTRIBUTE, value: 'foo2' }]); assert(foo1Prototype); assert(foo1Prototype.length === 1); + assert.strictEqual(foo1Prototype[0].className, 'FooLogger'); assert(foo2Prototype); assert(foo2Prototype.length === 1); await LoadUnitFactory.destroyLoadUnit(loadUnit); diff --git a/standalone/standalone/src/StandaloneInnerObjectProto.ts b/standalone/standalone/src/StandaloneInnerObjectProto.ts index fcfe48b6..a9f19d7d 100644 --- a/standalone/standalone/src/StandaloneInnerObjectProto.ts +++ b/standalone/standalone/src/StandaloneInnerObjectProto.ts @@ -25,6 +25,7 @@ export class StandaloneInnerObjectProto implements EggPrototype { readonly accessLevel: AccessLevel; readonly injectObjects: InjectObjectProto[]; readonly loadUnitId: Id; + readonly className?: string; constructor( id: string, @@ -33,6 +34,7 @@ export class StandaloneInnerObjectProto implements EggPrototype { initType: ObjectInitTypeLike, loadUnitId: Id, qualifiers: QualifierInfo[], + className?: string, ) { this.id = id; this.clazz = clazz; @@ -42,6 +44,7 @@ export class StandaloneInnerObjectProto implements EggPrototype { this.injectObjects = []; this.loadUnitId = loadUnitId; this.qualifiers = qualifiers; + this.className = className; } verifyQualifiers(qualifiers: QualifierInfo[]): boolean { @@ -73,9 +76,10 @@ export class StandaloneInnerObjectProto implements EggPrototype { static create(ctx: EggPrototypeLifecycleContext): EggPrototype { const { clazz, loadUnit } = ctx; const name = ctx.prototypeInfo.name; + const className = ctx.prototypeInfo.className; const id = IdenticalUtil.createProtoId(loadUnit.id, name); const proto = new StandaloneInnerObjectProto( - id, name, clazz, ctx.prototypeInfo.initType, loadUnit.id, QualifierUtil.getProtoQualifiers(clazz), + id, name, clazz, ctx.prototypeInfo.initType, loadUnit.id, QualifierUtil.getProtoQualifiers(clazz), className, ); return proto; } diff --git a/standalone/standalone/test/index.test.ts b/standalone/standalone/test/index.test.ts index 2203d03d..caa552da 100644 --- a/standalone/standalone/test/index.test.ts +++ b/standalone/standalone/test/index.test.ts @@ -161,4 +161,39 @@ describe('test/index.test.ts', () => { `withCrossAroundResult(withPointAroundResult(hello withPointAroundParam(withCrosscutAroundParam(aop))${JSON.stringify(pointcutAdviceParams)})${JSON.stringify(crosscutAdviceParams)})`); }); }); + + describe('load', () => { + let runner: Runner; + afterEach(async () => { + if (runner) await runner.destroy(); + }); + + it('should work', async () => { + runner = new Runner(path.join(__dirname, './fixtures/simple')); + const loadunits = await runner.load(); + for (const loadunit of loadunits) { + for (const proto of loadunit.iterateEggPrototype()) { + if (proto.id.match(/:hello$/)) { + assert.strictEqual(proto.className, 'Hello'); + } else if (proto.id.match(/:moduleConfigs$/)) { + assert.strictEqual(proto.className, undefined); + } else if (proto.id.match(/:moduleConfig$/)) { + assert.strictEqual(proto.className, undefined); + } + } + } + }); + + it('should work with multi', async () => { + runner = new Runner(path.join(__dirname, './fixtures/multi-callback-instance-module')); + const loadunits = await runner.load(); + for (const loadunit of loadunits) { + for (const proto of loadunit.iterateEggPrototype()) { + if (proto.id.match(/:dynamicLogger$/)) { + assert.strictEqual(proto.className, 'DynamicLogger'); + } + } + } + }); + }); }); From dc056965308054260abae98fdbe41ed0bbd49e3e Mon Sep 17 00:00:00 2001 From: popomore Date: Tue, 19 Sep 2023 21:36:54 +0800 Subject: [PATCH 2/3] f --- core/core-decorator/test/decorators.test.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/core/core-decorator/test/decorators.test.ts b/core/core-decorator/test/decorators.test.ts index 51f08625..2168edfe 100644 --- a/core/core-decorator/test/decorators.test.ts +++ b/core/core-decorator/test/decorators.test.ts @@ -114,6 +114,7 @@ describe('test/decorator.test.ts', () => { value: 'foo2', }], }], + className: 'FooLogger', }; assert.deepStrictEqual(PrototypeUtil.getMultiInstanceProperty(FooLogger, { unitPath: 'foo', From 50f3da205eb65c3a36d47edea9703912d805d66a Mon Sep 17 00:00:00 2001 From: popomore Date: Tue, 19 Sep 2023 22:10:41 +0800 Subject: [PATCH 3/3] f --- standalone/standalone/src/StandaloneInnerObjectProto.ts | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/standalone/standalone/src/StandaloneInnerObjectProto.ts b/standalone/standalone/src/StandaloneInnerObjectProto.ts index a9f19d7d..fcfe48b6 100644 --- a/standalone/standalone/src/StandaloneInnerObjectProto.ts +++ b/standalone/standalone/src/StandaloneInnerObjectProto.ts @@ -25,7 +25,6 @@ export class StandaloneInnerObjectProto implements EggPrototype { readonly accessLevel: AccessLevel; readonly injectObjects: InjectObjectProto[]; readonly loadUnitId: Id; - readonly className?: string; constructor( id: string, @@ -34,7 +33,6 @@ export class StandaloneInnerObjectProto implements EggPrototype { initType: ObjectInitTypeLike, loadUnitId: Id, qualifiers: QualifierInfo[], - className?: string, ) { this.id = id; this.clazz = clazz; @@ -44,7 +42,6 @@ export class StandaloneInnerObjectProto implements EggPrototype { this.injectObjects = []; this.loadUnitId = loadUnitId; this.qualifiers = qualifiers; - this.className = className; } verifyQualifiers(qualifiers: QualifierInfo[]): boolean { @@ -76,10 +73,9 @@ export class StandaloneInnerObjectProto implements EggPrototype { static create(ctx: EggPrototypeLifecycleContext): EggPrototype { const { clazz, loadUnit } = ctx; const name = ctx.prototypeInfo.name; - const className = ctx.prototypeInfo.className; const id = IdenticalUtil.createProtoId(loadUnit.id, name); const proto = new StandaloneInnerObjectProto( - id, name, clazz, ctx.prototypeInfo.initType, loadUnit.id, QualifierUtil.getProtoQualifiers(clazz), className, + id, name, clazz, ctx.prototypeInfo.initType, loadUnit.id, QualifierUtil.getProtoQualifiers(clazz), ); return proto; }