Skip to content

Commit

Permalink
Rename types (#49)
Browse files Browse the repository at this point in the history
* rename ClassLike => Class;
rename ClassFn => ClassResolver;

* rename `association` terms to `reference`;
  • Loading branch information
Chnapy authored Aug 23, 2019
1 parent 0710683 commit f7e8f81
Show file tree
Hide file tree
Showing 27 changed files with 461 additions and 461 deletions.
20 changes: 10 additions & 10 deletions src/decorators/JSONArray.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { JSONEntityArray, JSONTypeName } from '../types/JSONTypes';
import { ClassLike } from '../types/ClassTypes';
import { Class } from '../types/ClassTypes';
import { DecoratorEngine } from '../engine/DecoratorEngine';
import AssociationEngine from '../engine/AssociationEngine';
import { ClassFn } from '../types/AssociationTypes';
import ReferenceEngine from '../engine/ReferenceEngine';
import { ClassResolver } from '../types/ReferenceTypes';

// array schema props | item class fn | item json type
type JSONArrayValue = Omit<Partial<JSONEntityArray>, 'type'> | ClassFn | JSONTypeName;
// array schema props | item class resolver | item json type
type JSONArrayValue = Omit<Partial<JSONEntityArray>, 'type'> | ClassResolver | JSONTypeName;

/**
* Decorator for JSON array attribute.
Expand All @@ -14,19 +14,19 @@ export function JSONArray(value: JSONArrayValue): Function {
// Related class
if (typeof value === 'function') {
return (
prototype: ClassLike['prototype'],
key: keyof ClassLike['prototype'] & string,
prototype: Class['prototype'],
key: keyof Class['prototype'] & string,
descriptor?: PropertyDescriptor
): void => {
AssociationEngine.addAssociation(prototype, key, 'items', value);
ReferenceEngine.addReference(prototype, key, 'items', value);
};
}

// Given type
if (typeof value === 'string') {
return (
prototype: ClassLike['prototype'],
key: keyof ClassLike['prototype'] & string,
prototype: Class['prototype'],
key: keyof Class['prototype'] & string,
descriptor?: PropertyDescriptor
): void => {
DecoratorEngine.defineProperties([prototype, key, descriptor], {
Expand Down
16 changes: 8 additions & 8 deletions src/decorators/JSONObject.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { JSONEntityObject } from '../types/JSONTypes';
import { ClassLike, DecoratorClassProps } from '../types/ClassTypes';
import { Class, DecoratorClassProps } from '../types/ClassTypes';
import { DecoratorEngine } from '../engine/DecoratorEngine';
import AssociationEngine from '../engine/AssociationEngine';
import { ClassFn } from '../types/AssociationTypes';
import ReferenceEngine from '../engine/ReferenceEngine';
import { ClassResolver } from '../types/ReferenceTypes';

type JSONObjectValue = Omit<Partial<JSONEntityObject>, 'type'> | ClassFn;
type JSONObjectValue = Omit<Partial<JSONEntityObject>, 'type'> | ClassResolver;

/**
* Decorator for JSON object attribute.
Expand All @@ -15,13 +15,13 @@ export function JSONObject(...args: [JSONObjectValue] | DecoratorClassProps): Fu
// Related class
if (args.length === 1 && typeof args[0] === 'function') {
return (
prototype: ClassLike['prototype'],
key: keyof ClassLike['prototype'] & string,
prototype: Class['prototype'],
key: keyof Class['prototype'] & string,
descriptor?: PropertyDescriptor
): void => {
const [classFn] = args as [ClassFn];
const [classFn] = args as [ClassResolver];

AssociationEngine.addAssociation(prototype, key, null, classFn);
ReferenceEngine.addReference(prototype, key, null, classFn);
};
}

Expand Down
4 changes: 2 additions & 2 deletions src/decorators/JSONSchema.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import 'reflect-metadata';
import { JSONEntityObject, JSONRoot } from '../types/JSONTypes';
import { ClassLike } from '../types/ClassTypes';
import { Class } from '../types/ClassTypes';
import SchemaEngine from '../engine/SchemaEngine';

type JSONSchemaValue<T extends object> = Partial<JSONEntityObject<T> | JSONRoot<T>>;

const compute = <T extends object>(value: JSONSchemaValue<T> = {}) => {
return (target: T): void => {
SchemaEngine.defineReflectSchema(target as ClassLike, value);
SchemaEngine.defineReflectSchema(target as Class, value);
};
};

Expand Down
4 changes: 2 additions & 2 deletions src/decorators/ReflectKeys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export enum REFLECT_KEY {
JSON_PROPERTY = 'tabbouleh:property',

/**
* Concerns associations
* Concerns references
*/
JSON_ASSOCIATIONS = 'tabbouleh:associations'
JSON_REFERENCE = 'tabbouleh:references'
}
170 changes: 0 additions & 170 deletions src/engine/AssociationEngine.ts

This file was deleted.

16 changes: 8 additions & 8 deletions src/engine/DecoratorEngine.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { JSONEntity } from '../types/JSONTypes';
import { ClassLike, DecoratorClassProps } from '../types/ClassTypes';
import { Class, DecoratorClassProps } from '../types/ClassTypes';
import PropertyEngine from './PropertyEngine';
import { JSONSchema7 } from 'json-schema';
import AssociationEngine from './AssociationEngine';
import { ClassFn } from '../types/AssociationTypes';
import ReferenceEngine from './ReferenceEngine';
import { ClassResolver } from '../types/ReferenceTypes';

/**
* Handle decorator actions.
Expand Down Expand Up @@ -34,7 +34,7 @@ export class DecoratorEngine {

/**
* Return a decorator function
* which handle association mapping and define the class/property json schema.
* which handle reference mapping and define the class/property json schema.
*
* @param defaultValues
* @param value
Expand All @@ -48,20 +48,20 @@ export class DecoratorEngine {
...value
};

return <C extends ClassLike>(
return <C extends Class>(
prototype: C['prototype'],
key: keyof C['prototype'] & string,
descriptor?: PropertyDescriptor
): void => {
const valueSchema: JSONSchema7 = {};

// If we found ClassFn, we create association for each of them
// If we found ClassResolver, we create reference for each of them
Object.keys(value).forEach(_k => {
const k: keyof JSONSchema7 = _k as any;
const v: ClassFn | any = value[k as keyof J];
const v: ClassResolver | any = value[k as keyof J];

if (typeof v === 'function') {
AssociationEngine.addAssociation(prototype, key, k, v);
ReferenceEngine.addReference(prototype, key, k, v);
} else {
(valueSchema as any)[k] = v;
}
Expand Down
14 changes: 7 additions & 7 deletions src/engine/PropertyEngine.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'reflect-metadata';
import { JSONEntity, JSONEntityAny } from '../types/JSONTypes';
import { REFLECT_KEY } from '../decorators/ReflectKeys';
import { ClassLike } from '../types/ClassTypes';
import { Class } from '../types/ClassTypes';
import { JSONSchema7 } from 'json-schema';

/**
Expand All @@ -13,7 +13,7 @@ export default class PropertyEngine {
*
* @param typeTS
*/
private static getJSONSchemaType(typeTS: ClassLike): JSONSchema7 {
private static getJSONSchemaType(typeTS: Class): JSONSchema7 {
switch (typeTS) {
case Array:
return {
Expand Down Expand Up @@ -49,7 +49,7 @@ export default class PropertyEngine {
static getJSONPropertySchema<J extends JSONEntity<any, any>>(
reflectEntity: JSONSchema7,
paramEntity: JSONSchema7,
typeTS: ClassLike
typeTS: Class
): JSONSchema7 {

const partialEntity = {
Expand All @@ -75,7 +75,7 @@ export default class PropertyEngine {
* @param prototype prototype of the JSONSchema class
*/
static getReflectProperties(
prototype: ClassLike['prototype']
prototype: Class['prototype']
): object & Exclude<JSONSchema7['properties'], undefined> {
return Reflect.getMetadata(REFLECT_KEY.JSON_PROPERTY, prototype) || {};
}
Expand All @@ -87,7 +87,7 @@ export default class PropertyEngine {
* @param properties JSONSchema.properties of the JSONSchema
*/
private static setReflectProperties(
prototype: ClassLike['prototype'],
prototype: Class['prototype'],
properties: JSONSchema7['properties']
): void {
Reflect.defineMetadata(REFLECT_KEY.JSON_PROPERTY, properties, prototype);
Expand All @@ -100,7 +100,7 @@ export default class PropertyEngine {
* @param key property key of the JSONSchema class
* @param value partial schema given in param
*/
static defineReflectProperties<C extends ClassLike>(
static defineReflectProperties<C extends Class>(
prototype: C['prototype'],
key: keyof C['prototype'] & string,
value: JSONSchema7
Expand All @@ -113,7 +113,7 @@ export default class PropertyEngine {
Reflect.getMetadata(REFLECT_KEY.JSON_SCHEMA, prototype, key) || {};

// get the infered type of the property
const typeSchema: ClassLike = Reflect.getMetadata(REFLECT_KEY.TYPE, prototype, key);
const typeSchema: Class = Reflect.getMetadata(REFLECT_KEY.TYPE, prototype, key);

// construct the final schema
const fullSchema: JSONSchema7 = PropertyEngine.getJSONPropertySchema<JSONEntityAny>(
Expand Down
Loading

0 comments on commit f7e8f81

Please sign in to comment.