-
-
Notifications
You must be signed in to change notification settings - Fork 55
/
all.js
232 lines (212 loc) · 6.87 KB
/
all.js
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
const filter = module.exports;
const _ = require('lodash');
function defineType(prop, propName) {
if (prop.additionalProperties()) {
if (prop.additionalProperties() === true) {
return 'Map<String, Object>';
} else if (prop.additionalProperties().type() === 'object') {
return 'Map<String, ' + _.upperFirst(_.camelCase(prop.additionalProperties().uid())) + '>';
} else if (prop.additionalProperties().format()) {
return 'Map<String, ' + toClass(toJavaType(prop.additionalProperties().format())) + '>';
} else if (prop.additionalProperties().type()) {
return 'Map<String, ' + toClass(toJavaType(prop.additionalProperties().type())) + '>';
}
} else if (prop.type() === 'object') {
return _.upperFirst(_.camelCase(prop.uid()));
} else if (prop.type() === 'array') {
if (prop.items().type() === 'object') {
return 'List<' + _.upperFirst(_.camelCase(prop.items().uid())) + '>';
} else if (prop.items().format()) {
return 'List<' + toClass(toJavaType(prop.items().format())) + '>';
} else {
return 'List<' + toClass(toJavaType(prop.items().type())) + '>';
}
} else if (prop.enum() && (prop.type() === 'string' || prop.type() === 'integer')) {
return _.upperFirst(_.camelCase(propName)) + 'Enum';
} else if (prop.anyOf() || prop.oneOf()) {
let propType = 'OneOf';
let hasPrimitive = false;
[].concat(prop.anyOf(), prop.oneOf()).filter(obj => obj != null).forEach(obj => {
hasPrimitive |= obj.type() !== 'object';
propType += _.upperFirst(_.camelCase(obj.uid()));
});
if (hasPrimitive) {
propType = 'Object';
}
return propType;
} else if (prop.allOf()) {
let propType = 'AllOf';
prop.allOf().forEach(obj => {
propType += _.upperFirst(_.camelCase(obj.uid()));
});
return propType;
} else {
if (prop.format()) {
return toJavaType(prop.format());
} else {
return toJavaType(prop.type());
}
}
}
filter.defineType = defineType;
function toClass(couldBePrimitive) {
switch(couldBePrimitive) {
case 'int':
return 'Integer';
case 'long':
return 'Long';
case 'boolean':
return 'Boolean';
case 'float':
return 'Float';
case 'double':
return 'Double';
default:
return couldBePrimitive;
}
}
filter.toClass = toClass;
function toJavaType(str, isRequired) {
let resultType;
switch(str) {
case 'integer':
case 'int32':
resultType = 'int'; break;
case 'long':
case 'int64':
resultType = 'long'; break;
case 'boolean':
resultType = 'boolean'; break;
case 'date':
resultType = 'java.time.LocalDate'; break;
case 'time':
resultType = 'java.time.OffsetTime'; break;
case 'dateTime':
case 'date-time':
resultType = 'java.time.OffsetDateTime'; break;
case 'string':
case 'password':
case 'email':
case 'uri':
case 'hostname':
case 'ipv4':
case 'ipv6':
case 'byte':
resultType = 'String'; break;
case 'uuid':
resultType = 'java.util.UUID'; break;
case 'float':
resultType = 'float'; break;
case 'number':
case 'double':
resultType = 'double'; break;
case 'decimal':
resultType = 'java.math.BigDecimal'; break;
case 'binary':
resultType = 'byte[]'; break;
default:
resultType = 'Object'; break;
}
return isRequired ? resultType : toClass(resultType);
}
filter.toJavaType = toJavaType;
function isDefined(obj) {
return typeof obj !== 'undefined'
}
filter.isDefined = isDefined;
function isProtocol(api, protocol){
return api.constructor.stringify(api).includes('"protocol":"' + protocol + '"');
};
filter.isProtocol = isProtocol;
function isObjectType(schemas){
var res = [];
for (let obj of schemas) {
if (obj._json['type'] === 'object' && obj._json['x-parser-schema-id'] && !obj._json['x-parser-schema-id'].startsWith('<')) {
res.push(obj);
}
}
return res;
};
filter.isObjectType = isObjectType;
function examplesToString(ex){
let retStr = "";
ex.forEach(example => {
if (retStr !== "") {retStr += ", "}
if (typeof example == "object") {
try {
retStr += JSON.stringify(example);
} catch (ignore) {
retStr += example;
}
} else {
retStr += example;
}
});
return retStr;
};
filter.examplesToString = examplesToString;
function splitByLines(str){
if (str) {
return str.split(/\r?\n|\r/).filter((s) => s !== "");
} else {
return "";
}
};
filter.splitByLines = splitByLines;
function isRequired(name, list){
return list && list.includes(name);
};
filter.isRequired = isRequired;
function schemeExists(collection, scheme){
return _.some(collection, {'scheme': scheme});
};
filter.schemeExists = schemeExists;
function createEnum(val){
let result;
let withoutNonWordChars = val.replace(/[^A-Z^a-z^0-9]/g, "_");
if ((new RegExp('^[^A-Z^a-z]', 'i')).test(withoutNonWordChars)) {
result = '_' + withoutNonWordChars;
} else {
result = withoutNonWordChars;
}
return result;
};
filter.createEnum = createEnum;
function addBackSlashToPattern(val) {
let result = val.replace(/\\/g, "\\\\");
return result;
}
filter.addBackSlashToPattern = addBackSlashToPattern;
filter.currentTime = () => (new Date(Date.now())).toISOString();
function replaceAll(originalStr, replacePattern, replaceString) {
return originalStr.replaceAll(replacePattern, replaceString)
}
filter.replaceAll = replaceAll;
function toTopicString(channelName, hasParameters, parameters, convertDots, replaceParameterValue, replaceDots = "\\\\.") {
if (hasParameters) {
let topicName = channelName
if (convertDots) {
topicName = replaceAll(topicName, ".", replaceDots)
}
Object.keys(parameters).forEach(value => topicName = topicName.replace("{" + value + "}", replaceParameterValue))
return topicName
} else {
return channelName
}
}
function toKafkaTopicString(channelName, hasParameters, parameters) {
return toTopicString(channelName, hasParameters, parameters, true, ".*")
}
filter.toKafkaTopicString = toKafkaTopicString
function toMqttTopicString(channelName, hasParameters, parameters) {
return toTopicString(channelName, hasParameters, parameters, false, "+")
}
filter.toMqttTopicString = toMqttTopicString
function toAmqpNeutral(channelName, hasParameters, parameters) {
return toTopicString(_.camelCase(channelName), hasParameters, parameters, true, "", "")
}
filter.toAmqpNeutral = toAmqpNeutral
function toAmqpKey(channelName, hasParameters, parameters) {
return toTopicString(channelName, hasParameters, parameters, true, "*")
}
filter.toAmqpKey = toAmqpKey