Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

解决同一点的Tag实体类相互独立时,映射的Object类型可能发生错误的问题 #311

Merged
merged 1 commit into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public Object handle(

for (int i = 0; i < columnNames.size(); i++) {
ValueWrapper valueWrapper = record.values().get(i);
Object v = ResultSetUtil.getValue(valueWrapper);
Object v = ResultSetUtil.getValue(valueWrapper,resultType);
newResult = fillResult(v, newResult, columnNames, resultType, i);
}

Expand Down
46 changes: 22 additions & 24 deletions src/main/java/org/nebula/contrib/ngbatis/utils/ResultSetUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,24 +79,26 @@ public class ResultSetUtil {
* @param <T> 目标结果类型
* @return
*/
public static <T> T getValue(ValueWrapper value) {
public static <T> T getValue(ValueWrapper value,Class<T> resultType) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

公开方法如果要拓展,可能做一层重载会合适一些

try {
Object o = value.isLong() ? value.asLong()
: value.isBoolean() ? value.asBoolean()
: value.isDouble() ? value.asDouble()
: value.isString() ? value.asString()
: value.isBoolean() ? value.asBoolean()
: value.isDouble() ? value.asDouble()
: value.isString() ? value.asString()
: value.isTime() ? transformTime(value.asTime())
: value.isDate() ? transformDate(value.asDate())
: value.isDateTime() ? transformDateTime(value.asDateTime())
: value.isVertex() ? transformNode(value.asNode())
: value.isEdge() ? transformRelationship(value)
: value.isPath() ? value.asPath()
: value.isList() ? transformList(value.asList())
: value.isSet() ? transformSet(value.asSet())
: value.isMap() ? transformMap(value.asMap())
: value.isDuration() ? transformDuration(value.asDuration())
: null;

: value.isDate() ? transformDate(value.asDate())
: value.isDateTime() ? transformDateTime(value.asDateTime())
: value.isVertex() ? transformNode(value.asNode(),resultType)
: value.isEdge() ? transformRelationship(value)
: value.isPath() ? value.asPath()
: value.isList() ? transformList(value.asList())
: value.isSet() ? transformSet(value.asSet())
: value.isMap() ? transformMap(value.asMap())
: value.isDuration() ? transformDuration(value.asDuration())
: null;
Comment on lines +89 to +98
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我也觉得这个格式看起来会更符合一些,但是代码风格校验会通不过

if (o instanceof Number) {
o = castNumber((Number) o, resultType);
}
return (T) o;
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
Expand All @@ -106,15 +108,11 @@ public static <T> T getValue(ValueWrapper value) {
/**
* 根据 resultType 从 nebula 的数据类型中获取 java 类型数据
* @param valueWrapper nebula 的数据类型
* @param resultType 接口返回值类型(类型为集合时,为集合泛型)
* @param <T> 调用方用来接收结果的类型,即 resultType
* @return java类型结果
*/
public static <T> T getValue(ValueWrapper valueWrapper, Class<T> resultType) {
T value = getValue(valueWrapper);
if (value instanceof Number) {
value = (T) castNumber((Number) value, resultType);
}
public static <T> T getValue(ValueWrapper valueWrapper) {
T value = getValue(valueWrapper,null);
return value;
}

Expand Down Expand Up @@ -149,16 +147,16 @@ private static Object transformDuration(DurationWrapper du) {
return java.time.Duration.ofNanos(du.getSeconds() * 1000000000);
}

private static Object transformNode(Node node) {
private static Object transformNode(Node node,Class<?> resultType) {
MapperContext mapperContext = MapperProxy.ENV.getMapperContext();
Map<String, Class<?>> tagTypeMapping = mapperContext.getTagTypeMapping();
Class<?> nodeType = null;
Class<?> nodeType = resultType;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这边可能会导致 nodeType 始终都不为空,可能会影响到现有的 如果无映射类型会将节点转换成 map 的功能。虽然正常不推荐该用法

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

如果只是找到 tagType 的时候 增加一个判断 resultType和tagType 是否具备继承关系,具备继承关系才赋予 nodeType 为tagType,这样应该可以不影响 转Map

List<String> tagNames = node.tagNames();

for (String tagName : tagNames) {
Class<?> tagType = tagTypeMapping.get(tagName);
boolean tagTypeIsSuperClass = isCurrentTypeOrParentType(nodeType, tagType);
if (!tagTypeIsSuperClass) {
if (!tagTypeIsSuperClass && nodeType.isAssignableFrom(tagType)) {
nodeType = tagType;
}
}
Expand Down
Loading