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

feat(fieldconfig): decorator is added #76

Merged
merged 4 commits into from
Jun 17, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import 'reflect-metadata';
import { getFieldConfig, IPropFieldConfigInfo } from './fieldConfig.decorator';

const fieldConfigMetadataKey = Symbol('fieldconfigclass');
type KeyValueDict = Record<string, unknown>;
const target = {};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I should have thought about it earlier but since target is static and global it can be used to store the data directly without using reflect metadata.
it will make the code bit simpler to understand.

up to you if u want to change it, its ok either way by my standard (for this use case)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, reflection is removed, more clean code


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 <TFunction extends Function>(classCtr: TFunction): TFunction => {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-call
const classInstance = new (classCtr as any)();
cl-shaharshavit-rnd marked this conversation as resolved.
Show resolved Hide resolved
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;
}
32 changes: 32 additions & 0 deletions src/models/common/decorators/fieldConfig/fieldConfig.decorator.ts
Original file line number Diff line number Diff line change
@@ -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<T>(target: T, propertyKey: string): IFieldConfigInfo | undefined {
return Reflect.getMetadata(fieldConfigMetadataKey, target, propertyKey) as IFieldConfigInfo;
}
7 changes: 4 additions & 3 deletions src/models/common/index.ts
Original file line number Diff line number Diff line change
@@ -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';
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ export const TsTypes: Record<string, IDescribeTsType> = {
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 {
Expand Down
31 changes: 30 additions & 1 deletion src/models/layerMetadata/layer3DMetadata.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -651,6 +661,10 @@ export class Layer3DMetadata implements ILayer3DMetadata, IMetadataCommonModel {
return getPyCSWMapping<Layer3DMetadata>(new Layer3DMetadata(), prop);
}

public static getFieldConfig(prop: string): IFieldConfigInfo | undefined {
return getFieldConfig<Layer3DMetadata>(new Layer3DMetadata(), prop);
}

public static getPyCSWMappings(): IPropPYCSWMapping[] {
const ret = [];
const layer = new Layer3DMetadata();
Expand All @@ -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<Layer3DMetadata>(layer, prop);
if (fieldConfigMap) {
ret.push({
prop: prop,
...fieldConfigMap,
});
}
}
return ret;
}
}
86 changes: 85 additions & 1 deletion src/models/layerMetadata/layerMetadata.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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;

Expand All @@ -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;

Expand All @@ -86,6 +100,9 @@ export class LayerMetadata implements ILayerMetadata, IMetadataCommonModel {
mappingType: TsTypes.STRING,
})
@graphql()
@fieldConfig({
category: FieldCategory.MAIN,
})
//#endregion
public productId: string | undefined = 'UNKNOWN';

Expand All @@ -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;

Expand All @@ -130,6 +151,9 @@ export class LayerMetadata implements ILayerMetadata, IMetadataCommonModel {
@graphql({
nullable: true,
})
@fieldConfig({
category: FieldCategory.MAIN,
})
//#endregion
public productVersion: string | undefined = undefined;

Expand All @@ -151,6 +175,9 @@ export class LayerMetadata implements ILayerMetadata, IMetadataCommonModel {
mappingType: TsTypes.STRING,
})
@graphql()
@fieldConfig({
category: FieldCategory.MAIN,
})
//#endregion
public productType: string | undefined = undefined;

Expand Down Expand Up @@ -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;

Expand All @@ -201,6 +232,9 @@ export class LayerMetadata implements ILayerMetadata, IMetadataCommonModel {
@graphql({
nullable: true,
})
@fieldConfig({
category: FieldCategory.GEO_INFO,
})
//#endregion
public srsId: string | undefined = undefined;

Expand All @@ -224,6 +258,9 @@ export class LayerMetadata implements ILayerMetadata, IMetadataCommonModel {
@graphql({
nullable: true,
})
@fieldConfig({
category: FieldCategory.GEO_INFO,
})
//#endregion
public srsName: string | undefined = undefined;

Expand Down Expand Up @@ -270,6 +307,9 @@ export class LayerMetadata implements ILayerMetadata, IMetadataCommonModel {
@graphql({
nullable: true,
})
@fieldConfig({
category: FieldCategory.GENERAL,
})
//#endregion
public creationDate: Date | undefined = undefined;

Expand All @@ -292,6 +332,9 @@ export class LayerMetadata implements ILayerMetadata, IMetadataCommonModel {
@graphql({
nullable: true,
})
@fieldConfig({
category: FieldCategory.GENERAL,
})
//#endregion
public ingestionDate: Date | undefined = undefined;

Expand All @@ -318,6 +361,9 @@ export class LayerMetadata implements ILayerMetadata, IMetadataCommonModel {
@graphql({
nullable: true,
})
@fieldConfig({
category: FieldCategory.MAIN,
})
//#endregion
public updateDate: Date | undefined = undefined;

Expand All @@ -340,6 +386,9 @@ export class LayerMetadata implements ILayerMetadata, IMetadataCommonModel {
@graphql({
nullable: true,
})
@fieldConfig({
category: FieldCategory.GENERAL,
})
//#endregion
public sourceDateStart: Date | undefined = undefined;

Expand All @@ -362,6 +411,9 @@ export class LayerMetadata implements ILayerMetadata, IMetadataCommonModel {
@graphql({
nullable: true,
})
@fieldConfig({
category: FieldCategory.GENERAL,
})
//#endregion
public sourceDateEnd: Date | undefined = undefined;

Expand All @@ -388,6 +440,9 @@ export class LayerMetadata implements ILayerMetadata, IMetadataCommonModel {
@graphql({
nullable: true,
})
@fieldConfig({
category: FieldCategory.MAIN,
})
//#endregion
public resolution: number | undefined = undefined;

Expand All @@ -414,6 +469,9 @@ export class LayerMetadata implements ILayerMetadata, IMetadataCommonModel {
@graphql({
nullable: true,
})
@fieldConfig({
category: FieldCategory.GEO_INFO,
})
//#endregion
public accuracyCE90: number | undefined = undefined;

Expand Down Expand Up @@ -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;

Expand All @@ -466,6 +528,9 @@ export class LayerMetadata implements ILayerMetadata, IMetadataCommonModel {
@graphql({
nullable: true,
})
@fieldConfig({
category: FieldCategory.GENERAL,
})
//#endregion
public region: string | undefined = undefined;
//#endregion
Expand Down Expand Up @@ -588,6 +653,10 @@ export class LayerMetadata implements ILayerMetadata, IMetadataCommonModel {
return getCatalogDBMapping<LayerMetadata>(new LayerMetadata(), prop);
}

public static getFieldConfig(prop: string): IFieldConfigInfo | undefined {
return getFieldConfig<LayerMetadata>(new LayerMetadata(), prop);
}

public static getPyCSWMappings(): IPropPYCSWMapping[] {
const ret = [];
const layer = new LayerMetadata();
Expand Down Expand Up @@ -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<LayerMetadata>(layer, prop);
if (fieldConfigMap) {
ret.push({
prop: prop,
...fieldConfigMap,
});
}
}
return ret;
}
}
Loading