Commit e7b3f0bd authored by Rafael Dias's avatar Rafael Dias

Issue #80: Add enumHandler

Signed-off-by: Rafael Dias's avatarRafael <rpd17@inf.ufpr.br>
parent 6f4d8d18
Pipeline #16897 passed with stages
in 58 seconds
......@@ -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)]);
}
}
......
......@@ -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 "";
}
}
}
......@@ -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;
}
}
}
......@@ -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;
}
}
}
......@@ -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;
......
......@@ -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));
}
......
......@@ -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
});
}
}
/*
* 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;
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment