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 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { getFieldConfig, IPropFieldConfigInfo } from './fieldConfig.decorator';

type KeyValueDict = Record<string, unknown>;
const target: IFieldConfigClassInfo[] = [];

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;
}

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, @typescript-eslint/no-explicit-any
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,
};

target.push(classData);
return classCtr;
};
}

export function getFieldConfigClassInfo(className: string): IFieldConfigClassInfo {
return target.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;
}
Original file line number Diff line number Diff line change
@@ -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<string, unknown>;
const target = {};
const target: IGraphQLClassMapping[] = [];

export interface IGraphQLClass {
alias?: string;
Expand All @@ -19,22 +17,20 @@ export interface IGraphQLClassMapping {
export function graphqlClass(args?: IGraphQLClass): 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
// 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() ?? [];
classDataList.push(classData);
Reflect.defineMetadata(graphQLMetadataKey, classDataList, target);
target.push(classData);
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[] {
Expand Down
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;
}
}
Loading