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

Add getClassMetadataFromMetadataReader #67

Merged
merged 6 commits into from
Nov 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 5 additions & 0 deletions .changeset/tough-suits-repeat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@inversifyjs/core": minor
---

Added `getClassMetadataFromMetadataReader`.
7 changes: 6 additions & 1 deletion packages/container/libraries/core/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { getClassMetadata } from './metadata/calculations/getClassMetadata';
import { getClassMetadataFromMetadataReader } from './metadata/calculations/getClassMetadataFromMetadataReader';
import { ClassElementMetadata } from './metadata/models/ClassElementMetadata';
import { ClassElementMetadataKind } from './metadata/models/ClassElementMetadataKind';
import { ClassMetadata } from './metadata/models/ClassMetadata';
Expand All @@ -20,4 +21,8 @@ export type {
UnmanagedClassElementMetadata,
};

export { ClassElementMetadataKind, getClassMetadata };
export {
ClassElementMetadataKind,
getClassMetadata,
getClassMetadataFromMetadataReader,
};
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import { getReflectMetadata } from '@inversifyjs/reflect-metadata-utils';
jest.mock('./getClassMetadataConstructorArguments');
jest.mock('./getClassMetadataProperties');

import { Newable } from '@inversifyjs/common';

import { POST_CONSTRUCT, PRE_DESTROY } from '../../reflectMetadata/data/keys';
import { ClassElementMetadata } from '../models/ClassElementMetadata';
import { ClassElementMetadataKind } from '../models/ClassElementMetadataKind';
import { ClassMetadata } from '../models/ClassMetadata';
Expand All @@ -22,6 +25,8 @@ describe(getClassMetadata.name, () => {
let postConstructMetadataFixture: LegacyMetadata;
let preDestroyMetadataFixture: LegacyMetadata;

let typeFixture: Newable;

let result: unknown;

beforeAll(() => {
Expand Down Expand Up @@ -55,6 +60,8 @@ describe(getClassMetadata.name, () => {
value: 'pre-destroy-value-fixture',
};

typeFixture = class {};

(
getClassMetadataConstructorArguments as jest.Mock<
typeof getClassMetadataConstructorArguments
Expand All @@ -71,13 +78,39 @@ describe(getClassMetadata.name, () => {
.mockReturnValueOnce(postConstructMetadataFixture)
.mockReturnValueOnce(preDestroyMetadataFixture);

result = getClassMetadata(class {});
result = getClassMetadata(typeFixture);
});

afterAll(() => {
jest.clearAllMocks();
});

it('should call getReflectMetadata()', () => {
expect(getReflectMetadata).toHaveBeenCalledTimes(2);
expect(getReflectMetadata).toHaveBeenNthCalledWith(
1,
typeFixture,
POST_CONSTRUCT,
);
expect(getReflectMetadata).toHaveBeenNthCalledWith(
2,
typeFixture,
PRE_DESTROY,
);
});

it('should call getClassMetadataConstructorArguments()', () => {
expect(getClassMetadataConstructorArguments).toHaveBeenCalledTimes(1);
expect(getClassMetadataConstructorArguments).toHaveBeenCalledWith(
typeFixture,
);
});

it('should call getClassMetadataProperties()', () => {
expect(getClassMetadataProperties).toHaveBeenCalledTimes(1);
expect(getClassMetadataProperties).toHaveBeenCalledWith(typeFixture);
});

it('should return ClassMetadata', () => {
const expected: ClassMetadata = {
constructorArguments: constructorArgumentsMetadataFixture,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,246 @@
import { afterAll, beforeAll, describe, expect, it, jest } from '@jest/globals';

import { Newable } from '@inversifyjs/common';

jest.mock('./getClassElementMetadataFromLegacyMetadata');
jest.mock('./getClassElementMetadataFromNewable');

import { ClassElementMetadata } from '../models/ClassElementMetadata';
import { ClassElementMetadataKind } from '../models/ClassElementMetadataKind';
import { LegacyMetadata } from '../models/LegacyMetadata';
import { LegacyMetadataMap } from '../models/LegacyMetadataMap';
import { LegacyMetadataReader } from '../models/LegacyMetadataReader';
import { getClassElementMetadataFromLegacyMetadata } from './getClassElementMetadataFromLegacyMetadata';
import { getClassElementMetadataFromNewable } from './getClassElementMetadataFromNewable';
import { getClassMetadataConstructorArgumentsFromMetadataReader } from './getClassMetadataConstructorArgumentsFromMetadataReader';

describe(getClassMetadataConstructorArgumentsFromMetadataReader.name, () => {
describe('when called, and getReflectMetadata() provides typescript metadata', () => {
let typescriptTypeFixture: Newable;

let typeFixture: Newable;
let metadataReaderMock: jest.Mocked<LegacyMetadataReader>;

let classElementMetadataFixture: ClassElementMetadata;

let result: unknown;

beforeAll(() => {
typescriptTypeFixture = class {};

typeFixture = class {};
metadataReaderMock = {
getConstructorMetadata: jest.fn(),
getPropertiesMetadata: jest.fn(),
};

classElementMetadataFixture = {
kind: ClassElementMetadataKind.unmanaged,
};

metadataReaderMock.getConstructorMetadata.mockReturnValueOnce({
compilerGeneratedMetadata: [typescriptTypeFixture],
userGeneratedMetadata: {},
});

(
getClassElementMetadataFromNewable as jest.Mock<
typeof getClassElementMetadataFromNewable
>
).mockReturnValueOnce(classElementMetadataFixture);

result = getClassMetadataConstructorArgumentsFromMetadataReader(
typeFixture,
metadataReaderMock,
);
});

afterAll(() => {
jest.clearAllMocks();
});

it('should call metadataReader.getConstructorMetadata()', () => {
expect(metadataReaderMock.getConstructorMetadata).toHaveBeenCalledTimes(
1,
);
expect(metadataReaderMock.getConstructorMetadata).toHaveBeenCalledWith(
typeFixture,
);
});

it('should call getClassElementMetadataFromNewable()', () => {
expect(getClassElementMetadataFromNewable).toHaveBeenCalledTimes(1);
expect(getClassElementMetadataFromNewable).toHaveBeenCalledWith(
typescriptTypeFixture,
);
});

it('should return ClassElementMetadata[]', () => {
expect(result).toStrictEqual([classElementMetadataFixture]);
});
});

describe('when called, and getReflectMetadata() provides tag metadata', () => {
let legacyMetadataMapPropertyFixture: string | symbol;
let legacyMetadataListFixture: LegacyMetadata[];

let typeFixture: Newable;
let metadataReaderMock: jest.Mocked<LegacyMetadataReader>;

let classElementMetadataFixture: ClassElementMetadata;

let result: unknown;

beforeAll(() => {
legacyMetadataMapPropertyFixture = '0';
legacyMetadataListFixture = [
{
key: 'key-fixture',
value: 'value-fixture',
},
];

const legacyMetadataMap: LegacyMetadataMap = {
[legacyMetadataMapPropertyFixture]: legacyMetadataListFixture,
};

typeFixture = class {};
metadataReaderMock = {
getConstructorMetadata: jest.fn(),
getPropertiesMetadata: jest.fn(),
};

classElementMetadataFixture = {
kind: ClassElementMetadataKind.unmanaged,
};

metadataReaderMock.getConstructorMetadata.mockReturnValueOnce({
compilerGeneratedMetadata: undefined,
userGeneratedMetadata: legacyMetadataMap,
});

(
getClassElementMetadataFromLegacyMetadata as jest.Mock<
typeof getClassElementMetadataFromLegacyMetadata
>
).mockReturnValueOnce(classElementMetadataFixture);

result = getClassMetadataConstructorArgumentsFromMetadataReader(
typeFixture,
metadataReaderMock,
);
});

afterAll(() => {
jest.clearAllMocks();
});

it('should call metadataReader.getConstructorMetadata()', () => {
expect(metadataReaderMock.getConstructorMetadata).toHaveBeenCalledTimes(
1,
);
expect(metadataReaderMock.getConstructorMetadata).toHaveBeenCalledWith(
typeFixture,
);
});

it('should call getClassElementMetadataFromLegacyMetadata()', () => {
expect(getClassElementMetadataFromLegacyMetadata).toHaveBeenCalledTimes(
1,
);
expect(getClassElementMetadataFromLegacyMetadata).toHaveBeenCalledWith(
legacyMetadataListFixture,
);
});

it('should return ClassElementMetadata[]', () => {
expect(result).toStrictEqual([classElementMetadataFixture]);
});
});

describe('when called, and getReflectMetadata() provides both typescript and tag metadata', () => {
let legacyMetadataMapPropertyFixture: string | symbol;
let legacyMetadataListFixture: LegacyMetadata[];

let typescriptTypeFixture: Newable;

let typeFixture: Newable;
let metadataReaderMock: jest.Mocked<LegacyMetadataReader>;

let classElementMetadataFixture: ClassElementMetadata;

let result: unknown;

beforeAll(() => {
legacyMetadataMapPropertyFixture = '0';
legacyMetadataListFixture = [
{
key: 'key-fixture',
value: 'value-fixture',
},
];

const legacyMetadataMap: LegacyMetadataMap = {
[legacyMetadataMapPropertyFixture]: legacyMetadataListFixture,
};

typescriptTypeFixture = class {};

typeFixture = class {};
metadataReaderMock = {
getConstructorMetadata: jest.fn(),
getPropertiesMetadata: jest.fn(),
};

classElementMetadataFixture = {
kind: ClassElementMetadataKind.unmanaged,
};

metadataReaderMock.getConstructorMetadata.mockReturnValueOnce({
compilerGeneratedMetadata: [typescriptTypeFixture],
userGeneratedMetadata: legacyMetadataMap,
});

(
getClassElementMetadataFromLegacyMetadata as jest.Mock<
typeof getClassElementMetadataFromLegacyMetadata
>
).mockReturnValueOnce(classElementMetadataFixture);

result = getClassMetadataConstructorArgumentsFromMetadataReader(
typeFixture,
metadataReaderMock,
);
});

afterAll(() => {
jest.clearAllMocks();
});

it('should call metadataReader.getConstructorMetadata()', () => {
expect(metadataReaderMock.getConstructorMetadata).toHaveBeenCalledTimes(
1,
);
expect(metadataReaderMock.getConstructorMetadata).toHaveBeenCalledWith(
typeFixture,
);
});

it('should call getClassElementMetadataFromLegacyMetadata()', () => {
expect(getClassElementMetadataFromLegacyMetadata).toHaveBeenCalledTimes(
1,
);
expect(getClassElementMetadataFromLegacyMetadata).toHaveBeenCalledWith(
legacyMetadataListFixture,
);
});

it('should not call getClassElementMetadataFromNewable()', () => {
expect(getClassElementMetadataFromNewable).not.toHaveBeenCalled();
});

it('should return ClassElementMetadata[]', () => {
expect(result).toStrictEqual([classElementMetadataFixture]);
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { Newable } from '@inversifyjs/common';

import { ClassElementMetadata } from '../models/ClassElementMetadata';
import { LegacyConstructorMetadata } from '../models/LegacyConstructorMetadata';
import { LegacyMetadataReader } from '../models/LegacyMetadataReader';
import { getClassElementMetadataFromLegacyMetadata } from './getClassElementMetadataFromLegacyMetadata';
import { getClassElementMetadataFromNewable } from './getClassElementMetadataFromNewable';

export function getClassMetadataConstructorArgumentsFromMetadataReader<
TInstance,
TArgs extends unknown[],
>(
type: Newable<TInstance, TArgs>,
metadataReader: LegacyMetadataReader,
): ClassElementMetadata[] {
const legacyConstructorMetadata: LegacyConstructorMetadata =
metadataReader.getConstructorMetadata(type);

const constructorArgumentsMetadata: ClassElementMetadata[] = [];

for (const [stringifiedIndex, metadataList] of Object.entries(
legacyConstructorMetadata.userGeneratedMetadata,
)) {
const index: number = parseInt(stringifiedIndex);

constructorArgumentsMetadata[index] =
getClassElementMetadataFromLegacyMetadata(metadataList);
}

if (legacyConstructorMetadata.compilerGeneratedMetadata !== undefined) {
for (
let i: number = 0;
i < legacyConstructorMetadata.compilerGeneratedMetadata.length;
++i
) {
if (constructorArgumentsMetadata[i] === undefined) {
const typescriptMetadata: Newable = legacyConstructorMetadata
.compilerGeneratedMetadata[i] as Newable;

constructorArgumentsMetadata[i] =
getClassElementMetadataFromNewable(typescriptMetadata);
}
}
}

return constructorArgumentsMetadata;
}
Loading