diffusion
Version:
Diffusion JavaScript client
191 lines (155 loc) • 5.22 kB
JavaScript
var _implements = require('util/interface')._implements;
var DataTypes = require('../../data/datatypes');
var TopicType = require('../../topics/topics').TopicType;
var BinaryDataTypeImpl = require('data/binary/binary-datatype-impl');
var BinaryImpl = require('data/binary/binary-impl');
var JSONDataTypeImpl = require('data/json/json-datatype-impl');
var JSONImpl = require('data/json/json-impl');
var Int64DataTypeImpl = require('data/primitive/int64-datatype-impl');
var Int64Impl = require('data/primitive/int64-impl');
var StringDataTypeImpl = require('data/primitive/string-datatype-impl');
var DoubleDataTypeImpl = require('data/primitive/double-datatype-impl');
var RecordV2DataTypeImpl = require('data/record/recordv2-datatype-impl');
var RecordV2Impl = require('data/record/recordv2-impl');
var DataTypesImpl = _implements(DataTypes, function DataTypesImpl() {
var recordv2 = new RecordV2DataTypeImpl();
var binary = new BinaryDataTypeImpl();
var string = new StringDataTypeImpl();
var double = new DoubleDataTypeImpl();
var int64 = new Int64DataTypeImpl();
var json = new JSONDataTypeImpl();
var self = this;
this.binary = function() {
return binary;
};
this.string = function() {
return string;
};
this.double = function() {
return double;
};
this.int64 = function() {
return int64;
};
this.json = function() {
return json;
};
this.recordv2 = function() {
return recordv2;
};
this.getByName = function(name) {
switch (name.toLowerCase()) {
case 'json' :
return self.json();
case 'int64' :
return self.int64();
case 'binary' :
return self.binary();
case 'string' :
return self.string();
case 'double' :
return self.double();
case 'record_v2' :
return self.recordv2();
}
};
/*eslint complexity: ["error", 31]*/
this.getByValue = function(value) {
if (value === TopicType.BINARY || BinaryImpl.isPrototypeOf(value) || Buffer.isBuffer(value)) {
return self.binary();
}
if (value === TopicType.JSON || JSONImpl.isPrototypeOf(value) || value.constructor === Object) {
return self.json();
}
if (value === TopicType.STRING || typeof value === "string") {
return self.string();
}
if (value === TopicType.DOUBLE || typeof value === "number") {
return self.double();
}
if (value === TopicType.INT64 || Int64Impl.isPrototypeOf(value)) {
return self.int64();
}
if (value === TopicType.RECORD_V2 || RecordV2Impl.isPrototypeOf(value)) {
return self.recordv2();
}
};
this.get = function(type) {
var datatype;
if (typeof type === "string") {
datatype = this.getByName(type);
}
if (!datatype) {
datatype = this.getByValue(type);
}
if (!datatype) {
datatype = this.getByClassSafely(type);
}
return datatype ? datatype : null;
};
this.getChecked = function(type) {
if (!type) {
throw new Error("No data type for " + type);
}
if (RecordV2DataTypeImpl.isPrototypeOf(type) ||
BinaryDataTypeImpl.isPrototypeOf(type) ||
StringDataTypeImpl.isPrototypeOf(type) ||
DoubleDataTypeImpl.isPrototypeOf(type) ||
Int64DataTypeImpl.isPrototypeOf(type) ||
JSONDataTypeImpl.isPrototypeOf(type)) {
return type;
}
var datatype = this.get(type);
if (datatype === null) {
throw new Error("No data type for " + type);
}
return datatype;
};
this.getValueClassChecked = function(type) {
var datatype = this.getChecked(type);
if (datatype === json) {
return JSONImpl;
}
else if (datatype === binary) {
return BinaryImpl;
}
else if (datatype === string) {
return String;
}
else if (datatype === double) {
return Number;
}
else if (datatype === int64) {
return Int64Impl;
}
else if (datatype === recordv2) {
return RecordV2Impl;
}
};
this.getByClassSafely = function(clazz) {
switch (clazz) {
case JSONImpl :
return json;
case BinaryImpl :
return binary;
case String :
return string;
case Number :
return double;
case Int64Impl :
return int64;
case RecordV2Impl :
return recordv2;
}
};
this.getByClass = function(clazz) {
var datatype = this.getByClassSafely(clazz);
if (!datatype) {
throw new Error("No data type for " + clazz);
}
else {
return datatype;
}
};
});
module.exports = new DataTypesImpl();