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

feat: 支持 JS 对象和数组转 Map 和 Array #75

Closed
wants to merge 5 commits into from

Conversation

HarlonWang
Copy link
Owner

@HarlonWang HarlonWang commented Sep 27, 2024

#65

Sourcery 的总结

在 QuickJS 包装器中添加将 JavaScript 对象和数组转换为 Java HashMap 和 ArrayList 的功能,并支持处理循环引用。实现相应的测试以确保正确的转换行为。

新功能:

  • 在 QuickJS 包装器中添加支持将 JavaScript 对象转换为 Java HashMap 和数组转换为 ArrayList。

增强功能:

  • 实现一种方法,在将 JavaScript 对象和数组转换为 Java 集合时处理循环引用。

测试:

  • 引入测试以验证将 JavaScript 对象和数组转换为 Java HashMap 和 ArrayList 的过程,包括循环引用的处理。
Original summary in English

Summary by Sourcery

Add functionality to convert JavaScript objects and arrays to Java HashMap and ArrayList in the QuickJS wrapper, with support for handling circular references. Implement corresponding tests to ensure correct conversion behavior.

New Features:

  • Add support for converting JavaScript objects to Java HashMap and arrays to ArrayList in the QuickJS wrapper.

Enhancements:

  • Implement a method to handle circular references during the conversion of JavaScript objects and arrays to Java collections.

Tests:

  • Introduce tests to verify the conversion of JavaScript objects and arrays to Java HashMap and ArrayList, including handling of circular references.

Copy link

sourcery-ai bot commented Sep 27, 2024

审核者指南 by Sourcery

此拉取请求实现了将 JavaScript 对象和数组转换为 Java Map 和 Array 结构的支持。主要更改包括添加新方法以将 JSObject 和 JSArray 实例转换为它们的 Java 对应物,处理循环引用,并实现特定类型的转换逻辑。

序列图

sequenceDiagram
    participant Client
    participant JSObject
    participant ConvertToMap
    participant JavaMap

    Client->>JSObject: toMap()
    JSObject->>ConvertToMap: convertToMap(this, new HashMap)
    loop 对每个属性
        ConvertToMap->>ConvertToMap: 检查循环引用
        ConvertToMap->>ConvertToMap: 根据类型转换值
        ConvertToMap->>JavaMap: 放置转换后的值
    end
    ConvertToMap-->>JSObject: 返回转换后的 map
    JSObject-->>Client: 返回 Java Map
Loading

文件级更改

更改 详情 文件
实现 JSObject 到 Java Map 的转换
  • 在 JSObject 接口中添加 toMap() 方法
  • 在 QuickJSObject 类中实现 toMap()
  • 添加 convertToMap() 方法以处理递归转换
  • 使用新的 circulars ArrayList 处理循环引用
  • 将 JSArray、JSObject 和原始类型转换为适当的 Java 类型
wrapper-java/src/main/java/com/whl/quickjs/wrapper/QuickJSObject.java
wrapper-java/src/main/java/com/whl/quickjs/wrapper/JSObject.java
实现 JSArray 到 Java ArrayList 的转换
  • 在 JSObject 接口中添加 toArray() 方法
  • 在 QuickJSArray 类中实现 toArray()
  • 重用 convertToMap() 方法进行数组转换
wrapper-java/src/main/java/com/whl/quickjs/wrapper/QuickJSArray.java
wrapper-java/src/main/java/com/whl/quickjs/wrapper/JSObject.java
为不兼容的转换添加不支持的操作异常
  • 为 QuickJSObject.toArray() 抛出 UnsupportedOperationException
  • 为 QuickJSArray.toMap() 抛出 UnsupportedOperationException
  • 为 QuickJSFunction.toMap() 和 toArray() 抛出 UnsupportedOperationException
wrapper-java/src/main/java/com/whl/quickjs/wrapper/QuickJSObject.java
wrapper-java/src/main/java/com/whl/quickjs/wrapper/QuickJSArray.java
wrapper-java/src/main/java/com/whl/quickjs/wrapper/QuickJSFunction.java
为新的转换功能添加单元测试
  • 测试 JSObject 到 Map 的转换
  • 测试 JSArray 到 ArrayList 的转换
  • 测试全局对象到 Map 的转换
wrapper-android/src/androidTest/java/com/whl/quickjs/wrapper/QuickJSTest.java

提示和命令

与 Sourcery 互动

  • 触发新审核: 在拉取请求中评论 @sourcery-ai review
  • 继续讨论: 直接回复 Sourcery 的审核评论。
  • 从审核评论生成 GitHub 问题: 通过回复审核评论请求 Sourcery 创建一个问题。

自定义您的体验

访问您的仪表板以:

  • 启用或禁用审核功能,如 Sourcery 生成的拉取请求摘要、审核者指南等。
  • 更改审核语言。
  • 添加、删除或编辑自定义审核说明。
  • 调整其他审核设置。

获取帮助

Original review guide in English

Reviewer's Guide by Sourcery

This pull request implements support for converting JavaScript objects and arrays to Java Map and Array structures. The main changes include adding new methods to convert JSObject and JSArray instances to their Java counterparts, handling circular references, and implementing type-specific conversion logic.

Sequence Diagram

sequenceDiagram
    participant Client
    participant JSObject
    participant ConvertToMap
    participant JavaMap

    Client->>JSObject: toMap()
    JSObject->>ConvertToMap: convertToMap(this, new HashMap)
    loop For each property
        ConvertToMap->>ConvertToMap: Check circular reference
        ConvertToMap->>ConvertToMap: Convert value based on type
        ConvertToMap->>JavaMap: Put converted value
    end
    ConvertToMap-->>JSObject: Return converted map
    JSObject-->>Client: Return Java Map
Loading

File-Level Changes

Change Details Files
Implement conversion of JSObject to Java Map
  • Add toMap() method to JSObject interface
  • Implement toMap() in QuickJSObject class
  • Add convertToMap() method to handle recursive conversion
  • Handle circular references using a new circulars ArrayList
  • Convert JSArray, JSObject, and primitive types to appropriate Java types
wrapper-java/src/main/java/com/whl/quickjs/wrapper/QuickJSObject.java
wrapper-java/src/main/java/com/whl/quickjs/wrapper/JSObject.java
Implement conversion of JSArray to Java ArrayList
  • Add toArray() method to JSObject interface
  • Implement toArray() in QuickJSArray class
  • Reuse convertToMap() method for array conversion
wrapper-java/src/main/java/com/whl/quickjs/wrapper/QuickJSArray.java
wrapper-java/src/main/java/com/whl/quickjs/wrapper/JSObject.java
Add unsupported operation exceptions for incompatible conversions
  • Throw UnsupportedOperationException for QuickJSObject.toArray()
  • Throw UnsupportedOperationException for QuickJSArray.toMap()
  • Throw UnsupportedOperationException for QuickJSFunction.toMap() and toArray()
wrapper-java/src/main/java/com/whl/quickjs/wrapper/QuickJSObject.java
wrapper-java/src/main/java/com/whl/quickjs/wrapper/QuickJSArray.java
wrapper-java/src/main/java/com/whl/quickjs/wrapper/QuickJSFunction.java
Add unit tests for new conversion functionality
  • Test conversion of JSObject to Map
  • Test conversion of JSArray to ArrayList
  • Test conversion of global object to Map
wrapper-android/src/androidTest/java/com/whl/quickjs/wrapper/QuickJSTest.java

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

@HarlonWang - 我已经审查了你的更改 - 这里有一些反馈:

总体评论

  • 考虑改进内存管理,特别是对于嵌套结构。确保所有 JavaScript 对象在转换后都被正确释放。
  • 统一不同类型(JSObject、JSArray、JSFunction)不支持转换的错误信息。
这是我在审查期间查看的内容
  • 🟡 一般问题:发现 1 个问题
  • 🟢 安全性:一切看起来都很好
  • 🟢 测试:一切看起来都很好
  • 🟡 复杂性:发现 1 个问题
  • 🟢 文档:一切看起来都很好

Sourcery 对开源项目免费 - 如果你喜欢我们的审查,请考虑分享它们 ✨
帮助我变得更有用!请点击每条评论上的 👍 或 👎 告诉我它是否有帮助。
Original comment in English

Hey @HarlonWang - I've reviewed your changes - here's some feedback:

Overall Comments:

  • Consider improving memory management, especially for nested structures. Ensure all JavaScript objects are properly released after conversion.
  • Standardize error messages for unsupported conversions across different types (JSObject, JSArray, JSFunction).
Here's what I looked at during the review
  • 🟡 General issues: 1 issue found
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟡 Complexity: 1 issue found
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment to tell me if it was helpful.

throw new UnsupportedOperationException("Object types are not yet supported for conversion to array. You should use toMap.");
}

protected void convertToMap(Object target, Object map) {
Copy link

Choose a reason for hiding this comment

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

issue (complexity): 考虑将 'convertToMap' 方法重构为更小、更专注的方法。

convertToMap 方法引入了显著的复杂性。虽然功能很重要,但我们建议以下改进以增强可读性和可维护性:

  1. convertToMap 方法分解为更小、更专注的私有方法:
private void convertToMap(Object target, Object map) {
    if (handleCircularReference(target)) return;

    if (target instanceof JSArray) {
        handleArrayConversion((JSArray) target, map);
    } else {
        handleObjectConversion((JSObject) target, map);
    }
}

private boolean handleCircularReference(Object target) {
    long pointer = ((JSObject) target).getPointer();
    if (circulars.contains(pointer)) return true;
    circulars.add(pointer);
    return false;
}

private void handleArrayConversion(JSArray array, Object map) {
    // ... implementation ...
}

private void handleObjectConversion(JSObject object, Object map) {
    // ... implementation ...
}
  1. 封装循环引用处理:
    不要使用受保护的 circulars 字段,而是在 toMap 中使用局部变量:
public HashMap<String, Object> toMap() {
    HashMap<String, Object> objectMap = new HashMap<>();
    ArrayList<Long> circulars = new ArrayList<>();
    convertToMap(this, objectMap, circulars);
    return objectMap;
}

private void convertToMap(Object target, Object map, ArrayList<Long> circulars) {
    // 使用 circulars 参数而不是字段
}
  1. 移除 toArray 方法,因为它未实现并抛出异常。

这些更改将保留功能,同时减少复杂性并提高可维护性。

Original comment in English

issue (complexity): Consider refactoring the 'convertToMap' method into smaller, focused methods.

The convertToMap method introduces significant complexity. While the functionality is important, we suggest the following improvements to enhance readability and maintainability:

  1. Break down the convertToMap method into smaller, focused private methods:
private void convertToMap(Object target, Object map) {
    if (handleCircularReference(target)) return;

    if (target instanceof JSArray) {
        handleArrayConversion((JSArray) target, map);
    } else {
        handleObjectConversion((JSObject) target, map);
    }
}

private boolean handleCircularReference(Object target) {
    long pointer = ((JSObject) target).getPointer();
    if (circulars.contains(pointer)) return true;
    circulars.add(pointer);
    return false;
}

private void handleArrayConversion(JSArray array, Object map) {
    // ... implementation ...
}

private void handleObjectConversion(JSObject object, Object map) {
    // ... implementation ...
}
  1. Encapsulate circular reference handling:
    Instead of using a protected circulars field, use a local variable in toMap:
public HashMap<String, Object> toMap() {
    HashMap<String, Object> objectMap = new HashMap<>();
    ArrayList<Long> circulars = new ArrayList<>();
    convertToMap(this, objectMap, circulars);
    return objectMap;
}

private void convertToMap(Object target, Object map, ArrayList<Long> circulars) {
    // Use circulars parameter instead of field
}
  1. Remove the toArray method as it's not implemented and throws an exception.

These changes will retain the functionality while reducing complexity and improving maintainability.

@HarlonWang HarlonWang closed this Sep 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant