From d38469e43895890224be45949e74c0a31e8f3cc2 Mon Sep 17 00:00:00 2001 From: Etienne Noel Date: Sun, 17 Nov 2024 23:13:14 -0800 Subject: [PATCH 1/4] - Fix --- .../src/builders/auto-data-mapping.builder.ts | 12 ++++++------ .../src/normalizers/date.normalizer.ts | 4 +++- .../src/normalizers/lowercase.normalizer.ts | 4 +++- .../src/normalizers/number.normalizer.ts | 4 +++- .../src/normalizers/string.normalizer.ts | 4 +++- 5 files changed, 18 insertions(+), 10 deletions(-) diff --git a/packages/data-mapping-common/src/builders/auto-data-mapping.builder.ts b/packages/data-mapping-common/src/builders/auto-data-mapping.builder.ts index 1668c247..8fa2243c 100644 --- a/packages/data-mapping-common/src/builders/auto-data-mapping.builder.ts +++ b/packages/data-mapping-common/src/builders/auto-data-mapping.builder.ts @@ -3,9 +3,9 @@ import {DataMappingBuilder} from "./data-mapping.builder"; import {DataMappingNode} from "../nodes/data-mapping.node"; import {ClassMetadata, PropertyInformationEnum, PropertyMetadata, TypeEnum, TypeUtils} from "@pristine-ts/metadata"; import {DataMappingLeaf} from "../nodes/data-mapping.leaf"; -import {NumberNormalizer} from "../normalizers/number.normalizer"; -import {StringNormalizer} from "../normalizers/string.normalizer"; -import {DateNormalizer} from "../normalizers/date.normalizer"; +import {NumberNormalizer, NumberNormalizerUniqueKey} from "../normalizers/number.normalizer"; +import {StringNormalizer, StringNormalizerUniqueKey} from "../normalizers/string.normalizer"; +import {DateNormalizer, DateNormalizerUniqueKey} from "../normalizers/date.normalizer"; import {DataMappingNodeTypeEnum} from "../enums/data-mapping-node-type.enum"; import {AutoDataMappingBuilderOptions} from "../options/auto-data-mapping-builder.options"; import {MetadataEnum} from "../enums/metadata.enum"; @@ -102,15 +102,15 @@ export class AutoDataMappingBuilder { // todo: Allow for options to be specified per attribute. We should probably add a decorator to can customize the normalizer. switch (nestedElementType) { case TypeEnum.Number: - normalizers.push(NumberNormalizer.name); + normalizers.push(NumberNormalizerUniqueKey); break; case TypeEnum.String: - normalizers.push(StringNormalizer.name); + normalizers.push(StringNormalizerUniqueKey); break; case TypeEnum.Date: - normalizers.push(DateNormalizer.name); + normalizers.push(DateNormalizerUniqueKey); break; } normalizers.forEach(normalizer => dataMappingLeaf.addNormalizer(normalizer)); diff --git a/packages/data-mapping-common/src/normalizers/date.normalizer.ts b/packages/data-mapping-common/src/normalizers/date.normalizer.ts index 8b34a363..8743f6a2 100644 --- a/packages/data-mapping-common/src/normalizers/date.normalizer.ts +++ b/packages/data-mapping-common/src/normalizers/date.normalizer.ts @@ -3,9 +3,11 @@ import {DateNormalizerOptions} from "../normalizer-options/date-normalizer.optio import {TypeEnum, TypeUtils} from "@pristine-ts/metadata"; import {BaseNormalizer} from "./base.normalizer"; +export const DateNormalizerUniqueKey = "PRISTINE_DATE_NORMALIZER"; + export class DateNormalizer extends BaseNormalizer implements DataNormalizerInterface { getUniqueKey(): string { - return "PRISTINE_DATE_NORMALIZER"; + return DateNormalizerUniqueKey; } normalize(source: any, options?: DateNormalizerOptions): Date | undefined { diff --git a/packages/data-mapping-common/src/normalizers/lowercase.normalizer.ts b/packages/data-mapping-common/src/normalizers/lowercase.normalizer.ts index 2b0443cf..472dd78d 100644 --- a/packages/data-mapping-common/src/normalizers/lowercase.normalizer.ts +++ b/packages/data-mapping-common/src/normalizers/lowercase.normalizer.ts @@ -4,9 +4,11 @@ import {NormalizerInvalidSourceTypeError} from "../errors/normalizer-invalid-sou import {LowercaseNormalizerOptions} from "../normalizer-options/lowercase-normalizer.options"; import {BaseNormalizer} from "./base.normalizer"; +export const LowercaseNormalizerUniqueKey = "PRISTINE_LOWERCASE_NORMALIZER"; + export class LowercaseNormalizer extends BaseNormalizer implements DataNormalizerInterface{ getUniqueKey(): DataNormalizerUniqueKey { - return "PRISTINE_LOWERCASE_NORMALIZER"; + return LowercaseNormalizerUniqueKey; } normalize(source: any, options?: LowercaseNormalizerOptions): string { diff --git a/packages/data-mapping-common/src/normalizers/number.normalizer.ts b/packages/data-mapping-common/src/normalizers/number.normalizer.ts index 86999835..277479c5 100644 --- a/packages/data-mapping-common/src/normalizers/number.normalizer.ts +++ b/packages/data-mapping-common/src/normalizers/number.normalizer.ts @@ -3,9 +3,11 @@ import {NumberNormalizerOptions} from "../normalizer-options/number-normalizer.o import {TypeEnum, TypeUtils} from "@pristine-ts/metadata"; import {BaseNormalizer} from "./base.normalizer"; +export const NumberNormalizerUniqueKey = "PRISTINE_NUMBER_NORMALIZER"; + export class NumberNormalizer extends BaseNormalizer implements DataNormalizerInterface { getUniqueKey(): string { - return "PRISTINE_NUMBER_NORMALIZER"; + return NumberNormalizerUniqueKey; } normalize(source: any, options?: NumberNormalizerOptions): number | undefined { diff --git a/packages/data-mapping-common/src/normalizers/string.normalizer.ts b/packages/data-mapping-common/src/normalizers/string.normalizer.ts index 6a8cbfb1..e6bf7df0 100644 --- a/packages/data-mapping-common/src/normalizers/string.normalizer.ts +++ b/packages/data-mapping-common/src/normalizers/string.normalizer.ts @@ -4,9 +4,11 @@ import {StringNormalizerOptions} from "../normalizer-options/string-normalizer.o import {format} from "date-fns"; import {BaseNormalizer} from "./base.normalizer"; +export const StringNormalizerUniqueKey = "PRISTINE_STRING_NORMALIZER"; + export class StringNormalizer extends BaseNormalizer implements DataNormalizerInterface { getUniqueKey(): string { - return "PRISTINE_STRING_NORMALIZER"; + return StringNormalizerUniqueKey; } normalize(source: any, options?: StringNormalizerOptions): string | undefined { From 180ae2b473e40f34af168fb6d09ccbd814507f19 Mon Sep 17 00:00:00 2001 From: Etienne Noel Date: Sun, 17 Nov 2024 23:17:20 -0800 Subject: [PATCH 2/4] - update. --- .../src/builders/auto-data-mapping.builder.ts | 6 +++--- .../src/mappers/data.mapper.spec.ts | 18 +++++++++--------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/packages/data-mapping-common/src/builders/auto-data-mapping.builder.ts b/packages/data-mapping-common/src/builders/auto-data-mapping.builder.ts index 8fa2243c..98f3b5ff 100644 --- a/packages/data-mapping-common/src/builders/auto-data-mapping.builder.ts +++ b/packages/data-mapping-common/src/builders/auto-data-mapping.builder.ts @@ -142,15 +142,15 @@ export class AutoDataMappingBuilder { // todo: Allow for options to be specified per attribute. We should probably add a decorator to can customize the normalizer. switch (propertyInformation.typeEnum) { case TypeEnum.Number: - normalizers.push(NumberNormalizer.name); + normalizers.push(NumberNormalizerUniqueKey); break; case TypeEnum.String: - normalizers.push(StringNormalizer.name); + normalizers.push(StringNormalizerUniqueKey); break; case TypeEnum.Date: - normalizers.push(DateNormalizer.name); + normalizers.push(DateNormalizerUniqueKey); break; } diff --git a/packages/data-mapping-common/src/mappers/data.mapper.spec.ts b/packages/data-mapping-common/src/mappers/data.mapper.spec.ts index fa185b9f..db026df7 100644 --- a/packages/data-mapping-common/src/mappers/data.mapper.spec.ts +++ b/packages/data-mapping-common/src/mappers/data.mapper.spec.ts @@ -1,4 +1,4 @@ -import {LowercaseNormalizer} from "../normalizers/lowercase.normalizer"; +import {LowercaseNormalizer, LowercaseNormalizerUniqueKey} from "../normalizers/lowercase.normalizer"; import {DataMappingInterceptorInterface} from "../interfaces/data-mapping-interceptor.interface"; import {DataMappingInterceptorUniqueKeyType} from "../types/data-mapping-interceptor-unique-key.type"; import {DataMappingInterceptorNotFoundError} from "../errors/data-mapping-interceptor-not-found.error"; @@ -59,11 +59,11 @@ describe("Data Mapper", () =>{ let dataMappingBuilder = new DataMappingBuilder(); dataMappingBuilder - .addNormalizer(LowercaseNormalizer.name) + .addNormalizer(LowercaseNormalizerUniqueKey) .add() .setSourceProperty("title") .setDestinationProperty("name") - .excludeNormalizer(LowercaseNormalizer.name) + .excludeNormalizer(LowercaseNormalizerUniqueKey) .end() .addNestingLevel() .setSourceProperty("nested") @@ -169,7 +169,7 @@ describe("Data Mapper", () =>{ .end() .add() .setSourceProperty("province") - .addNormalizer(LowercaseNormalizer.name) + .addNormalizer(LowercaseNormalizerUniqueKey) .setDestinationProperty("province") .end() .add() @@ -202,7 +202,7 @@ describe("Data Mapper", () =>{ .end() .add() .setSourceProperty("1") - .addNormalizer(LowercaseNormalizer.name) + .addNormalizer(LowercaseNormalizerUniqueKey) .setDestinationProperty("province") .end() .add() @@ -273,7 +273,7 @@ describe("Data Mapper", () =>{ .end() .add() .setSourceProperty("1") - .addNormalizer(LowercaseNormalizer.name) + .addNormalizer(LowercaseNormalizerUniqueKey) .setDestinationProperty("province") .end() .add() @@ -356,7 +356,7 @@ describe("Data Mapper", () =>{ .add() .setSourceProperty("title") .setDestinationProperty("name") - .addNormalizer(LowercaseNormalizer.name) + .addNormalizer(LowercaseNormalizerUniqueKey) .end() const destination = await dataMapper.map(dataMappingBuilder, source, Destination); @@ -385,7 +385,7 @@ describe("Data Mapper", () =>{ .add() .setSourceProperty("title") .setDestinationProperty("name") - .addNormalizer(LowercaseNormalizer.name) + .addNormalizer(LowercaseNormalizerUniqueKey) .end() const destination = await dataMapper.map(dataMappingBuilder, source, Destination); @@ -559,7 +559,7 @@ describe("Data Mapper", () =>{ const dataMapper = new DataMapper(new AutoDataMappingBuilder(), [new LowercaseNormalizer(), new DateNormalizer(), new StringNormalizer(), new NumberNormalizer()], []); - const mapped = await dataMapper.autoMap(source, Source) + const mapped = await dataMapper.autoMap(source, Source, new AutoDataMappingBuilderOptions({throwOnErrors: true})) expect(mapped).toBeInstanceOf(Source); }) From 3e14a9fa41f0f13f5a8cc4805e43252eea38dd6f Mon Sep 17 00:00:00 2001 From: Etienne Noel Date: Sun, 17 Nov 2024 23:21:01 -0800 Subject: [PATCH 3/4] - Fix the test. --- .../builders/auto-data-mapping.builder.spec.ts | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/packages/data-mapping-common/src/builders/auto-data-mapping.builder.spec.ts b/packages/data-mapping-common/src/builders/auto-data-mapping.builder.spec.ts index 79ea4cdd..9d2a99bd 100644 --- a/packages/data-mapping-common/src/builders/auto-data-mapping.builder.spec.ts +++ b/packages/data-mapping-common/src/builders/auto-data-mapping.builder.spec.ts @@ -2,9 +2,9 @@ import "reflect-metadata" import {AutoDataMappingBuilder} from "./auto-data-mapping.builder"; import {classMetadata, property} from "@pristine-ts/metadata"; import {DataMapper} from "../mappers/data.mapper"; -import {StringNormalizer} from "../normalizers/string.normalizer"; -import {NumberNormalizer} from "../normalizers/number.normalizer"; -import {DateNormalizer} from "../normalizers/date.normalizer"; +import {StringNormalizer, StringNormalizerUniqueKey} from "../normalizers/string.normalizer"; +import {NumberNormalizer, NumberNormalizerUniqueKey} from "../normalizers/number.normalizer"; +import {DateNormalizer, DateNormalizerUniqueKey} from "../normalizers/date.normalizer"; import {type} from "../decorators/type.decorator"; import {DataMappingNode} from "../nodes/data-mapping.node"; import {DataMappingLeaf} from "../nodes/data-mapping.leaf"; @@ -112,7 +112,7 @@ describe("Auto DataMappingBuilder", () => { const titleLeaf = dataMappingBuilder.nodes["title"] as DataMappingLeaf; expect(titleLeaf).toBeInstanceOf(DataMappingLeaf); expect(titleLeaf.normalizers).toHaveLength(1) - expect(titleLeaf.normalizers[0].key).toBe(StringNormalizer.name) + expect(titleLeaf.normalizers[0].key).toBe(StringNormalizerUniqueKey) const nestedNode = dataMappingBuilder.nodes["nested"] as DataMappingNode; expect(nestedNode).toBeInstanceOf(DataMappingNode); @@ -135,12 +135,12 @@ describe("Auto DataMappingBuilder", () => { const nameLeaf = headNode.nodes["name"] as DataMappingLeaf; expect(nameLeaf).toBeInstanceOf(DataMappingLeaf); expect(nameLeaf.normalizers).toHaveLength(1) - expect(nameLeaf.normalizers[0].key).toBe(StringNormalizer.name) + expect(nameLeaf.normalizers[0].key).toBe(StringNormalizerUniqueKey) const dateLeaf = dataMappingBuilder.nodes["date"] as DataMappingLeaf; expect(dateLeaf).toBeInstanceOf(DataMappingLeaf); expect(dateLeaf.normalizers).toHaveLength(1) - expect(dateLeaf.normalizers[0].key).toBe(DateNormalizer.name); + expect(dateLeaf.normalizers[0].key).toBe(DateNormalizerUniqueKey); const arrayNode = dataMappingBuilder.nodes["array"] as DataMappingNode; expect(arrayNode).toBeInstanceOf(DataMappingNode); @@ -149,12 +149,12 @@ describe("Auto DataMappingBuilder", () => { const rankLeaf = arrayNode.nodes["rank"] as DataMappingLeaf; expect(rankLeaf).toBeInstanceOf(DataMappingLeaf); expect(rankLeaf.normalizers).toHaveLength(1) - expect(rankLeaf.normalizers[0].key).toBe(NumberNormalizer.name); + expect(rankLeaf.normalizers[0].key).toBe(NumberNormalizerUniqueKey); const childrenLeaf = dataMappingBuilder.nodes["children"] as DataMappingLeaf; expect(childrenLeaf).toBeInstanceOf(DataMappingLeaf); expect(childrenLeaf.type).toBe(DataMappingNodeTypeEnum.ScalarArray); expect(childrenLeaf.normalizers).toHaveLength(1) - expect(childrenLeaf.normalizers[0].key).toBe(StringNormalizer.name); + expect(childrenLeaf.normalizers[0].key).toBe(StringNormalizerUniqueKey); }) }) \ No newline at end of file From 879ef746ec05dd9de94ab92526c3f62d5d473577 Mon Sep 17 00:00:00 2001 From: Etienne Noel Date: Sun, 17 Nov 2024 23:23:59 -0800 Subject: [PATCH 4/4] - Fix test --- .../src/interceptors/body-mapping.request-interceptor.spec.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/networking/src/interceptors/body-mapping.request-interceptor.spec.ts b/packages/networking/src/interceptors/body-mapping.request-interceptor.spec.ts index 6839e424..c85ec429 100644 --- a/packages/networking/src/interceptors/body-mapping.request-interceptor.spec.ts +++ b/packages/networking/src/interceptors/body-mapping.request-interceptor.spec.ts @@ -11,7 +11,7 @@ import {classMetadata, property} from "@pristine-ts/metadata"; import { AutoDataMappingBuilder, DataMapper, DataMappingBuilder, DateNormalizer, - LowercaseNormalizer, NumberNormalizer, + LowercaseNormalizer, LowercaseNormalizerUniqueKey, NumberNormalizer, StringNormalizer } from "@pristine-ts/data-mapping"; import {Route} from "../models/route"; @@ -120,7 +120,7 @@ describe("Body Mapping Request Interceptor", () => { .add() .setSourceProperty("title") .setDestinationProperty("name") - .addNormalizer(LowercaseNormalizer.name) + .addNormalizer(LowercaseNormalizerUniqueKey) .end() .add() .setSourceProperty("rank")