[DEV] full rework of Typescript generator (operational)
This commit is contained in:
parent
a7220e0f76
commit
4fc5f68813
@ -5,14 +5,21 @@ import java.util.UUID;
|
||||
|
||||
import org.kar.archidata.tools.UuidUtils;
|
||||
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
import jakarta.ws.rs.core.Response;
|
||||
|
||||
public class RestErrorResponse {
|
||||
@NotNull
|
||||
public UUID uuid = UuidUtils.nextUUID();
|
||||
@NotNull
|
||||
public String name; // Mandatory for TS generic error
|
||||
@NotNull
|
||||
public String message; // Mandatory for TS generic error
|
||||
@NotNull
|
||||
public String time;
|
||||
@NotNull
|
||||
final public int status;
|
||||
@NotNull
|
||||
final public String statusMessage;
|
||||
|
||||
public RestErrorResponse(final Response.Status status, final String time, final String error,
|
||||
|
@ -11,16 +11,64 @@ import org.slf4j.LoggerFactory;
|
||||
|
||||
public class AnalyzeApi {
|
||||
static final Logger LOGGER = LoggerFactory.getLogger(AnalyzeApi.class);
|
||||
public List<ApiGroupModel> apiModels = new ArrayList<>();
|
||||
public List<ClassModel> classModels = new ArrayList<>();
|
||||
protected final List<ApiGroupModel> apiModels = new ArrayList<>();
|
||||
protected final ModelGroup modelGroup = new ModelGroup();
|
||||
|
||||
public void createApi(final List<Class<?>> classs) throws Exception {
|
||||
final ModelGroup previousModel = new ModelGroup(this.classModels);
|
||||
for (final Class<?> clazz : classs) {
|
||||
final ApiGroupModel parsed = new ApiGroupModel(clazz, previousModel);
|
||||
this.apiModels.add(parsed);
|
||||
}
|
||||
AnalyzeModel.fillModel(previousModel.previousModel);
|
||||
public void addAllModel(final List<Class<?>> classes) throws Exception {
|
||||
this.modelGroup.addAll(classes);
|
||||
analyzeModels();
|
||||
}
|
||||
|
||||
public void addModel(final Class<?> clazz) throws Exception {
|
||||
this.modelGroup.add(clazz);
|
||||
analyzeModels();
|
||||
}
|
||||
|
||||
public void addApi(final Class<?> clazz) throws Exception {
|
||||
this.apiModels.add(new ApiGroupModel(clazz, this.modelGroup));
|
||||
analyzeModels();
|
||||
}
|
||||
|
||||
public void addAllApi(final List<Class<?>> classes) throws Exception {
|
||||
for (final Class<?> clazz : classes) {
|
||||
this.apiModels.add(new ApiGroupModel(clazz, this.modelGroup));
|
||||
}
|
||||
analyzeModels();
|
||||
}
|
||||
|
||||
public List<ApiGroupModel> getAllApi() {
|
||||
return this.apiModels;
|
||||
}
|
||||
|
||||
public List<ClassModel> getAllModel() {
|
||||
return this.modelGroup.getModels();
|
||||
}
|
||||
|
||||
private void analyzeModels() throws Exception {
|
||||
final List<ClassModel> dones = new ArrayList<>();
|
||||
while (dones.size() < getAllModel().size()) {
|
||||
final List<ClassModel> copyList = new ArrayList<>(this.modelGroup.getModels());
|
||||
for (final ClassModel model : copyList) {
|
||||
if (dones.contains(model)) {
|
||||
continue;
|
||||
}
|
||||
LOGGER.info("Analyze: {}", model);
|
||||
model.analyze(this.modelGroup);
|
||||
dones.add(model);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public List<ClassModel> getCompatibleModels(final List<Class<?>> search) {
|
||||
final List<ClassModel> out = new ArrayList<>();
|
||||
for (final ClassModel model : getAllModel()) {
|
||||
if (search.contains(model.getOriginClasses())) {
|
||||
out.add(model);
|
||||
}
|
||||
}
|
||||
if (out.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
}
|
||||
|
@ -1,32 +0,0 @@
|
||||
package org.kar.archidata.externalRestApi;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.kar.archidata.externalRestApi.model.ClassModel;
|
||||
import org.kar.archidata.externalRestApi.model.ModelGroup;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class AnalyzeModel {
|
||||
static final Logger LOGGER = LoggerFactory.getLogger(AnalyzeModel.class);
|
||||
|
||||
public static void fillModel(final List<ClassModel> models) throws Exception {
|
||||
final ModelGroup previousModel = new ModelGroup(models);
|
||||
final List<ClassModel> dones = new ArrayList<>();
|
||||
while (dones.size() < previousModel.previousModel.size()) {
|
||||
LOGGER.info("Do a cycle of annalyze : new model detected: {} < {}", dones.size(),
|
||||
previousModel.previousModel.size());
|
||||
final List<ClassModel> copyList = new ArrayList<>(previousModel.previousModel);
|
||||
for (final ClassModel model : copyList) {
|
||||
if (dones.contains(model)) {
|
||||
continue;
|
||||
}
|
||||
LOGGER.info("Analyze: {}", model);
|
||||
model.analyze(previousModel);
|
||||
dones.add(model);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -1,5 +0,0 @@
|
||||
package org.kar.archidata.externalRestApi;
|
||||
|
||||
public class GeneratePythonModel {
|
||||
|
||||
}
|
@ -1,5 +0,0 @@
|
||||
package org.kar.archidata.externalRestApi;
|
||||
|
||||
public class GenerateTsModel {
|
||||
|
||||
}
|
@ -1,6 +1,6 @@
|
||||
package org.kar.archidata.externalRestApi;
|
||||
|
||||
public class GeneratePythonApi {
|
||||
public class PythonGenerateApi {
|
||||
|
||||
public static void generateApi(final AnalyzeApi api) {
|
||||
|
@ -3,13 +3,22 @@ package org.kar.archidata.externalRestApi;
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.Set;
|
||||
|
||||
import org.kar.archidata.dataAccess.DataExport;
|
||||
import org.kar.archidata.externalRestApi.TsClassElement.DefinedPosition;
|
||||
import org.kar.archidata.externalRestApi.model.ApiGroupModel;
|
||||
import org.kar.archidata.externalRestApi.model.ApiModel;
|
||||
import org.kar.archidata.externalRestApi.model.ClassEnumModel;
|
||||
import org.kar.archidata.externalRestApi.model.ClassListModel;
|
||||
import org.kar.archidata.externalRestApi.model.ClassMapModel;
|
||||
import org.kar.archidata.externalRestApi.model.ClassModel;
|
||||
import org.kar.archidata.externalRestApi.model.ClassObjectModel;
|
||||
import org.kar.archidata.externalRestApi.model.RestTypeRequest;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
@ -24,47 +33,146 @@ public class TsApiGeneration {
|
||||
/**
|
||||
* Interface of the server (auto-generated code)
|
||||
*/
|
||||
import {
|
||||
HTTPMimeType,
|
||||
HTTPRequestModel,
|
||||
ModelResponseHttp,
|
||||
RESTCallbacks,
|
||||
RESTConfig,
|
||||
RESTRequestJson,
|
||||
RESTRequestJsonArray,
|
||||
RESTRequestVoid
|
||||
} from "../rest-tools"
|
||||
|
||||
""";
|
||||
}
|
||||
|
||||
public static String generateClassEnumModelTypescript(
|
||||
final ClassEnumModel model,
|
||||
final TsClassElementGroup tsGroup,
|
||||
final Set<ClassModel> imports,
|
||||
final boolean writeMode) throws IOException {
|
||||
imports.add(model);
|
||||
final TsClassElement tsModel = tsGroup.find(model);
|
||||
return tsModel.tsTypeName;
|
||||
}
|
||||
|
||||
public static String generateClassObjectModelTypescript(
|
||||
final ClassObjectModel model,
|
||||
final TsClassElementGroup tsGroup,
|
||||
final Set<ClassModel> imports,
|
||||
final boolean writeMode) throws IOException {
|
||||
final TsClassElement tsModel = tsGroup.find(model);
|
||||
if (tsModel.nativeType != DefinedPosition.NATIVE) {
|
||||
imports.add(model);
|
||||
}
|
||||
if (tsModel.nativeType == DefinedPosition.BASIC) {
|
||||
return tsModel.tsTypeName;
|
||||
}
|
||||
if (writeMode) {
|
||||
return tsModel.tsTypeName + "Write";
|
||||
}
|
||||
return tsModel.tsTypeName;
|
||||
}
|
||||
|
||||
public static String generateClassMapModelTypescript(
|
||||
final ClassMapModel model,
|
||||
final TsClassElementGroup tsGroup,
|
||||
final Set<ClassModel> imports,
|
||||
final boolean writeMode) throws IOException {
|
||||
final StringBuilder out = new StringBuilder();
|
||||
out.append("{[key: ");
|
||||
out.append(generateClassModelTypescript(model.keyModel, tsGroup, imports, writeMode));
|
||||
out.append("]: ");
|
||||
out.append(generateClassModelTypescript(model.valueModel, tsGroup, imports, writeMode));
|
||||
out.append(";}");
|
||||
return out.toString();
|
||||
}
|
||||
|
||||
public static String generateClassListModelTypescript(
|
||||
final ClassListModel model,
|
||||
final TsClassElementGroup tsGroup,
|
||||
final Set<ClassModel> imports,
|
||||
final boolean writeMode) throws IOException {
|
||||
final StringBuilder out = new StringBuilder();
|
||||
out.append(generateClassModelTypescript(model.valueModel, tsGroup, imports, writeMode));
|
||||
out.append("[]");
|
||||
return out.toString();
|
||||
}
|
||||
|
||||
public static String generateClassModelTypescript(
|
||||
final ClassModel model,
|
||||
final TsClassElementGroup tsGroup,
|
||||
final Set<ClassModel> imports,
|
||||
final boolean writeMode) throws IOException {
|
||||
if (model instanceof final ClassObjectModel objectModel) {
|
||||
return generateClassObjectModelTypescript(objectModel, tsGroup, imports, writeMode);
|
||||
}
|
||||
if (model instanceof final ClassListModel listModel) {
|
||||
return generateClassListModelTypescript(listModel, tsGroup, imports, writeMode);
|
||||
}
|
||||
if (model instanceof final ClassMapModel mapModel) {
|
||||
return generateClassMapModelTypescript(mapModel, tsGroup, imports, writeMode);
|
||||
}
|
||||
if (model instanceof final ClassEnumModel enumModel) {
|
||||
return generateClassEnumModelTypescript(enumModel, tsGroup, imports, writeMode);
|
||||
}
|
||||
throw new IOException("Impossible model:" + model);
|
||||
}
|
||||
|
||||
public static String generateClassModelsTypescript(
|
||||
final List<ClassModel> models,
|
||||
final TsClassElementGroup tsGroup,
|
||||
final Set<ClassModel> imports,
|
||||
final boolean writeMode) throws IOException {
|
||||
if (models.size() == 0) {
|
||||
return "void";
|
||||
}
|
||||
final StringBuilder out = new StringBuilder();
|
||||
boolean isFirst = true;
|
||||
for (final ClassModel model : models) {
|
||||
if (isFirst) {
|
||||
isFirst = false;
|
||||
} else {
|
||||
out.append(" | ");
|
||||
}
|
||||
final String data = generateClassModelTypescript(model, tsGroup, imports, writeMode);
|
||||
out.append(data);
|
||||
}
|
||||
return out.toString();
|
||||
}
|
||||
|
||||
public static String capitalizeFirstLetter(final String str) {
|
||||
if (str == null || str.isEmpty()) {
|
||||
return str;
|
||||
}
|
||||
return str.substring(0, 1).toUpperCase() + str.substring(1);
|
||||
}
|
||||
|
||||
public static void generateApiFile(
|
||||
final ApiGroupModel element,
|
||||
final String pathPackage,
|
||||
final TsClassElementGroup tsGroup) throws IOException {
|
||||
final StringBuilder data = new StringBuilder();
|
||||
data.append(getBaseHeader());
|
||||
|
||||
data.append("export namespace ");
|
||||
data.append(element.name);
|
||||
data.append(" {\n");
|
||||
|
||||
final Set<ClassModel> imports = new HashSet<>();
|
||||
final Set<ClassModel> zodImports = new HashSet<>();
|
||||
final Set<ClassModel> isImports = new HashSet<>();
|
||||
final Set<ClassModel> writeImports = new HashSet<>();
|
||||
final Set<String> toolImports = new HashSet<>();
|
||||
for (final ApiModel interfaceElement : element.interfaces) {
|
||||
final String methodName = interfaceElement.name;
|
||||
final String methodPath = interfaceElement.restEndPoint;
|
||||
final RestTypeRequest methodType = interfaceElement.restTypeRequest;
|
||||
final List<String> consumes = interfaceElement.consumes;
|
||||
final List<String> produces = interfaceElement.produces;
|
||||
final boolean needGenerateProgress = interfaceElement.needGenerateProgress;
|
||||
final List<ClassModel> returnTypeModel = interfaceElement.returnTypes;
|
||||
|
||||
final String returnModelNameIfComplex = capitalizeFirstLetter(interfaceElement.name) + "TypeReturn";
|
||||
final String returnComplexModel = TsClassElement.generateLocalModel(returnModelNameIfComplex,
|
||||
interfaceElement.returnTypes, tsGroup);
|
||||
if (returnComplexModel != null) {
|
||||
data.append("\n\n");
|
||||
data.append(returnComplexModel.replaceAll("(?m)^", "\t"));
|
||||
for (final ClassModel elem : interfaceElement.returnTypes) {
|
||||
zodImports.addAll(elem.getDependencyGroupModels());
|
||||
}
|
||||
}
|
||||
if (interfaceElement.description != null) {
|
||||
data.append("\n\t/**\n\t * ");
|
||||
data.append(interfaceElement.description);
|
||||
data.append("\n\t */");
|
||||
}
|
||||
data.append("\n\texport function ");
|
||||
data.append(methodName);
|
||||
data.append(interfaceElement.name);
|
||||
data.append("({\n\t\t\trestConfig,");
|
||||
if (!interfaceElement.queries.isEmpty()) {
|
||||
data.append("\n\t\t\tqueries,");
|
||||
@ -83,31 +191,33 @@ public class TsApiGeneration {
|
||||
}
|
||||
data.append("\n\t\t}: {");
|
||||
data.append("\n\t\trestConfig: RESTConfig,");
|
||||
toolImports.add("RESTConfig");
|
||||
if (!interfaceElement.queries.isEmpty()) {
|
||||
data.append("\n\t\tqueries: {");
|
||||
for (final Entry<String, List<ClassModel>> queryEntry : interfaceElement.queries.entrySet()) {
|
||||
data.append("\n\t\t\t");
|
||||
data.append(queryEntry.getKey());
|
||||
data.append("?: ");
|
||||
data.append(queryEntry.getValue());
|
||||
data.append(generateClassModelsTypescript(queryEntry.getValue(), tsGroup, imports, false));
|
||||
data.append(",");
|
||||
}
|
||||
data.append("\n\t\t},");
|
||||
}
|
||||
if (!interfaceElement.parameters.isEmpty()) {
|
||||
data.append("\n\t\tparams: {");
|
||||
for (final Entry<String, List<ClassModel>> pathEntry : interfaceElement.parameters.entrySet()) {
|
||||
for (final Entry<String, List<ClassModel>> paramEntry : interfaceElement.parameters.entrySet()) {
|
||||
data.append("\n\t\t\t");
|
||||
data.append(pathEntry.getKey());
|
||||
data.append(paramEntry.getKey());
|
||||
data.append(": ");
|
||||
data.append(pathEntry.getValue());
|
||||
data.append(generateClassModelsTypescript(paramEntry.getValue(), tsGroup, imports, false));
|
||||
data.append(",");
|
||||
}
|
||||
data.append("\n\t\t},");
|
||||
}
|
||||
if (interfaceElement.unnamedElement.size() == 1) {
|
||||
data.append("\n\t\tdata: ");
|
||||
data.append(interfaceElement.unnamedElement.get(0));
|
||||
data.append(generateClassModelTypescript(interfaceElement.unnamedElement.get(0), tsGroup, writeImports,
|
||||
true));
|
||||
data.append(",");
|
||||
} else if (interfaceElement.multiPartParameters.size() != 0) {
|
||||
data.append("\n\t\tdata: {");
|
||||
@ -116,7 +226,7 @@ public class TsApiGeneration {
|
||||
data.append("\n\t\t\t");
|
||||
data.append(pathEntry.getKey());
|
||||
data.append(": ");
|
||||
data.append(pathEntry.getValue());
|
||||
data.append(generateClassModelsTypescript(pathEntry.getValue(), tsGroup, writeImports, true));
|
||||
data.append(",");
|
||||
}
|
||||
data.append("\n\t\t},");
|
||||
@ -129,12 +239,15 @@ public class TsApiGeneration {
|
||||
|
||||
if (MediaType.APPLICATION_JSON.equals(elem)) {
|
||||
lastElement = "HTTPMimeType.JSON";
|
||||
toolImports.add("HTTPMimeType");
|
||||
}
|
||||
if (MediaType.MULTIPART_FORM_DATA.equals(elem)) {
|
||||
lastElement = "HTTPMimeType.MULTIPART";
|
||||
toolImports.add("HTTPMimeType");
|
||||
}
|
||||
if (DataExport.CSV_TYPE.equals(elem)) {
|
||||
lastElement = "HTTPMimeType.CSV";
|
||||
toolImports.add("HTTPMimeType");
|
||||
}
|
||||
if (lastElement != null) {
|
||||
if (isFist == null) {
|
||||
@ -151,51 +264,55 @@ public class TsApiGeneration {
|
||||
}
|
||||
if (needGenerateProgress) {
|
||||
data.append("\n\t\tcallback?: RESTCallbacks,");
|
||||
toolImports.add("RESTCallbacks");
|
||||
}
|
||||
data.append("\n\t}): Promise<");
|
||||
/**
|
||||
if (interfaceElement.returnTypes.size() == 0 //
|
||||
|| tmpReturn.get(0).tsTypeName == null //
|
||||
|| tmpReturn.get(0).tsTypeName.equals("void")) {
|
||||
data.append("void");
|
||||
} else {
|
||||
data.append(ApiTool.convertInTypeScriptType(tmpReturn, returnModelIsArray));
|
||||
}
|
||||
*/
|
||||
if (returnComplexModel != null) {
|
||||
data.append(returnModelNameIfComplex);
|
||||
data.append("> {");
|
||||
/**
|
||||
if (tmpReturn.size() == 0 //
|
||||
|| tmpReturn.get(0).tsTypeName == null //
|
||||
|| tmpReturn.get(0).tsTypeName.equals("void")) {
|
||||
data.append("\n\t\treturn RESTRequestVoid({");
|
||||
} else if (returnModelIsArray) {
|
||||
data.append("\n\t\treturn RESTRequestJsonArray({");
|
||||
} else {
|
||||
data.append("\n\t\treturn RESTRequestJson({");
|
||||
toolImports.add("RESTRequestJson");
|
||||
} else {
|
||||
final String returnType = generateClassModelsTypescript(interfaceElement.returnTypes, tsGroup, imports,
|
||||
false);
|
||||
data.append(returnType);
|
||||
data.append("> {");
|
||||
if ("void".equals(returnType)) {
|
||||
data.append("\n\t\treturn RESTRequestVoid({");
|
||||
toolImports.add("RESTRequestVoid");
|
||||
} else {
|
||||
isImports.addAll(interfaceElement.returnTypes);
|
||||
data.append("\n\t\treturn RESTRequestJson({");
|
||||
toolImports.add("RESTRequestJson");
|
||||
}
|
||||
}
|
||||
*/
|
||||
data.append("\n\t\t\trestModel: {");
|
||||
data.append("\n\t\t\t\tendPoint: \"");
|
||||
data.append(interfaceElement.restEndPoint);
|
||||
data.append("\",");
|
||||
data.append("\n\t\t\t\trequestType: HTTPRequestModel.");
|
||||
data.append(methodType);
|
||||
toolImports.add("HTTPRequestModel");
|
||||
data.append(interfaceElement.restTypeRequest);
|
||||
data.append(",");
|
||||
if (consumes != null) {
|
||||
for (final String elem : consumes) {
|
||||
if (MediaType.APPLICATION_JSON.equals(elem)) {
|
||||
data.append("\n\t\t\t\tcontentType: HTTPMimeType.JSON,");
|
||||
toolImports.add("HTTPMimeType");
|
||||
break;
|
||||
} else if (MediaType.MULTIPART_FORM_DATA.equals(elem)) {
|
||||
data.append("\n\t\t\t\tcontentType: HTTPMimeType.MULTIPART,");
|
||||
toolImports.add("HTTPMimeType");
|
||||
break;
|
||||
} else if (MediaType.TEXT_PLAIN.equals(elem)) {
|
||||
data.append("\n\t\t\t\tcontentType: HTTPMimeType.TEXT_PLAIN,");
|
||||
toolImports.add("HTTPMimeType");
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else if ("DELETE".equals(methodType)) {
|
||||
} else if (RestTypeRequest.DELETE.equals(interfaceElement.restTypeRequest)) {
|
||||
data.append("\n\t\t\t\tcontentType: HTTPMimeType.TEXT_PLAIN,");
|
||||
toolImports.add("HTTPMimeType");
|
||||
}
|
||||
if (produces != null) {
|
||||
if (produces.size() > 1) {
|
||||
@ -204,6 +321,7 @@ public class TsApiGeneration {
|
||||
for (final String elem : produces) {
|
||||
if (MediaType.APPLICATION_JSON.equals(elem)) {
|
||||
data.append("\n\t\t\t\taccept: HTTPMimeType.JSON,");
|
||||
toolImports.add("HTTPMimeType");
|
||||
break;
|
||||
}
|
||||
}
|
||||
@ -226,22 +344,89 @@ public class TsApiGeneration {
|
||||
data.append("\n\t\t\tcallback,");
|
||||
}
|
||||
data.append("\n\t\t}");
|
||||
/**
|
||||
if (tmpReturn.size() != 0 && tmpReturn.get(0).tsTypeName != null
|
||||
&& !tmpReturn.get(0).tsTypeName.equals("void")) {
|
||||
data.append(", ");
|
||||
// TODO: correct this it is really bad ...
|
||||
data.append(ApiTool.convertInTypeScriptCheckType(tmpReturn));
|
||||
if (returnComplexModel != null) {
|
||||
data.append(", is");
|
||||
data.append(returnModelNameIfComplex);
|
||||
} else {
|
||||
final String returnType = generateClassModelsTypescript(interfaceElement.returnTypes, tsGroup, imports,
|
||||
false);
|
||||
if (!"void".equals(returnType)) {
|
||||
data.append(", is");
|
||||
data.append(returnType);
|
||||
}
|
||||
}
|
||||
**/
|
||||
data.append(");");
|
||||
data.append("\n\t};");
|
||||
}
|
||||
data.append("\n}\n");
|
||||
|
||||
final StringBuilder out = new StringBuilder();
|
||||
out.append(getBaseHeader());
|
||||
|
||||
final List<String> toolImportsList = new ArrayList<>(toolImports);
|
||||
Collections.sort(toolImportsList);
|
||||
if (toolImportsList.size() != 0) {
|
||||
out.append("import {");
|
||||
for (final String elem : toolImportsList) {
|
||||
out.append("\n\t");
|
||||
out.append(elem);
|
||||
out.append(",");
|
||||
}
|
||||
out.append("\n} from \"../rest-tools\";\n\n");
|
||||
}
|
||||
|
||||
if (zodImports.size() != 0) {
|
||||
out.append("import { z as zod } from \"zod\"\n");
|
||||
}
|
||||
|
||||
final List<String> finalImportList = new ArrayList<>();
|
||||
|
||||
for (final ClassModel model : imports) {
|
||||
final TsClassElement tsModel = tsGroup.find(model);
|
||||
if (tsModel.nativeType == DefinedPosition.NATIVE) {
|
||||
continue;
|
||||
}
|
||||
finalImportList.add(tsModel.tsTypeName);
|
||||
}
|
||||
for (final ClassModel model : isImports) {
|
||||
final TsClassElement tsModel = tsGroup.find(model);
|
||||
if (tsModel.nativeType == DefinedPosition.NATIVE) {
|
||||
continue;
|
||||
}
|
||||
finalImportList.add("is" + tsModel.tsTypeName);
|
||||
}
|
||||
for (final ClassModel model : zodImports) {
|
||||
final TsClassElement tsModel = tsGroup.find(model);
|
||||
if (tsModel.nativeType == DefinedPosition.NATIVE) {
|
||||
continue;
|
||||
}
|
||||
finalImportList.add("Zod" + tsModel.tsTypeName);
|
||||
}
|
||||
for (final ClassModel model : writeImports) {
|
||||
final TsClassElement tsModel = tsGroup.find(model);
|
||||
if (tsModel.nativeType == DefinedPosition.NATIVE) {
|
||||
continue;
|
||||
}
|
||||
finalImportList.add(tsModel.tsTypeName + "Write");
|
||||
}
|
||||
Collections.sort(finalImportList);
|
||||
|
||||
if (finalImportList.size() != 0) {
|
||||
out.append("import {");
|
||||
for (final String elem : finalImportList) {
|
||||
out.append("\n\t");
|
||||
out.append(elem);
|
||||
out.append(",");
|
||||
}
|
||||
out.append("\n} from \"../model\";\n\n");
|
||||
}
|
||||
|
||||
out.append(data.toString());
|
||||
|
||||
final String fileName = TsClassElement.determineFileName(element.name);
|
||||
final FileWriter myWriter = new FileWriter(
|
||||
pathPackage + File.separator + "api" + File.separator + fileName + ".ts");
|
||||
myWriter.write(data.toString());
|
||||
myWriter.write(out.toString());
|
||||
myWriter.close();
|
||||
}
|
||||
|
||||
|
@ -102,12 +102,7 @@ public class TsClassElement {
|
||||
out.append("\n\t])");
|
||||
}
|
||||
out.append(";\n");
|
||||
|
||||
out.append("\nexport type ");
|
||||
out.append(this.tsTypeName);
|
||||
out.append(" = zod.infer<typeof ");
|
||||
out.append(this.zodName);
|
||||
out.append(">;\n");
|
||||
out.append(generateZodInfer(this.tsTypeName, this.zodName));
|
||||
} else {
|
||||
boolean first = true;
|
||||
out.append("export enum ");
|
||||
@ -141,24 +136,23 @@ public class TsClassElement {
|
||||
return out.toString();
|
||||
}
|
||||
|
||||
private String generateExportCheckFunctionWrite(final String writeString) {
|
||||
private static String generateExportCheckFunction(
|
||||
final String tsCheckType,
|
||||
final String tsTypeName,
|
||||
final String zodName) {
|
||||
final StringBuilder out = new StringBuilder();
|
||||
out.append("\nexport function ");
|
||||
out.append(this.tsCheckType);
|
||||
out.append(writeString);
|
||||
out.append(tsCheckType);
|
||||
out.append("(data: any): data is ");
|
||||
out.append(this.tsTypeName);
|
||||
out.append(writeString);
|
||||
out.append(tsTypeName);
|
||||
out.append(" {\n\ttry {\n\t\t");
|
||||
out.append(this.zodName);
|
||||
out.append(writeString);
|
||||
out.append(zodName);
|
||||
out.append("""
|
||||
.parse(data);
|
||||
return true;
|
||||
} catch (e: any) {
|
||||
console.log(`Fail to parse data type='""");
|
||||
out.append(this.zodName);
|
||||
out.append(writeString);
|
||||
out.append(zodName);
|
||||
out.append("""
|
||||
' error=${e}`);
|
||||
return false;
|
||||
@ -168,6 +162,11 @@ public class TsClassElement {
|
||||
return out.toString();
|
||||
}
|
||||
|
||||
private String generateExportCheckFunctionWrite(final String writeString) {
|
||||
return generateExportCheckFunction(this.tsCheckType + writeString, this.tsTypeName + writeString,
|
||||
this.zodName + writeString);
|
||||
}
|
||||
|
||||
public String generateImports(final List<ClassModel> depModels, final TsClassElementGroup tsGroup)
|
||||
throws IOException {
|
||||
final StringBuilder out = new StringBuilder();
|
||||
@ -214,6 +213,9 @@ public class TsClassElement {
|
||||
if (field.model().getOriginClasses() == null || field.model().getOriginClasses().isPrimitive()) {
|
||||
return "";
|
||||
}
|
||||
if (field.notNull()) {
|
||||
return "";
|
||||
}
|
||||
return ".optional()";
|
||||
}
|
||||
|
||||
@ -245,7 +247,7 @@ public class TsClassElement {
|
||||
out.append(" = ");
|
||||
out.append(this.declaration);
|
||||
out.append(";");
|
||||
generateZodInfer(this.tsTypeName, this.zodName);
|
||||
out.append(generateZodInfer(this.tsTypeName, this.zodName));
|
||||
return out.toString();
|
||||
}
|
||||
|
||||
@ -314,7 +316,7 @@ public class TsClassElement {
|
||||
}
|
||||
out.append("\n})");
|
||||
}
|
||||
out.append(";\n");
|
||||
out.append(".partial();\n");
|
||||
out.append(generateZodInfer(this.tsTypeName + "Write", this.zodName + "Write"));
|
||||
|
||||
// Check only the input value ==> no need of the output
|
||||
@ -323,7 +325,7 @@ public class TsClassElement {
|
||||
return out.toString();
|
||||
}
|
||||
|
||||
private String generateZodInfer(final String tsName, final String zodName) {
|
||||
private static String generateZodInfer(final String tsName, final String zodName) {
|
||||
final StringBuilder out = new StringBuilder();
|
||||
out.append("\nexport type ");
|
||||
out.append(tsName);
|
||||
@ -333,7 +335,7 @@ public class TsClassElement {
|
||||
return out.toString();
|
||||
}
|
||||
|
||||
private String generateTsMap(final ClassMapModel model, final TsClassElementGroup tsGroup) {
|
||||
private static String generateTsMap(final ClassMapModel model, final TsClassElementGroup tsGroup) {
|
||||
final StringBuilder out = new StringBuilder();
|
||||
out.append("zod.record(");
|
||||
if (model.keyModel instanceof final ClassListModel fieldListModel) {
|
||||
@ -367,17 +369,17 @@ public class TsClassElement {
|
||||
return out.toString();
|
||||
}
|
||||
|
||||
private String generateTsEnum(final ClassEnumModel model, final TsClassElementGroup tsGroup) {
|
||||
private static String generateTsEnum(final ClassEnumModel model, final TsClassElementGroup tsGroup) {
|
||||
final TsClassElement tsParentModel = tsGroup.find(model);
|
||||
return tsParentModel.zodName;
|
||||
}
|
||||
|
||||
private String generateTsObject(final ClassObjectModel model, final TsClassElementGroup tsGroup) {
|
||||
private static String generateTsObject(final ClassObjectModel model, final TsClassElementGroup tsGroup) {
|
||||
final TsClassElement tsParentModel = tsGroup.find(model);
|
||||
return tsParentModel.zodName;
|
||||
}
|
||||
|
||||
private String generateTsList(final ClassListModel model, final TsClassElementGroup tsGroup) {
|
||||
private static String generateTsList(final ClassListModel model, final TsClassElementGroup tsGroup) {
|
||||
final StringBuilder out = new StringBuilder();
|
||||
out.append("zod.array(");
|
||||
if (model.valueModel instanceof final ClassListModel fieldListModel) {
|
||||
@ -400,7 +402,7 @@ public class TsClassElement {
|
||||
}
|
||||
final ClassModel model = this.models.get(0);
|
||||
String data = "";
|
||||
if (this.nativeType == DefinedPosition.BASIC && model instanceof final ClassObjectModel modelObject) {
|
||||
if (this.nativeType == DefinedPosition.BASIC && model instanceof ClassObjectModel) {
|
||||
data = generateBaseObject();
|
||||
} else if (model instanceof final ClassEnumModel modelEnum) {
|
||||
data = generateEnum(modelEnum, tsGroup);
|
||||
@ -417,4 +419,55 @@ public class TsClassElement {
|
||||
myWriter.close();
|
||||
}
|
||||
|
||||
private static String generateLocalModelBase(final ClassModel model, final TsClassElementGroup tsGroup)
|
||||
throws IOException {
|
||||
if (model instanceof final ClassObjectModel objectModel) {
|
||||
return generateTsObject(objectModel, tsGroup);
|
||||
}
|
||||
if (model instanceof final ClassEnumModel enumModel) {
|
||||
return generateTsEnum(enumModel, tsGroup);
|
||||
}
|
||||
if (model instanceof final ClassListModel listModel) {
|
||||
return generateTsList(listModel, tsGroup);
|
||||
}
|
||||
if (model instanceof final ClassMapModel mapModel) {
|
||||
return generateTsMap(mapModel, tsGroup);
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
public static String generateLocalModel(
|
||||
final String ModelName,
|
||||
final List<ClassModel> models,
|
||||
final TsClassElementGroup tsGroup) throws IOException {
|
||||
if (models.size() == 1) {
|
||||
if (models.get(0) instanceof ClassObjectModel) {
|
||||
return null;
|
||||
}
|
||||
if (models.get(0) instanceof ClassEnumModel) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
final StringBuilder out = new StringBuilder();
|
||||
out.append("export const Zod");
|
||||
out.append(ModelName);
|
||||
out.append(" = ");
|
||||
if (models.size() == 1) {
|
||||
out.append(generateLocalModelBase(models.get(0), tsGroup));
|
||||
out.append(";");
|
||||
} else {
|
||||
out.append("z.union([\n");
|
||||
for (final ClassModel model : models) {
|
||||
out.append("\t");
|
||||
out.append(generateLocalModelBase(models.get(0), tsGroup));
|
||||
out.append(",\n");
|
||||
}
|
||||
out.append("]);");
|
||||
}
|
||||
//model.getDependencyModels()
|
||||
out.append(generateZodInfer(ModelName, "Zod" + ModelName));
|
||||
out.append(generateExportCheckFunction("is" + ModelName, ModelName, "Zod" + ModelName));
|
||||
return out.toString();
|
||||
}
|
||||
|
||||
}
|
@ -11,11 +11,13 @@ import java.time.Instant;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.kar.archidata.catcher.RestErrorResponse;
|
||||
import org.kar.archidata.dataAccess.DataFactoryTsApi;
|
||||
import org.kar.archidata.externalRestApi.TsClassElement.DefinedPosition;
|
||||
import org.kar.archidata.externalRestApi.model.ApiGroupModel;
|
||||
@ -23,22 +25,7 @@ import org.kar.archidata.externalRestApi.model.ClassModel;
|
||||
|
||||
public class TsGenerateApi {
|
||||
|
||||
public static List<ClassModel> getCompatibleModels(
|
||||
final List<ClassModel> requestedModel,
|
||||
final List<Class<?>> search) {
|
||||
final List<ClassModel> out = new ArrayList<>();
|
||||
for (final ClassModel model : requestedModel) {
|
||||
if (search.contains(model.getOriginClasses())) {
|
||||
out.add(model);
|
||||
}
|
||||
}
|
||||
if (out.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
public static void generateApi(final AnalyzeApi api, final String pathPackage) throws IOException {
|
||||
public static void generateApi(final AnalyzeApi api, final String pathPackage) throws Exception {
|
||||
final List<TsClassElement> localModel = generateApiModel(api);
|
||||
final TsClassElementGroup tsGroup = new TsClassElementGroup(localModel);
|
||||
// Generates all MODEL files
|
||||
@ -53,10 +40,25 @@ public class TsGenerateApi {
|
||||
}
|
||||
// Generate index of model files
|
||||
createResourceIndex(pathPackage, api.apiModels);
|
||||
|
||||
createIndex(pathPackage);
|
||||
copyResourceFile("rest-tools.ts", pathPackage + File.separator + "rest-tools.ts");
|
||||
}
|
||||
|
||||
private static void createIndex(final String pathPackage) throws IOException {
|
||||
final String out = """
|
||||
/**
|
||||
* Interface of the server (auto-generated code)
|
||||
*/
|
||||
export * from \"./model\";
|
||||
export * from \"./api\";
|
||||
export * from \"./rest-tools\";
|
||||
|
||||
""";
|
||||
final FileWriter myWriter = new FileWriter(pathPackage + File.separator + "index.ts");
|
||||
myWriter.write(out);
|
||||
myWriter.close();
|
||||
}
|
||||
|
||||
private static void createResourceIndex(final String pathPackage, final List<ApiGroupModel> apiModels)
|
||||
throws IOException {
|
||||
final StringBuilder out = new StringBuilder("""
|
||||
@ -64,10 +66,14 @@ public class TsGenerateApi {
|
||||
* Interface of the server (auto-generated code)
|
||||
*/
|
||||
""");
|
||||
final List<String> files = new ArrayList<>();
|
||||
for (final ApiGroupModel elem : apiModels) {
|
||||
final String fileName = TsClassElement.determineFileName(elem.name);
|
||||
files.add(TsClassElement.determineFileName(elem.name));
|
||||
}
|
||||
Collections.sort(files);
|
||||
for (final String elem : files) {
|
||||
out.append("export * from \"./");
|
||||
out.append(fileName);
|
||||
out.append(elem);
|
||||
out.append("\"\n");
|
||||
}
|
||||
final FileWriter myWriter = new FileWriter(pathPackage + File.separator + "api" + File.separator + "index.ts");
|
||||
@ -82,12 +88,17 @@ public class TsGenerateApi {
|
||||
* Interface of the server (auto-generated code)
|
||||
*/
|
||||
""");
|
||||
final List<String> files = new ArrayList<>();
|
||||
for (final TsClassElement elem : tsGroup.getTsElements()) {
|
||||
if (elem.nativeType == DefinedPosition.NATIVE) {
|
||||
continue;
|
||||
}
|
||||
files.add(elem.fileName);
|
||||
}
|
||||
Collections.sort(files);
|
||||
for (final String elem : files) {
|
||||
out.append("export * from \"./");
|
||||
out.append(elem.fileName);
|
||||
out.append(elem);
|
||||
out.append("\"\n");
|
||||
}
|
||||
final FileWriter myWriter = new FileWriter(
|
||||
@ -96,94 +107,96 @@ public class TsGenerateApi {
|
||||
myWriter.close();
|
||||
}
|
||||
|
||||
private static List<TsClassElement> generateApiModel(final AnalyzeApi api) {
|
||||
private static List<TsClassElement> generateApiModel(final AnalyzeApi api) throws Exception {
|
||||
// First step is to add all specific basic elements the wrap correctly the model
|
||||
final List<TsClassElement> tsModels = new ArrayList<>();
|
||||
List<ClassModel> models = getCompatibleModels(api.classModels, List.of(Void.class, void.class));
|
||||
List<ClassModel> models = api.getCompatibleModels(List.of(Void.class, void.class));
|
||||
if (models != null) {
|
||||
tsModels.add(new TsClassElement(models, "void", "void", null, null, DefinedPosition.NATIVE));
|
||||
}
|
||||
models = getCompatibleModels(api.classModels, List.of(Object.class));
|
||||
models = api.getCompatibleModels(List.of(Object.class));
|
||||
if (models != null) {
|
||||
tsModels.add(
|
||||
new TsClassElement(models, "zod.object()", "object", null, "zod.object()", DefinedPosition.NATIVE));
|
||||
}
|
||||
// Map is binded to any ==> can not determine this complex model for now
|
||||
models = getCompatibleModels(api.classModels, List.of(Map.class));
|
||||
models = api.getCompatibleModels(List.of(Map.class));
|
||||
if (models != null) {
|
||||
tsModels.add(new TsClassElement(models, "zod.any()", "any", null, null, DefinedPosition.NATIVE));
|
||||
}
|
||||
models = getCompatibleModels(api.classModels, List.of(String.class));
|
||||
models = api.getCompatibleModels(List.of(String.class));
|
||||
if (models != null) {
|
||||
tsModels.add(
|
||||
new TsClassElement(models, "zod.string()", "string", null, "zod.string()", DefinedPosition.NATIVE));
|
||||
}
|
||||
models = getCompatibleModels(api.classModels, List.of(InputStream.class));
|
||||
models = api.getCompatibleModels(List.of(InputStream.class));
|
||||
if (models != null) {
|
||||
tsModels.add(new TsClassElement(models, "z.instanceof(File)", "File", null, "z.instanceof(File)",
|
||||
DefinedPosition.NATIVE));
|
||||
}
|
||||
models = getCompatibleModels(api.classModels, List.of(Boolean.class, boolean.class));
|
||||
models = api.getCompatibleModels(List.of(Boolean.class, boolean.class));
|
||||
if (models != null) {
|
||||
tsModels.add(new TsClassElement(models, "zod.boolean()", "boolean", null, "zod.boolean()",
|
||||
DefinedPosition.NATIVE));
|
||||
}
|
||||
models = getCompatibleModels(api.classModels, List.of(UUID.class));
|
||||
models = api.getCompatibleModels(List.of(UUID.class));
|
||||
if (models != null) {
|
||||
tsModels.add(new TsClassElement(models, "ZodUUID", "UUID", "isUUID", "zod.string().uuid()",
|
||||
DefinedPosition.BASIC));
|
||||
}
|
||||
models = getCompatibleModels(api.classModels, List.of(Long.class, long.class));
|
||||
models = api.getCompatibleModels(List.of(Long.class, long.class));
|
||||
if (models != null) {
|
||||
tsModels.add(
|
||||
new TsClassElement(models, "ZodLong", "Long", "isLong", "zod.number()", DefinedPosition.BASIC));
|
||||
}
|
||||
models = getCompatibleModels(api.classModels, List.of(Short.class, short.class));
|
||||
models = api.getCompatibleModels(List.of(Short.class, short.class));
|
||||
if (models != null) {
|
||||
tsModels.add(new TsClassElement(models, "ZodShort", "Short", "isShort", "zod.number().safe()",
|
||||
DefinedPosition.BASIC));
|
||||
}
|
||||
models = getCompatibleModels(api.classModels, List.of(Integer.class, int.class));
|
||||
models = api.getCompatibleModels(List.of(Integer.class, int.class));
|
||||
if (models != null) {
|
||||
tsModels.add(new TsClassElement(models, "ZodInteger", "Integer", "isInteger", "zod.number().safe()",
|
||||
DefinedPosition.BASIC));
|
||||
}
|
||||
models = getCompatibleModels(api.classModels, List.of(Double.class, double.class));
|
||||
models = api.getCompatibleModels(List.of(Double.class, double.class));
|
||||
if (models != null) {
|
||||
tsModels.add(new TsClassElement(models, "ZodDouble", "Double", "isDouble", "zod.number()",
|
||||
DefinedPosition.BASIC));
|
||||
}
|
||||
models = getCompatibleModels(api.classModels, List.of(Float.class, float.class));
|
||||
models = api.getCompatibleModels(List.of(Float.class, float.class));
|
||||
if (models != null) {
|
||||
tsModels.add(
|
||||
new TsClassElement(models, "ZodFloat", "Float", "isFloat", "zod.number()", DefinedPosition.BASIC));
|
||||
}
|
||||
models = getCompatibleModels(api.classModels, List.of(Instant.class));
|
||||
models = api.getCompatibleModels(List.of(Instant.class));
|
||||
if (models != null) {
|
||||
tsModels.add(new TsClassElement(models, "ZodInstant", "Instant", "isInstant", "zod.string()",
|
||||
DefinedPosition.BASIC));
|
||||
}
|
||||
models = getCompatibleModels(api.classModels, List.of(Date.class));
|
||||
models = api.getCompatibleModels(List.of(Date.class));
|
||||
if (models != null) {
|
||||
tsModels.add(new TsClassElement(models, "ZodIsoDate", "IsoDate", "isIsoDate",
|
||||
"zod.string().datetime({ precision: 3 })", DefinedPosition.BASIC));
|
||||
}
|
||||
models = getCompatibleModels(api.classModels, List.of(Timestamp.class));
|
||||
models = api.getCompatibleModels(List.of(Timestamp.class));
|
||||
if (models != null) {
|
||||
tsModels.add(new TsClassElement(models, "ZodTimestamp", "Timestamp", "isTimestamp",
|
||||
"zod.string().datetime({ precision: 3 })", DefinedPosition.BASIC));
|
||||
}
|
||||
models = getCompatibleModels(api.classModels, List.of(LocalDate.class));
|
||||
models = api.getCompatibleModels(List.of(LocalDate.class));
|
||||
if (models != null) {
|
||||
tsModels.add(new TsClassElement(models, "ZodLocalDate", "LocalDate", "isLocalDate", "zod.string().date()",
|
||||
DefinedPosition.BASIC));
|
||||
}
|
||||
models = getCompatibleModels(api.classModels, List.of(LocalTime.class));
|
||||
models = api.getCompatibleModels(List.of(LocalTime.class));
|
||||
if (models != null) {
|
||||
tsModels.add(new TsClassElement(models, "ZodLocalTime", "LocalTime", "isLocalTime", "zod.string().time()",
|
||||
DefinedPosition.BASIC));
|
||||
}
|
||||
for (final ClassModel model : api.classModels) {
|
||||
// needed for Rest interface
|
||||
api.addModel(RestErrorResponse.class);
|
||||
for (final ClassModel model : api.getAllModel()) {
|
||||
boolean alreadyExist = false;
|
||||
for (final TsClassElement elem : tsModels) {
|
||||
if (elem.isCompatible(model)) {
|
||||
|
@ -24,7 +24,12 @@ public class ClassEnumModel extends ClassModel {
|
||||
|
||||
@Override
|
||||
public void analyze(final ModelGroup group) throws IOException {
|
||||
if (this.analyzeDone) {
|
||||
return;
|
||||
}
|
||||
this.analyzeDone = true;
|
||||
// TODO: check if we really need to have multiple type for enums ???
|
||||
// TODO: manage enum with int, String and bitField ...
|
||||
final Class<?> clazz = this.originClasses;
|
||||
final Object[] arr = clazz.getEnumConstants();
|
||||
for (final Object elem : arr) {
|
||||
@ -40,4 +45,9 @@ public class ClassEnumModel extends ClassModel {
|
||||
public Set<ClassModel> getAlls() {
|
||||
return Set.of(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<ClassModel> getDependencyGroupModels() {
|
||||
return Set.of(this);
|
||||
}
|
||||
}
|
||||
|
@ -39,4 +39,9 @@ public class ClassListModel extends ClassModel {
|
||||
public Set<ClassModel> getAlls() {
|
||||
return this.valueModel.getAlls();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<ClassModel> getDependencyGroupModels() {
|
||||
return this.valueModel.getDependencyGroupModels();
|
||||
}
|
||||
}
|
||||
|
@ -42,4 +42,11 @@ public class ClassMapModel extends ClassModel {
|
||||
out.addAll(this.valueModel.getAlls());
|
||||
return out;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<ClassModel> getDependencyGroupModels() {
|
||||
final Set<ClassModel> out = new HashSet<>(this.valueModel.getDependencyGroupModels());
|
||||
out.addAll(this.keyModel.getDependencyGroupModels());
|
||||
return out;
|
||||
}
|
||||
}
|
||||
|
@ -9,6 +9,7 @@ import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
public abstract class ClassModel {
|
||||
protected boolean analyzeDone = false;
|
||||
protected Class<?> originClasses = null;
|
||||
protected List<ClassModel> dependencyModels = new ArrayList<>();
|
||||
|
||||
@ -24,6 +25,8 @@ public abstract class ClassModel {
|
||||
return this.dependencyModels;
|
||||
}
|
||||
|
||||
public abstract Set<ClassModel> getDependencyGroupModels();
|
||||
|
||||
public static ClassModel getModel(final Type type, final ModelGroup previousModel) throws IOException {
|
||||
if (type instanceof final ParameterizedType paramType) {
|
||||
final Type[] typeArguments = paramType.getActualTypeArguments();
|
||||
|
@ -54,15 +54,20 @@ public class ClassObjectModel extends ClassModel {
|
||||
ClassModel model,
|
||||
String comment,
|
||||
int limitSize,
|
||||
boolean readOnly) {
|
||||
boolean readOnly,
|
||||
boolean notNull,
|
||||
boolean nullable) {
|
||||
|
||||
public FieldProperty(final String name, final ClassModel model, final String comment, final int limitSize,
|
||||
final boolean readOnly) {
|
||||
final boolean readOnly, final boolean notNull, final boolean nullable) {
|
||||
this.name = name;
|
||||
this.model = model;
|
||||
this.comment = comment;
|
||||
this.limitSize = limitSize;
|
||||
this.readOnly = readOnly;
|
||||
this.notNull = notNull;
|
||||
this.nullable = nullable;
|
||||
|
||||
}
|
||||
|
||||
public FieldProperty(final Field field, final ModelGroup previous) throws Exception {
|
||||
@ -70,7 +75,9 @@ public class ClassObjectModel extends ClassModel {
|
||||
ClassModel.getModel(field.getGenericType(), previous), //
|
||||
AnnotationTools.getComment(field), //
|
||||
AnnotationTools.getLimitSize(field), //
|
||||
AnnotationTools.getSchemaReadOnly(field));
|
||||
AnnotationTools.getSchemaReadOnly(field), //
|
||||
AnnotationTools.getConstraintsNotNull(field), //
|
||||
AnnotationTools.getColumnNotNull(field));
|
||||
}
|
||||
|
||||
}
|
||||
@ -108,6 +115,10 @@ public class ClassObjectModel extends ClassModel {
|
||||
|
||||
@Override
|
||||
public void analyze(final ModelGroup previous) throws Exception {
|
||||
if (this.analyzeDone) {
|
||||
return;
|
||||
}
|
||||
this.analyzeDone = true;
|
||||
final Class<?> clazz = this.originClasses;
|
||||
this.isPrimitive = clazz.isPrimitive();
|
||||
if (this.isPrimitive) {
|
||||
@ -164,6 +175,11 @@ public class ClassObjectModel extends ClassModel {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<ClassModel> getDependencyGroupModels() {
|
||||
return Set.of(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<ClassModel> getAlls() {
|
||||
return Set.of(this);
|
||||
|
@ -10,12 +10,14 @@ import jakarta.ws.rs.core.Response;
|
||||
|
||||
public class ModelGroup {
|
||||
static final Logger LOGGER = LoggerFactory.getLogger(ModelGroup.class);
|
||||
public List<ClassModel> previousModel = new ArrayList<>();
|
||||
public List<ClassModel> models = new ArrayList<>();
|
||||
|
||||
public ModelGroup() {}
|
||||
|
||||
public ModelGroup(final List<ClassModel> models) {
|
||||
this.previousModel = models;
|
||||
public void addAll(final List<Class<?>> classes) {
|
||||
for (final Class<?> clazz : classes) {
|
||||
add(clazz);
|
||||
}
|
||||
}
|
||||
|
||||
public ClassModel add(Class<?> clazz) {
|
||||
@ -26,7 +28,7 @@ public class ModelGroup {
|
||||
return null;
|
||||
}
|
||||
//LOGGER.trace("Search element {}", clazz.getCanonicalName());
|
||||
for (final ClassModel value : this.previousModel) {
|
||||
for (final ClassModel value : this.models) {
|
||||
if (value.isCompatible(clazz)) {
|
||||
//LOGGER.trace(" ==> return {}", value);
|
||||
return value;
|
||||
@ -34,14 +36,18 @@ public class ModelGroup {
|
||||
}
|
||||
if (clazz.isEnum()) {
|
||||
final ClassModel elem = new ClassEnumModel(clazz);
|
||||
this.previousModel.add(elem);
|
||||
this.models.add(elem);
|
||||
//LOGGER.trace(" ==> return enum {}", elem);
|
||||
return elem;
|
||||
}
|
||||
// create new model:
|
||||
final ClassModel elem = new ClassObjectModel(clazz);
|
||||
this.previousModel.add(elem);
|
||||
this.models.add(elem);
|
||||
//LOGGER.trace(" ==> return object {}", elem);
|
||||
return elem;
|
||||
}
|
||||
|
||||
public List<ClassModel> getModels() {
|
||||
return this.models;
|
||||
}
|
||||
}
|
||||
|
@ -5,7 +5,6 @@ import org.kar.archidata.annotation.DataNotRead;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import jakarta.persistence.Column;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
import jakarta.ws.rs.DefaultValue;
|
||||
|
||||
public class GenericDataSoftDelete extends GenericData {
|
||||
@ -13,7 +12,6 @@ public class GenericDataSoftDelete extends GenericData {
|
||||
@Column(nullable = false)
|
||||
@DefaultValue("'0'")
|
||||
@DataDeleted
|
||||
@NotNull
|
||||
@Schema(description = "Deleted state", hidden = true, required = false, readOnly = true)
|
||||
public Boolean deleted = null;
|
||||
}
|
||||
|
@ -10,20 +10,17 @@ import com.fasterxml.jackson.annotation.JsonFormat;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import jakarta.persistence.Column;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
|
||||
public class GenericTiming {
|
||||
@DataNotRead
|
||||
@CreationTimestamp
|
||||
@Column(nullable = false)
|
||||
@NotNull
|
||||
@Schema(description = "Create time of the object", required = false, example = "2000-01-23T01:23:45.678+01:00", readOnly = true)
|
||||
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX")
|
||||
public Date createdAt = null;
|
||||
@DataNotRead
|
||||
@UpdateTimestamp
|
||||
@Column(nullable = false)
|
||||
@NotNull
|
||||
@Schema(description = "When update the object", required = false, example = "2000-01-23T00:23:45.678Z", readOnly = true)
|
||||
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX")
|
||||
// public Instant updatedAt = null;
|
||||
|
@ -5,6 +5,7 @@ import java.util.UUID;
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import jakarta.persistence.Column;
|
||||
import jakarta.persistence.Id;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
import jakarta.ws.rs.DefaultValue;
|
||||
|
||||
public class UUIDGenericData extends GenericTiming {
|
||||
@ -12,5 +13,6 @@ public class UUIDGenericData extends GenericTiming {
|
||||
@DefaultValue("(UUID_TO_BIN(UUID(), TRUE))")
|
||||
@Column(nullable = false, unique = true)
|
||||
@Schema(description = "Unique UUID of the object", required = false, readOnly = true, example = "e6b33c1c-d24d-11ee-b616-02420a030102")
|
||||
@NotNull
|
||||
public UUID uuid = null;
|
||||
}
|
||||
|
@ -5,7 +5,6 @@ import org.kar.archidata.annotation.DataNotRead;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import jakarta.persistence.Column;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
import jakarta.ws.rs.DefaultValue;
|
||||
|
||||
public class UUIDGenericDataSoftDelete extends UUIDGenericData {
|
||||
@ -13,7 +12,6 @@ public class UUIDGenericDataSoftDelete extends UUIDGenericData {
|
||||
@Column(nullable = false)
|
||||
@DefaultValue("'0'")
|
||||
@DataDeleted
|
||||
@NotNull
|
||||
@Schema(description = "Deleted state", hidden = true, required = false, readOnly = true)
|
||||
public Boolean deleted = null;
|
||||
}
|
||||
|
@ -50,23 +50,6 @@ export interface ModelResponseHttp {
|
||||
data: any;
|
||||
}
|
||||
|
||||
export function isArrayOf<TYPE>(
|
||||
data: any,
|
||||
typeChecker: (subData: any) => subData is TYPE,
|
||||
length?: number
|
||||
): data is TYPE[] {
|
||||
if (!Array.isArray(data)) {
|
||||
return false;
|
||||
}
|
||||
if (!data.every(typeChecker)) {
|
||||
return false;
|
||||
}
|
||||
if (length !== undefined && data.length != length) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
function isNullOrUndefined(data: any): data is undefined | null {
|
||||
return data === undefined || data === null;
|
||||
}
|
||||
@ -325,8 +308,6 @@ export function RESTRequest({ restModel, restConfig, data, params, queries, call
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
|
||||
export function RESTRequestJson<TYPE>(request: RESTRequestType, checker: (data: any) => data is TYPE): Promise<TYPE> {
|
||||
return new Promise((resolve, reject) => {
|
||||
RESTRequest(request).then((value: ModelResponseHttp) => {
|
||||
@ -349,25 +330,6 @@ export function RESTRequestJson<TYPE>(request: RESTRequestType, checker: (data:
|
||||
});
|
||||
});
|
||||
}
|
||||
export function RESTRequestJsonArray<TYPE>(request: RESTRequestType, checker: (data: any) => data is TYPE): Promise<TYPE[]> {
|
||||
return new Promise((resolve, reject) => {
|
||||
RESTRequest(request).then((value: ModelResponseHttp) => {
|
||||
if (isArrayOf(value.data, checker)) {
|
||||
resolve(value.data);
|
||||
} else {
|
||||
reject({
|
||||
time: Date().toString(),
|
||||
status: 950,
|
||||
error: "REST Fail to verify the data",
|
||||
statusMessage: "API cast ERROR",
|
||||
message: "api.ts Check type as fail"
|
||||
} as RestErrorResponse);
|
||||
}
|
||||
}).catch((reason: RestErrorResponse) => {
|
||||
reject(reason);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
export function RESTRequestVoid(request: RESTRequestType): Promise<void> {
|
||||
return new Promise((resolve, reject) => {
|
||||
|
@ -1,21 +0,0 @@
|
||||
/** @file
|
||||
* @author Edouard DUPIN
|
||||
* @copyright 2024, Edouard DUPIN, all right reserved
|
||||
* @license MPL-2
|
||||
*/
|
||||
|
||||
import { z as zod, ZodTypeAny, ZodObject } from 'zod';
|
||||
|
||||
export function removeReadonly<T extends ZodTypeAny>(schema: T): T {
|
||||
if (schema instanceof ZodObject) {
|
||||
const shape: Record<string, ZodTypeAny> = {};
|
||||
for (const key in schema.shape) {
|
||||
const field = schema.shape[key];
|
||||
shape[key] = field._def.typeName === 'ZodReadonly'
|
||||
? field._def.innerType
|
||||
: removeReadonly(field);
|
||||
}
|
||||
return zod.object(shape) as T;
|
||||
}
|
||||
return schema;
|
||||
}
|
@ -29,7 +29,7 @@ public class TestAnalyzeApiName {
|
||||
@Test
|
||||
public void testNames() throws Exception {
|
||||
final AnalyzeApi api = new AnalyzeApi();
|
||||
api.createApi(List.of(ApiName.class));
|
||||
api.addAllApi(List.of(ApiName.class));
|
||||
|
||||
Assertions.assertEquals(1, api.apiModels.size());
|
||||
Assertions.assertEquals("ApiName", api.apiModels.get(0).name);
|
||||
|
@ -0,0 +1,17 @@
|
||||
package test.kar.archidata.externalRestApi;
|
||||
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class TestAnalyzeApiParameterParamQuery {
|
||||
final static private Logger LOGGER = LoggerFactory.getLogger(TestAnalyzeApiParameterParamQuery.class);
|
||||
|
||||
@Test
|
||||
public void testNotImplemented() throws Exception {
|
||||
Assertions.assertEquals(1, 0);
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -47,7 +47,7 @@ public class TestAnalyzeApiParameterType {
|
||||
@Test
|
||||
public void testBasicParameter() throws Exception {
|
||||
final AnalyzeApi api = new AnalyzeApi();
|
||||
api.createApi(List.of(BasicParameter.class));
|
||||
api.addAllApi(List.of(BasicParameter.class));
|
||||
|
||||
Assertions.assertEquals(1, api.apiModels.size());
|
||||
Assertions.assertEquals(5, api.apiModels.get(0).interfaces.size());
|
||||
@ -102,7 +102,7 @@ public class TestAnalyzeApiParameterType {
|
||||
@Test
|
||||
public void testListParameter() throws Exception {
|
||||
final AnalyzeApi api = new AnalyzeApi();
|
||||
api.createApi(List.of(ListParameter.class));
|
||||
api.addAllApi(List.of(ListParameter.class));
|
||||
|
||||
Assertions.assertEquals(1, api.apiModels.size());
|
||||
Assertions.assertEquals(1, api.apiModels.get(0).interfaces.size());
|
||||
@ -125,7 +125,7 @@ public class TestAnalyzeApiParameterType {
|
||||
@Test
|
||||
public void testMapParameter() throws Exception {
|
||||
final AnalyzeApi api = new AnalyzeApi();
|
||||
api.createApi(List.of(MapParameter.class));
|
||||
api.addAllApi(List.of(MapParameter.class));
|
||||
|
||||
Assertions.assertEquals(1, api.apiModels.size());
|
||||
Assertions.assertEquals(1, api.apiModels.get(0).interfaces.size());
|
||||
|
@ -0,0 +1,17 @@
|
||||
package test.kar.archidata.externalRestApi;
|
||||
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class TestAnalyzeApiParameterTypeAsync {
|
||||
final static private Logger LOGGER = LoggerFactory.getLogger(TestAnalyzeApiParameterTypeAsync.class);
|
||||
|
||||
@Test
|
||||
public void testNotImplemented() throws Exception {
|
||||
Assertions.assertEquals(1, 0);
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -37,7 +37,7 @@ public class TestAnalyzeApiPath {
|
||||
@Test
|
||||
public void testNoPath() throws Exception {
|
||||
final AnalyzeApi api = new AnalyzeApi();
|
||||
api.createApi(List.of(NoPath.class));
|
||||
api.addAllApi(List.of(NoPath.class));
|
||||
|
||||
Assertions.assertEquals(1, api.apiModels.size());
|
||||
Assertions.assertEquals("", api.apiModels.get(0).restEndPoint);
|
||||
@ -82,7 +82,7 @@ public class TestAnalyzeApiPath {
|
||||
@Test
|
||||
public void testWithPath() throws Exception {
|
||||
final AnalyzeApi api = new AnalyzeApi();
|
||||
api.createApi(List.of(WithPath.class));
|
||||
api.addAllApi(List.of(WithPath.class));
|
||||
|
||||
Assertions.assertEquals(1, api.apiModels.size());
|
||||
Assertions.assertEquals("/kaboom", api.apiModels.get(0).restEndPoint);
|
||||
|
@ -41,7 +41,7 @@ public class TestAnalyzeApiReturn {
|
||||
@Test
|
||||
public void testReturnVoid() throws Exception {
|
||||
final AnalyzeApi api = new AnalyzeApi();
|
||||
api.createApi(List.of(ReturnValueVoid.class));
|
||||
api.addAllApi(List.of(ReturnValueVoid.class));
|
||||
|
||||
Assertions.assertEquals(1, api.apiModels.size());
|
||||
Assertions.assertEquals(2, api.apiModels.get(0).interfaces.size());
|
||||
@ -80,7 +80,7 @@ public class TestAnalyzeApiReturn {
|
||||
@Test
|
||||
public void testReturnInteger() throws Exception {
|
||||
final AnalyzeApi api = new AnalyzeApi();
|
||||
api.createApi(List.of(ReturnValueInteger.class));
|
||||
api.addAllApi(List.of(ReturnValueInteger.class));
|
||||
|
||||
Assertions.assertEquals(1, api.apiModels.size());
|
||||
Assertions.assertEquals(2, api.apiModels.get(0).interfaces.size());
|
||||
@ -121,7 +121,7 @@ public class TestAnalyzeApiReturn {
|
||||
@Test
|
||||
public void testReturnShort() throws Exception {
|
||||
final AnalyzeApi api = new AnalyzeApi();
|
||||
api.createApi(List.of(ReturnValueShort.class));
|
||||
api.addAllApi(List.of(ReturnValueShort.class));
|
||||
|
||||
Assertions.assertEquals(1, api.apiModels.size());
|
||||
Assertions.assertEquals(2, api.apiModels.get(0).interfaces.size());
|
||||
@ -162,7 +162,7 @@ public class TestAnalyzeApiReturn {
|
||||
@Test
|
||||
public void testReturnLong() throws Exception {
|
||||
final AnalyzeApi api = new AnalyzeApi();
|
||||
api.createApi(List.of(ReturnValueLong.class));
|
||||
api.addAllApi(List.of(ReturnValueLong.class));
|
||||
|
||||
Assertions.assertEquals(1, api.apiModels.size());
|
||||
Assertions.assertEquals(2, api.apiModels.get(0).interfaces.size());
|
||||
@ -204,7 +204,7 @@ public class TestAnalyzeApiReturn {
|
||||
@Test
|
||||
public void testReturnFloat() throws Exception {
|
||||
final AnalyzeApi api = new AnalyzeApi();
|
||||
api.createApi(List.of(ReturnValueFloat.class));
|
||||
api.addAllApi(List.of(ReturnValueFloat.class));
|
||||
|
||||
Assertions.assertEquals(1, api.apiModels.size());
|
||||
Assertions.assertEquals(2, api.apiModels.get(0).interfaces.size());
|
||||
@ -245,7 +245,7 @@ public class TestAnalyzeApiReturn {
|
||||
@Test
|
||||
public void testReturnDouble() throws Exception {
|
||||
final AnalyzeApi api = new AnalyzeApi();
|
||||
api.createApi(List.of(ReturnValueDouble.class));
|
||||
api.addAllApi(List.of(ReturnValueDouble.class));
|
||||
|
||||
Assertions.assertEquals(1, api.apiModels.size());
|
||||
Assertions.assertEquals(2, api.apiModels.get(0).interfaces.size());
|
||||
@ -281,7 +281,7 @@ public class TestAnalyzeApiReturn {
|
||||
@Test
|
||||
public void testReturnString() throws Exception {
|
||||
final AnalyzeApi api = new AnalyzeApi();
|
||||
api.createApi(List.of(ReturnValueString.class));
|
||||
api.addAllApi(List.of(ReturnValueString.class));
|
||||
|
||||
Assertions.assertEquals(1, api.apiModels.size());
|
||||
Assertions.assertEquals(1, api.apiModels.get(0).interfaces.size());
|
||||
@ -312,7 +312,7 @@ public class TestAnalyzeApiReturn {
|
||||
@Test
|
||||
public void testReturnAny() throws Exception {
|
||||
final AnalyzeApi api = new AnalyzeApi();
|
||||
api.createApi(List.of(ReturnValueAny.class));
|
||||
api.addAllApi(List.of(ReturnValueAny.class));
|
||||
|
||||
Assertions.assertEquals(1, api.apiModels.size());
|
||||
Assertions.assertEquals(2, api.apiModels.get(0).interfaces.size());
|
||||
@ -348,7 +348,7 @@ public class TestAnalyzeApiReturn {
|
||||
@Test
|
||||
public void testReturnEnum() throws Exception {
|
||||
final AnalyzeApi api = new AnalyzeApi();
|
||||
api.createApi(List.of(ReturnValueEnum.class));
|
||||
api.addAllApi(List.of(ReturnValueEnum.class));
|
||||
|
||||
Assertions.assertEquals(1, api.apiModels.size());
|
||||
Assertions.assertEquals(1, api.apiModels.get(0).interfaces.size());
|
||||
@ -394,7 +394,7 @@ public class TestAnalyzeApiReturn {
|
||||
@Test
|
||||
public void testReturnList() throws Exception {
|
||||
final AnalyzeApi api = new AnalyzeApi();
|
||||
api.createApi(List.of(ReturnValueList.class));
|
||||
api.addAllApi(List.of(ReturnValueList.class));
|
||||
|
||||
Assertions.assertEquals(1, api.apiModels.size());
|
||||
Assertions.assertEquals(5, api.apiModels.get(0).interfaces.size());
|
||||
@ -507,7 +507,7 @@ public class TestAnalyzeApiReturn {
|
||||
@Test
|
||||
public void testReturnMap() throws Exception {
|
||||
final AnalyzeApi api = new AnalyzeApi();
|
||||
api.createApi(List.of(ReturnValueMap.class));
|
||||
api.addAllApi(List.of(ReturnValueMap.class));
|
||||
|
||||
Assertions.assertEquals(1, api.apiModels.size());
|
||||
Assertions.assertEquals(5, api.apiModels.get(0).interfaces.size());
|
||||
|
@ -0,0 +1,17 @@
|
||||
package test.kar.archidata.externalRestApi;
|
||||
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class TestAnalyzeApiReturnAsync {
|
||||
final static private Logger LOGGER = LoggerFactory.getLogger(TestAnalyzeApiReturnAsync.class);
|
||||
|
||||
@Test
|
||||
public void testNotImplemented() throws Exception {
|
||||
Assertions.assertEquals(1, 0);
|
||||
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user