support set order for rpc.

This commit is contained in:
oppofind 2020-09-05 13:45:32 +08:00
parent 64a913b796
commit ab0b94219c
3 changed files with 30 additions and 17 deletions

View File

@ -5,7 +5,7 @@
<modelVersion>4.0.0</modelVersion>
<artifactId>smart-doc</artifactId>
<packaging>jar</packaging>
<version>1.9.3</version>
<version>1.9.4</version>
<name>smart-doc</name>
<url>https://github.com/smart-doc-group/smart-doc.git</url>

View File

@ -23,6 +23,7 @@
package com.power.doc.template;
import com.power.common.util.StringUtil;
import com.power.common.util.ValidateUtil;
import com.power.doc.builder.ProjectDocConfigBuilder;
import com.power.doc.constants.DocAnnotationConstants;
import com.power.doc.constants.DocGlobalConstants;
@ -41,6 +42,8 @@ import com.power.doc.utils.JavaClassValidateUtil;
import com.thoughtworks.qdox.model.*;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
import static com.power.doc.constants.DocTags.IGNORE;
@ -49,28 +52,42 @@ import static com.power.doc.constants.DocTags.IGNORE;
*/
public class RpcDocBuildTemplate implements IDocBuildTemplate<RpcApiDoc> {
/**
* api index
*/
private final AtomicInteger atomicInteger = new AtomicInteger(1);
public List<RpcApiDoc> getApiData(ProjectDocConfigBuilder projectBuilder) {
ApiConfig apiConfig = projectBuilder.getApiConfig();
List<RpcApiDoc> apiDocList = new ArrayList<>();
int order = 0;
boolean setCustomOrder = false;
for (JavaClass cls : projectBuilder.getJavaProjectBuilder().getClasses()) {
if (!checkDubboInterface(cls)) {
continue;
}
if (StringUtil.isNotEmpty(apiConfig.getPackageFilters())) {
if (DocUtil.isMatch(apiConfig.getPackageFilters(), cls.getCanonicalName())) {
if (!DocUtil.isMatch(apiConfig.getPackageFilters(), cls.getCanonicalName())) {
continue;
}
}
String strOrder = JavaClassUtil.getClassTagsValue(cls, DocTags.ORDER, Boolean.TRUE);
order++;
if (ValidateUtil.isNonnegativeInteger(strOrder)) {
order = Integer.parseInt(strOrder);
setCustomOrder = true;
}
List<JavaMethodDoc> apiMethodDocs = buildServiceMethod(cls, apiConfig, projectBuilder);
this.handleJavaApiDoc(cls, apiDocList, apiMethodDocs, order, projectBuilder);
}
} else {
order++;
List<JavaMethodDoc> apiMethodDocs = buildServiceMethod(cls, apiConfig, projectBuilder);
this.handleJavaApiDoc(cls, apiDocList, apiMethodDocs, order, projectBuilder);
}
}
// sort
if (apiConfig.isSortByTitle()) {
Collections.sort(apiDocList);
} else if (setCustomOrder) {
// while set custom oder
return apiDocList.stream()
.sorted(Comparator.comparing(RpcApiDoc::getOrder))
.peek(p -> p.setOrder(atomicInteger.getAndAdd(1))).collect(Collectors.toList());
}
return apiDocList;
}
@ -97,7 +114,7 @@ public class RpcDocBuildTemplate implements IDocBuildTemplate<RpcApiDoc> {
throw new RuntimeException("Unable to find comment for method " + method.getName() + " in " + cls.getCanonicalName());
}
boolean deprecated = false;
//设置当前接口是否过时
//Deprecated
List<JavaAnnotation> annotations = method.getAnnotations();
for (JavaAnnotation annotation : annotations) {
String annotationName = annotation.getType().getName();

View File

@ -56,7 +56,7 @@ public class SpringBootDocBuildTemplate implements IDocBuildTemplate<ApiDoc> {
/**
* api index
*/
private AtomicInteger atomicInteger = new AtomicInteger(1);
private final AtomicInteger atomicInteger = new AtomicInteger(1);
@Override
public List<ApiDoc> getApiData(ProjectDocConfigBuilder projectBuilder) {
@ -90,13 +90,9 @@ public class SpringBootDocBuildTemplate implements IDocBuildTemplate<ApiDoc> {
Collections.sort(apiDocList);
} else if (setCustomOrder) {
// while set custom oder
List<ApiDoc> sortedApiDoc = apiDocList.stream()
return apiDocList.stream()
.sorted(Comparator.comparing(ApiDoc::getOrder))
.map(p -> {
p.setOrder(atomicInteger.getAndAdd(1));
return p;
}).collect(Collectors.toList());
return sortedApiDoc;
.peek(p -> p.setOrder(atomicInteger.getAndAdd(1))).collect(Collectors.toList());
}
return apiDocList;
}