From 1d1ad13536e87e0a8ddd913e7b71fb01a90a887f Mon Sep 17 00:00:00 2001
From: Rafael <rpd17@inf.ufpr.br>
Date: Mon, 23 Jul 2018 10:32:07 -0300
Subject: [PATCH] Issue #80: Add enumHandler

Signed-off-by: Rafael <rpd17@inf.ufpr.br>
---
 src/core/dimension.ts    |  6 ++---
 src/core/source.ts       |  5 +++--
 src/util/configParser.ts |  5 +++--
 src/util/enumHandler.ts  | 48 ++++++++++++++++++++++++++++++++++++++++
 4 files changed, 57 insertions(+), 7 deletions(-)
 create mode 100644 src/util/enumHandler.ts

diff --git a/src/core/dimension.ts b/src/core/dimension.ts
index 202d5b62..37161a4f 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,14 +96,14 @@ export class Dimension {
         if (this.relation === RelationType.NONE) {
             return {
                 name: this.name,
-                dataType: (this.dataType !== DataType.NONE) ? EnumType.stringfyDataType(this.dataType) : this.enumType ,
+                dataType: EnumHandler.selectDataType(this.dataType, this.enumType),
                 description: this.description
             };
         }
         else {
             return {
                 name: this.name,
-                dataType: (this.dataType !== DataType.NONE) ? EnumType.stringfyDataType(this.dataType) : this.enumType ,
+                dataType: EnumHandler.selectDataType(this.dataType, this.enumType),
                 parent: this.parent.name,
                 relation: Dimension.stringifyRelationType(this.relation),
                 description: this.description
diff --git a/src/core/source.ts b/src/core/source.ts
index 7a04687f..f9e11281 100644
--- a/src/core/source.ts
+++ b/src/core/source.ts
@@ -20,6 +20,7 @@
 
 import { EnumType } from "./enumType";
 import { DataType } from "../common/types";
+import { EnumHandler } from "../util/enumHandler";
 
 /** Attribute of a source. */
 export interface Field {
@@ -121,7 +122,7 @@ export class Source {
             return {
                 name : i.name,
                 description: i.description,
-                dataType: (i.dataType !== DataType.NONE) ? EnumType.stringfyDataType(i.dataType) : i.enumType
+                dataType: EnumHandler.selectDataType(i.dataType, i.enumType)
             };
         });
         return str;
@@ -138,7 +139,7 @@ export class Source {
                 name : i.name,
                 description: i.description,
                 dataType: EnumType.parseDataType(i.dataType),
-                enumType: (EnumType.parseDataType(i.dataType) === DataType.NONE) ? i.dataType : ""
+                enumType: EnumHandler.selectEnumType(i.dataType)
             };
         });
         return str;
diff --git a/src/util/configParser.ts b/src/util/configParser.ts
index 8dbf87f6..96aaf88e 100644
--- a/src/util/configParser.ts
+++ b/src/util/configParser.ts
@@ -22,6 +22,7 @@ import { Metric, MetricOptions, MetricStrOptions } from "../core/metric";
 import { Dimension, DimensionOptions, DimensionStrOptions } from "../core/dimension";
 import { View, ViewOptions, LoadView } from "../core/view";
 import { EnumType, EnumTypeOptions } from "../core/enumType";
+import { EnumHandler } from "./enumHandler";
 import { RelationType, DataType } from "../common/types";
 import { Opcode } from "../common/expression";
 import { Filter } from "../core/filter";
@@ -334,7 +335,7 @@ export class ConfigParser {
                         description: opts.description,
                         parent: dims[i],
                         relation: Dimension.parseRelationType(opts.relation),
-                        enumType: (EnumType.parseDataType(opts.dataType) === DataType.NONE) ? opts.dataType : ""
+                        enumType: EnumHandler.selectEnumType(opts.dataType)
                     };
                 }
             }
@@ -347,7 +348,7 @@ export class ConfigParser {
             description: opts.description,
             parent: null,
             relation: RelationType.NONE,
-            enumType: (EnumType.parseDataType(opts.dataType) === DataType.NONE) ? opts.dataType : ""
+            enumType: EnumHandler.selectEnumType(opts.dataType)
         };
     }
 
diff --git a/src/util/enumHandler.ts b/src/util/enumHandler.ts
new file mode 100644
index 00000000..21ff9a19
--- /dev/null
+++ b/src/util/enumHandler.ts
@@ -0,0 +1,48 @@
+import { DataType } from "../common/types";
+
+/*
+ * 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 { EnumType } from "../core/enumType";
+
+/**
+ * Enum's handler. Manage changes between
+ * string , datatype and enumtype.
+ */
+export class EnumHandler {
+
+    /**
+     * Checks if it's a valid datatype, otherwise will be an enumtype
+     * @param dt datatype to compare, used as return if comparation succeed
+     * @param en enumtype as return if comparation fails
+     */
+    public static selectDataType(dt: DataType , en: string): string{
+        return (dt !== DataType.NONE) ? EnumType.stringfyDataType(dt) : en;
+    }
+
+    /**
+     * Checks if it's a valid datatype, otherwise return an empity string 
+     * @param data datatype to compare, used as return if comparation succeed
+     */
+    public static selectEnumType(dt: string): string{
+        return (EnumType.parseDataType(dt) === DataType.NONE) ? dt : "";
+    }
+
+}
-- 
GitLab