-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathTypes.utils.js
166 lines (148 loc) · 4.35 KB
/
Types.utils.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
/*
* (c) Copyright IBM Corporation 2021
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* Converts from async api defined types (https://www.asyncapi.com/docs/specifications/v2.5.0#dataTypeFormat)
* to native Java types
*/
export function asyncApiToJavaType(type, format) {
// try to use the format first, as it is more specific
let asyncApiType = asyncApiFormatToJavaType(format);
if (!asyncApiType) {
// no recognised format, so resort to using the type
asyncApiType = asyncApiTypeToJavaType(type);
}
if (!asyncApiType) {
// still nothing recognised, so need to report an error
throw new Error('Unsupported Type');
}
return asyncApiType;
}
function asyncApiFormatToJavaType(format) {
switch (format) {
case 'int32':
return 'int';
case 'int64':
return 'long';
case 'float':
return 'float';
case 'double':
return 'double';
case 'byte':
return 'byte';
case 'binary':
return 'String';
case 'date':
return 'String';
case 'date-time':
return 'String';
case 'password':
return 'String';
case 'uuid':
return 'UUID';
}
}
function asyncApiTypeToJavaType(type) {
switch (type) {
case 'integer':
return 'int';
case 'number':
// using double by default, as no format
// was specified
return 'double';
case 'string':
return 'String';
case 'boolean':
return 'boolean';
}
}
/*
* Helper class to easily assign local class properties
*/
export function setLocalVariables(properties) {
return Object.entries(properties).map(([name, property]) => {
return `
this.${name} = ${name};
`;
});
}
/*
* Helper class to define variables
*/
export function defineVariablesForProperties(payload) {
return Object.entries(payload.properties()).map(([name, property]) => {
let varDeclaration = `public ${asyncApiToJavaType(property.type(), property.format())} ${name};`;
if (payload.required() && payload.required().includes(name)) {
varDeclaration = `@JsonProperty(required = true)\n${ varDeclaration}`;
}
return varDeclaration;
});
}
/*
* Helper class to pass variables into a function defined by properties
*/
export function passJavaArgs(properties) {
return Object.entries(properties).map(([name, property]) => {
return `${name}`;
}).join(',');
}
/*
* Helper class to convert from async api properties to Java arguments
*/
export function createJavaArgsFromProperties(properties) {
return Object.entries(properties).map(([name, property]) => {
return `${asyncApiToJavaType(property.type(), property.format())} ${name}`;
});
}
/*
* Helper class to create Java constructor input from asyncapi properties
*/
export function createJavaConstructorArgs(properties) {
return Object.entries(properties).map(([name, property]) => {
return `${asyncApiToDemoValue(property.type(), property.format())}`;
});
}
/*
* Generates an example value from asyncAPI datatype in Java
*/
export function asyncApiToDemoValue(type, format) {
const strWords = ['ASyncAPI', 'Java', 'React', 'Hackathon', 'Community', 'Open Source', 'Publish', 'Subscribe', 'Topic', 'Demo', 'Example', 'Template', 'Producer', 'Consumer', 'Generator', 'Message', 'Endpoint'];
const boolWords = ['true', 'false'];
switch (type) {
case 'integer':
case 'long':
return parseInt(Math.random() * 1000, 10);
case 'float':
case 'double':
case 'number':
return Math.random();
case 'string':
case 'binary':
case 'password':
if (format === 'uuid') {
return 'UUID.randomUUID()';
}
return `"${ strWords[Math.floor(Math.random()*strWords.length)]}"`;
case 'byte':
return 0 + parseInt(Math.random() * ((127 - 1) + 1), 10);
case 'boolean':
return boolWords[Math.floor(Math.random()*boolWords.length)];
case 'date':
case 'dateTime':
return (new Date()).toISOString().split('T')[0];
default:
throw new Error('Unsupported Type');
}
}