Commit f00a8a08 authored by Lucas Fernandes de Oliveira's avatar Lucas Fernandes de Oliveira

Merge branch 'issue/82' into 'develop'

Issue #82: Add optional field for Enumtype

See merge request !71
parents ea6ddc63 f3fc716a
Pipeline #17086 passed with stages
in 1 minute and 8 seconds
......@@ -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"
......
......@@ -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);
}
}
}
......
......@@ -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;
}
}
......@@ -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;
}
......
......@@ -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;
});
});
......@@ -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,
......
......@@ -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;
}
......
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