diffusion
Version:
Diffusion JavaScript client
275 lines (274 loc) • 9.74 kB
JavaScript
"use strict";
/**
* @module diffusion.datatypes
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.DataTypes = exports.DataTypesImpl = void 0;
var errors_1 = require("./../../errors/errors");
var any_datatype_1 = require("./../data/any-datatype");
var binary_datatype_impl_1 = require("./../data/binary/binary-datatype-impl");
var binary_impl_1 = require("./../data/binary/binary-impl");
var json_datatype_impl_1 = require("./../data/json/json-datatype-impl");
var json_impl_1 = require("./../data/json/json-impl");
var double_datatype_impl_1 = require("./../data/primitive/double-datatype-impl");
var int64_datatype_impl_1 = require("./../data/primitive/int64-datatype-impl");
var int64_impl_1 = require("./../data/primitive/int64-impl");
var string_datatype_impl_1 = require("./../data/primitive/string-datatype-impl");
var recordv2_datatype_impl_1 = require("./../data/record/recordv2-datatype-impl");
var recordv2_impl_1 = require("./../data/record/recordv2-impl");
var time_series_event_datatype_1 = require("./../timeseries/time-series-event-datatype");
var uint8array_1 = require("./../util/uint8array");
var topic_type_1 = require("../../topics/topic-type");
/**
* The implementation of the {@link DataTypes} interface
*/
var DataTypesImpl = /** @class */ (function () {
function DataTypesImpl() {
/**
* The RecordV2 datatype
*/
this.recordv2Impl = new recordv2_datatype_impl_1.RecordV2DataTypeImpl();
/**
* The binary datatype
*/
this.binaryImpl = new binary_datatype_impl_1.BinaryDataTypeImpl();
/**
* The string datatype
*/
this.stringImpl = new string_datatype_impl_1.StringDataTypeImpl();
/**
* The double datatype
*/
this.doubleImpl = new double_datatype_impl_1.DoubleDataTypeImpl();
/**
* The Int64 datatype
*/
this.int64Impl = new int64_datatype_impl_1.Int64DataTypeImpl();
/**
* The JSON datatype
*/
this.jsonImpl = new json_datatype_impl_1.JSONDataTypeImpl();
/**
* The Any datatype
*/
this.anyImpl = new any_datatype_1.AnyDataTypeImpl();
}
/**
* @inheritdoc
*/
DataTypesImpl.prototype.binary = function () {
return this.binaryImpl;
};
/**
* @inheritdoc
*/
DataTypesImpl.prototype.string = function () {
return this.stringImpl;
};
/**
* @inheritdoc
*/
DataTypesImpl.prototype.double = function () {
return this.doubleImpl;
};
/**
* @inheritdoc
*/
DataTypesImpl.prototype.int64 = function () {
return this.int64Impl;
};
/**
* @inheritdoc
*/
DataTypesImpl.prototype.json = function () {
return this.jsonImpl;
};
/**
* @inheritdoc
*/
DataTypesImpl.prototype.recordv2 = function () {
return this.recordv2Impl;
};
/**
* @inheritdoc
*/
DataTypesImpl.prototype.any = function () {
return this.anyImpl;
};
/**
* @inheritdoc
*/
DataTypesImpl.prototype.timeseries = function (valueType) {
return time_series_event_datatype_1.TimeSeriesEventDataType.create(valueType);
};
/**
* Obtain a {@link DataType} implementation by type
* name
*
* @param name the type name as returned by {@link DataType.name DataType.name}
* @return the data type or `null` if no datatype was found
*/
DataTypesImpl.prototype.getByName = function (name) {
switch (name.toLowerCase()) {
case 'json':
return this.json();
case 'int64':
return this.int64();
case 'binary':
return this.binary();
case 'string':
return this.string();
case 'double':
return this.double();
case 'record_v2':
return this.recordv2();
default:
return undefined;
}
};
/**
* Obtain a {@link DataType} implementation by topic
* type, or value class
*
* @param name the value or a topic type
* @return the data type or `undefined` if no datatype was found
*/ // eslint-disable-next-line complexity
DataTypesImpl.prototype.getByValue = function (value) {
if (value === topic_type_1.TopicTypeEnum.BINARY || value instanceof binary_impl_1.BinaryImpl || uint8array_1.isUint8Array(value)) {
return this.binary();
}
if (value === topic_type_1.TopicTypeEnum.JSON
|| value instanceof json_impl_1.JSONImpl
|| value.constructor === Object
|| typeof value === 'boolean') {
return this.json();
}
if (value === topic_type_1.TopicTypeEnum.STRING || typeof value === 'string') {
return this.string();
}
if (value === topic_type_1.TopicTypeEnum.DOUBLE || typeof value === 'number') {
return this.double();
}
if (value === topic_type_1.TopicTypeEnum.INT64 || value instanceof int64_impl_1.Int64Impl) {
return this.int64();
}
if (value === topic_type_1.TopicTypeEnum.RECORD_V2 || value instanceof recordv2_impl_1.RecordV2Impl) {
return this.recordv2();
}
return undefined;
};
/**
* @inheritdoc
*/
DataTypesImpl.prototype.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;
};
/**
* Obtain a {@link DataType} implementation by type
* name, topic type, or value class
*
* @param name the type name as returned by {@link DataType.name DataType.name}, the value
* or a topic type.
* @return the data type or `null` if no datatype was found
* @throws an {@link IllegalArgumentError} if there is no data type for provided class
*/
DataTypesImpl.prototype.getChecked = function (type) {
if (type === null || type === undefined) {
throw new errors_1.IllegalArgumentError("No data type for " + type);
}
if (type instanceof recordv2_datatype_impl_1.RecordV2DataTypeImpl ||
type instanceof binary_datatype_impl_1.BinaryDataTypeImpl ||
type instanceof string_datatype_impl_1.StringDataTypeImpl ||
type instanceof double_datatype_impl_1.DoubleDataTypeImpl ||
type instanceof int64_datatype_impl_1.Int64DataTypeImpl ||
type instanceof json_datatype_impl_1.JSONDataTypeImpl) {
return type;
}
var datatype = this.get(type);
if (datatype === null) {
throw new errors_1.IllegalArgumentError("No data type for " + type);
}
return datatype;
};
/**
* Obtain a value class implementation by {@link DataType}, string, or topic type
*
* For {@link DoubleDataType}, the associated value class is `Number`.
*
* @param valueClass the class, type name, or topic type
* @return the data type or `undefined` if no data type was found
* @throws an {@link IllegalArgumentError} if there is no data type for provided class
*/
DataTypesImpl.prototype.getValueClassChecked = function (type) {
var datatype = this.getChecked(type);
switch (datatype) {
case this.jsonImpl:
return json_impl_1.JSONImpl;
case this.binaryImpl:
return binary_impl_1.BinaryImpl;
case this.stringImpl:
return String;
case this.doubleImpl:
return Number;
case this.int64Impl:
return int64_impl_1.Int64Impl;
case this.recordv2Impl:
return recordv2_impl_1.RecordV2Impl;
default:
return undefined;
}
};
/**
* Obtain a {@link DataType} implementation by value class.
*
* For {@link DoubleDataType}, the associated value class is `Number`.
*
* @param valueClass the class
* @return the data type or `undefined` if no data type was found
*/
DataTypesImpl.prototype.getByClassSafely = function (valueClass) {
switch (valueClass) {
case json_impl_1.JSONImpl:
return this.jsonImpl;
case binary_impl_1.BinaryImpl:
return this.binaryImpl;
case String:
return this.stringImpl;
case Number:
return this.doubleImpl;
case int64_impl_1.Int64Impl:
return this.int64Impl;
case recordv2_impl_1.RecordV2Impl:
return this.recordv2Impl;
default:
return undefined;
}
};
/**
* @inheritdoc
*/
DataTypesImpl.prototype.getByClass = function (valueClass) {
var datatype = this.getByClassSafely(valueClass);
if (!datatype) {
throw new errors_1.IllegalArgumentError("No data type for " + valueClass);
}
else {
return datatype;
}
};
return DataTypesImpl;
}());
exports.DataTypesImpl = DataTypesImpl;
// eslint-disable-next-line @typescript-eslint/naming-convention
var DataTypesSingleton = new DataTypesImpl();
exports.DataTypes = DataTypesSingleton;