Skip to content

Commit

Permalink
Merge pull request #255 from CorvusYe/master
Browse files Browse the repository at this point in the history
use antlr 4.11.1, beetl 3.15.10
  • Loading branch information
wey-gu authored Nov 14, 2023
2 parents 386b730 + 11b6cf9 commit 7e3d38c
Show file tree
Hide file tree
Showing 10 changed files with 167 additions and 145 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ This source code is licensed under Apache 2.0 License.
## Dependencies upgrade

- nebula-java: 3.5.0 -> 3.6.0
- beetl: 3.1.8-RELEASE -> 3.15.10.RELEASE
- antlr4: 4.7.2 -> 4.11.1

## Bugfix

Expand Down
18 changes: 17 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<beetl.version>3.15.10.RELEASE</beetl.version>
</properties>

<dependencies>
Expand Down Expand Up @@ -91,7 +92,22 @@
<dependency>
<groupId>com.ibeetl</groupId>
<artifactId>beetl</artifactId>
<version>3.1.8.RELEASE</version>
<version>${beetl.version}</version>
<exclusions>
<exclusion>
<groupId>com.ibeetl</groupId>
<artifactId>beetl-default-antlr4.9-support</artifactId>
</exclusion>
<exclusion>
<groupId>org.antlr</groupId>
<artifactId>antlr4-runtime</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.ibeetl</groupId>
<artifactId>beetl-antlr4.11-support</artifactId>
<version>${beetl.version}</version>
</dependency>

<!-- for nGQL escape in xml -->
Expand Down
Original file line number Diff line number Diff line change
@@ -1,51 +1,53 @@
package org.nebula.contrib.ngbatis.binding.beetl.functions;

import java.util.LinkedHashMap;
import java.util.Map;
import org.beetl.core.Template;
import org.nebula.contrib.ngbatis.models.ClassModel;
import org.nebula.contrib.ngbatis.models.MapperContext;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.LinkedHashMap;
import java.util.Map;

/**
* TODO
* 2023-9-6 14:28 lyw.
*/
public class IncludeFn extends AbstractFunction<String,Map<String,Object>,Void,Void,Void,Void>{
public class IncludeFn extends
AbstractFunction<String, Map<String, Object>, Void, Void, Void, Void> {

@Override
public Object call(String ngql,Map<String,Object> args) {
if(StringUtils.isEmpty(ngql)){
throw new RuntimeException("未指定nGQL片段");
}
int idx = ngql.lastIndexOf(".");
ClassModel classModel;
String ngqlId;
if(idx < 0){
ngqlId = ngql;
classModel = (ClassModel) ctx.globalVar.get("ng_cm");
}else{
String namespace = ngql.substring(0,idx);
ngqlId = ngql.substring(idx + 1);
classModel = MapperContext.newInstance().getInterfaces().get(namespace + ClassModel.PROXY_SUFFIX);
}
if(CollectionUtils.isEmpty(classModel.getNgqls()) || StringUtils.isEmpty(classModel.getNgqls().get(ngqlId))){
throw new RuntimeException("未找到 nGQL(" + ngql + ") 的定义");
}
Map<String,Object> param;
if(!CollectionUtils.isEmpty(args)){
//防止同名的 子片段参数 覆盖 父片段参数,导致渲染结果与预期不一致。
param = new LinkedHashMap<>(ctx.globalVar);
param.putAll(args);
}else{
param = ctx.globalVar;
}
String text = classModel.getNgqls().get(ngqlId).getText();
Template template = ctx.gt.getTemplate(text);
template.fastBinding(param);
template.renderTo(ctx.byteWriter);
return null;
@Override
public Object call(String ngql, Map<String, Object> args) {
if (StringUtils.isEmpty(ngql)) {
throw new RuntimeException("未指定nGQL片段");
}
int idx = ngql.lastIndexOf(".");
ClassModel classModel;
String ngqlId;
if (idx < 0) {
ngqlId = ngql;
classModel = (ClassModel) ctx.globalVar.get("ng_cm");
} else {
String namespace = ngql.substring(0, idx);
ngqlId = ngql.substring(idx + 1);
classModel = MapperContext.newInstance().getInterfaces()
.get(namespace + ClassModel.PROXY_SUFFIX);
}
if (CollectionUtils.isEmpty(classModel.getNgqls()) || StringUtils.isEmpty(
classModel.getNgqls().get(ngqlId))) {
throw new RuntimeException("未找到 nGQL(" + ngql + ") 的定义");
}
Map<String, Object> param;
if (!CollectionUtils.isEmpty(args)) {
//防止同名的 子片段参数 覆盖 父片段参数,导致渲染结果与预期不一致。
param = new LinkedHashMap<>(ctx.globalVar);
param.putAll(args);
} else {
param = ctx.globalVar;
}
String text = classModel.getNgqls().get(ngqlId).getText();
Template template = ctx.gt.getTemplate(text);
template.fastBinding(param);
template.renderTo(ctx.byteWriter);
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@
import com.vesoft.nebula.client.graph.data.PathWrapper;
import com.vesoft.nebula.client.graph.data.ResultSet;
import com.vesoft.nebula.client.graph.data.ResultSet.Record;
import java.io.UnsupportedEncodingException;
import javax.annotation.Resource;
import org.nebula.contrib.ngbatis.models.data.NgPath;
import org.nebula.contrib.ngbatis.utils.ResultSetUtil;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;

/**
* Convert the path data from ResultSet to NgPath.
* Convert the path data from ResultSet to NgPath.
*
* @author yeweicheng
* @since 2023-01-07 4:54
* <br> Now is history!
* <br> Now is history!
*/
@Component
public class NgPathResultHandler extends AbstractResultHandler<NgPath<?>, NgPath<?>> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,19 @@
//
// This source code is licensed under Apache 2.0 License.

import static org.nebula.contrib.ngbatis.utils.ResultSetUtil.nodePropsToMap;

import com.vesoft.nebula.client.graph.data.Node;
import com.vesoft.nebula.client.graph.data.ResultSet;
import com.vesoft.nebula.client.graph.data.ResultSet.Record;
import com.vesoft.nebula.client.graph.data.ValueWrapper;
import java.io.UnsupportedEncodingException;
import java.util.List;
import org.nebula.contrib.ngbatis.exception.ResultHandleException;
import org.nebula.contrib.ngbatis.models.data.NgVertex;
import org.nebula.contrib.ngbatis.utils.ResultSetUtil;
import org.springframework.stereotype.Component;

import java.io.UnsupportedEncodingException;
import java.util.List;

import static org.nebula.contrib.ngbatis.utils.ResultSetUtil.nodePropsToMap;

/**
* Convert the vertex data from ResultSet to NgVertex.
* @author yeweicheng
Expand Down Expand Up @@ -58,7 +57,7 @@ public NgVertex<?> handle(NgVertex<?> newResult, Node node) {
return newResult;
} catch (UnsupportedEncodingException e) {
throw new ResultHandleException(
String.format("%s : %s", e.getClass().toString(), e.getMessage()));
String.format("%s : %s", e.getClass().toString(), e.getMessage()));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.*;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
Expand Down Expand Up @@ -163,25 +167,23 @@ private Map<String, MethodModel> parseMethodModel(ClassModel cm, List<Node> node
if (methodNode instanceof Element) {


if(((Element) methodNode).tagName().equalsIgnoreCase("nGQL")){
if(Objects.isNull(cm.getNgqls())){
if (((Element) methodNode).tagName().equalsIgnoreCase("nGQL")) {
if (Objects.isNull(cm.getNgqls())) {
cm.setNgqls(new HashMap<>());
}
NgqlModel ngqlModel = parseNgqlModel((Element) methodNode);
cm.getNgqls().put(ngqlModel.getId(),ngqlModel);
}else{
} else {
MethodModel methodModel = parseMethodModel(methodNode);
addSpaceToSessionPool(methodModel.getSpace());
Method method = getNameUniqueMethod(namespace, methodModel.getId());
methodModel.setMethod(method);
Assert.notNull(method,
"接口 " + namespace.getName() + " 中,未声明 xml 中的出现的方法:" + methodModel.getId());
"接口 " + namespace.getName() + " 中,未声明 xml 中的出现的方法:" + methodModel.getId());
checkReturnType(method, namespace);
pageSupport(method, methodModel, methodNames, methods, namespace);
methods.put(methodModel.getId(), methodModel);
}


}
}
return methods;
Expand All @@ -208,10 +210,10 @@ protected MethodModel parseMethodModel(Node node) {

/**
* 解析nGQL语句片段
* @param ngqlEl
* @param ngqlEl xml 中的 &lt;nGQL&gt;标签
* @return
*/
protected NgqlModel parseNgqlModel(Element ngqlEl){
protected NgqlModel parseNgqlModel(Element ngqlEl) {
return new NgqlModel(ngqlEl.id(),ngqlEl.text());
}

Expand All @@ -238,21 +240,21 @@ private void checkReturnType(Method method, Class namespace) {
* @param methods 用于将需要分页的接口,自动追加两个接口,用于生成动态代理
*/
private void pageSupport(Method method, MethodModel methodModel, List<String> methodNames,
Map<String, MethodModel> methods, Class<?> namespace) throws NoSuchMethodException {
Map<String, MethodModel> methods, Class<?> namespace) throws NoSuchMethodException {
Class<?>[] parameterTypes = method.getParameterTypes();
List<Class<?>> parameterTypeList = Arrays.asList(parameterTypes);
if (parameterTypeList.contains(Page.class)) {
int pageParamIndex = parameterTypeList.indexOf(Page.class);
MethodModel pageMethod =
createPageMethod(
methodModel, methodNames, parameterTypes, pageParamIndex, namespace
);
createPageMethod(
methodModel, methodNames, parameterTypes, pageParamIndex, namespace
);
methods.put(pageMethod.getId(), pageMethod);

MethodModel countMethod = createCountMethod(
methodModel, methodNames, parameterTypes, namespace
);

methods.put(countMethod.getId(), countMethod);
}
}
Expand All @@ -267,11 +269,11 @@ private void pageSupport(Method method, MethodModel methodModel, List<String> me
* @return 自动分页的计数方法
*/
private MethodModel createCountMethod(MethodModel methodModel, List<String> methodNames,
Class<?>[] parameterTypes, Class<?> namespace) throws NoSuchMethodException {
Class<?>[] parameterTypes, Class<?> namespace) throws NoSuchMethodException {
String methodName = methodModel.getId();
String countMethodName = String.format("%s$Count", methodName);
Assert.isTrue(!methodNames.contains(countMethodName),
"There is a method name conflicts with " + countMethodName);
"There is a method name conflicts with " + countMethodName);
MethodModel countMethodModel = new MethodModel();
setParamAnnotations(parameterTypes, namespace, methodName, countMethodModel);
countMethodModel.setParameterTypes(parameterTypes);
Expand All @@ -298,12 +300,12 @@ private MethodModel createCountMethod(MethodModel methodModel, List<String> meth
* @return 查询范围条目方法 的方法模型
*/
private MethodModel createPageMethod(MethodModel methodModel, List<String> methodNames,
Class<?>[] parameterTypes, int pageParamIndex, Class<?> namespace)
Class<?>[] parameterTypes, int pageParamIndex, Class<?> namespace)
throws NoSuchMethodException {
String methodName = methodModel.getId();
String pageMethodName = String.format("%s$Page", methodName);
Assert.isTrue(!methodNames.contains(pageMethodName),
"There is a method name conflicts with " + pageMethodName);
"There is a method name conflicts with " + pageMethodName);
MethodModel pageMethodModel = new MethodModel();
Annotation[][] parameterAnnotations = setParamAnnotations(parameterTypes, namespace,
methodName, pageMethodModel);
Expand Down
36 changes: 18 additions & 18 deletions src/main/java/org/nebula/contrib/ngbatis/models/NgqlModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,28 @@
*/
public class NgqlModel {

private String id;
private String text;
private String id;
private String text;

public NgqlModel(String id, String text) {
this.id = id;
this.text = text;
}
public NgqlModel(String id, String text) {
this.id = id;
this.text = text;
}

public String getId() {
return id;
}
public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}
public void setId(String id) {
this.id = id;
}

public String getText() {
return text;
}
public String getText() {
return text;
}

public void setText(String text) {
this.text = text;
}
public void setText(String text) {
this.text = text;
}

}
Loading

0 comments on commit 7e3d38c

Please sign in to comment.