[FIX] correct the typescript gneration

This commit is contained in:
Edouard DUPIN 2024-04-22 23:02:52 +02:00
parent 0605ef0824
commit cfc7ec2ff1
2 changed files with 56 additions and 20 deletions

View File

@ -269,6 +269,16 @@ public class DataFactoryTsApi {
} }
return out; return out;
} }
public static String convertInTypeScriptCheckType(final List<ClassElement> tmp) {
String out = "";
for (final ClassElement elem : tmp) {
if (out.length() != 0) {
out += " | ";
}
out += elem.tsCheckType;
}
return out;
}
public static APIModel createSingleApi(final Class<?> clazz, final Set<Class<?>> includeModel, final Set<Class<?>> includeCheckerModel, final GeneratedTypes previous) throws Exception { public static APIModel createSingleApi(final Class<?> clazz, final Set<Class<?>> includeModel, final Set<Class<?>> includeCheckerModel, final GeneratedTypes previous) throws Exception {
final StringBuilder builder = new StringBuilder(); final StringBuilder builder = new StringBuilder();
@ -297,23 +307,29 @@ public class DataFactoryTsApi {
} }
final boolean needGenerateProgress = apiAnnotationTypeScriptProgress(method); final boolean needGenerateProgress = apiAnnotationTypeScriptProgress(method);
Class<?>[] returnTypeModel = apiAnnotationGetAsyncType(method); Class<?>[] returnTypeModel = apiAnnotationGetAsyncType(method);
Class<?> returnTypeModelRaw = method.getReturnType();
if (returnTypeModel == null) {
returnTypeModel = new Class<?>[] { returnTypeModelRaw };
returnTypeModelRaw = null;
}
boolean isUnmanagedReturnType = false; boolean isUnmanagedReturnType = false;
if (returnTypeModelRaw == Response.class) {
isUnmanagedReturnType = true;
returnTypeModel = new Class<?>[] { Void.class };
}
boolean returnModelIsArray = false; boolean returnModelIsArray = false;
List<ClassElement> tmpReturn; List<ClassElement> tmpReturn;
if (returnTypeModelRaw == List.class) { if (returnTypeModel == null) {
final ParameterizedType listType = (ParameterizedType) method.getGenericReturnType(); Class<?> returnTypeModelRaw = method.getReturnType();
returnTypeModelRaw = (Class<?>) listType.getActualTypeArguments()[0]; LOGGER.info("Get type: {}", returnTypeModelRaw);
tmpReturn = List.of(DataFactoryZod.createTable(returnTypeModelRaw, previous)); if (returnTypeModelRaw == Response.class) {
returnModelIsArray = true; LOGGER.info("Get type: {}", returnTypeModelRaw);
}
if (returnTypeModelRaw == Response.class) {
isUnmanagedReturnType = true;
returnTypeModel = new Class<?>[] { Void.class };
tmpReturn = new ArrayList<>();
} else if (returnTypeModelRaw == List.class) {
final ParameterizedType listType = (ParameterizedType) method.getGenericReturnType();
returnTypeModelRaw = (Class<?>) listType.getActualTypeArguments()[0];
returnModelIsArray = true;
returnTypeModel = new Class<?>[] { returnTypeModelRaw };
tmpReturn = DataFactoryZod.createTables(returnTypeModel, previous);
} else {
returnTypeModel = new Class<?>[] { returnTypeModelRaw };
tmpReturn = DataFactoryZod.createTables(returnTypeModel, previous);
}
} else { } else {
tmpReturn = DataFactoryZod.createTables(returnTypeModel, previous); tmpReturn = DataFactoryZod.createTables(returnTypeModel, previous);
} }
@ -377,12 +393,14 @@ public class DataFactoryTsApi {
if (parameterTypeString.length() != 0) { if (parameterTypeString.length() != 0) {
parameterTypeString += " | "; parameterTypeString += " | ";
} }
parameterTypeString += elem.tsTypeName + "[]"; parameterTypeString += elem.tsTypeName;
} }
emptyElement.add(parameterTypeString);
} else if (parameterType == List.class) { } else if (parameterType == List.class) {
parameterTypeString = "any[]"; parameterTypeString = "any[]";
final Class<?> plop = parameterType.arrayType(); final Class<?> plop = parameterType.arrayType();
LOGGER.info("ArrayType = {}", plop); LOGGER.info("ArrayType = {}", plop);
emptyElement.add(parameterTypeString);
} else { } else {
final ClassElement tmp = DataFactoryZod.createTable(parameterType, previous); final ClassElement tmp = DataFactoryZod.createTable(parameterType, previous);
includeModel.add(tmp.model[0]); includeModel.add(tmp.model[0]);
@ -422,7 +440,7 @@ public class DataFactoryTsApi {
} }
builder.append("\n\texport function "); builder.append("\n\texport function ");
builder.append(methodName); builder.append(methodName);
builder.append("({\t\t\trestConfig,"); builder.append("({\n\t\t\trestConfig,");
if (!queryParams.isEmpty()) { if (!queryParams.isEmpty()) {
builder.append("\n\t\t\tqueries,"); builder.append("\n\t\t\tqueries,");
} }
@ -436,7 +454,7 @@ public class DataFactoryTsApi {
builder.append("\n\t\t\tdata,"); builder.append("\n\t\t\tdata,");
} }
if (needGenerateProgress) { if (needGenerateProgress) {
builder.append(" callback,"); builder.append("\n\t\t\tcallback,");
} }
builder.append("\n\t\t}: {"); builder.append("\n\t\t}: {");
builder.append("\n\t\trestConfig: RESTConfig,"); builder.append("\n\t\trestConfig: RESTConfig,");
@ -509,9 +527,17 @@ public class DataFactoryTsApi {
builder.append("\n\t\tcallback?: RESTCallbacks,"); builder.append("\n\t\tcallback?: RESTCallbacks,");
} }
builder.append("\n\t}): Promise<"); builder.append("\n\t}): Promise<");
builder.append(convertInTypeScriptType(tmpReturn, returnModelIsArray)); if (tmpReturn.size() == 0 //
|| tmpReturn.get(0).tsTypeName == null //
|| tmpReturn.get(0).tsTypeName.equals("void")) {
builder.append("void");
} else {
builder.append(convertInTypeScriptType(tmpReturn, returnModelIsArray));
}
builder.append("> {"); builder.append("> {");
if (tmpReturn.size() == 0 || tmpReturn.get(0).tsTypeName == null || tmpReturn.get(0).tsTypeName.equals("void")) { if (tmpReturn.size() == 0 //
|| tmpReturn.get(0).tsTypeName == null //
|| tmpReturn.get(0).tsTypeName.equals("void")) {
builder.append("\n\t\treturn RESTRequestVoid({"); builder.append("\n\t\treturn RESTRequestVoid({");
} else if (returnModelIsArray) { } else if (returnModelIsArray) {
builder.append("\n\t\treturn RESTRequestJsonArray({"); builder.append("\n\t\treturn RESTRequestJsonArray({");
@ -576,7 +602,8 @@ public class DataFactoryTsApi {
builder.append("\n\t\t}"); builder.append("\n\t\t}");
if (tmpReturn.size() != 0 && tmpReturn.get(0).tsTypeName != null && !tmpReturn.get(0).tsTypeName.equals("void")) { if (tmpReturn.size() != 0 && tmpReturn.get(0).tsTypeName != null && !tmpReturn.get(0).tsTypeName.equals("void")) {
builder.append(", "); builder.append(", ");
builder.append(convertInTypeScriptType(tmpReturn, returnModelIsArray)); // TODO: correct this it is really bad ...
builder.append(convertInTypeScriptCheckType(tmpReturn));
} }
builder.append(");"); builder.append(");");
builder.append("\n\t};"); builder.append("\n\t};");

View File

@ -2,6 +2,7 @@ package org.kar.archidata.dataAccess;
import java.io.File; import java.io.File;
import java.io.FileWriter; import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType; import java.lang.reflect.ParameterizedType;
@ -20,6 +21,8 @@ import org.kar.archidata.exception.DataAccessException;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import jakarta.ws.rs.core.Response;
public class DataFactoryZod { public class DataFactoryZod {
static final Logger LOGGER = LoggerFactory.getLogger(DataFactoryZod.class); static final Logger LOGGER = LoggerFactory.getLogger(DataFactoryZod.class);
@ -293,6 +296,9 @@ public class DataFactoryZod {
public static List<ClassElement> createTables(final Class<?>[] classs, final GeneratedTypes previous) throws Exception { public static List<ClassElement> createTables(final Class<?>[] classs, final GeneratedTypes previous) throws Exception {
final List<ClassElement> out = new ArrayList<>(); final List<ClassElement> out = new ArrayList<>();
for (final Class<?> clazz : classs) { for (final Class<?> clazz : classs) {
if (clazz == Response.class) {
throw new IOException("Can not generate a Zod element for an unknow type Response");
}
out.add(createTable(clazz, previous)); out.add(createTable(clazz, previous));
} }
return out; return out;
@ -302,6 +308,9 @@ public class DataFactoryZod {
if (clazz == null) { if (clazz == null) {
return null; return null;
} }
if (clazz == Response.class) {
throw new IOException("Can not generate a Zod element for an unknow type Response");
}
final ClassElement alreadyExist = previous.find(clazz); final ClassElement alreadyExist = previous.find(clazz);
if (previous.find(clazz) != null) { if (previous.find(clazz) != null) {
return alreadyExist; return alreadyExist;