-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathdto_es6.ejs
125 lines (109 loc) · 2.86 KB
/
dto_es6.ejs
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<%
for (var i = 0; i < refs.length; i += 1) {
if (refs[i] !== className) {
%>
import <%=refs[i]%> from './<%=refs[i]%>.class';<%
}
}
if (options.isValidator) {
%>
/**
* @param {string} type - the typeof the value
* @param {*} value - the value
* @returns {boolean} returns true if type is correct
* @private
*/
const _isValidType = (type, value) => {
switch (type) {
case 'Array': return (value instanceof Array);
default: return (typeof value === type.toLowerCase());
}
};
<%
}
%>const _array = (arr) => arr || [];
/**
* @constructor <%=classNameDecapitalize%>
*/
class <%=className%> {
/**
* @param {Object} <%=classNameDecapitalize%> - initial data
* @returns {void}
* @public
*/
constructor(<%=classNameDecapitalize%>) {
<%=classNameDecapitalize%> = <%=classNameDecapitalize%> || {};
<%
for (var i = 0; i < attributes.length; i += 1) {
var attr = attributes[i];
if (attr.type === 'Object') {
%> <%=classNameDecapitalize%>.<%=attr.name%> = <%=classNameDecapitalize%>.<%=attr.name%> || {};
<%
}
}
for (var i = 0; i < attributes.length; i += 1) { var attr = attributes[i]; %><% if (attr.type === 'string' || attr.type === 'number' || attr.type === 'boolean') { %>
this.<%=attr.nameCamel%> = <%=classNameDecapitalize%>.<%=attr.name%>;<% } %><% if (attr.type === 'ref') { %>
this.<%=attr.nameCamel%> = new <%=attr.ref%>(<%=classNameDecapitalize%>.<%=attr.nameCamel%>);<% } %><% if (attr.type === 'Array' && attr.subType === 'ref') { %>
this.<%=attr.nameCamel%> = _array(<%=classNameDecapitalize%>.<%=attr.nameCamel%>).map((<%=attr.subRefDecapitalize%>) => new <%=attr.subRef%>(<%=attr.subRefDecapitalize%>));<% } %><% if (attr.type === 'Array' && attr.subType !== 'ref') { %>
this.<%=attr.nameCamel%> = <%=classNameDecapitalize%>.<%=attr.name%>;<% } %><% } %>
}
<%
for (var i = 0; i < attributes.length; i += 1) {
var attr = attributes[i];
var type;
if (attr.type === 'Array') {
if (attr.subType === 'ref') {
type = `Array<${attr.subRef}>`;
} else {
type = `Array<${attr.subType}>`;
}
} else if (attr.type === 'ref') {
type = attr.ref;
} else {
type = attr.type;
}
if (attr.type !== 'Object') {
if (options.isGetter) {
%>
/**
* @returns {<%-type%>} returns <%=attr.nameCamel%>
* @public
*/
get <%=attr.nameCamel%>() {
return this.<%=attr.nameCamel%>;
}
<%
}
if (options.isSetter) {
%>
/**
* @param {<%-type%>} value - the set value
* @returns {void}
* @public
*/
set <%=attr.nameCamel%>(value) {
this.<%=attr.nameCamel%> = value;
}
<%
}
if (options.isValidator) {
%>
/**
* @returns {boolean} returns true if <%=attr.nameCapitalize%> is valid
* @public
*/
valid<%=attr.nameCapitalize%>() {
try {
const v = this.<%=attr.name%>;
return (v && _isValidType('<%=attr.type%>', v));
} catch (err) {
return false;
}
}
<%
}
}
}
%>
}
export default <%=className%>;