-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
AbstractJSONSchema2Pojo.java
345 lines (314 loc) · 11.6 KB
/
AbstractJSONSchema2Pojo.java
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/*
* Copyright (C) 2015 Red Hat, Inc.
*
* 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.
*/
package io.fabric8.java.generator.nodes;
import com.fasterxml.jackson.databind.JsonNode;
import com.github.javaparser.ast.expr.AnnotationExpr;
import com.github.javaparser.ast.expr.Name;
import com.github.javaparser.ast.expr.SingleMemberAnnotationExpr;
import com.github.javaparser.ast.expr.StringLiteralExpr;
import io.fabric8.java.generator.Config;
import io.fabric8.java.generator.exceptions.JavaGeneratorException;
import io.fabric8.kubernetes.api.model.apiextensions.v1.JSONSchemaProps;
import java.util.Locale;
import java.util.function.Function;
import static io.fabric8.java.generator.nodes.Keywords.*;
public abstract class AbstractJSONSchema2Pojo {
static final String BOOLEAN_CRD_TYPE = "boolean";
static final String INTEGER_CRD_TYPE = "integer";
static final String INT32_CRD_TYPE = "int32";
static final String INT64_CRD_TYPE = "int64";
static final String NUMBER_CRD_TYPE = "number";
static final String FLOAT_CRD_TYPE = "float";
static final String DOUBLE_CRD_TYPE = "double";
static final String STRING_CRD_TYPE = "string";
static final String DATETIME_CRD_TYPE = "date-time";
static final String OBJECT_CRD_TYPE = "object";
static final String ARRAY_CRD_TYPE = "array";
public static final AnnotationExpr newGeneratedAnnotation() {
return new SingleMemberAnnotationExpr(
new Name("javax.annotation.processing.Generated"),
new StringLiteralExpr("io.fabric8.java.generator.CRGeneratorRunner"));
}
protected final String description;
protected final Config config;
protected final boolean isNullable;
protected final JsonNode defaultValue;
protected Double maximum;
protected Double minimum;
protected String format;
protected String pattern;
public Double getMaximum() {
return maximum;
}
public Double getMinimum() {
return minimum;
}
public String getFormat() {
return format;
}
public String getPattern() {
return pattern;
}
public abstract String getType();
public abstract GeneratorResult generateJava();
/** Takes a string and return the corresponding package name */
public static String packageName(String str) {
String pkg = str.toLowerCase(Locale.ROOT);
if (pkg.equals(str)) { // avoid package/class name clash
pkg = "_" + pkg;
}
// https://docs.oracle.com/javase/tutorial/java/package/namingpkgs.html
// if the package name contains a reserved Java keyword ... the suggested convention is to add an underscore
if (JAVA_KEYWORDS.contains(pkg)) {
pkg = pkg + "_";
}
return pkg;
}
public String getDescription() {
return description;
}
protected JsonNode getDefaultValue() {
return defaultValue;
}
protected String getClassType() {
return getType();
}
protected AbstractJSONSchema2Pojo(Config config, String description, final boolean isNullable, JsonNode defaultValue,
final ValidationProperties validationProperties) {
this.config = config;
this.description = description;
this.isNullable = isNullable;
this.defaultValue = defaultValue;
if (validationProperties != null) {
this.maximum = validationProperties.getMaximum();
this.minimum = validationProperties.getMinimum();
this.format = validationProperties.getFormat();
this.pattern = validationProperties.getPattern();
}
}
/** Takes a random string and manipulate it to be a valid Java identifier */
public static String sanitizeString(String str) {
str = str.trim();
String sanitized = "";
// https://docs.oracle.com/javase/specs/jls/se7/html/jls-3.html#jls-3.8
if (JAVA_KEYWORDS.contains(str) || (!str.isEmpty() && !Character.isJavaIdentifierStart(str.charAt(0)))) {
sanitized = "_" + str;
} else {
sanitized = str;
}
if (sanitized.startsWith("-")) {
sanitized = sanitized.replaceFirst("-", "minus");
}
int index = sanitized.indexOf('-');
while (index != -1) {
int next = Math.min(index + 2, sanitized.length());
sanitized = sanitized.substring(0, index)
+ sanitized.substring(index + 1, next).toUpperCase()
+ sanitized.substring(next);
index = sanitized.indexOf('-');
}
StringBuilder sanitizedSb = new StringBuilder(sanitized.length());
for (char originalChar : sanitized.toCharArray()) {
char sanitizedChar = Character.isJavaIdentifierPart(originalChar) ? originalChar : '_';
sanitizedSb.append(sanitizedChar);
}
sanitized = sanitizedSb.toString();
return sanitized;
}
public static String escapeQuotes(String str) {
return str.replace("\"", "\\\"").replace("\'", "\\\'");
}
private static String getRefinedIntegerType(String format) {
if (format == null || format.equals(INT64_CRD_TYPE)) {
return INT64_CRD_TYPE;
} else if (format.equals(INT32_CRD_TYPE)) {
return INT32_CRD_TYPE;
} else {
throw new JavaGeneratorException("Unsupported format for integer found " + format);
}
}
private static String getRefinedNumberType(String format) {
if (format == null || format.equals(DOUBLE_CRD_TYPE)) {
return DOUBLE_CRD_TYPE;
} else if (format.equals(FLOAT_CRD_TYPE)) {
return FLOAT_CRD_TYPE;
} else {
throw new JavaGeneratorException("Unsupported format for number found " + format);
}
}
private static String getRefinedStringType(String format) {
if (format == null || format.equals(STRING_CRD_TYPE)) {
return STRING_CRD_TYPE;
} else if (format.equals(DATETIME_CRD_TYPE)) {
return DATETIME_CRD_TYPE;
} else {
// TODO: there are more string format to support: byte, date etc.
return STRING_CRD_TYPE;
}
}
public static AbstractJSONSchema2Pojo fromJsonSchema(
String key,
JSONSchemaProps prop,
String parentPkg,
Config config) {
Function<JavaNameAndType, AbstractJSONSchema2Pojo> fromJsonSchema = javaNameAndType -> fromJsonSchema(
key,
javaNameAndType,
prop,
parentPkg,
config);
String type = prop.getType();
if (Boolean.TRUE.equals(prop.getXKubernetesIntOrString())) {
return fromJsonSchema.apply(JPrimitiveNameAndType.INT_OR_STRING);
} else if (type == null
&& Boolean.TRUE.equals(prop.getXKubernetesPreserveUnknownFields())) {
return fromJsonSchema.apply(JPrimitiveNameAndType.ANY_TYPE);
} else if (prop.getEnum() != null && prop.getEnum().size() > 0) {
return fromJsonSchema.apply(new JEnumNameAndType(key));
} else {
if (type == null) {
throw new IllegalArgumentException("Type for key:" + key + " is null");
}
switch (type) {
case BOOLEAN_CRD_TYPE:
return fromJsonSchema.apply(JPrimitiveNameAndType.BOOL);
case INTEGER_CRD_TYPE:
switch (getRefinedIntegerType(prop.getFormat())) {
case INT32_CRD_TYPE:
return fromJsonSchema.apply(JPrimitiveNameAndType.INTEGER);
case INT64_CRD_TYPE:
default:
return fromJsonSchema.apply(JPrimitiveNameAndType.LONG);
}
case NUMBER_CRD_TYPE:
switch (getRefinedNumberType(prop.getFormat())) {
case FLOAT_CRD_TYPE:
return fromJsonSchema.apply(JPrimitiveNameAndType.FLOAT);
case DOUBLE_CRD_TYPE:
default:
return fromJsonSchema.apply(JPrimitiveNameAndType.DOUBLE);
}
case STRING_CRD_TYPE:
switch (getRefinedStringType(prop.getFormat())) {
case DATETIME_CRD_TYPE:
return fromJsonSchema.apply(JPrimitiveNameAndType.DATETIME);
case STRING_CRD_TYPE:
default:
return fromJsonSchema.apply(JPrimitiveNameAndType.STRING);
}
case OBJECT_CRD_TYPE:
if (prop.getAdditionalProperties() != null && prop.getAdditionalProperties().getSchema() != null) {
return fromJsonSchema.apply(new JMapNameAndType(key));
} else if (prop.getAdditionalProperties() != null
&& Boolean.TRUE.equals(prop.getAdditionalProperties().getAllows())) {
return fromJsonSchema.apply(JPrimitiveNameAndType.ANY_TYPE);
} else {
return fromJsonSchema.apply(new JObjectNameAndType(key));
}
case ARRAY_CRD_TYPE:
return fromJsonSchema.apply(new JArrayNameAndType(key));
default:
throw new JavaGeneratorException("Unmanaged type " + prop.getType());
}
}
}
private static AbstractJSONSchema2Pojo fromJsonSchema(
String key,
JavaNameAndType nt,
JSONSchemaProps prop,
String parentPkg,
Config config) {
final boolean isNullable = Boolean.TRUE.equals(prop.getNullable());
switch (nt.getType()) {
case PRIMITIVE:
return new JPrimitive(
nt.getName(),
config,
prop.getDescription(),
isNullable,
prop.getDefault(),
ValidationProperties.Builder.getInstance()
.withMaximum(prop.getMaximum())
.withMinimum(prop.getMinimum())
.withFormat(prop.getFormat())
.withPattern(prop.getPattern())
.build());
case ARRAY:
return new JArray(
fromJsonSchema(
key,
prop.getItems().getSchema(),
parentPkg,
config),
config,
prop.getDescription(),
isNullable,
prop.getDefault());
case MAP:
return new JMap(
fromJsonSchema(
key,
prop.getAdditionalProperties().getSchema(),
parentPkg,
config),
config,
prop.getDescription(),
isNullable,
prop.getDefault());
case OBJECT:
final boolean preserveUnknownFields = Boolean.TRUE.equals(prop.getXKubernetesPreserveUnknownFields());
return new JObject(
parentPkg,
key,
prop.getProperties(),
prop.getRequired(),
preserveUnknownFields,
config,
prop.getDescription(),
isNullable,
prop.getDefault());
case ENUM:
String enumType = JAVA_LANG_STRING;
switch (prop.getType()) {
case INTEGER_CRD_TYPE:
switch (getRefinedIntegerType(prop.getFormat())) {
case INT32_CRD_TYPE:
enumType = JAVA_LANG_INTEGER;
break;
case INT64_CRD_TYPE:
default:
enumType = JAVA_LANG_LONG;
break;
}
break;
case STRING_CRD_TYPE:
break;
default:
throw new JavaGeneratorException("Unsupported enumeration type/format" + prop.getType() + "/" + prop.getFormat());
}
return new JEnum(
key,
enumType,
prop.getEnum(),
config,
prop.getDescription(),
isNullable,
prop.getDefault());
default:
throw new JavaGeneratorException("Unreachable " + nt.getType());
}
}
}