Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Prototype should not be inherited #243

Merged
merged 2 commits into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions core/core-decorator/src/util/MetadataUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ export class MetadataUtil {
return !!this.getMetaData(metadataKey, clazz);
}

static getOwnBooleanMetaData(metadataKey: MetaDataKey, clazz: EggProtoImplClass): boolean {
return !!this.getOwnMetaData(metadataKey, clazz);
}

static getArrayMetaData<T>(metadataKey: MetaDataKey, clazz: EggProtoImplClass): Array<T> {
return this.getMetaData(metadataKey, clazz) || [];
}
Expand Down
18 changes: 9 additions & 9 deletions core/core-decorator/src/util/PrototypeUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export class PrototypeUtil {
* @param {Function} clazz -
*/
static isEggPrototype(clazz: EggProtoImplClass): boolean {
return MetadataUtil.getBooleanMetaData(PrototypeUtil.IS_EGG_OBJECT_PROTOTYPE, clazz);
return MetadataUtil.getOwnBooleanMetaData(PrototypeUtil.IS_EGG_OBJECT_PROTOTYPE, clazz);
}

/**
Expand All @@ -56,7 +56,7 @@ export class PrototypeUtil {
* @param {Function} clazz -
*/
static isEggMultiInstancePrototype(clazz: EggProtoImplClass): boolean {
return MetadataUtil.getBooleanMetaData(PrototypeUtil.IS_EGG_OBJECT_MULTI_INSTANCE_PROTOTYPE, clazz);
return MetadataUtil.getOwnBooleanMetaData(PrototypeUtil.IS_EGG_OBJECT_MULTI_INSTANCE_PROTOTYPE, clazz);
}

/**
Expand All @@ -67,7 +67,7 @@ export class PrototypeUtil {
if (!PrototypeUtil.isEggMultiInstancePrototype(clazz)) {
return;
}
const metadata = MetadataUtil.getMetaData<EggMultiInstancePrototypeInfo>(PrototypeUtil.MULTI_INSTANCE_PROTOTYPE_STATIC_PROPERTY, clazz);
const metadata = MetadataUtil.getOwnMetaData<EggMultiInstancePrototypeInfo>(PrototypeUtil.MULTI_INSTANCE_PROTOTYPE_STATIC_PROPERTY, clazz);
if (metadata) {
return MultiInstanceType.STATIC;
}
Expand All @@ -88,7 +88,7 @@ export class PrototypeUtil {
* @param {Function} clazz -
*/
static getFilePath(clazz: EggProtoImplClass): string | undefined {
return MetadataUtil.getMetaData(PrototypeUtil.FILE_PATH, clazz);
return MetadataUtil.getOwnMetaData(PrototypeUtil.FILE_PATH, clazz);
}

/**
Expand All @@ -106,7 +106,7 @@ export class PrototypeUtil {
* @return {EggPrototypeInfo} -
*/
static getProperty(clazz: EggProtoImplClass): EggPrototypeInfo | undefined {
return MetadataUtil.getMetaData(PrototypeUtil.PROTOTYPE_PROPERTY, clazz);
return MetadataUtil.getOwnMetaData(PrototypeUtil.PROTOTYPE_PROPERTY, clazz);
killagu marked this conversation as resolved.
Show resolved Hide resolved
}

static getInitType(clazz: EggProtoImplClass, ctx: MultiInstancePrototypeGetObjectsContext): string | undefined {
Expand Down Expand Up @@ -151,7 +151,7 @@ export class PrototypeUtil {
* @param {EggProtoImplClass} clazz -
*/
static getStaticMultiInstanceProperty(clazz: EggProtoImplClass): EggMultiInstancePrototypeInfo | undefined {
const metadata = MetadataUtil.getMetaData<EggMultiInstancePrototypeInfo>(PrototypeUtil.MULTI_INSTANCE_PROTOTYPE_STATIC_PROPERTY, clazz);
const metadata = MetadataUtil.getOwnMetaData<EggMultiInstancePrototypeInfo>(PrototypeUtil.MULTI_INSTANCE_PROTOTYPE_STATIC_PROPERTY, clazz);
if (metadata) {
return metadata;
}
Expand All @@ -163,7 +163,7 @@ export class PrototypeUtil {
* @param {MultiInstancePrototypeGetObjectsContext} ctx -
*/
static getDynamicMultiInstanceProperty(clazz: EggProtoImplClass, ctx: MultiInstancePrototypeGetObjectsContext): EggMultiInstancePrototypeInfo | undefined {
const callBackMetadata = MetadataUtil.getMetaData<EggMultiInstanceCallbackPrototypeInfo>(PrototypeUtil.MULTI_INSTANCE_PROTOTYPE_CALLBACK_PROPERTY, clazz);
const callBackMetadata = MetadataUtil.getOwnMetaData<EggMultiInstanceCallbackPrototypeInfo>(PrototypeUtil.MULTI_INSTANCE_PROTOTYPE_CALLBACK_PROPERTY, clazz);
if (callBackMetadata) {
const objects = callBackMetadata.getObjects(ctx);
return {
Expand All @@ -179,11 +179,11 @@ export class PrototypeUtil {
* @param {MultiInstancePrototypeGetObjectsContext} ctx -
*/
static getMultiInstanceProperty(clazz: EggProtoImplClass, ctx: MultiInstancePrototypeGetObjectsContext): EggMultiInstancePrototypeInfo | undefined {
const metadata = MetadataUtil.getMetaData<EggMultiInstancePrototypeInfo>(PrototypeUtil.MULTI_INSTANCE_PROTOTYPE_STATIC_PROPERTY, clazz);
const metadata = MetadataUtil.getOwnMetaData<EggMultiInstancePrototypeInfo>(PrototypeUtil.MULTI_INSTANCE_PROTOTYPE_STATIC_PROPERTY, clazz);
if (metadata) {
return metadata;
}
const callBackMetadata = MetadataUtil.getMetaData<EggMultiInstanceCallbackPrototypeInfo>(PrototypeUtil.MULTI_INSTANCE_PROTOTYPE_CALLBACK_PROPERTY, clazz);
const callBackMetadata = MetadataUtil.getOwnMetaData<EggMultiInstanceCallbackPrototypeInfo>(PrototypeUtil.MULTI_INSTANCE_PROTOTYPE_CALLBACK_PROPERTY, clazz);
if (callBackMetadata) {
const objects = callBackMetadata.getObjects(ctx);
// TODO delete in next major version, default qualifier be added in ProtoDescriptorHelper.addDefaultQualifier
Expand Down
56 changes: 56 additions & 0 deletions core/core-decorator/test/decorators.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
LoadUnitNameQualifierAttribute,
InitTypeQualifierAttribute,
DEFAULT_PROTO_IMPL_TYPE,
MultiInstanceType,
} from '@eggjs/tegg-types';
import type { EggPrototypeInfo, EggMultiInstancePrototypeInfo, InjectObjectInfo } from '@eggjs/tegg-types';

Expand All @@ -16,6 +17,14 @@ import { PrototypeUtil, QualifierUtil } from '..';
import QualifierCacheService from './fixtures/decators/QualifierCacheService';
import { FOO_ATTRIBUTE, FooLogger } from './fixtures/decators/FooLogger';
import { ConstructorObject } from './fixtures/decators/ConstructorObject';
import {
ChildDynamicMultiInstanceProto,
ChildSingletonProto,
ChildStaticMultiInstanceProto,
ParentDynamicMultiInstanceProto,
ParentSingletonProto,
ParentStaticMultiInstanceProto,
} from './fixtures/decators/ChildService';
killagu marked this conversation as resolved.
Show resolved Hide resolved

describe('test/decorator.test.ts', () => {
describe('ContextProto', () => {
Expand Down Expand Up @@ -141,4 +150,51 @@ describe('test/decorator.test.ts', () => {
it('should get the right file path', () => {
assert(PrototypeUtil.getFilePath(CacheService) === CacheService.fileName);
});

describe('inherited', () => {
const fakeCtx = {
unitPath: 'foo',
moduleName: '',
};

it('Prototype should not be inherited', () => {
assert(PrototypeUtil.isEggPrototype(ParentSingletonProto));
assert(PrototypeUtil.getProperty(ParentSingletonProto));
assert(PrototypeUtil.getFilePath(ParentSingletonProto));

assert.strictEqual(PrototypeUtil.isEggPrototype(ChildSingletonProto), undefined);
assert.strictEqual(PrototypeUtil.getProperty(ChildSingletonProto), undefined);
assert.strictEqual(PrototypeUtil.getFilePath(ChildSingletonProto), undefined);
});

it('static multiInstanceProto should not be inherited', () => {
assert(PrototypeUtil.isEggMultiInstancePrototype(ParentStaticMultiInstanceProto));
assert.strictEqual(
PrototypeUtil.getEggMultiInstancePrototypeType(ParentStaticMultiInstanceProto),
MultiInstanceType.STATIC,
);
assert(PrototypeUtil.getStaticMultiInstanceProperty(ParentStaticMultiInstanceProto));
assert(PrototypeUtil.getMultiInstanceProperty(ParentStaticMultiInstanceProto, fakeCtx));
assert(PrototypeUtil.getFilePath(ParentStaticMultiInstanceProto));

assert.strictEqual(PrototypeUtil.isEggMultiInstancePrototype(ChildStaticMultiInstanceProto), undefined);
assert.strictEqual(PrototypeUtil.getEggMultiInstancePrototypeType(ChildStaticMultiInstanceProto), undefined);
assert.strictEqual(PrototypeUtil.getStaticMultiInstanceProperty(ChildStaticMultiInstanceProto), undefined);
assert.strictEqual(PrototypeUtil.getMultiInstanceProperty(ChildStaticMultiInstanceProto, fakeCtx), undefined);
assert.strictEqual(PrototypeUtil.getFilePath(ChildStaticMultiInstanceProto), undefined);
});

it('dynamic multipleInstanceProto should not be inherited', () => {
assert.strictEqual(
PrototypeUtil.getEggMultiInstancePrototypeType(ParentDynamicMultiInstanceProto),
MultiInstanceType.DYNAMIC,
);
assert(PrototypeUtil.getDynamicMultiInstanceProperty(ParentDynamicMultiInstanceProto, fakeCtx));
assert(PrototypeUtil.getMultiInstanceProperty(ParentDynamicMultiInstanceProto, fakeCtx));

assert.strictEqual(PrototypeUtil.getEggMultiInstancePrototypeType(ChildDynamicMultiInstanceProto), undefined);
assert.strictEqual(PrototypeUtil.getDynamicMultiInstanceProperty(ChildDynamicMultiInstanceProto, fakeCtx), undefined);
assert.strictEqual(PrototypeUtil.getMultiInstanceProperty(ChildDynamicMultiInstanceProto, fakeCtx), undefined);
});
});
});
22 changes: 22 additions & 0 deletions core/core-decorator/test/fixtures/decators/ChildService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { MultiInstanceProto, SingletonProto } from '../../..';

@SingletonProto()
export class ParentSingletonProto {}

export class ChildSingletonProto extends ParentSingletonProto {}


@MultiInstanceProto({
objects: [],
})
export class ParentStaticMultiInstanceProto {}

export class ChildStaticMultiInstanceProto extends ParentSingletonProto {}
killagu marked this conversation as resolved.
Show resolved Hide resolved


@MultiInstanceProto({
getObjects: () => [],
})
export class ParentDynamicMultiInstanceProto {}

export class ChildDynamicMultiInstanceProto extends ParentDynamicMultiInstanceProto {}
Loading