"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var utils_1 = require("../../commons/utils/utils");
var ramlUtil_1 = require("../../commons/utils/ramlUtil");
var cacheUtil_1 = require("../../commons/utils/cacheUtil");
var schemaUtil_1 = require("../../commons/utils/schemaUtil");
var config_1 = require("../../configs/config");
/**
* Class which holds the logic for generating example data.
*
* @export
* @class ExamplesGenerator
*/
var ExamplesGenerator = (function () {
function ExamplesGenerator() {
/**
* Array of default raml types supported.
*
* @private
* @type {string[]}
* @memberof ExamplesGenerator
*/
this.defaultRamlTypes = [
'number', 'boolean', 'string', 'date-only', 'datetime',
'time-only', 'datetime-only', 'file', 'nil', 'union', 'enum',
];
/**
* Key used in storing and searching for cached elements.
*
* @public
* @type {string}
* @memberof ExamplesGenerator
*/
this.PRIME_KEY = 'raml';
}
/**
* Generate type example for a certain table
* @param {*} schema - entire collection of tables
* @param {*} table - table information
* @param {int} depthLevel - columns depth level to be computed
* @return {Example} object containing - type name(with titleCase)
* - table content object to be stringified
*/
ExamplesGenerator.prototype.generateTypeExampleContent = function (schema, table, depthLevel) {
var _this = this;
var object = {
type: utils_1.default.toTitleCase(table.name),
data: {},
};
object.data = cacheUtil_1.default.get(this.PRIME_KEY, object.type) || {};
if (Object.keys(object.data).length) {
return object;
}
table.columns.map(function (column) {
if (_this.defaultRamlTypes.indexOf(column.dataType.type) > -1) {
object.data[column.name] = ramlUtil_1.default.generateFakeData(column.name, column.dataType.type, column.dataType.values);
}
else {
var cachedObject = cacheUtil_1.default.get(_this.PRIME_KEY, "" + column.dataType.type + (column.dataType.isArray ? '[]' : ''));
Eif (cachedObject) {
object.data[column.name] = cachedObject;
}
else {
/* istanbul ignore next */
if (depthLevel >= config_1.default.DEFAULT_MAX_DEPTH_LEVEL) {
object.data[column.name] = column.dataType.isArray ? [] : {};
}
else {
var normalizedTable = schemaUtil_1.default.getNormalizedTableByType(schema, column.dataType.type);
if (normalizedTable) {
object.data[column.name] = column.dataType.isArray ?
utils_1.default.fillArray(_this.generateTypeExampleContent(schema, normalizedTable, depthLevel + config_1.default.DEPTH_INCREMENT).data, config_1.default.DEFAULT_ARRAY_LENGTH) :
_this.generateTypeExampleContent(schema, normalizedTable, depthLevel + config_1.default.DEPTH_INCREMENT).data;
}
}
}
}
});
cacheUtil_1.default.add(this.PRIME_KEY, object.type, object.data);
cacheUtil_1.default.add(this.PRIME_KEY, object.type + "[]", utils_1.default.fillArray(object.data, config_1.default.DEFAULT_ARRAY_LENGTH));
return object;
};
return ExamplesGenerator;
}());
exports.ExamplesGenerator = ExamplesGenerator;
exports.default = new ExamplesGenerator();
|