diff --git a/config/ci_test.yaml.example b/config/ci_test.yaml.example index 260fa663a38104c6d4f1d70766f8c1ffebc81a82..0e0dc70e25c3d2db0ef345b67e77caf4b7ab6223 100644 --- a/config/ci_test.yaml.example +++ b/config/ci_test.yaml.example @@ -259,7 +259,8 @@ sources: - name: "fields:0" description: "first entry" - dataType: "enumtype:0" + dataType: "enumtype" + enumType: "enumtype:0" - name: "fields:1" description: "second entry" diff --git a/src/api/controllers/collect.ts b/src/api/controllers/collect.ts index 28fb505cce09c2fa9782175907524a82795ac0c0..7d01ef7996c38f1630db5325ad2d92d170f4c536 100644 --- a/src/api/controllers/collect.ts +++ b/src/api/controllers/collect.ts @@ -133,15 +133,8 @@ export class CollectCtrl { } for (let i = 0; i < fields.length; i++){ - if (fields[i].dataType !== DataType.NONE){ - 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 " + [EnumHandler.stringfyDataType(fields[i].dataType)]); - } - - } - else { + // check if it's a valid enumtype + if (fields[i].dataType === DataType.ENUMTYPE){ enumType = req.engine.getEnumTypeByName(fields[i].enumType); types = enumType.values; let found: boolean = false; @@ -157,6 +150,21 @@ export class CollectCtrl { "' isn't listed on " + fields[i].enumType); } } + + else if (fields[i].dataType !== DataType.NONE){ + // check if it's a valid datatype from query + 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 " + [EnumHandler.stringfyDataType(fields[i].dataType)]); + } + } + // undefined DataType + else{ + throw new Error( + "The value '" + data[i] + "' from '" + fields[i].name + + "' isn't listed on " + fields[i].enumType); + } } } diff --git a/src/core/dimension.ts b/src/core/dimension.ts index a55cd8d9514a11fa250cff7c4d8d7e0c08b2995d..24b5ec161daa89861f8149a899f9e356cb916583 100644 --- a/src/core/dimension.ts +++ b/src/core/dimension.ts @@ -52,6 +52,8 @@ export interface DimensionStrOptions { relation?: string; /** Breif description of what this dimension represents. */ description?: string; + /** Dimension enum type */ + enumType?: string; } /** @@ -85,7 +87,7 @@ export class Dimension { this.relation = (options.relation) ? options.relation : RelationType.NONE; this.parent = (options.parent) ? options.parent : null; this.description = (options.description) ? options.description : ""; - this.enumType = (options.enumType) ? (options.enumType) : ""; + this.enumType = (options.enumType) ? options.enumType : ""; } /** @@ -93,21 +95,19 @@ export class Dimension { * dimension as strings. Used to inform the API users. */ public strOptions(): DimensionStrOptions { - if (this.relation === RelationType.NONE) { - return { - name: this.name, - dataType: (this.dataType !== DataType.NONE) ? EnumHandler.stringfyDataType(this.dataType) : this.enumType , - description: this.description - }; + let o: DimensionStrOptions = { + name: this.name, + dataType: EnumHandler.stringfyDataType(this.dataType), + description: this.description + + }; + if (this.relation !== RelationType.NONE){ + o.relation = EnumHandler.stringifyRelationType(this.relation); + o.parent = this.parent.name; } - else { - return { - name: this.name, - dataType: (this.dataType !== DataType.NONE) ? EnumHandler.stringfyDataType(this.dataType) : this.enumType , - parent: this.parent.name, - relation: EnumHandler.stringifyRelationType(this.relation), - description: this.description - }; + if (this.dataType === DataType.ENUMTYPE){ + o.enumType = this.enumType; } + return o; } } diff --git a/src/core/source.ts b/src/core/source.ts index ecc84ef27acdf8cffb7e18250fd71a8ddc71c372..bd8704f686a08c693472f236fe161a33d3f3ee98 100644 --- a/src/core/source.ts +++ b/src/core/source.ts @@ -41,6 +41,8 @@ export interface FieldStr { description?: string; /* Field data type. */ dataType: string; + /* Field enum type. */ + enumType?: string; } /** Parameters used to create a source object. */ @@ -118,12 +120,17 @@ export class Source { public static stringfyFieldDataType(opts: Field[]): FieldStr[] { let str: FieldStr[]; str = opts.map((i) => { - return { - name : i.name, + let o: FieldStr = { + name: i.name, description: i.description, - dataType: (i.dataType !== DataType.NONE) ? EnumHandler.stringfyDataType(i.dataType) : i.enumType + dataType: EnumHandler.stringfyDataType(i.dataType) }; + if (i.dataType === DataType.ENUMTYPE) { + o.enumType = i.enumType; + } + return o; }); + return str; } @@ -134,12 +141,15 @@ export class Source { public static parseFieldDataType(opts: FieldStr[]): Field[] { let str: Field[]; str = opts.map((i) => { - return { - name : i.name, + let o: Field = { + name: i.name, description: i.description, dataType: EnumHandler.parseDataType(i.dataType), - enumType: (EnumHandler.parseDataType(i.dataType) === DataType.NONE) ? i.dataType : "" }; + if (o.dataType === DataType.ENUMTYPE){ + o.enumType = i.enumType; + } + return o; }); return str; } diff --git a/src/util/configParser.spec.ts b/src/util/configParser.spec.ts index ff1dc33ac1d56b2140baed3e8b53c7d4070090d1..f31d5610783c93a87cd4dc6a9476999ec3dfa55f 100644 --- a/src/util/configParser.spec.ts +++ b/src/util/configParser.spec.ts @@ -208,7 +208,8 @@ describe("configParser utility library", () => { let opts: DimensionStrOptions = { name: "dim:day", - dataType: "enumtype:5", + dataType: "enumtype", + enumType: "enumtype:5", parent: "dim:0", relation: "day" }; @@ -281,7 +282,8 @@ describe("configParser utility library", () => { dataType: "string"}, {name: "fields:1", description: "second entry", - dataType: "enumtype:10"} + dataType: "enumtype", + enumType: "enumtype:10"} ] }; let enumMap: {[key: string]: EnumType} = { @@ -294,10 +296,9 @@ describe("configParser utility library", () => { catch (e) { error = true; expect(e.message).to.be - .equal("[Parsing error] DataType: '" + sourc.fields[1].dataType + "' does not exist on Source"); + .equal("[Parsing error] EnumType: '" + sourc.fields[1].enumType + "' does not exist on Source"); } expect(error).to.be.true; - }); }); diff --git a/src/util/configParser.ts b/src/util/configParser.ts index 679cc9fda3f436b0046e33d30cde000ae733ba28..0e845f88439f7ae88759516d558d3a1232e93f73 100644 --- a/src/util/configParser.ts +++ b/src/util/configParser.ts @@ -322,8 +322,11 @@ export class ConfigParser { public static parseDimOpts (opts: DimensionStrOptions, dims: Dimension[], map: EnumTypeMap): DimensionOptions { 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"); + } + else if (type === DataType.ENUMTYPE) { + if (!(map[opts.enumType])) { + throw new Error("[Parsing error] EnumType: '" + opts.enumType + "' does not exist on Dimension"); } } if (opts.parent || opts.relation) { @@ -335,7 +338,7 @@ export class ConfigParser { description: opts.description, parent: dims[i], relation: EnumHandler.parseRelationType(opts.relation), - enumType: (EnumHandler.parseDataType(opts.dataType) === DataType.NONE) ? opts.dataType : "" + enumType: opts.enumType }; } } @@ -348,7 +351,7 @@ export class ConfigParser { description: opts.description, parent: null, relation: RelationType.NONE, - enumType: (EnumHandler.parseDataType(opts.dataType) === DataType.NONE) ? opts.dataType : "" + enumType: opts.enumType }; } @@ -399,14 +402,15 @@ export class ConfigParser { */ public static parseSourceOpts (opts: SourceStrOptions , map: EnumTypeMap): SourceOptions { for ( let k = 0; k < opts.fields.length ; k++) { - let type = EnumHandler.parseDataType(opts.fields[k].dataType); - if (type === DataType.NONE) { - if (!(map[opts.fields[k].dataType])){ - - throw new Error("[Parsing error] DataType: '" + opts.fields[k].dataType + "' does not exist on Source"); - - } - } + let type = EnumHandler.parseDataType(opts.fields[k].dataType); + if (type === DataType.NONE) { + throw new Error("[Parsing error] DataType: '" + opts.fields[k].dataType + "' does not exist on Source"); + } + else if (type === DataType.ENUMTYPE){ + if (!(map[opts.fields[k].enumType])){ + throw new Error("[Parsing error] EnumType: '" + opts.fields[k].enumType + "' does not exist on Source"); + } + } } return { name: opts.name, diff --git a/src/util/enumHandler.ts b/src/util/enumHandler.ts index 02e8818946e62aef2de5303cf9bd3a8f4c65782b..d7a9c245bcbde23874023170922faebd2128a4fc 100644 --- a/src/util/enumHandler.ts +++ b/src/util/enumHandler.ts @@ -41,6 +41,8 @@ export class EnumHandler { return "date"; case DataType.BOOLEAN: return "boolean"; + case DataType.ENUMTYPE: + return "enumtype"; default: return ""; } @@ -63,6 +65,8 @@ export class EnumHandler { return DataType.DATE; case "boolean": return DataType.BOOLEAN; + case "enumtype": + return DataType.ENUMTYPE; default: return DataType.NONE; }