Skip to content

Commit

Permalink
Merge branch 'support-copying-prototype-metadata-in-the-mapping' of g…
Browse files Browse the repository at this point in the history
…ithub.com:ima-tech/pristine-ts into support-copying-prototype-metadata-in-the-mapping
  • Loading branch information
etiennenoel committed Jan 19, 2024
2 parents 0902c14 + 03a8bd4 commit 049963d
Show file tree
Hide file tree
Showing 22 changed files with 166 additions and 165 deletions.
1 change: 0 additions & 1 deletion packages/common/src/common.module.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {CommonModuleKeyname} from "./common.module.keyname";
import {ModuleInterface} from "./interfaces/module.interface";
export * from "./constants/constants";
export * from "./contexts/contexts";
export * from "./decorators/decorators";
export * from "./enums/enums";
Expand Down
2 changes: 0 additions & 2 deletions packages/common/src/constants/constants.ts

This file was deleted.

This file was deleted.

This file was deleted.

1 change: 1 addition & 0 deletions packages/common/src/enums/enums.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from "./http-method.enum";
export * from "./metadata.enum";
export * from "./service-definition-tag.enum";
export * from "./internal-container-parameter.enum";
7 changes: 7 additions & 0 deletions packages/common/src/enums/metadata.enum.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export enum MetadataEnum {
RouteContext = "route:context",
RouteMethodArguments = "route:method:arguments",
Route = "route",
ControllerRoutes = "controller:routes",
ControllerBasePath = "controller:basePath",
}
15 changes: 7 additions & 8 deletions packages/common/src/utils/metadata.util.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import "reflect-metadata"
import {methodArgumentsMetadataKeynameConstant} from "../constants/method-arguments-metadata-keyname.constant";
import {routeContextMetadataKeynameConstant} from "../constants/route-context-metadata-keyname.constant";
import {ClassMetadata, MethodMetadata} from "@pristine-ts/metadata";
import {MetadataEnum} from "../enums/metadata.enum";

export class MetadataUtil {
/**
Expand All @@ -12,7 +11,7 @@ export class MetadataUtil {
* @param propertyKey
*/
static getMethodParametersMetadata(target: any, propertyKey: string | symbol): any[] {
const methodParameters = MethodMetadata.getMetadata(target, propertyKey, methodArgumentsMetadataKeynameConstant);
const methodParameters = MethodMetadata.getMetadata(target, propertyKey, MetadataEnum.RouteMethodArguments);

if(methodParameters === undefined || Array.isArray(methodParameters) === false) {
return [];
Expand All @@ -34,7 +33,7 @@ export class MetadataUtil {

methodArguments[parameterIndex] = metadata;

MethodMetadata.defineMetadata(target, propertyKey, methodArgumentsMetadataKeynameConstant, methodArguments);
MethodMetadata.defineMetadata(target, propertyKey, MetadataEnum.RouteMethodArguments, methodArguments);
}

/**
Expand All @@ -46,9 +45,9 @@ export class MetadataUtil {
let routeContext;

if(propertyKey === undefined) {
routeContext = ClassMetadata.getMetadata(target.prototype, routeContextMetadataKeynameConstant);
routeContext = ClassMetadata.getMetadata(target.prototype, MetadataEnum.RouteContext);
} else {
routeContext = MethodMetadata.getMetadata(target, propertyKey, routeContextMetadataKeynameConstant);
routeContext = MethodMetadata.getMetadata(target, propertyKey, MetadataEnum.RouteContext);
}

if(routeContext === undefined || typeof routeContext !== "object") {
Expand All @@ -74,9 +73,9 @@ export class MetadataUtil {

if(propertyKey === undefined) {
// When there are no properties, the metadata is defined on the prototype.
ClassMetadata.defineMetadata(target.prototype, routeContextMetadataKeynameConstant, routeContext);
ClassMetadata.defineMetadata(target.prototype, MetadataEnum.RouteContext, routeContext);
} else {
MethodMetadata.defineMetadata(target, propertyKey, routeContextMetadataKeynameConstant, routeContext);
MethodMetadata.defineMetadata(target, propertyKey, MetadataEnum.RouteContext, routeContext);
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,13 +144,13 @@ describe("Data Mapping Builder", () => {
expect((dataMappingBuilder.nodes.nested as DataMappingNode).nodes.nestedTitle.destinationProperty).toBe("nestedName")

expect(dataMappingBuilder.nodes.array).toBeDefined()
expect(dataMappingBuilder.nodes.array.type).toBe(DataMappingNodeTypeEnum.Array)
expect(dataMappingBuilder.nodes.array.type).toBe(DataMappingNodeTypeEnum.ObjectArray)
expect(dataMappingBuilder.nodes.array.destinationProperty).toBe("list")
expect((dataMappingBuilder.nodes.array as DataMappingNode).nodes.rank.sourceProperty).toBe("rank")
expect((dataMappingBuilder.nodes.array as DataMappingNode).nodes.rank.destinationProperty).toBe("position")

expect(dataMappingBuilder.nodes.children).toBeDefined()
expect(dataMappingBuilder.nodes.children.type).toBe(DataMappingNodeTypeEnum.Array)
expect(dataMappingBuilder.nodes.children.type).toBe(DataMappingNodeTypeEnum.ScalarArray)
expect(dataMappingBuilder.nodes.children.destinationProperty).toBe("infants")
expect((dataMappingBuilder.nodes.children as DataMappingLeaf).sourceProperty).toBe("children")
expect((dataMappingBuilder.nodes.children as DataMappingLeaf).destinationProperty).toBe("infants")
Expand Down
16 changes: 8 additions & 8 deletions packages/data-mapping/src/builders/data-mapping.builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import {DataNormalizerUniqueKey} from "../types/data-normalizer-unique-key.type"
import {DataNormalizerAlreadyAdded} from "../errors/data-normalizer-already-added.error";
import {DataMappingInterceptorUniqueKeyType} from "../types/data-mapping-interceptor-unique-key.type";
import {
DataBeforeRowTransformerInterceptorAlreadyAddedError
} from "../errors/data-before-row-transformer-interceptor-already-added.error";
DataBeforeMappingInterceptorAlreadyAddedError
} from "../errors/data-before-mapping-interceptor-already-added.error";
import {
DataAfterRowTransformerInterceptorAlreadyAddedError
} from "../errors/data-after-row-transformer-interceptor-already-added.error";
DataAfterMappingInterceptorAlreadyAddedError
} from "../errors/data-after-mapping-interceptor-already-added.error";
import {DataMappingLeaf} from "../nodes/data-mapping.leaf";
import {BaseDataMappingNode} from "../nodes/base-data-mapping.node";
import {DataMappingNodeTypeEnum} from "../enums/data-mapping-node-type.enum";
Expand Down Expand Up @@ -53,7 +53,7 @@ export class DataMappingBuilder extends BaseDataMappingNode{
*/
public addBeforeMappingInterceptor(key: DataMappingInterceptorUniqueKeyType, options?: any): DataMappingBuilder {
if(this.hasBeforeMappingInterceptor(key)) {
throw new DataBeforeRowTransformerInterceptorAlreadyAddedError("The before row transform interceptor has already been added to this Tree.", key, options)
throw new DataBeforeMappingInterceptorAlreadyAddedError("The before row transform interceptor has already been added to this Tree.", key, options)
}

this.beforeMappingInterceptors.push({
Expand All @@ -80,7 +80,7 @@ export class DataMappingBuilder extends BaseDataMappingNode{
*/
public addAfterMappingInterceptor(key: DataMappingInterceptorUniqueKeyType, options?: any): DataMappingBuilder {
if(this.hasAfterMappingInterceptor(key)) {
throw new DataAfterRowTransformerInterceptorAlreadyAddedError("The after row transform interceptor has already been added to this Tree.", key, options)
throw new DataAfterMappingInterceptorAlreadyAddedError("The after row transform interceptor has already been added to this Tree.", key, options)
}

this.afterMappingInterceptors.push({
Expand Down Expand Up @@ -153,7 +153,7 @@ export class DataMappingBuilder extends BaseDataMappingNode{

const nodes = schema.nodes;

for(let key in nodes) {
for(const key in nodes) {
if(nodes.hasOwnProperty(key) === false) {
continue;
}
Expand Down Expand Up @@ -186,7 +186,7 @@ export class DataMappingBuilder extends BaseDataMappingNode{
public export() {
const nodes = this.nodes;

for (let key in nodes) {
for (const key in nodes) {
if(nodes.hasOwnProperty(key) === false) {
continue;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/data-mapping/src/data-mapping.module.keyname.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export const DataMappingModuleKeyname: string = "pristine.data-transformer";
export const DataMappingModuleKeyname: string = "pristine.data-mapping";
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {DataMappingInterceptorUniqueKeyType} from "../types/data-mapping-interce
/**
* This Error is thrown when the before row interceptor is added more than once to the builder.
*/
export class DataAfterRowTransformerInterceptorAlreadyAddedError extends LoggableError {
export class DataAfterMappingInterceptorAlreadyAddedError extends LoggableError {

public constructor(message: string, uniqueKey: DataMappingInterceptorUniqueKeyType, options?: any) {
super(message, {
Expand All @@ -16,6 +16,6 @@ export class DataAfterRowTransformerInterceptorAlreadyAddedError extends Loggabl
// Set the prototype explicitly.
// As specified in the documentation in TypeScript
// https://github.com/Microsoft/TypeScript/wiki/Breaking-Changes#extending-built-ins-like-error-array-and-map-may-no-longer-work
Object.setPrototypeOf(this, DataAfterRowTransformerInterceptorAlreadyAddedError.prototype);
Object.setPrototypeOf(this, DataAfterMappingInterceptorAlreadyAddedError.prototype);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {DataMappingInterceptorUniqueKeyType} from "../types/data-mapping-interce
/**
* This Error is thrown when the after row interceptor is added more than once to the builder.
*/
export class DataBeforeRowTransformerInterceptorAlreadyAddedError extends LoggableError {
export class DataBeforeMappingInterceptorAlreadyAddedError extends LoggableError {

public constructor(message: string, uniqueKey: DataMappingInterceptorUniqueKeyType, options?: any) {
super(message, {
Expand All @@ -16,6 +16,6 @@ export class DataBeforeRowTransformerInterceptorAlreadyAddedError extends Loggab
// Set the prototype explicitly.
// As specified in the documentation in TypeScript
// https://github.com/Microsoft/TypeScript/wiki/Breaking-Changes#extending-built-ins-like-error-array-and-map-may-no-longer-work
Object.setPrototypeOf(this, DataBeforeRowTransformerInterceptorAlreadyAddedError.prototype);
Object.setPrototypeOf(this, DataBeforeMappingInterceptorAlreadyAddedError.prototype);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {DataMappingInterceptorUniqueKeyType} from "../types/data-mapping-interce
/**
* This Error is thrown if the Data Transformer Class is not found in the list of available interceptors. It might be missing a tag.
*/
export class DataTransformerInterceptorNotFoundError extends LoggableError {
export class DataMappingInterceptorNotFoundError extends LoggableError {

public constructor(message: string, uniqueKey: DataMappingInterceptorUniqueKeyType, options?: any) {
super(message, {
Expand All @@ -16,6 +16,6 @@ export class DataTransformerInterceptorNotFoundError extends LoggableError {
// Set the prototype explicitly.
// As specified in the documentation in TypeScript
// https://github.com/Microsoft/TypeScript/wiki/Breaking-Changes#extending-built-ins-like-error-array-and-map-may-no-longer-work
Object.setPrototypeOf(this, DataTransformerInterceptorNotFoundError.prototype);
Object.setPrototypeOf(this, DataMappingInterceptorNotFoundError.prototype);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {Request} from "@pristine-ts/common";
/**
* This Error is thrown when a property isn't optional and should be found in the source object.
*/
export class DataTransformerSourcePropertyNotFoundError extends LoggableError {
export class DataMappingSourcePropertyNotFoundError extends LoggableError {

public constructor(message: string, sourceProperty: string) {
super(message, {
Expand All @@ -15,6 +15,6 @@ export class DataTransformerSourcePropertyNotFoundError extends LoggableError {
// Set the prototype explicitly.
// As specified in the documentation in TypeScript
// https://github.com/Microsoft/TypeScript/wiki/Breaking-Changes#extending-built-ins-like-error-array-and-map-may-no-longer-work
Object.setPrototypeOf(this, DataTransformerSourcePropertyNotFoundError.prototype);
Object.setPrototypeOf(this, DataMappingSourcePropertyNotFoundError.prototype);
}
}
8 changes: 4 additions & 4 deletions packages/data-mapping/src/errors/errors.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
export * from "./array-data-mapping-node-invalid-source-property-type.error";
export * from "./data-after-row-transformer-interceptor-already-added.error";
export * from "./data-before-row-transformer-interceptor-already-added.error";
export * from "./data-after-mapping-interceptor-already-added.error";
export * from "./data-before-mapping-interceptor-already-added.error";
export * from "./data-normalizer-already-added.error";
export * from "./data-transformer-interceptor-not-found.error";
export * from "./data-transformer-source-property-not-found.error";
export * from "./data-mapping-interceptor-not-found.error";
export * from "./data-mapping-source-property-not-found.error";
export * from "./normalizer-invalid-source-type.error";
export * from "./undefined-source-property.error";
Loading

0 comments on commit 049963d

Please sign in to comment.