-
Notifications
You must be signed in to change notification settings - Fork 7
/
examples.ts
97 lines (86 loc) · 3.2 KB
/
examples.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import utils from '../../commons/utils/utils';
import ramlUtil from '../../commons/utils/ramlUtil';
import cacheUtil from '../../commons/utils/cacheUtil';
import schemaUtil from '../../commons/utils/schemaUtil';
import config from '../../configs/config';
import { Schema, Table, Column, Example, Everything } from '../../commons/types';
/**
* Class which holds the logic for generating example data.
*
* @export
* @class ExamplesGenerator
*/
export class ExamplesGenerator {
/**
* Array of default raml types supported.
*
* @private
* @type {string[]}
* @memberof ExamplesGenerator
*/
private defaultRamlTypes: string[] = [
'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
*/
public PRIME_KEY: string = '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
*/
public generateTypeExampleContent(schema: Schema, table: Table, depthLevel: number): Example {
const object: Example = {
type: utils.toTitleCase(table.name),
data: {},
};
object.data = cacheUtil.get(this.PRIME_KEY, object.type) || {};
if (Object.keys(object.data).length) {
return object;
}
table.columns.map((column: Column) => {
if (this.defaultRamlTypes.indexOf(column.dataType.type) > -1) {
object.data[column.name] = ramlUtil.generateFakeData(column.name, column.dataType.type, column.dataType.values);
} else {
const cachedObject: Everything = cacheUtil.get(
this.PRIME_KEY,
`${column.dataType.type}${column.dataType.isArray ? '[]' : ''}`,
);
if (cachedObject) {
object.data[column.name] = cachedObject;
} else {
/* istanbul ignore next */
if (depthLevel >= config.DEFAULT_MAX_DEPTH_LEVEL) {
object.data[column.name] = column.dataType.isArray ? [] : {};
} else {
const normalizedTable: Table | undefined = schemaUtil.getNormalizedTableByType(
schema,
column.dataType.type,
);
if (normalizedTable) {
object.data[column.name] = column.dataType.isArray ?
utils.fillArray(
this.generateTypeExampleContent(schema, normalizedTable, depthLevel + config.DEPTH_INCREMENT).data,
config.DEFAULT_ARRAY_LENGTH,
) :
this.generateTypeExampleContent(schema, normalizedTable, depthLevel + config.DEPTH_INCREMENT).data;
}
}
}
}
});
cacheUtil.add(this.PRIME_KEY, object.type, object.data);
cacheUtil.add(this.PRIME_KEY, `${object.type}[]`, utils.fillArray(object.data, config.DEFAULT_ARRAY_LENGTH));
return object;
}
}
export default new ExamplesGenerator();