Skip to content
Snippets Groups Projects
Commit ea6ddc63 authored by Lucas Fernandes de Oliveira's avatar Lucas Fernandes de Oliveira
Browse files

Merge branch 'issue/80' into 'develop'

Issue #80: Add enumHandler

See merge request !70
parents 6f4d8d18 e7b3f0bd
Branches
No related tags found
1 merge request!70Issue #80: Add enumHandler
Pipeline #16930 passed
...@@ -23,6 +23,7 @@ import { Request } from "../types"; ...@@ -23,6 +23,7 @@ import { Request } from "../types";
import { Source, Field } from "../../core/source"; import { Source, Field } from "../../core/source";
import { EnumType } from "../../core/enumType"; import { EnumType } from "../../core/enumType";
import { DataType } from "../../common/types"; import { DataType } from "../../common/types";
import { EnumHandler } from "../../util/enumHandler";
/** /**
* Dictionary indexed by a type name which return a * Dictionary indexed by a type name which return a
...@@ -133,10 +134,10 @@ export class CollectCtrl { ...@@ -133,10 +134,10 @@ export class CollectCtrl {
for (let i = 0; i < fields.length; i++){ for (let i = 0; i < fields.length; i++){
if (fields[i].dataType !== DataType.NONE){ 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( throw new Error(
"The value '" + data[i] + "' from '" + fields[i].name + "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 @@ ...@@ -19,7 +19,7 @@
*/ */
import { RelationType, DataType } from "../common/types"; import { RelationType, DataType } from "../common/types";
import { EnumType } from "./enumType"; import { EnumHandler } from "../util/enumHandler";
/** Parameters used to create a Dimension object. */ /** Parameters used to create a Dimension object. */
export interface DimensionOptions { export interface DimensionOptions {
...@@ -96,52 +96,18 @@ export class Dimension { ...@@ -96,52 +96,18 @@ export class Dimension {
if (this.relation === RelationType.NONE) { if (this.relation === RelationType.NONE) {
return { return {
name: this.name, 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 description: this.description
}; };
} }
else { else {
return { return {
name: this.name, 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, parent: this.parent.name,
relation: Dimension.stringifyRelationType(this.relation), relation: EnumHandler.stringifyRelationType(this.relation),
description: this.description 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 @@ ...@@ -18,8 +18,6 @@
* along with blend. If not, see <http://www.gnu.org/licenses/>. * along with blend. If not, see <http://www.gnu.org/licenses/>.
*/ */
import { DataType } from "../common/types";
/** Parameters used to create a Enumerable type object. */ /** Parameters used to create a Enumerable type object. */
export interface EnumTypeOptions { export interface EnumTypeOptions {
/** The type name. */ /** The type name. */
...@@ -56,47 +54,4 @@ export class EnumType { ...@@ -56,47 +54,4 @@ export class EnumType {
values: this.values 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 @@ ...@@ -19,7 +19,7 @@
*/ */
import { AggregationType, DataType } from "../common/types"; import { AggregationType, DataType } from "../common/types";
import { EnumType } from "./enumType"; import { EnumHandler } from "../util/enumHandler";
/** Parameters used to create a metric object. */ /** Parameters used to create a metric object. */
export interface MetricOptions { export interface MetricOptions {
...@@ -83,51 +83,9 @@ export class Metric { ...@@ -83,51 +83,9 @@ export class Metric {
public strOptions(): MetricStrOptions { public strOptions(): MetricStrOptions {
return { return {
name: this.name, name: this.name,
aggregation: Metric.stringifyAggrType(this.aggregation), aggregation: EnumHandler.stringifyAggrType(this.aggregation),
dataType: EnumType.stringfyDataType(this.dataType), dataType: EnumHandler.stringfyDataType(this.dataType),
description: this.description 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 @@ ...@@ -18,8 +18,8 @@
* along with blendb. If not, see <http://www.gnu.org/licenses/>. * along with blendb. If not, see <http://www.gnu.org/licenses/>.
*/ */
import { EnumType } from "./enumType";
import { DataType } from "../common/types"; import { DataType } from "../common/types";
import { EnumHandler } from "../util/enumHandler";
/** Attribute of a source. */ /** Attribute of a source. */
export interface Field { export interface Field {
...@@ -121,7 +121,7 @@ export class Source { ...@@ -121,7 +121,7 @@ export class Source {
return { return {
name : i.name, name : i.name,
description: i.description, 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; return str;
...@@ -137,8 +137,8 @@ export class Source { ...@@ -137,8 +137,8 @@ export class Source {
return { return {
name : i.name, name : i.name,
description: i.description, description: i.description,
dataType: EnumType.parseDataType(i.dataType), dataType: EnumHandler.parseDataType(i.dataType),
enumType: (EnumType.parseDataType(i.dataType) === DataType.NONE) ? i.dataType : "" enumType: (EnumHandler.parseDataType(i.dataType) === DataType.NONE) ? i.dataType : ""
}; };
}); });
return str; return str;
......
...@@ -19,11 +19,11 @@ ...@@ -19,11 +19,11 @@
*/ */
import { expect } from "chai"; import { expect } from "chai";
import { ConfigParser, ViewParsingOptions } from "./configParser"; import { ConfigParser, ViewParsingOptions } from "./configParser";
import { Dimension, DimensionStrOptions } from "../core/dimension"; import { Dimension, DimensionStrOptions } from "../core/dimension";
import { RelationType , DataType} from "../common/types"; import { RelationType , DataType} from "../common/types";
import { EnumType } from "../core/enumType"; import { EnumType } from "../core/enumType";
import { EnumHandler } from "../util/enumHandler";
import { MetricStrOptions } from "../core/metric"; import { MetricStrOptions } from "../core/metric";
import { SourceStrOptions } from "../core/source"; import { SourceStrOptions } from "../core/source";
...@@ -198,7 +198,7 @@ describe("configParser utility library", () => { ...@@ -198,7 +198,7 @@ describe("configParser utility library", () => {
for (let i = 0; i < opts.length; ++i) { for (let i = 0; i < opts.length; ++i) {
let parsed = ConfigParser.parseDimOpts(opts[i], dims, null); let parsed = ConfigParser.parseDimOpts(opts[i], dims, null);
expect(parsed.name).to.be.equal(opts[i].name); 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.parent).to.be.equal(dims[1]);
expect(parsed.relation).to.be.equal(strToRelationType(opts[i].relation)); expect(parsed.relation).to.be.equal(strToRelationType(opts[i].relation));
} }
......
...@@ -30,6 +30,7 @@ import { Source, SourceOptions, SourceStrOptions} from "../core/source"; ...@@ -30,6 +30,7 @@ import { Source, SourceOptions, SourceStrOptions} from "../core/source";
import { Tsort, TsortDep } from "./tsort"; import { Tsort, TsortDep } from "./tsort";
import * as fs from "fs"; import * as fs from "fs";
import * as yaml from "js-yaml"; import * as yaml from "js-yaml";
import { EnumHandler } from "./enumHandler";
/** /**
* Parameters used to define view object in the configuration file. * Parameters used to define view object in the configuration file.
...@@ -319,7 +320,7 @@ export class ConfigParser { ...@@ -319,7 +320,7 @@ export class ConfigParser {
* @param map - Enumerable types available. * @param map - Enumerable types available.
*/ */
public static parseDimOpts (opts: DimensionStrOptions, dims: Dimension[], map: EnumTypeMap): DimensionOptions { 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 (type === DataType.NONE) {
if (!(map[opts.dataType])) { if (!(map[opts.dataType])) {
throw new Error("[Parsing error] DataType: '" + opts.dataType + "' does not exist on Dimension"); throw new Error("[Parsing error] DataType: '" + opts.dataType + "' does not exist on Dimension");
...@@ -330,11 +331,11 @@ export class ConfigParser { ...@@ -330,11 +331,11 @@ export class ConfigParser {
if (dims[i].name === opts.parent) { if (dims[i].name === opts.parent) {
return { return {
name: opts.name, name: opts.name,
dataType: EnumType.parseDataType(opts.dataType), dataType: EnumHandler.parseDataType(opts.dataType),
description: opts.description, description: opts.description,
parent: dims[i], parent: dims[i],
relation: Dimension.parseRelationType(opts.relation), relation: EnumHandler.parseRelationType(opts.relation),
enumType: (EnumType.parseDataType(opts.dataType) === DataType.NONE) ? opts.dataType : "" enumType: (EnumHandler.parseDataType(opts.dataType) === DataType.NONE) ? opts.dataType : ""
}; };
} }
} }
...@@ -343,11 +344,11 @@ export class ConfigParser { ...@@ -343,11 +344,11 @@ export class ConfigParser {
} }
return { return {
name: opts.name, name: opts.name,
dataType: EnumType.parseDataType(opts.dataType), dataType: EnumHandler.parseDataType(opts.dataType),
description: opts.description, description: opts.description,
parent: null, parent: null,
relation: RelationType.NONE, 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 { ...@@ -356,7 +357,7 @@ export class ConfigParser {
* @param opts - Metric struct in configuration file. * @param opts - Metric struct in configuration file.
*/ */
public static parseMetOpts (opts: MetricStrOptions): MetricOptions { 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)){ if (!(type === DataType.FLOAT || type === DataType.INTEGER)){
throw new Error("[Parsing error] DataType: '" + opts.dataType + "' does not exist on Metric"); throw new Error("[Parsing error] DataType: '" + opts.dataType + "' does not exist on Metric");
...@@ -364,8 +365,8 @@ export class ConfigParser { ...@@ -364,8 +365,8 @@ export class ConfigParser {
} }
return { return {
name: opts.name, name: opts.name,
aggregation: Metric.parseAggrType(opts.aggregation), aggregation: EnumHandler.parseAggrType(opts.aggregation),
dataType : EnumType.parseDataType(opts.dataType), dataType : EnumHandler.parseDataType(opts.dataType),
description: opts.description description: opts.description
}; };
} }
...@@ -398,7 +399,7 @@ export class ConfigParser { ...@@ -398,7 +399,7 @@ export class ConfigParser {
*/ */
public static parseSourceOpts (opts: SourceStrOptions , map: EnumTypeMap): SourceOptions { public static parseSourceOpts (opts: SourceStrOptions , map: EnumTypeMap): SourceOptions {
for ( let k = 0; k < opts.fields.length ; k++) { 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 (type === DataType.NONE) {
if (!(map[opts.fields[k].dataType])){ if (!(map[opts.fields[k].dataType])){
...@@ -441,5 +442,4 @@ export class ConfigParser { ...@@ -441,5 +442,4 @@ export class ConfigParser {
value: strFilter.value 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;
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment