diff --git a/src/api/controllers/collect.ts b/src/api/controllers/collect.ts index d2dad1506d6dd24656b67bd2dba5ad17b6818533..28fb505cce09c2fa9782175907524a82795ac0c0 100644 --- a/src/api/controllers/collect.ts +++ b/src/api/controllers/collect.ts @@ -23,6 +23,7 @@ import { Request } from "../types"; import { Source, Field } from "../../core/source"; import { EnumType } from "../../core/enumType"; import { DataType } from "../../common/types"; +import { EnumHandler } from "../../util/enumHandler"; /** * Dictionary indexed by a type name which return a @@ -133,10 +134,10 @@ export class CollectCtrl { for (let i = 0; i < fields.length; i++){ if (fields[i].dataType !== DataType.NONE){ - if (!validador[EnumType.stringfyDataType(fields[i].dataType)](data[i]) === true){ + if (!validador[EnumHandler.stringfyDataType(fields[i].dataType)](data[i]) === true){ throw new Error( "The value '" + data[i] + "' from '" + fields[i].name + - "' isn't a type " + [EnumType.stringfyDataType(fields[i].dataType)]); + "' isn't a type " + [EnumHandler.stringfyDataType(fields[i].dataType)]); } } diff --git a/src/core/dimension.ts b/src/core/dimension.ts index 202d5b62522dbffcb2df9de165a8b659ee6f3bf2..a55cd8d9514a11fa250cff7c4d8d7e0c08b2995d 100644 --- a/src/core/dimension.ts +++ b/src/core/dimension.ts @@ -19,7 +19,7 @@ */ import { RelationType, DataType } from "../common/types"; -import { EnumType } from "./enumType"; +import { EnumHandler } from "../util/enumHandler"; /** Parameters used to create a Dimension object. */ export interface DimensionOptions { @@ -96,52 +96,18 @@ export class Dimension { if (this.relation === RelationType.NONE) { return { name: this.name, - dataType: (this.dataType !== DataType.NONE) ? EnumType.stringfyDataType(this.dataType) : this.enumType , + dataType: (this.dataType !== DataType.NONE) ? EnumHandler.stringfyDataType(this.dataType) : this.enumType , description: this.description }; } else { return { name: this.name, - dataType: (this.dataType !== DataType.NONE) ? EnumType.stringfyDataType(this.dataType) : this.enumType , + dataType: (this.dataType !== DataType.NONE) ? EnumHandler.stringfyDataType(this.dataType) : this.enumType , parent: this.parent.name, - relation: Dimension.stringifyRelationType(this.relation), + relation: EnumHandler.stringifyRelationType(this.relation), description: this.description }; } } - - /** - * Parse a string to enum(Relation Type). - * @param r - Relation in string format. - */ - public static parseRelationType(r: string): RelationType { - switch (r) { - case "day": - return RelationType.DAY; - case "month": - return RelationType.MONTH; - case "year": - return RelationType.YEAR; - default: - return RelationType.NONE; - } - } - - /** - * Parse an enum(Relation Type) to string. - * @param r - Relation to be stringified. - */ - public static stringifyRelationType(r: RelationType): string { - switch (r) { - case RelationType.DAY: - return "day"; - case RelationType.MONTH: - return "month"; - case RelationType.YEAR: - return "year"; - default: - return ""; - } - } } diff --git a/src/core/enumType.ts b/src/core/enumType.ts index e65d78c77ffc9295c8bf07f98507e79f3261e7d4..76eba3341aabfe7416dd28d884bf21f25641298d 100644 --- a/src/core/enumType.ts +++ b/src/core/enumType.ts @@ -18,8 +18,6 @@ * along with blend. If not, see <http://www.gnu.org/licenses/>. */ -import { DataType } from "../common/types"; - /** Parameters used to create a Enumerable type object. */ export interface EnumTypeOptions { /** The type name. */ @@ -56,47 +54,4 @@ export class EnumType { values: this.values }; } - - /** - * Parse an enum(Data type) to string. - * @param a - Data type to be stringified. - */ - public static stringfyDataType(a: DataType): string { - switch (a) { - case DataType.INTEGER: - return "integer"; - case DataType.FLOAT: - return "float"; - case DataType.STRING: - return "string"; - case DataType.DATE: - return "date"; - case DataType.BOOLEAN: - return "boolean"; - default: - return ""; - } - } - - /** - * Parse a string to enum(Data Type). - * @param str - Data type in string format. - */ - public static parseDataType (str: string): DataType { - str = str.toLocaleLowerCase(); - switch (str) { - case "integer": - return DataType.INTEGER; - case "float": - return DataType.FLOAT; - case "string": - return DataType.STRING; - case "date": - return DataType.DATE; - case "boolean": - return DataType.BOOLEAN; - default: - return DataType.NONE; - } - } } diff --git a/src/core/metric.ts b/src/core/metric.ts index 75ad55d05e0e6b0d106cca42d4542bf85217d378..9b76b102a4d38bac4a935277e5f21c06d0730cbd 100644 --- a/src/core/metric.ts +++ b/src/core/metric.ts @@ -19,7 +19,7 @@ */ import { AggregationType, DataType } from "../common/types"; -import { EnumType } from "./enumType"; +import { EnumHandler } from "../util/enumHandler"; /** Parameters used to create a metric object. */ export interface MetricOptions { @@ -83,51 +83,9 @@ export class Metric { public strOptions(): MetricStrOptions { return { name: this.name, - aggregation: Metric.stringifyAggrType(this.aggregation), - dataType: EnumType.stringfyDataType(this.dataType), + aggregation: EnumHandler.stringifyAggrType(this.aggregation), + dataType: EnumHandler.stringfyDataType(this.dataType), description: this.description }; } - - /** - * Parse an enum(Aggregation Type) to string. - * @param a - Aggregation function to be stringified. - */ - public static stringifyAggrType(a: AggregationType): string { - switch (a) { - case AggregationType.SUM: - return "sum"; - case AggregationType.AVG: - return "avg"; - case AggregationType.COUNT: - return "count"; - case AggregationType.MAX: - return "max"; - case AggregationType.MIN: - return "min"; - default: - return ""; - } - } - - /** - * Parse a string to enum(Aggregation Type). - * @param str - Aggregation function in string format. - */ - public static parseAggrType (str: string): AggregationType { - switch (str) { - case "sum": - return AggregationType.SUM; - case "avg": - return AggregationType.AVG; - case "count": - return AggregationType.COUNT; - case "min": - return AggregationType.MIN; - case "max": - return AggregationType.MAX; - default: - return AggregationType.NONE; - } - } } diff --git a/src/core/source.ts b/src/core/source.ts index 7a04687fd169aff80a284e6bb84ea5fa2575962b..ecc84ef27acdf8cffb7e18250fd71a8ddc71c372 100644 --- a/src/core/source.ts +++ b/src/core/source.ts @@ -18,8 +18,8 @@ * along with blendb. If not, see <http://www.gnu.org/licenses/>. */ -import { EnumType } from "./enumType"; import { DataType } from "../common/types"; +import { EnumHandler } from "../util/enumHandler"; /** Attribute of a source. */ export interface Field { @@ -121,7 +121,7 @@ export class Source { return { name : i.name, description: i.description, - dataType: (i.dataType !== DataType.NONE) ? EnumType.stringfyDataType(i.dataType) : i.enumType + dataType: (i.dataType !== DataType.NONE) ? EnumHandler.stringfyDataType(i.dataType) : i.enumType }; }); return str; @@ -137,8 +137,8 @@ export class Source { return { name : i.name, description: i.description, - dataType: EnumType.parseDataType(i.dataType), - enumType: (EnumType.parseDataType(i.dataType) === DataType.NONE) ? i.dataType : "" + dataType: EnumHandler.parseDataType(i.dataType), + enumType: (EnumHandler.parseDataType(i.dataType) === DataType.NONE) ? i.dataType : "" }; }); return str; diff --git a/src/util/configParser.spec.ts b/src/util/configParser.spec.ts index 8c41cd0071b7f065cdae1d22b9ae1a2ecfc0178f..ff1dc33ac1d56b2140baed3e8b53c7d4070090d1 100644 --- a/src/util/configParser.spec.ts +++ b/src/util/configParser.spec.ts @@ -19,11 +19,11 @@ */ import { expect } from "chai"; - import { ConfigParser, ViewParsingOptions } from "./configParser"; import { Dimension, DimensionStrOptions } from "../core/dimension"; import { RelationType , DataType} from "../common/types"; import { EnumType } from "../core/enumType"; +import { EnumHandler } from "../util/enumHandler"; import { MetricStrOptions } from "../core/metric"; import { SourceStrOptions } from "../core/source"; @@ -198,7 +198,7 @@ describe("configParser utility library", () => { for (let i = 0; i < opts.length; ++i) { let parsed = ConfigParser.parseDimOpts(opts[i], dims, null); expect(parsed.name).to.be.equal(opts[i].name); - expect(EnumType.stringfyDataType(parsed.dataType)).to.be.equal(opts[i].dataType); + expect(EnumHandler.stringfyDataType(parsed.dataType)).to.be.equal(opts[i].dataType); expect(parsed.parent).to.be.equal(dims[1]); expect(parsed.relation).to.be.equal(strToRelationType(opts[i].relation)); } diff --git a/src/util/configParser.ts b/src/util/configParser.ts index 8dbf87f669a53d7062fb2ebabf75ed3c47041354..679cc9fda3f436b0046e33d30cde000ae733ba28 100644 --- a/src/util/configParser.ts +++ b/src/util/configParser.ts @@ -30,6 +30,7 @@ import { Source, SourceOptions, SourceStrOptions} from "../core/source"; import { Tsort, TsortDep } from "./tsort"; import * as fs from "fs"; import * as yaml from "js-yaml"; +import { EnumHandler } from "./enumHandler"; /** * Parameters used to define view object in the configuration file. @@ -319,7 +320,7 @@ export class ConfigParser { * @param map - Enumerable types available. */ public static parseDimOpts (opts: DimensionStrOptions, dims: Dimension[], map: EnumTypeMap): DimensionOptions { - let type = EnumType.parseDataType(opts.dataType); + let type = EnumHandler.parseDataType(opts.dataType); if (type === DataType.NONE) { if (!(map[opts.dataType])) { throw new Error("[Parsing error] DataType: '" + opts.dataType + "' does not exist on Dimension"); @@ -330,11 +331,11 @@ export class ConfigParser { if (dims[i].name === opts.parent) { return { name: opts.name, - dataType: EnumType.parseDataType(opts.dataType), + dataType: EnumHandler.parseDataType(opts.dataType), description: opts.description, parent: dims[i], - relation: Dimension.parseRelationType(opts.relation), - enumType: (EnumType.parseDataType(opts.dataType) === DataType.NONE) ? opts.dataType : "" + relation: EnumHandler.parseRelationType(opts.relation), + enumType: (EnumHandler.parseDataType(opts.dataType) === DataType.NONE) ? opts.dataType : "" }; } } @@ -343,11 +344,11 @@ export class ConfigParser { } return { name: opts.name, - dataType: EnumType.parseDataType(opts.dataType), + dataType: EnumHandler.parseDataType(opts.dataType), description: opts.description, parent: null, relation: RelationType.NONE, - enumType: (EnumType.parseDataType(opts.dataType) === DataType.NONE) ? opts.dataType : "" + enumType: (EnumHandler.parseDataType(opts.dataType) === DataType.NONE) ? opts.dataType : "" }; } @@ -356,7 +357,7 @@ export class ConfigParser { * @param opts - Metric struct in configuration file. */ public static parseMetOpts (opts: MetricStrOptions): MetricOptions { - let type = EnumType.parseDataType(opts.dataType); + let type = EnumHandler.parseDataType(opts.dataType); if (!(type === DataType.FLOAT || type === DataType.INTEGER)){ throw new Error("[Parsing error] DataType: '" + opts.dataType + "' does not exist on Metric"); @@ -364,8 +365,8 @@ export class ConfigParser { } return { name: opts.name, - aggregation: Metric.parseAggrType(opts.aggregation), - dataType : EnumType.parseDataType(opts.dataType), + aggregation: EnumHandler.parseAggrType(opts.aggregation), + dataType : EnumHandler.parseDataType(opts.dataType), description: opts.description }; } @@ -398,7 +399,7 @@ export class ConfigParser { */ public static parseSourceOpts (opts: SourceStrOptions , map: EnumTypeMap): SourceOptions { for ( let k = 0; k < opts.fields.length ; k++) { - let type = EnumType.parseDataType(opts.fields[k].dataType); + let type = EnumHandler.parseDataType(opts.fields[k].dataType); if (type === DataType.NONE) { if (!(map[opts.fields[k].dataType])){ @@ -441,5 +442,4 @@ export class ConfigParser { value: strFilter.value }); } - } diff --git a/src/util/enumHandler.ts b/src/util/enumHandler.ts new file mode 100644 index 0000000000000000000000000000000000000000..02e8818946e62aef2de5303cf9bd3a8f4c65782b --- /dev/null +++ b/src/util/enumHandler.ts @@ -0,0 +1,147 @@ +/* + * Copyright (C) 2018 Centro de Computacao Cientifica e Software Livre + * Departamento de Informatica - Universidade Federal do Parana + * + * This file is part of blendb. + * + * blendb is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * blendb is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with blendb. If not, see <http://www.gnu.org/licenses/>. + */ + +import {RelationType, DataType , AggregationType} from "../common/types"; + +/** + * Enum's handler. Manage parse through the project. + */ +export class EnumHandler { + + /** + * Parse an enum(Data type) to string. + * @param a - Data type to be stringified. + */ + public static stringfyDataType(a: DataType): string { + switch (a) { + case DataType.INTEGER: + return "integer"; + case DataType.FLOAT: + return "float"; + case DataType.STRING: + return "string"; + case DataType.DATE: + return "date"; + case DataType.BOOLEAN: + return "boolean"; + default: + return ""; + } + } + + /** + * Parse a string to enum(Data Type). + * @param str - Data type in string format. + */ + public static parseDataType (str: string): DataType { + str = str.toLocaleLowerCase(); + switch (str) { + case "integer": + return DataType.INTEGER; + case "float": + return DataType.FLOAT; + case "string": + return DataType.STRING; + case "date": + return DataType.DATE; + case "boolean": + return DataType.BOOLEAN; + default: + return DataType.NONE; + } + } + + /** + * Parse a string to enum(Relation Type). + * @param r - Relation in string format. + */ + public static parseRelationType(r: string): RelationType { + switch (r) { + case "day": + return RelationType.DAY; + case "month": + return RelationType.MONTH; + case "year": + return RelationType.YEAR; + default: + return RelationType.NONE; + } + } + + /** + * Parse an enum(Relation Type) to string. + * @param r - Relation to be stringified. + */ + public static stringifyRelationType(r: RelationType): string { + switch (r) { + case RelationType.DAY: + return "day"; + case RelationType.MONTH: + return "month"; + case RelationType.YEAR: + return "year"; + default: + return ""; + } + } + + /** + * Parse an enum(Aggregation Type) to string. + * @param a - Aggregation function to be stringified. + */ + public static stringifyAggrType(a: AggregationType): string { + switch (a) { + case AggregationType.SUM: + return "sum"; + case AggregationType.AVG: + return "avg"; + case AggregationType.COUNT: + return "count"; + case AggregationType.MAX: + return "max"; + case AggregationType.MIN: + return "min"; + default: + return ""; + } + } + + /** + * Parse a string to enum(Aggregation Type). + * @param str - Aggregation function in string format. + */ + public static parseAggrType (str: string): AggregationType { + switch (str) { + case "sum": + return AggregationType.SUM; + case "avg": + return AggregationType.AVG; + case "count": + return AggregationType.COUNT; + case "min": + return AggregationType.MIN; + case "max": + return AggregationType.MAX; + default: + return AggregationType.NONE; + } + } + +}