[FEAT] continue refacto of the generation code

This commit is contained in:
Edouard DUPIN 2024-05-18 08:27:47 +02:00
parent b64825eaba
commit be1bb9c52a
17 changed files with 165 additions and 197 deletions

View File

@ -5,6 +5,7 @@
<artifactId>archidata</artifactId>
<version>0.8.9</version>
<properties>
<java.version>21</java.version>
<maven.compiler.version>3.1</maven.compiler.version>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>

View File

@ -8,7 +8,6 @@ import java.io.InputStreamReader;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
@ -173,23 +172,9 @@ public class DataFactoryTsApi {
tmpReturn = new ArrayList<>();
produces = null;
} else if (returnTypeModelRaw == Map.class) {
LOGGER.warn("Not manage the Map Model ... set any");
final ParameterizedType listType = (ParameterizedType) method.getGenericReturnType();
final Type typeKey = listType.getActualTypeArguments()[0];
final Type typeValue = listType.getActualTypeArguments()[1];
if (typeKey == String.class) {
if (typeValue instanceof ParameterizedType) {
final Type typeSubKey = listType.getActualTypeArguments()[0];
final Type typeSubValue = listType.getActualTypeArguments()[1];
if (typeKey == String.class) {
}
}
} else {
LOGGER.warn("Not manage the Map Model ... set any");
returnTypeModel = new Class<?>[] { Map.class };
tmpReturn = DataFactoryZod.createTables(returnTypeModel, previous);
}
} else if (returnTypeModelRaw == List.class) {
final ParameterizedType listType = (ParameterizedType) method.getGenericReturnType();
returnTypeModelRaw = (Class<?>) listType.getActualTypeArguments()[0];

View File

@ -24,7 +24,7 @@ public class ApiGroupModel {
// Origin class reference
Class<?> originClass;
// List of all API
public List<ApiModel> interfaces;
public List<ApiModel> interfaces = new ArrayList<>();
public ApiGroupModel(final Class<?> clazz, final ModelGroup previousModel) throws Exception {
this.originClass = clazz;

View File

@ -10,13 +10,9 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.kar.archidata.dataAccess.DataFactoryZod;
import org.kar.archidata.dataAccess.DataFactoryZod.ClassElement;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import jakarta.ws.rs.core.Response;
public class ApiModel {
static final Logger LOGGER = LoggerFactory.getLogger(ApiModel.class);
@ -46,10 +42,10 @@ public class ApiModel {
// Possible output type of the REST API
public List<String> produces = new ArrayList<>();
private void updateReturnTypes(final Method method, final ModelGroup previousModel) {
private void updateReturnTypes(final Method method, final ModelGroup previousModel) throws Exception {
// get return type from the user specification:
final Class<?>[] returnTypeModel = ApiTool.apiAnnotationGetAsyncType(method);
LOGGER.info("Get return Type async = {}", returnTypeModel);
if (returnTypeModel != null) {
if (returnTypeModel.length == 0) {
throw new IOException("Create a @AsyncType with empty elements ...");
@ -69,20 +65,26 @@ public class ApiModel {
}
final Class<?> returnTypeModelRaw = method.getReturnType();
LOGGER.info("Get type: {}", returnTypeModelRaw);
LOGGER.info("Get return Type RAW = {}", returnTypeModelRaw.getCanonicalName());
if (returnTypeModelRaw == Map.class) {
LOGGER.warn("Not manage the Map Model ... set any");
LOGGER.warn("Model Map");
final ParameterizedType listType = (ParameterizedType) method.getGenericReturnType();
this.returnTypes.add(new ClassMapModel(listType, previousModel));
return;
}
if (returnTypeModelRaw == List.class) {
} else if (returnTypeModelRaw == List.class) {
LOGGER.warn("Model List");
final ParameterizedType listType = (ParameterizedType) method.getGenericReturnType();
this.returnTypes.add(new ClassListModel(listType, previousModel));
return;
}
} else {
LOGGER.warn("Model Object");
this.returnTypes.add(previousModel.add(returnTypeModelRaw));
}
LOGGER.warn("List of returns elements:");
for (final ClassModel elem : this.returnTypes) {
LOGGER.warn(" - {}", elem);
}
}
public ApiModel(final Class<?> clazz, final Method method, final String baseRestEndPoint,
final List<String> consume, final List<String> produce, final ModelGroup previousModel) throws Exception {
@ -99,80 +101,16 @@ public class ApiModel {
LOGGER.trace(" [{}] {} => {}/{}", methodType, methodName, baseRestEndPoint, methodPath);
this.needGenerateProgress = ApiTool.apiAnnotationTypeScriptProgress(method);
Class<?>[] returnTypeModel = ApiTool.apiAnnotationGetAsyncType(method);
boolean isUnmanagedReturnType = false;
boolean returnModelIsArray = false;
List<ClassElement> tmpReturn;
if (returnTypeModel == null) {
Class<?> returnTypeModelRaw = method.getReturnType();
LOGGER.info("Get type: {}", returnTypeModelRaw);
if (returnTypeModelRaw == Response.class) {
LOGGER.info("Get type: {}", returnTypeModelRaw);
}
if (returnTypeModelRaw == Response.class || returnTypeModelRaw == void.class
|| returnTypeModelRaw == Void.class) {
if (returnTypeModelRaw == Response.class) {
isUnmanagedReturnType = true;
}
returnTypeModel = new Class<?>[] { Void.class };
tmpReturn = new ArrayList<>();
this.produces = null;
} else if (returnTypeModelRaw == Map.class) {
LOGGER.warn("Not manage the Map Model ... set any");
final ParameterizedType listType = (ParameterizedType) method.getGenericReturnType();
final Type typeKey = listType.getActualTypeArguments()[0];
final Type typeValue = listType.getActualTypeArguments()[1];
if (typeKey == String.class) {
if (typeValue instanceof ParameterizedType) {
final Type typeSubKey = listType.getActualTypeArguments()[0];
final Type typeSubValue = listType.getActualTypeArguments()[1];
if (typeKey == String.class) {
}
}
} else {
LOGGER.warn("Not manage the Map Model ... set any");
returnTypeModel = new Class<?>[] { Map.class };
tmpReturn = DataFactoryZod.createTables(returnTypeModel, previous);
}
} 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 if (returnTypeModel.length >= 0 && (returnTypeModel[0] == Response.class
|| returnTypeModel[0] == Void.class || returnTypeModel[0] == void.class)) {
if (returnTypeModel[0] == Response.class) {
isUnmanagedReturnType = true;
}
returnTypeModel = new Class<?>[] { Void.class };
tmpReturn = new ArrayList<>();
this.produces = null;
} else if (returnTypeModel.length > 0 && returnTypeModel[0] == Map.class) {
LOGGER.warn("Not manage the Map Model ...");
returnTypeModel = new Class<?>[] { Map.class };
tmpReturn = DataFactoryZod.createTables(returnTypeModel, previous);
} else {
tmpReturn = DataFactoryZod.createTables(returnTypeModel, previous);
}
for (final ClassElement elem : tmpReturn) {
includeModel.add(elem.model[0]);
includeCheckerModel.add(elem.model[0]);
}
LOGGER.trace(" return: {}", tmpReturn.size());
for (final ClassElement elem : tmpReturn) {
LOGGER.trace(" - {}", elem.tsTypeName);
updateReturnTypes(method, previousModel);
LOGGER.trace(" return: {}", this.returnTypes.size());
for (final ClassModel elem : this.returnTypes) {
LOGGER.trace(" - {}", elem);
}
final Map<String, String> queryParams = new HashMap<>();
final Map<String, String> pathParams = new HashMap<>();
final Map<String, String> formDataParams = new HashMap<>();
final List<String> emptyElement = new ArrayList<>();
final Map<String, List<ClassModel>> queryParams = new HashMap<>();
final Map<String, List<ClassModel>> pathParams = new HashMap<>();
final Map<String, List<ClassModel>> formDataParams = new HashMap<>();
final List<ClassModel> emptyElement = new ArrayList<>();
// LOGGER.info(" Parameters:");
for (final Parameter parameter : method.getParameters()) {
// Security context are internal parameter (not available from API)
@ -180,59 +118,33 @@ public class ApiModel {
continue;
}
final Class<?> parameterType = parameter.getType();
String parameterTypeString;
final List<ClassModel> parameterModel = new ArrayList<>();
final Class<?>[] asyncType = ApiTool.apiAnnotationGetAsyncType(parameter);
if (parameterType == List.class) {
if (asyncType == null) {
LOGGER.warn("Detect List param ==> not managed type ==> any[] !!!");
parameterTypeString = "any[]";
if (asyncType != null) {
for (final Class<?> elem : asyncType) {
parameterModel.add(new ClassListModel(elem, previousModel));
}
} else if (parameterType == List.class) {
final Type parameterrizedType = parameter.getParameterizedType();
parameterModel.add(ClassModel.getModelBase(parameterType, parameterrizedType, previousModel));
} else if (parameterType == Map.class) {
final Type parameterrizedType = parameter.getParameterizedType();
parameterModel.add(ClassModel.getModelBase(parameterType, parameterrizedType, previousModel));
} else {
final List<ClassElement> tmp = DataFactoryZod.createTables(asyncType, previous);
for (final ClassElement elem : tmp) {
includeModel.add(elem.model[0]);
}
parameterTypeString = ApiTool.convertInTypeScriptType(tmp, true);
}
} else if (asyncType == null) {
final ClassElement tmp = DataFactoryZod.createTable(parameterType, previous);
includeModel.add(tmp.model[0]);
parameterTypeString = tmp.tsTypeName;
} else {
final List<ClassElement> tmp = DataFactoryZod.createTables(asyncType, previous);
for (final ClassElement elem : tmp) {
includeModel.add(elem.model[0]);
}
parameterTypeString = ApiTool.convertInTypeScriptType(tmp, true);
parameterModel.add(ClassModel.getModel(parameterType, previousModel));
}
final String pathParam = ApiTool.apiAnnotationGetPathParam(parameter);
final String queryParam = ApiTool.apiAnnotationGetQueryParam(parameter);
final String formDataParam = ApiTool.apiAnnotationGetFormDataParam(parameter);
if (queryParam != null) {
queryParams.put(queryParam, parameterTypeString);
queryParams.put(queryParam, parameterModel);
} else if (pathParam != null) {
pathParams.put(pathParam, parameterTypeString);
pathParams.put(pathParam, parameterModel);
} else if (formDataParam != null) {
formDataParams.put(formDataParam, parameterTypeString);
} else if (asyncType != null) {
final List<ClassElement> tmp = DataFactoryZod.createTables(asyncType, previous);
parameterTypeString = "";
for (final ClassElement elem : tmp) {
includeModel.add(elem.model[0]);
if (parameterTypeString.length() != 0) {
parameterTypeString += " | ";
}
parameterTypeString += elem.tsTypeName;
}
emptyElement.add(parameterTypeString);
} else if (parameterType == List.class) {
parameterTypeString = "any[]";
final Class<?> plop = parameterType.arrayType();
LOGGER.info("ArrayType = {}", plop);
emptyElement.add(parameterTypeString);
formDataParams.put(formDataParam, parameterModel);
} else {
final ClassElement tmp = DataFactoryZod.createTable(parameterType, previous);
includeModel.add(tmp.model[0]);
emptyElement.add(tmp.tsTypeName);
emptyElement.addAll(parameterModel);
}
}
}

View File

@ -5,4 +5,15 @@ public class ClassEnumModel extends ClassModel {
protected ClassEnumModel(final Class<?> clazz) {
this.originClasses.add(clazz);
}
@Override
public String toString() {
final StringBuilder out = new StringBuilder();
out.append("ClassEnumModel [");
for (final Class<?> elem : this.originClasses) {
out.append(elem.getCanonicalName());
}
out.append("]");
return out.toString();
}
}

View File

@ -11,9 +11,18 @@ public class ClassListModel extends ClassModel {
this.valueModel = valueModel;
}
public ClassListModel(final Class<?> clazz, final ModelGroup previousModel) throws IOException {
this.valueModel = getModel(clazz, previousModel);
}
public ClassListModel(final ParameterizedType listType, final ModelGroup previousModel) throws IOException {
final Type model = listType.getActualTypeArguments()[0];
this.valueModel = getModel(model, previousModel);
}
@Override
public String toString() {
return "ClassListModel [valueModel=" + this.valueModel + "]";
}
}

View File

@ -17,4 +17,9 @@ public class ClassMapModel extends ClassModel {
this.valueModel = getModel(listType.getActualTypeArguments()[1], previousModel);
}
@Override
public String toString() {
return "ClassMapModel [keyModel=" + this.keyModel + ", valueModel=" + this.valueModel + "]";
}
}

View File

@ -14,7 +14,7 @@ public abstract class ClassModel {
return this.originClasses.contains(clazz);
}
public ClassModel getModel(final Type type, final ModelGroup previousModel) throws IOException {
public static ClassModel getModel(final Type type, final ModelGroup previousModel) throws IOException {
if (type == List.class) {
if (type instanceof final ParameterizedType parameterizedType) {
return new ClassListModel(parameterizedType, previousModel);
@ -32,4 +32,27 @@ public abstract class ClassModel {
return previousModel.add((Class<?>) type);
}
public static ClassModel getModelBase(
final Class<?> clazz,
final Type parameterizedType,
final ModelGroup previousModel) throws IOException {
if (clazz == List.class) {
return new ClassListModel((ParameterizedType) parameterizedType, previousModel);
}
if (clazz == Map.class) {
return new ClassMapModel((ParameterizedType) parameterizedType, previousModel);
}
return previousModel.add(clazz);
}
public static ClassModel getModel(final Class<?> type, final ModelGroup previousModel) throws IOException {
if (type == List.class) {
throw new IOException("Fail to manage parametrized type...");
}
if (type == Map.class) {
throw new IOException("Fail to manage parametrized type...");
}
return previousModel.add(type);
}
}

View File

@ -5,4 +5,15 @@ public class ClassObjectModel extends ClassModel {
public ClassObjectModel(final Class<?> clazz) {
this.originClasses.add(clazz);
}
@Override
public String toString() {
final StringBuilder out = new StringBuilder();
out.append("ClassObjectModel [");
for (final Class<?> elem : this.originClasses) {
out.append(elem.getCanonicalName());
}
out.append("]");
return out.toString();
}
}

View File

@ -3,7 +3,11 @@ package org.kar.archidata.externalRestApi.model;
import java.util.ArrayList;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class ModelGroup {
static final Logger LOGGER = LoggerFactory.getLogger(ModelGroup.class);
public List<ClassModel> previousModel = new ArrayList<>();
public ModelGroup() {}
@ -13,16 +17,23 @@ public class ModelGroup {
}
public ClassModel add(final Class<?> clazz) {
//LOGGER.trace("Search element {}", clazz.getCanonicalName());
for (final ClassModel value : this.previousModel) {
if (value.isCompatible(clazz)) {
//LOGGER.trace(" ==> return {}", value);
return value;
}
}
if (clazz.isEnum()) {
final ClassModel elem = new ClassEnumModel(clazz);
this.previousModel.add(elem);
//LOGGER.trace(" ==> return enum {}", elem);
return elem;
}
// create new model:
return null;
final ClassModel elem = new ClassObjectModel(clazz);
this.previousModel.add(elem);
//LOGGER.trace(" ==> return object {}", elem);
return elem;
}
}