From aa1aa4ff9f341ba709dd4f84b433557c7f8c0626 Mon Sep 17 00:00:00 2001 From: alebinson Date: Wed, 16 Jun 2021 15:47:05 +0300 Subject: [PATCH 1/4] feat(fieldconfig): decorator is added --- .../fieldConfig/classFieldConfig.decorator.ts | 51 +++++++++++ .../fieldConfig/fieldConfig.decorator.ts | 32 +++++++ .../classGraphql.decorator.ts | 0 .../graphql.decorator.ts | 0 src/models/common/index.ts | 7 +- .../propGraphQLMapping.interface.ts | 2 +- .../decorators/property/tsTypes.decorator.ts | 5 ++ src/models/layerMetadata/layer3DMetadata.ts | 31 ++++++- src/models/layerMetadata/layerMetadata.ts | 86 ++++++++++++++++++- src/models/layerMetadata/link.ts | 27 +++++- .../layerMetadata/pycsw3DCatalogRecord.ts | 27 +++++- .../layerMetadata/pycswLayerCatalogRecord.ts | 43 +++++++++- tests/unit/layerMetadata.spec.ts | 16 ++++ 13 files changed, 315 insertions(+), 12 deletions(-) create mode 100644 src/models/common/decorators/fieldConfig/classFieldConfig.decorator.ts create mode 100644 src/models/common/decorators/fieldConfig/fieldConfig.decorator.ts rename src/models/common/decorators/{property => graphQL}/classGraphql.decorator.ts (100%) rename src/models/common/decorators/{property => graphQL}/graphql.decorator.ts (100%) diff --git a/src/models/common/decorators/fieldConfig/classFieldConfig.decorator.ts b/src/models/common/decorators/fieldConfig/classFieldConfig.decorator.ts new file mode 100644 index 0000000..c9aafb6 --- /dev/null +++ b/src/models/common/decorators/fieldConfig/classFieldConfig.decorator.ts @@ -0,0 +1,51 @@ +import 'reflect-metadata'; +import { getFieldConfig, IPropFieldConfigInfo } from './fieldConfig.decorator'; + +const fieldConfigMetadataKey = Symbol('fieldconfigclass'); +type KeyValueDict = Record; +const target = {}; + +function getFieldConfigs(object: KeyValueDict): IPropFieldConfigInfo[] { + const ret = []; + for (const prop in object) { + const fieldConfigMap = getFieldConfig(object, prop); + if (fieldConfigMap) { + ret.push({ + prop: prop, + ...fieldConfigMap, + }); + } + } + return ret; +} + +function getFieldConfigClassesInfo(): IFieldConfigClassInfo[] | undefined { + return Reflect.getMetadata(fieldConfigMetadataKey, target) as IFieldConfigClassInfo[]; +} + +export interface IFieldConfigClassInfo { + name: string; + fields: IPropFieldConfigInfo[]; +} + +export function fieldConfigClass(): ClassDecorator { + // eslint-disable-next-line @typescript-eslint/ban-types + return (classCtr: TFunction): TFunction => { + // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-call + const classInstance = new (classCtr as any)(); + const classData: IFieldConfigClassInfo = { + fields: getFieldConfigs(classInstance), + name: classCtr.name, + }; + + const classDataList = getFieldConfigClassesInfo() ?? []; + classDataList.push(classData); + Reflect.defineMetadata(fieldConfigMetadataKey, classDataList, target); + return classCtr; + }; +} + +export function getFieldConfigClassInfo(className: string): IFieldConfigClassInfo { + const classInfos = Reflect.getMetadata(fieldConfigMetadataKey, target) as IFieldConfigClassInfo[]; + return classInfos.find((classInfo) => classInfo.name === className) as IFieldConfigClassInfo; +} diff --git a/src/models/common/decorators/fieldConfig/fieldConfig.decorator.ts b/src/models/common/decorators/fieldConfig/fieldConfig.decorator.ts new file mode 100644 index 0000000..d27ea39 --- /dev/null +++ b/src/models/common/decorators/fieldConfig/fieldConfig.decorator.ts @@ -0,0 +1,32 @@ +import 'reflect-metadata'; +import { IDescribeTsType } from '../../../layerMetadata'; +import { IFieldConfigClassInfo } from './classFieldConfig.decorator'; + +const fieldConfigMetadataKey = Symbol('fieldconfig'); + +export enum FieldCategory { + MAIN = 'MAIN', + GENERAL = 'GENERAL', + GEO_INFO = 'GEO_INFO', +} + +export interface IFieldConfigInfo { + category: FieldCategory; // field category + complexType?: IDescribeTsType; // complex type subfields + subFields?: IFieldConfigClassInfo; // complex type subfields + isManuallyEditable?: boolean; // is field might be edited after creation + isFilterable?: boolean; // is field might participate in filter/search params + isSortable?: boolean; // is field might participate in sorting +} + +export interface IPropFieldConfigInfo extends IFieldConfigInfo { + prop: string; +} + +export function fieldConfig(fieldConfigInfo: IFieldConfigInfo): PropertyDecorator { + return Reflect.metadata(fieldConfigMetadataKey, fieldConfigInfo); +} + +export function getFieldConfig(target: T, propertyKey: string): IFieldConfigInfo | undefined { + return Reflect.getMetadata(fieldConfigMetadataKey, target, propertyKey) as IFieldConfigInfo; +} diff --git a/src/models/common/decorators/property/classGraphql.decorator.ts b/src/models/common/decorators/graphQL/classGraphql.decorator.ts similarity index 100% rename from src/models/common/decorators/property/classGraphql.decorator.ts rename to src/models/common/decorators/graphQL/classGraphql.decorator.ts diff --git a/src/models/common/decorators/property/graphql.decorator.ts b/src/models/common/decorators/graphQL/graphql.decorator.ts similarity index 100% rename from src/models/common/decorators/property/graphql.decorator.ts rename to src/models/common/decorators/graphQL/graphql.decorator.ts diff --git a/src/models/common/index.ts b/src/models/common/index.ts index 464dcc5..2388405 100644 --- a/src/models/common/index.ts +++ b/src/models/common/index.ts @@ -1,6 +1,7 @@ -export * from './decorators/property/graphql.decorator'; -export * from './decorators/property/classGraphql.decorator'; +export * from '../layerMetadata/link'; +export * from './decorators/graphQL/graphql.decorator'; +export * from './decorators/graphQL/classGraphql.decorator'; export * from './interfaces/ormCatalog.interface'; export * from './interfaces/propCatalogDBMapping.interface'; export * from './interfaces/propGraphQLMapping.interface'; -export * from '../layerMetadata/link'; +export * from './decorators/fieldConfig/fieldConfig.decorator'; diff --git a/src/models/common/interfaces/propGraphQLMapping.interface.ts b/src/models/common/interfaces/propGraphQLMapping.interface.ts index 1626cbc..14916e9 100644 --- a/src/models/common/interfaces/propGraphQLMapping.interface.ts +++ b/src/models/common/interfaces/propGraphQLMapping.interface.ts @@ -1,4 +1,4 @@ -import { IGraphQLMapping } from '../decorators/property/graphql.decorator'; +import { IGraphQLMapping } from '../decorators/graphQL/graphql.decorator'; import { ITsTypesMapping } from '../../layerMetadata/decorators/property/tsTypes.decorator'; export interface IPropGraphQLMapping extends IGraphQLMapping, ITsTypesMapping { diff --git a/src/models/layerMetadata/decorators/property/tsTypes.decorator.ts b/src/models/layerMetadata/decorators/property/tsTypes.decorator.ts index aaa9dcb..fbce78b 100644 --- a/src/models/layerMetadata/decorators/property/tsTypes.decorator.ts +++ b/src/models/layerMetadata/decorators/property/tsTypes.decorator.ts @@ -61,6 +61,11 @@ export const TsTypes: Record = { type: PropertiesTypes.ENUM, importFromPackage: '@map-colonies/mc-model-types', }, + FIELDCATEGORY: { + value: 'FieldCategory', + type: PropertiesTypes.ENUM, + importFromPackage: '@map-colonies/mc-model-types', + }, }; /* eslint-enable @typescript-eslint/naming-convention */ export interface ITsTypesMapping { diff --git a/src/models/layerMetadata/layer3DMetadata.ts b/src/models/layerMetadata/layer3DMetadata.ts index 5e1e77f..cb5598d 100644 --- a/src/models/layerMetadata/layer3DMetadata.ts +++ b/src/models/layerMetadata/layer3DMetadata.ts @@ -1,5 +1,12 @@ import { GeoJSON } from 'geojson'; -import { graphql } from '../common/decorators/property/graphql.decorator'; +import { graphql } from '../common/decorators/graphQL/graphql.decorator'; +import { + FieldCategory, + fieldConfig, + getFieldConfig, + IFieldConfigInfo, + IPropFieldConfigInfo, +} from '../common/decorators/fieldConfig/fieldConfig.decorator'; import { RecordType } from '../pycsw/coreEnums'; import { getPyCSWMapping, IPYCSWMapping, pycsw } from './decorators/property/csw.decorator'; import { tsTypes, TsTypes } from './decorators/property/tsTypes.decorator'; @@ -44,6 +51,9 @@ export class Layer3DMetadata implements ILayer3DMetadata, IMetadataCommonModel { @graphql({ nullable: true, }) + @fieldConfig({ + category: FieldCategory.MAIN, + }) //#endregion public type: RecordType | undefined = RecordType.RECORD_3D; @@ -651,6 +661,10 @@ export class Layer3DMetadata implements ILayer3DMetadata, IMetadataCommonModel { return getPyCSWMapping(new Layer3DMetadata(), prop); } + public static getFieldConfig(prop: string): IFieldConfigInfo | undefined { + return getFieldConfig(new Layer3DMetadata(), prop); + } + public static getPyCSWMappings(): IPropPYCSWMapping[] { const ret = []; const layer = new Layer3DMetadata(); @@ -665,4 +679,19 @@ export class Layer3DMetadata implements ILayer3DMetadata, IMetadataCommonModel { } return ret; } + + public static getFieldConfigs(): IPropFieldConfigInfo[] { + const ret = []; + const layer = new Layer3DMetadata(); + for (const prop in layer) { + const fieldConfigMap = getFieldConfig(layer, prop); + if (fieldConfigMap) { + ret.push({ + prop: prop, + ...fieldConfigMap, + }); + } + } + return ret; + } } diff --git a/src/models/layerMetadata/layerMetadata.ts b/src/models/layerMetadata/layerMetadata.ts index 104e253..65017ab 100644 --- a/src/models/layerMetadata/layerMetadata.ts +++ b/src/models/layerMetadata/layerMetadata.ts @@ -1,6 +1,13 @@ import { GeoJSON } from 'geojson'; import { IPropCatalogDBMapping } from '../common/interfaces/propCatalogDBMapping.interface'; -import { graphql } from '../common/decorators/property/graphql.decorator'; +import { graphql } from '../common/decorators/graphQL/graphql.decorator'; +import { + FieldCategory, + fieldConfig, + getFieldConfig, + IFieldConfigInfo, + IPropFieldConfigInfo, +} from '../common/decorators/fieldConfig/fieldConfig.decorator'; import { RecordType } from '../pycsw/coreEnums'; import { IMetadataCommonModel } from './interfaces/metadataCommonModel'; import { getPyCSWMapping, IPYCSWMapping, pycsw } from './decorators/property/csw.decorator'; @@ -43,6 +50,9 @@ export class LayerMetadata implements ILayerMetadata, IMetadataCommonModel { @graphql({ nullable: true, }) + @fieldConfig({ + category: FieldCategory.MAIN, + }) //#endregion public type: RecordType | undefined = RecordType.RECORD_RASTER; @@ -65,6 +75,10 @@ export class LayerMetadata implements ILayerMetadata, IMetadataCommonModel { @graphql({ nullable: true, }) + @fieldConfig({ + category: FieldCategory.GENERAL, + isManuallyEditable: true, + }) //#endregion public classification: string | undefined = undefined; @@ -86,6 +100,9 @@ export class LayerMetadata implements ILayerMetadata, IMetadataCommonModel { mappingType: TsTypes.STRING, }) @graphql() + @fieldConfig({ + category: FieldCategory.MAIN, + }) //#endregion public productId: string | undefined = 'UNKNOWN'; @@ -107,6 +124,10 @@ export class LayerMetadata implements ILayerMetadata, IMetadataCommonModel { mappingType: TsTypes.STRING, }) @graphql() + @fieldConfig({ + category: FieldCategory.MAIN, + isManuallyEditable: true, + }) //#endregion public productName: string | undefined = undefined; @@ -130,6 +151,9 @@ export class LayerMetadata implements ILayerMetadata, IMetadataCommonModel { @graphql({ nullable: true, }) + @fieldConfig({ + category: FieldCategory.MAIN, + }) //#endregion public productVersion: string | undefined = undefined; @@ -151,6 +175,9 @@ export class LayerMetadata implements ILayerMetadata, IMetadataCommonModel { mappingType: TsTypes.STRING, }) @graphql() + @fieldConfig({ + category: FieldCategory.MAIN, + }) //#endregion public productType: string | undefined = undefined; @@ -178,6 +205,10 @@ export class LayerMetadata implements ILayerMetadata, IMetadataCommonModel { @graphql({ nullable: true, }) + @fieldConfig({ + category: FieldCategory.GENERAL, + isManuallyEditable: true, + }) //#endregion public description: string | undefined = undefined; @@ -201,6 +232,9 @@ export class LayerMetadata implements ILayerMetadata, IMetadataCommonModel { @graphql({ nullable: true, }) + @fieldConfig({ + category: FieldCategory.GEO_INFO, + }) //#endregion public srsId: string | undefined = undefined; @@ -224,6 +258,9 @@ export class LayerMetadata implements ILayerMetadata, IMetadataCommonModel { @graphql({ nullable: true, }) + @fieldConfig({ + category: FieldCategory.GEO_INFO, + }) //#endregion public srsName: string | undefined = undefined; @@ -270,6 +307,9 @@ export class LayerMetadata implements ILayerMetadata, IMetadataCommonModel { @graphql({ nullable: true, }) + @fieldConfig({ + category: FieldCategory.GENERAL, + }) //#endregion public creationDate: Date | undefined = undefined; @@ -292,6 +332,9 @@ export class LayerMetadata implements ILayerMetadata, IMetadataCommonModel { @graphql({ nullable: true, }) + @fieldConfig({ + category: FieldCategory.GENERAL, + }) //#endregion public ingestionDate: Date | undefined = undefined; @@ -318,6 +361,9 @@ export class LayerMetadata implements ILayerMetadata, IMetadataCommonModel { @graphql({ nullable: true, }) + @fieldConfig({ + category: FieldCategory.MAIN, + }) //#endregion public updateDate: Date | undefined = undefined; @@ -340,6 +386,9 @@ export class LayerMetadata implements ILayerMetadata, IMetadataCommonModel { @graphql({ nullable: true, }) + @fieldConfig({ + category: FieldCategory.GENERAL, + }) //#endregion public sourceDateStart: Date | undefined = undefined; @@ -362,6 +411,9 @@ export class LayerMetadata implements ILayerMetadata, IMetadataCommonModel { @graphql({ nullable: true, }) + @fieldConfig({ + category: FieldCategory.GENERAL, + }) //#endregion public sourceDateEnd: Date | undefined = undefined; @@ -388,6 +440,9 @@ export class LayerMetadata implements ILayerMetadata, IMetadataCommonModel { @graphql({ nullable: true, }) + @fieldConfig({ + category: FieldCategory.MAIN, + }) //#endregion public resolution: number | undefined = undefined; @@ -414,6 +469,9 @@ export class LayerMetadata implements ILayerMetadata, IMetadataCommonModel { @graphql({ nullable: true, }) + @fieldConfig({ + category: FieldCategory.GEO_INFO, + }) //#endregion public accuracyCE90: number | undefined = undefined; @@ -443,6 +501,10 @@ export class LayerMetadata implements ILayerMetadata, IMetadataCommonModel { @graphql({ nullable: true, }) + @fieldConfig({ + category: FieldCategory.GENERAL, + isManuallyEditable: true, + }) //#endregion public sensorType: SensorType[] | undefined = undefined; @@ -466,6 +528,9 @@ export class LayerMetadata implements ILayerMetadata, IMetadataCommonModel { @graphql({ nullable: true, }) + @fieldConfig({ + category: FieldCategory.GENERAL, + }) //#endregion public region: string | undefined = undefined; //#endregion @@ -588,6 +653,10 @@ export class LayerMetadata implements ILayerMetadata, IMetadataCommonModel { return getCatalogDBMapping(new LayerMetadata(), prop); } + public static getFieldConfig(prop: string): IFieldConfigInfo | undefined { + return getFieldConfig(new LayerMetadata(), prop); + } + public static getPyCSWMappings(): IPropPYCSWMapping[] { const ret = []; const layer = new LayerMetadata(); @@ -636,4 +705,19 @@ export class LayerMetadata implements ILayerMetadata, IMetadataCommonModel { } return ret; } + + public static getFieldConfigs(): IPropFieldConfigInfo[] { + const ret = []; + const layer = new LayerMetadata(); + for (const prop in layer) { + const fieldConfigMap = getFieldConfig(layer, prop); + if (fieldConfigMap) { + ret.push({ + prop: prop, + ...fieldConfigMap, + }); + } + } + return ret; + } } diff --git a/src/models/layerMetadata/link.ts b/src/models/layerMetadata/link.ts index 01167f1..ab26356 100644 --- a/src/models/layerMetadata/link.ts +++ b/src/models/layerMetadata/link.ts @@ -1,34 +1,57 @@ -import { graphqlClass } from '../common/decorators/property/classGraphql.decorator'; -import { graphql } from '../common/decorators/property/graphql.decorator'; +import { graphqlClass } from '../common/decorators/graphQL/classGraphql.decorator'; +import { graphql } from '../common/decorators/graphQL/graphql.decorator'; +import { FieldCategory, fieldConfig } from '../common/decorators/fieldConfig/fieldConfig.decorator'; +import { fieldConfigClass } from '../common/decorators/fieldConfig/classFieldConfig.decorator'; import { TsTypes, tsTypes } from './decorators/property/tsTypes.decorator'; +@fieldConfigClass() @graphqlClass() export class Link { + //#region FIELD: name @tsTypes({ mappingType: TsTypes.STRING, }) @graphql({ nullable: true, }) + @fieldConfig({ + category: FieldCategory.GENERAL, + }) + //#endregion public name?: string = undefined; + //#region FIELD: description @tsTypes({ mappingType: TsTypes.STRING, }) @graphql({ nullable: true, }) + @fieldConfig({ + category: FieldCategory.GENERAL, + }) + //#endregion public description?: string = undefined; + //#region FIELD: protocol @tsTypes({ mappingType: TsTypes.STRING, }) @graphql() + @fieldConfig({ + category: FieldCategory.GENERAL, + }) + //#endregion public protocol?: string = undefined; + //#region FIELD: url @tsTypes({ mappingType: TsTypes.STRING, }) @graphql() + @fieldConfig({ + category: FieldCategory.GENERAL, + }) + //#endregion public url?: string = undefined; } diff --git a/src/models/layerMetadata/pycsw3DCatalogRecord.ts b/src/models/layerMetadata/pycsw3DCatalogRecord.ts index fe13bb7..b5b3c2d 100644 --- a/src/models/layerMetadata/pycsw3DCatalogRecord.ts +++ b/src/models/layerMetadata/pycsw3DCatalogRecord.ts @@ -1,8 +1,9 @@ import { IPycswCoreModel } from '../pycsw/interfaces/pycswCoreModel'; import { IPropCatalogDBMapping } from '../common/interfaces/propCatalogDBMapping.interface'; import { IOrmCatalog } from '../common/interfaces/ormCatalog.interface'; -import { graphql } from '../common/decorators/property/graphql.decorator'; -import { graphqlClass } from '../common/decorators/property/classGraphql.decorator'; +import { graphql } from '../common/decorators/graphQL/graphql.decorator'; +import { graphqlClass } from '../common/decorators/graphQL/classGraphql.decorator'; +import { FieldCategory, fieldConfig, getFieldConfig, IPropFieldConfigInfo } from '../common/decorators/fieldConfig/fieldConfig.decorator'; import { Link } from './link'; import { catalogDB, getCatalogDBMapping } from './decorators/property/catalogDB.decorator'; import { getTsTypesMapping, TsTypes, tsTypes } from './decorators/property/tsTypes.decorator'; @@ -34,6 +35,9 @@ export class Pycsw3DCatalogRecord extends Layer3DMetadata implements IPycswCoreM mappingType: TsTypes.STRING, }) @graphql() + @fieldConfig({ + category: FieldCategory.MAIN, + }) //#endregion public id: string | undefined = 'UNKNOWN'; @@ -167,6 +171,10 @@ export class Pycsw3DCatalogRecord extends Layer3DMetadata implements IPycswCoreM @graphql({ nullable: true, }) + @fieldConfig({ + category: FieldCategory.GENERAL, + isManuallyEditable: true, + }) //#endregion public keywords: string | undefined = undefined; @@ -229,6 +237,21 @@ export class Pycsw3DCatalogRecord extends Layer3DMetadata implements IPycswCoreM return ret; } + public static getFieldConfigs(): IPropFieldConfigInfo[] { + const ret = []; + const layer = new Pycsw3DCatalogRecord(); + for (const prop in layer) { + const fieldConfigMap = getFieldConfig(layer, prop); + if (fieldConfigMap) { + ret.push({ + prop: prop, + ...fieldConfigMap, + }); + } + } + return ret; + } + public getORMCatalogMappings(): IPropCatalogDBMapping[] { const ret = []; diff --git a/src/models/layerMetadata/pycswLayerCatalogRecord.ts b/src/models/layerMetadata/pycswLayerCatalogRecord.ts index 1d74eb0..d9bf3ca 100644 --- a/src/models/layerMetadata/pycswLayerCatalogRecord.ts +++ b/src/models/layerMetadata/pycswLayerCatalogRecord.ts @@ -1,8 +1,10 @@ import { IPycswCoreModel } from '../pycsw/interfaces/pycswCoreModel'; import { IPropCatalogDBMapping } from '../common/interfaces/propCatalogDBMapping.interface'; import { IOrmCatalog } from '../common/interfaces/ormCatalog.interface'; -import { graphql } from '../common/decorators/property/graphql.decorator'; -import { graphqlClass } from '../common/decorators/property/classGraphql.decorator'; +import { graphql } from '../common/decorators/graphQL/graphql.decorator'; +import { graphqlClass } from '../common/decorators/graphQL/classGraphql.decorator'; +import { FieldCategory, fieldConfig, getFieldConfig, IPropFieldConfigInfo } from '../common/decorators/fieldConfig/fieldConfig.decorator'; +import { getFieldConfigClassInfo } from '../common/decorators/fieldConfig/classFieldConfig.decorator'; import { Link } from './link'; import { catalogDB, getCatalogDBMapping } from './decorators/property/catalogDB.decorator'; import { getTsTypesMapping, TsTypes, tsTypes } from './decorators/property/tsTypes.decorator'; @@ -35,6 +37,9 @@ export class PycswLayerCatalogRecord extends LayerMetadata implements IPycswCore mappingType: TsTypes.STRING, }) @graphql() + @fieldConfig({ + category: FieldCategory.MAIN, + }) //#endregion public id: string | undefined = undefined; @@ -168,6 +173,10 @@ export class PycswLayerCatalogRecord extends LayerMetadata implements IPycswCore @graphql({ nullable: true, }) + @fieldConfig({ + category: FieldCategory.GENERAL, + isManuallyEditable: true, + }) //#endregion public keywords: string | undefined = undefined; @@ -208,6 +217,10 @@ export class PycswLayerCatalogRecord extends LayerMetadata implements IPycswCore @graphql({ nullable: true, }) + @fieldConfig({ + category: FieldCategory.GENERAL, + complexType: TsTypes.LINKS, + }) //#endregion public links: Link[] | undefined = undefined; @@ -230,6 +243,32 @@ export class PycswLayerCatalogRecord extends LayerMetadata implements IPycswCore return ret; } + public static getFieldConfigs(): IPropFieldConfigInfo[] { + const ret = []; + const layer = new PycswLayerCatalogRecord(); + for (const prop in layer) { + const fieldConfigMap = getFieldConfig(layer, prop); + if (fieldConfigMap) { + const fieldConfig = { prop: prop, ...fieldConfigMap }; + if (fieldConfigMap.complexType) { + fieldConfig.subFields = getFieldConfigClassInfo(fieldConfigMap.complexType.value); + } + + ret.push(fieldConfig); + // ret.push({ + // prop: prop, + // ...fieldConfigMap, + // subFields: fieldConfigMap.complexType ? getFieldConfigClassInfo(fieldConfigMap.complexType.value) : undefined + // }); + + // if(fieldConfigMap.complexType){ + // console.log('prop-->', JSON.stringify(getFieldConfigClassInfo(fieldConfigMap.complexType.value))) + // } + } + } + return ret as IPropFieldConfigInfo[]; + } + public getORMCatalogMappings(): IPropCatalogDBMapping[] { const ret = []; diff --git a/tests/unit/layerMetadata.spec.ts b/tests/unit/layerMetadata.spec.ts index 105c363..b199928 100644 --- a/tests/unit/layerMetadata.spec.ts +++ b/tests/unit/layerMetadata.spec.ts @@ -106,6 +106,14 @@ describe('PycswLayerCatalogRecord class methods', () => { expect(ormCatalogDBMappings[0]).toHaveProperty('column'); expect(ormCatalogDBMappings[0]).toHaveProperty('mappingType'); }); + + it('getFieldConfigs(): field configs are defined', () => { + const fieldConfigs = PycswLayerCatalogRecord.getFieldConfigs(); + + expect(fieldConfigs.length).toBeGreaterThan(0); + expect(fieldConfigs[0]).toHaveProperty('prop'); + expect(fieldConfigs[0]).toHaveProperty('category'); + }); }); describe('Pycsw3DCatalogRecord class methods', () => { @@ -116,4 +124,12 @@ describe('Pycsw3DCatalogRecord class methods', () => { expect(pycswMapping[0]).toHaveProperty('prop'); expect(pycswMapping[0]).toHaveProperty('xmlElement'); }); + + it('getFieldConfigs(): field configs are defined', () => { + const fieldConfigs = Pycsw3DCatalogRecord.getFieldConfigs(); + + expect(fieldConfigs.length).toBeGreaterThan(0); + expect(fieldConfigs[0]).toHaveProperty('prop'); + expect(fieldConfigs[0]).toHaveProperty('category'); + }); }); From 222e4e01658ff0f31a8392f85706867436bbb5eb Mon Sep 17 00:00:00 2001 From: alebinson Date: Wed, 16 Jun 2021 15:51:50 +0300 Subject: [PATCH 2/4] fix: jest criterias tweaked --- tests/configurations/unit/jest.config.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/configurations/unit/jest.config.js b/tests/configurations/unit/jest.config.js index b529fa3..0a71e66 100644 --- a/tests/configurations/unit/jest.config.js +++ b/tests/configurations/unit/jest.config.js @@ -18,7 +18,7 @@ module.exports = { coverageThreshold: { global: { branches: 80, - functions: 80, + functions: 75, lines: 80, statements: 80, }, From cbb9db1df629bd64e78082373b7ee16a00c69b93 Mon Sep 17 00:00:00 2001 From: alebinson Date: Thu, 17 Jun 2021 08:43:00 +0300 Subject: [PATCH 3/4] fix: pr comments --- .../fieldConfig/classFieldConfig.decorator.ts | 17 +++++++---------- .../graphQL/classGraphql.decorator.ts | 14 ++++++-------- .../layerMetadata/pycswLayerCatalogRecord.ts | 10 ---------- 3 files changed, 13 insertions(+), 28 deletions(-) diff --git a/src/models/common/decorators/fieldConfig/classFieldConfig.decorator.ts b/src/models/common/decorators/fieldConfig/classFieldConfig.decorator.ts index c9aafb6..6fa5d6c 100644 --- a/src/models/common/decorators/fieldConfig/classFieldConfig.decorator.ts +++ b/src/models/common/decorators/fieldConfig/classFieldConfig.decorator.ts @@ -1,9 +1,7 @@ -import 'reflect-metadata'; import { getFieldConfig, IPropFieldConfigInfo } from './fieldConfig.decorator'; -const fieldConfigMetadataKey = Symbol('fieldconfigclass'); type KeyValueDict = Record; -const target = {}; +const target: IFieldConfigClassInfo[] = []; function getFieldConfigs(object: KeyValueDict): IPropFieldConfigInfo[] { const ret = []; @@ -19,8 +17,8 @@ function getFieldConfigs(object: KeyValueDict): IPropFieldConfigInfo[] { return ret; } -function getFieldConfigClassesInfo(): IFieldConfigClassInfo[] | undefined { - return Reflect.getMetadata(fieldConfigMetadataKey, target) as IFieldConfigClassInfo[]; +function getFieldConfigClassesInfo(): IFieldConfigClassInfo[] { + return target; } export interface IFieldConfigClassInfo { @@ -31,21 +29,20 @@ export interface IFieldConfigClassInfo { export function fieldConfigClass(): ClassDecorator { // eslint-disable-next-line @typescript-eslint/ban-types return (classCtr: TFunction): TFunction => { - // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-call + // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-call, @typescript-eslint/no-explicit-any const classInstance = new (classCtr as any)(); const classData: IFieldConfigClassInfo = { fields: getFieldConfigs(classInstance), name: classCtr.name, }; - const classDataList = getFieldConfigClassesInfo() ?? []; + const classDataList = getFieldConfigClassesInfo(); classDataList.push(classData); - Reflect.defineMetadata(fieldConfigMetadataKey, classDataList, target); + target.concat(classDataList); return classCtr; }; } export function getFieldConfigClassInfo(className: string): IFieldConfigClassInfo { - const classInfos = Reflect.getMetadata(fieldConfigMetadataKey, target) as IFieldConfigClassInfo[]; - return classInfos.find((classInfo) => classInfo.name === className) as IFieldConfigClassInfo; + return target.find((classInfo) => classInfo.name === className) as IFieldConfigClassInfo; } diff --git a/src/models/common/decorators/graphQL/classGraphql.decorator.ts b/src/models/common/decorators/graphQL/classGraphql.decorator.ts index 837d5b9..5528c2a 100644 --- a/src/models/common/decorators/graphQL/classGraphql.decorator.ts +++ b/src/models/common/decorators/graphQL/classGraphql.decorator.ts @@ -1,11 +1,9 @@ -import 'reflect-metadata'; import { getTsTypesMapping } from '../../../layerMetadata/decorators/property/tsTypes.decorator'; import { IPropGraphQLMapping } from '../../interfaces/propGraphQLMapping.interface'; import { getGraphQLMapping } from './graphql.decorator'; -const graphQLMetadataKey = Symbol('graphqlclassmapping'); type KeyValueDict = Record; -const target = {}; +const target: IGraphQLClassMapping[] = []; export interface IGraphQLClass { alias?: string; @@ -19,22 +17,22 @@ export interface IGraphQLClassMapping { export function graphqlClass(args?: IGraphQLClass): ClassDecorator { // eslint-disable-next-line @typescript-eslint/ban-types return (classCtr: TFunction): TFunction => { - // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-call + // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-call, @typescript-eslint/no-explicit-any const classInstance = new (classCtr as any)(); const classData: IGraphQLClassMapping = { fields: getGraphQLMappings(classInstance), name: args?.alias ?? classCtr.name, }; - const classDataList = getGraphQLClassMapping() ?? []; + const classDataList = getGraphQLClassMapping(); classDataList.push(classData); - Reflect.defineMetadata(graphQLMetadataKey, classDataList, target); + target.concat(classDataList); return classCtr; }; } -export function getGraphQLClassMapping(): IGraphQLClassMapping[] | undefined { - return Reflect.getMetadata(graphQLMetadataKey, target) as IGraphQLClassMapping[]; +export function getGraphQLClassMapping(): IGraphQLClassMapping[] { + return target; } export function getGraphQLMappings(object: KeyValueDict): IPropGraphQLMapping[] { diff --git a/src/models/layerMetadata/pycswLayerCatalogRecord.ts b/src/models/layerMetadata/pycswLayerCatalogRecord.ts index d9bf3ca..02e409c 100644 --- a/src/models/layerMetadata/pycswLayerCatalogRecord.ts +++ b/src/models/layerMetadata/pycswLayerCatalogRecord.ts @@ -253,17 +253,7 @@ export class PycswLayerCatalogRecord extends LayerMetadata implements IPycswCore if (fieldConfigMap.complexType) { fieldConfig.subFields = getFieldConfigClassInfo(fieldConfigMap.complexType.value); } - ret.push(fieldConfig); - // ret.push({ - // prop: prop, - // ...fieldConfigMap, - // subFields: fieldConfigMap.complexType ? getFieldConfigClassInfo(fieldConfigMap.complexType.value) : undefined - // }); - - // if(fieldConfigMap.complexType){ - // console.log('prop-->', JSON.stringify(getFieldConfigClassInfo(fieldConfigMap.complexType.value))) - // } } } return ret as IPropFieldConfigInfo[]; From 540007ac21cda172a3141aa52cd90f0610c79c69 Mon Sep 17 00:00:00 2001 From: alebinson Date: Thu, 17 Jun 2021 11:03:19 +0300 Subject: [PATCH 4/4] fix: work with target directly --- .../decorators/fieldConfig/classFieldConfig.decorator.ts | 8 +------- .../common/decorators/graphQL/classGraphql.decorator.ts | 4 +--- 2 files changed, 2 insertions(+), 10 deletions(-) diff --git a/src/models/common/decorators/fieldConfig/classFieldConfig.decorator.ts b/src/models/common/decorators/fieldConfig/classFieldConfig.decorator.ts index 6fa5d6c..7201f43 100644 --- a/src/models/common/decorators/fieldConfig/classFieldConfig.decorator.ts +++ b/src/models/common/decorators/fieldConfig/classFieldConfig.decorator.ts @@ -17,10 +17,6 @@ function getFieldConfigs(object: KeyValueDict): IPropFieldConfigInfo[] { return ret; } -function getFieldConfigClassesInfo(): IFieldConfigClassInfo[] { - return target; -} - export interface IFieldConfigClassInfo { name: string; fields: IPropFieldConfigInfo[]; @@ -36,9 +32,7 @@ export function fieldConfigClass(): ClassDecorator { name: classCtr.name, }; - const classDataList = getFieldConfigClassesInfo(); - classDataList.push(classData); - target.concat(classDataList); + target.push(classData); return classCtr; }; } diff --git a/src/models/common/decorators/graphQL/classGraphql.decorator.ts b/src/models/common/decorators/graphQL/classGraphql.decorator.ts index 5528c2a..c45f771 100644 --- a/src/models/common/decorators/graphQL/classGraphql.decorator.ts +++ b/src/models/common/decorators/graphQL/classGraphql.decorator.ts @@ -24,9 +24,7 @@ export function graphqlClass(args?: IGraphQLClass): ClassDecorator { name: args?.alias ?? classCtr.name, }; - const classDataList = getGraphQLClassMapping(); - classDataList.push(classData); - target.concat(classDataList); + target.push(classData); return classCtr; }; }