Skip to content

Commit

Permalink
Improve engine dataTypes type
Browse files Browse the repository at this point in the history
  • Loading branch information
getlarge committed Mar 23, 2020
1 parent 74aeb76 commit 586bef0
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 42 deletions.
2 changes: 1 addition & 1 deletion src/engine/datatype/ComplexDataType.ts
@@ -1,5 +1,5 @@
export class ComplexDataType {}

export const isComplexDataType = obj => {
export const isComplexDataType = (obj: any): boolean => {
return obj instanceof ComplexDataType;
};
8 changes: 4 additions & 4 deletions src/engine/datatype/DataType.ts
Expand Up @@ -25,7 +25,7 @@ export class DataType {
defaultValue?: () => any;
enforceIndex?: boolean;

constructor(setup: DataTypeSetup = <DataTypeSetup>{}) {
constructor(setup: DataTypeSetup = {} as DataTypeSetup) {
const {
name,
description,
Expand Down Expand Up @@ -78,17 +78,17 @@ export class DataType {
}
}

validate = async (value, context) => {
validate = async (value: any, context: any): Promise<void> => {
if (value && this.validator) {
await this.validator(value, context);
}
};

toString() {
toString(): string {
return this.name;
}
}

export const isDataType = obj => {
export const isDataType = (obj: any): boolean => {
return obj instanceof DataType;
};
10 changes: 5 additions & 5 deletions src/engine/datatype/DataTypeEnum.ts
@@ -1,7 +1,7 @@
import { uniq } from 'lodash';
import { passOrThrow, isMap } from '../util';
import { enumValueRegex, ENUM_VALUE_PATTERN } from '../constants';
import { DataType } from './DataType';
import * as _ from 'lodash';

export type ValueType = {
[key: string]: number;
Expand All @@ -16,7 +16,7 @@ export type DataTypeEnumSetupType = {
export class DataTypeEnum extends DataType {
values: ValueType;

constructor(setup: DataTypeEnumSetupType = <DataTypeEnumSetupType>{}) {
constructor(setup: DataTypeEnumSetupType = {} as DataTypeEnumSetupType) {
const { name, description, values } = setup;

passOrThrow(
Expand Down Expand Up @@ -45,7 +45,7 @@ export class DataTypeEnum extends DataType {
});

passOrThrow(
uniqueIds.length === _.uniq(uniqueIds).length,
uniqueIds.length === uniq(uniqueIds).length,
() =>
`Each value defined for data type '${name}' needs to have a unique ID`,
);
Expand All @@ -63,11 +63,11 @@ export class DataTypeEnum extends DataType {
this.values = values;
}

toString() {
toString(): string {
return this.name;
}
}

export const isDataTypeEnum = obj => {
export const isDataTypeEnum = (obj: any): boolean => {
return obj instanceof DataTypeEnum;
};
13 changes: 5 additions & 8 deletions src/engine/datatype/DataTypeState.ts
@@ -1,9 +1,6 @@
import { uniq } from 'lodash';
import { passOrThrow, isMap } from '../util';

import { stateNameRegex, STATE_NAME_PATTERN } from '../constants';

import * as _ from 'lodash';

import { DataType } from './DataType';

export type StateType = {
Expand All @@ -18,7 +15,7 @@ export type DataTypeStateSetupType = {

export class DataTypeState extends DataType {
states: StateType;
constructor(setup: DataTypeStateSetupType = <DataTypeStateSetupType>{}) {
constructor(setup: DataTypeStateSetupType = {} as DataTypeStateSetupType) {
const { name, description, states } = setup;

passOrThrow(
Expand Down Expand Up @@ -47,7 +44,7 @@ export class DataTypeState extends DataType {
});

passOrThrow(
uniqueIds.length === _.uniq(uniqueIds).length,
uniqueIds.length === uniq(uniqueIds).length,
() =>
`Each state defined for data type '${name}' needs to have a unique ID`,
);
Expand All @@ -65,11 +62,11 @@ export class DataTypeState extends DataType {
this.states = states;
}

toString() {
toString(): string {
return this.name;
}
}

export const isDataTypeState = obj => {
export const isDataTypeState = (obj: any): boolean => {
return obj instanceof DataTypeState;
};
2 changes: 1 addition & 1 deletion src/engine/datatype/DataTypeUser.ts
Expand Up @@ -2,6 +2,6 @@ import { DataType } from './DataType';

export class DataTypeUser extends DataType {}

export const isDataTypeUser = obj => {
export const isDataTypeUser = (obj: any): boolean => {
return obj instanceof DataTypeUser;
};
26 changes: 12 additions & 14 deletions src/engine/datatype/ListDataType.ts
@@ -1,5 +1,4 @@
import { passOrThrow, isFunction, isArray } from '../util';

import { Entity, isEntity } from '../entity/Entity';
import { DataType, isDataType, DataTypeFunction } from './DataType';
import { ComplexDataType, isComplexDataType } from './ComplexDataType';
Expand All @@ -20,7 +19,7 @@ export class ListDataType extends ComplexDataType {
minItems?: number;
maxItems?: number;

constructor(setup: ListDataTypeSetupType = <ListDataTypeSetupType>{}) {
constructor(setup: ListDataTypeSetupType = {} as ListDataTypeSetupType) {
super();

const { name, description, itemType, minItems, maxItems } = setup;
Expand Down Expand Up @@ -78,7 +77,8 @@ export class ListDataType extends ComplexDataType {

_processItemType(): DataType | ComplexDataType {
if (isFunction(this.itemType)) {
const itemTypeBuilder: DataTypeFunction = <DataTypeFunction>this.itemType;
const itemTypeBuilder: DataTypeFunction = this
.itemType as DataTypeFunction;
const itemType = itemTypeBuilder({
name: this.name,
description: this.description,
Expand Down Expand Up @@ -109,7 +109,7 @@ export class ListDataType extends ComplexDataType {
return ret;
}

validate = payload => {
validate = (payload: any): void => {
if (payload) {
passOrThrow(
isArray(payload),
Expand All @@ -119,32 +119,30 @@ export class ListDataType extends ComplexDataType {
passOrThrow(
payload.length >= this.minItems,
() =>
`List data type '${this.name}' requires a minimum of ${
this.minItems
} items`,
`List data type '${this.name}' requires a minimum of ${this.minItems} items`,
);

passOrThrow(
this.maxItems === 0 || payload.length <= this.maxItems,
() =>
`List data type '${this.name}' requires a maximum of ${
this.maxItems
} items`,
`List data type '${this.name}' requires a maximum of ${this.maxItems} items`,
);
}
};

toString() {
toString(): string {
return this.name;
}
}

export const isListDataType = obj => {
export const isListDataType = (obj: any): boolean => {
return obj instanceof ListDataType;
};

export const buildListDataType = obj => {
return ({ name, description }) =>
export const buildListDataType = (obj: {
itemType: Entity | ComplexDataType | DataTypeFunction;
}): Function => {
return ({ name, description }): ListDataType =>
new ListDataType({
description,
...obj,
Expand Down
28 changes: 19 additions & 9 deletions src/engine/datatype/ObjectDataType.ts
Expand Up @@ -2,7 +2,11 @@ import { passOrThrow, resolveFunctionMap, isMap, isFunction } from '../util';
import { isEntity } from '../entity/Entity';
import { isDataType } from './DataType';
import { ComplexDataType, isComplexDataType } from './ComplexDataType';
import { AttributesMap, AttributesMapGenerator } from '../attribute/Attribute';
import {
AttributesMap,
AttributesMapGenerator,
AttributeBase,
} from '../attribute/Attribute';

export type ObjectDataTypeSetupType = {
name: string;
Expand Down Expand Up @@ -53,9 +57,13 @@ export class ObjectDataType extends ComplexDataType {
return ret;
}

_processAttribute(rawAttribute, attributeName) {
_processAttribute(
rawAttribute: AttributeBase,
attributeName: string,
): AttributeBase {
if (isFunction(rawAttribute.type)) {
rawAttribute.type = rawAttribute.type({
const rawAttributeTypeFn = rawAttribute.type as Function;
rawAttribute.type = rawAttributeTypeFn({
name: attributeName,
description: rawAttribute.description,
});
Expand Down Expand Up @@ -112,7 +120,7 @@ export class ObjectDataType extends ComplexDataType {
return attribute;
}

_processAttributeMap() {
_processAttributeMap(): AttributesMap {
// if it's a function, resolve it to get that map
const attributeMap = resolveFunctionMap(this._attributesMap);

Expand Down Expand Up @@ -140,7 +148,7 @@ export class ObjectDataType extends ComplexDataType {
return resultAttributes;
}

validate = value => {
validate = (value: any): void => {
if (value) {
passOrThrow(
isMap(value),
Expand All @@ -149,17 +157,19 @@ export class ObjectDataType extends ComplexDataType {
}
};

toString() {
toString(): string {
return this.name;
}
}

export const isObjectDataType = obj => {
export const isObjectDataType = (obj: any): boolean => {
return obj instanceof ObjectDataType;
};

export const buildObjectDataType = obj => {
return ({ name, description }) =>
export const buildObjectDataType = (obj: {
attributes: AttributesMap | AttributesMapGenerator;
}): Function => {
return ({ name, description }): ObjectDataType =>
new ObjectDataType({
description,
...obj,
Expand Down

0 comments on commit 586bef0

Please sign in to comment.