org.coodex.concrete.apitools.jaxrs.axios.AxiosCodeRenderer Maven / Gradle / Ivy
/*
* Copyright (c) 2018 coodex.org ([email protected])
*
* 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 org.coodex.concrete.apitools.jaxrs.axios;
import org.coodex.concrete.api.Description;
import org.coodex.concrete.apitools.AbstractRenderer;
import org.coodex.concrete.apitools.jaxrs.EnumElementInfo;
import org.coodex.concrete.apitools.jaxrs.JaxrsRenderHelper;
import org.coodex.concrete.common.ConcreteHelper;
import org.coodex.concrete.jaxrs.JaxRSModuleMaker;
import org.coodex.concrete.jaxrs.struct.JaxrsModule;
import org.coodex.concrete.jaxrs.struct.JaxrsParam;
import org.coodex.concrete.jaxrs.struct.JaxrsUnit;
import org.coodex.concrete.own.OwnServiceUnit;
import org.coodex.util.Common;
import org.coodex.util.PojoInfo;
import java.io.IOException;
import java.lang.reflect.GenericArrayType;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.util.*;
import java.util.function.Consumer;
import java.util.function.Function;
import static org.coodex.concrete.apitools.jaxrs.axios.TsDefineHelper.ifEnum;
import static org.coodex.concrete.common.ConcreteHelper.isPrimitive;
import static org.coodex.util.Common.cast;
public class AxiosCodeRenderer extends AbstractRenderer {
public static final String RENDER_NAME =
JaxRSModuleMaker.JAX_RS_PREV + ".code.axios.js.v1";
private static final String RESOURCE_PACKAGE = "concrete/templates/jaxrs/axios/code/v1/";
private boolean constUtilExist = false;
@Override
protected String getTemplatePath() {
return RESOURCE_PACKAGE;
}
@Override
protected String getRenderName() {
return RENDER_NAME;
}
// @Override
// public void writeTo(String... packages) throws IOException {
// List moduleList = loadModules(RENDER_NAME, packages);
// render(moduleList);
// }
private void processEnum(JaxrsUnit unit, Set processed) {
// 1.返回值
processEnum(unit.getGenericReturnType(), processed);
for (JaxrsParam param : unit.getParameters()) {
processEnum(param.getGenericType(), processed);
}
}
private void processEnum(Type t, Set processed) {
if (processed.contains(t)) return;
processed.add(t);
if (t instanceof ParameterizedType) {
ParameterizedType pt = (ParameterizedType) t;
processEnum(pt.getRawType(), processed);
for (Type type : pt.getActualTypeArguments()) {
processEnum(type, processed);
}
} else if (t instanceof TypeVariable) {
} else if (t instanceof GenericArrayType) {
processEnum(((GenericArrayType) t).getGenericComponentType(), processed);
} else if (t instanceof Class) {
Class> c = (Class>) t;
if (c.isArray()) {
processEnum(c.getComponentType(), processed);
} else if (c.isEnum()) {
Map map = new HashMap<>();
map.put("elements", EnumElementInfo.of(Common.cast(c)));
try {
writeTo(
"jaxrs/constants/" + c.getName() + ".js",
"enumerable.ftl",
map
);
} catch (IOException e) {
throw new RuntimeException(e);
}
// for d.ts
map.put("enumTypeName", c.getSimpleName());
map.put("valueType", TsDefineHelper.getTypeScriptValueType(Common.cast(c)));
try {
writeTo("jaxrs/constants/" + c.getName() + ".d.ts",
"enumerable.d.ts.ftl",
map);
} catch (IOException e) {
throw new RuntimeException(e);
}
} else if (!isPrimitive(c) && !c.getPackage().getName().startsWith("java")) {
PojoInfo pojoInfo = new PojoInfo(c);
pojoInfo.getProperties().forEach(pojoProperty -> {
processEnum(pojoProperty.getType(), processed);
});
}
}
}
private void processEnum(JaxrsModule module, Set processed) throws IOException {
if (!constUtilExist) {
copyTo("EnumBase.d.ts.ftl", "jaxrs/constants/EnumBase.d.ts");
writeTo("jaxrs/constants/constUtil.js", "const allValues = c => {\n" +
" var a = []\n" +
" for (var k in c) {\n" +
" if (typeof c[k] === 'function') continue\n" +
" a.push(c[k])\n" +
" }\n" +
" return a\n" +
"}\n" +
"\n" +
"export function valueOf(c, value) {\n" +
" for (var k in c) {\n" +
" if (typeof c[k] === 'function') continue\n" +
" if (c[k] === value) {\n" +
" return {\n" +
" key: k,\n" +
" value: c[k],\n" +
" }\n" +
" }\n" +
" }\n" +
" return null\n" +
"}\n" +
"\n" +
"export function toArray(c, values) {\n" +
" var result = []\n" +
" if (!values) values = allValues(c)\n" +
" for (var i = 0, l = values.length; i < l; i++) {\n" +
" var v = valueOf(c, values[i])\n" +
" if (v) result.push(v)\n" +
" }\n" +
" return result\n" +
"}");
constUtilExist = true;
}
for (JaxrsUnit unit : module.getUnits()) {
processEnum(unit, processed);
}
}
@Override
public void render(List modules) throws IOException {
constUtilExist = false;
String moduleName = getModuleName();
Map versionAndStyle = new HashMap<>();
versionAndStyle.put("version", ConcreteHelper.VERSION);
// versionAndStyle.put("style", JaxRSHelper.used024Behavior());
writeTo("jaxrs/concrete.js", "concrete.ftl", versionAndStyle);
copyTo("concrete.d.ts.ftl", "jaxrs/concrete.d.ts");
Set processed = new HashSet<>();
for (JaxrsModule module : modules) {
processEnum(module, processed);
Map param = new HashMap<>();
param.put("moduleName", moduleName);
param.put("serviceName", module.getInterfaceClass().getSimpleName());
Map> methods = new HashMap<>();
for (JaxrsUnit unit : module.getUnits()) {
String methodName = unit.getMethod().getName();
Map method = methods.get(methodName);
if (method == null) {
method = new HashMap<>();
method.put("name", methodName);
method.put("serviceId", OwnServiceUnit.getUnitKey(unit));
methods.put(methodName, method);
}
method.put("jsdoc", JaxrsRenderHelper.getDoc(unit));
List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy