support dictionary
This commit is contained in:
parent
a98c5c9c58
commit
929be19d3f
|
@ -7,13 +7,15 @@ import com.power.common.util.StringUtil;
|
|||
import com.power.doc.constants.DocGlobalConstants;
|
||||
import com.power.doc.constants.DocLanguage;
|
||||
import com.power.doc.constants.TemplateVariable;
|
||||
import com.power.doc.model.ApiConfig;
|
||||
import com.power.doc.model.ApiDoc;
|
||||
import com.power.doc.model.ApiErrorCode;
|
||||
import com.power.doc.model.*;
|
||||
import com.power.doc.utils.BeetlTemplateUtil;
|
||||
import org.beetl.core.Template;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
import static com.power.doc.constants.DocGlobalConstants.FILE_SEPARATOR;
|
||||
|
||||
|
@ -23,6 +25,7 @@ import static com.power.doc.constants.DocGlobalConstants.FILE_SEPARATOR;
|
|||
public class DocBuilderTemplate {
|
||||
|
||||
private static long now = System.currentTimeMillis();
|
||||
|
||||
/**
|
||||
* check condition and init
|
||||
*
|
||||
|
@ -101,18 +104,28 @@ public class DocBuilderTemplate {
|
|||
tpl.binding(TemplateVariable.API_DOC_LIST.getVariable(), apiDocList);
|
||||
tpl.binding(TemplateVariable.ERROR_CODE_LIST.getVariable(), config.getErrorCodes());
|
||||
tpl.binding(TemplateVariable.VERSION_LIST.getVariable(), config.getRevisionLogs());
|
||||
tpl.binding(TemplateVariable.VERSION.getVariable(),now);
|
||||
tpl.binding(TemplateVariable.VERSION.getVariable(), now);
|
||||
tpl.binding(TemplateVariable.CREATE_TIME.getVariable(), strTime);
|
||||
tpl.binding(TemplateVariable.PROJECT_NAME.getVariable(),config.getProjectName());
|
||||
tpl.binding(TemplateVariable.PROJECT_NAME.getVariable(), config.getProjectName());
|
||||
if (CollectionUtil.isEmpty(config.getErrorCodes())) {
|
||||
tpl.binding(TemplateVariable.DICT_ORDER.getVariable(), apiDocList.size() + 1);
|
||||
} else {
|
||||
tpl.binding(TemplateVariable.DICT_ORDER.getVariable(), apiDocList.size() + 2);
|
||||
}
|
||||
if (null != config.getLanguage()) {
|
||||
if (DocLanguage.CHINESE.code.equals(config.getLanguage().getCode())) {
|
||||
tpl.binding(TemplateVariable.ERROR_LIST_TITLE.getVariable(), DocGlobalConstants.ERROR_CODE_LIST_CN_TITLE);
|
||||
tpl.binding(TemplateVariable.DICT_LIST_TITLE.getVariable(), DocGlobalConstants.DICT_CN_TITLE);
|
||||
} else {
|
||||
tpl.binding(TemplateVariable.ERROR_LIST_TITLE.getVariable(), DocGlobalConstants.ERROR_CODE_LIST_EN_TITLE);
|
||||
tpl.binding(TemplateVariable.DICT_LIST_TITLE.getVariable(), DocGlobalConstants.DICT_EN_TITLE);
|
||||
}
|
||||
} else {
|
||||
tpl.binding(TemplateVariable.ERROR_LIST_TITLE.getVariable(), DocGlobalConstants.ERROR_CODE_LIST_CN_TITLE);
|
||||
tpl.binding(TemplateVariable.DICT_LIST_TITLE.getVariable(), DocGlobalConstants.DICT_CN_TITLE);
|
||||
}
|
||||
List<ApiDocDict> apiDocDictList = buildDictionary(config);
|
||||
tpl.binding(TemplateVariable.DICT_LIST.getVariable(), apiDocDictList);
|
||||
FileUtil.nioWriteFile(tpl.render(), outPath + FILE_SEPARATOR + outPutFileName);
|
||||
}
|
||||
|
||||
|
@ -151,4 +164,53 @@ public class DocBuilderTemplate {
|
|||
mapper.binding(TemplateVariable.LIST.getVariable(), doc.getList());
|
||||
FileUtil.writeFileNotAppend(mapper.render(), outPath + FILE_SEPARATOR + doc.getName() + fileExtension);
|
||||
}
|
||||
|
||||
/**
|
||||
* Build dictionary
|
||||
*
|
||||
* @param config api config
|
||||
* @return list of ApiDocDict
|
||||
*/
|
||||
public List<ApiDocDict> buildDictionary(ApiConfig config) {
|
||||
List<ApiDataDictConfig> apiDataDictionaryList = config.getDataDictConfigs();
|
||||
List<ApiDocDict> apiDocDictList = new ArrayList<>();
|
||||
try {
|
||||
int order = 0;
|
||||
for (ApiDataDictConfig apiDataDictionary : apiDataDictionaryList) {
|
||||
order++;
|
||||
ApiDocDict apiDocDict = new ApiDocDict();
|
||||
apiDocDict.setOrder(order);
|
||||
apiDocDict.setTitle(apiDataDictionary.getTitle());
|
||||
Class<?> clzz = apiDataDictionary.getEnumClass();
|
||||
if (Objects.isNull(clzz)) {
|
||||
if (StringUtil.isEmpty(apiDataDictionary.getEnumClassName())) {
|
||||
throw new RuntimeException(" enum class name can't be null.");
|
||||
}
|
||||
clzz = Class.forName(apiDataDictionary.getEnumClassName());
|
||||
}
|
||||
if (!clzz.isEnum()) {
|
||||
throw new RuntimeException(clzz.getCanonicalName() + " is not an enum class.");
|
||||
}
|
||||
List<DataDict> dataDictList = new ArrayList<>();
|
||||
Object[] objects = clzz.getEnumConstants();
|
||||
String valueMethodName = "get" + StringUtil.firstToUpperCase(apiDataDictionary.getValueField());
|
||||
String descMethodName = "get" + StringUtil.firstToUpperCase(apiDataDictionary.getDescField());
|
||||
Method valueMethod = clzz.getMethod(valueMethodName);
|
||||
Method descMethod = clzz.getMethod(descMethodName);
|
||||
for (Object object : objects) {
|
||||
Object val = valueMethod.invoke(object);
|
||||
Object desc = descMethod.invoke(object);
|
||||
DataDict dataDict = new DataDict();
|
||||
dataDict.setDesc(desc.toString());
|
||||
dataDict.setValue(val.toString());
|
||||
dataDictList.add(dataDict);
|
||||
}
|
||||
apiDocDict.setDataDictList(dataDictList);
|
||||
apiDocDictList.add(apiDocDict);
|
||||
}
|
||||
} catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException | ClassNotFoundException e) {
|
||||
e.fillInStackTrace();
|
||||
}
|
||||
return apiDocDictList;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -77,6 +77,10 @@ public class DocGlobalConstants {
|
|||
|
||||
public static final String ERROR_CODE_LIST_EN_TITLE = "Error Code List";
|
||||
|
||||
public static final String DICT_CN_TITLE = "数据字典";
|
||||
|
||||
public static final String DICT_EN_TITLE = "Data Dictionary";
|
||||
|
||||
public static final String FIELD_SPACE = " ";
|
||||
|
||||
public static final String ANY_OBJECT_MSG = "any object.";
|
||||
|
|
|
@ -16,6 +16,9 @@ public enum TemplateVariable {
|
|||
ERROR_LIST_TITLE("errorListTitle"),
|
||||
CREATE_TIME("createTime"),
|
||||
PROJECT_NAME("projectName"),
|
||||
DICT_LIST("dictList"),
|
||||
DICT_LIST_TITLE("dictListTitle"),
|
||||
DICT_ORDER("dictListOrder"),
|
||||
VERSION("version");
|
||||
|
||||
|
||||
|
|
|
@ -98,4 +98,24 @@ for(error in errorCodeList){
|
|||
|${error.value}|${error.desc}
|
||||
<%}%>
|
||||
|====================
|
||||
<%}%>
|
||||
|
||||
<%if(isNotEmpty(dictList)){%>
|
||||
== ${dictListTitle}
|
||||
<%
|
||||
for(dict in dictList){
|
||||
%>
|
||||
=== ${dict.title}
|
||||
|
||||
[width="100%",options="header"]
|
||||
[stripes=even]
|
||||
|====================
|
||||
|Value |Description
|
||||
<%
|
||||
for(dataDict in dict.dataDictList){
|
||||
%>
|
||||
|${dataDict.value}|${dataDict.desc}
|
||||
<%}%>
|
||||
|====================
|
||||
<%}%>
|
||||
<%}%>
|
|
@ -85,4 +85,21 @@ for(error in errorCodeList){
|
|||
%>
|
||||
${error.value}|${error.desc}
|
||||
<%}%>
|
||||
<%}%>
|
||||
|
||||
<%if(isNotEmpty(dictList)){%>
|
||||
## ${dictListTitle}
|
||||
<%
|
||||
for(dict in dictList){
|
||||
%>
|
||||
### ${dict.title}
|
||||
|
||||
Value |Description
|
||||
---|---
|
||||
<%
|
||||
for(dataDict in dict.dataDictList){
|
||||
%>
|
||||
${dataDict.value}|${dataDict.desc}
|
||||
<%}%>
|
||||
<%}%>
|
||||
<%}%>
|
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue