Skip to content

[ISSUE #1477]🚀derive RequestHeaderCodec support Struct🔥#1487

Merged
rocketmq-rust-bot merged 1 commit intomainfrom
feature-1477
Dec 1, 2024
Merged

[ISSUE #1477]🚀derive RequestHeaderCodec support Struct🔥#1487
rocketmq-rust-bot merged 1 commit intomainfrom
feature-1477

Conversation

@mxsm
Copy link
Copy Markdown
Owner

@mxsm mxsm commented Dec 1, 2024

Which Issue(s) This PR Fixes(Closes)

Fixes #1477

Brief Description

How Did You Test This Change?

Summary by CodeRabbit

  • New Features

    • Introduced new functions for string manipulation and type checking, including snake_to_camel_case, is_struct_type, is_basic_or_string_type, and has_serde_flatten_attribute.
    • Enhanced mapping logic to support complex data structures with optional fields and serde(flatten) attribute.
  • Bug Fixes

    • Improved error handling for required fields in mapping logic.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Dec 1, 2024

Walkthrough

The pull request introduces several new functions and modifies existing ones in the rocketmq-macros library. Key additions include snake_to_camel_case, is_struct_type, is_basic_or_string_type, and has_serde_flatten_attribute, enhancing type checking and string manipulation capabilities. The request_header_custom.rs file also sees the introduction of utility functions that improve the handling of struct fields, particularly regarding the serde(flatten) attribute. These changes collectively enhance the macro's ability to process complex data structures and improve mapping logic.

Changes

File Change Summary
rocketmq-macros/src/lib.rs Added functions: snake_to_camel_case, is_struct_type, is_basic_or_string_type, has_serde_flatten_attribute. Removed old snake_to_camel_case implementation.
rocketmq-macros/src/request_header_custom.rs Added functions: has_serde_flatten_attribute, is_struct_type. Enhanced request_header_codec_inner for struct field processing.

Assessment against linked issues

Objective Addressed Explanation
Support for deriving RequestHeaderCodec for structs (#[1477])

Possibly related issues

Possibly related PRs

Suggested labels

feature, enhancement, auto merge, ready to review, waiting-review, AI review first

Suggested reviewers

  • TeslaRustor
  • SpaceXCN

Poem

In the land of code where rabbits play,
New functions hop in, brightening the day.
From snake to camel, they leap with glee,
Structs and fields dance in harmony.
With serde's magic, they flatten and blend,
A joyful journey, where changes transcend! 🐇✨


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@rocketmq-rust-robot rocketmq-rust-robot added this to the v0.4.0 milestone Dec 1, 2024
@rocketmq-rust-robot rocketmq-rust-robot added the feature🚀 Suggest an idea for this project. label Dec 1, 2024
@rocketmq-rust-bot
Copy link
Copy Markdown
Collaborator

🔊@mxsm 🚀Thanks for your contribution 🎉. CodeRabbit(AI) will review your code first 🔥

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Outside diff range and nitpick comments (2)
rocketmq-macros/src/request_header_custom.rs (1)

100-107: Refactor duplicated code when handling flattened structs

The logic for handling fields that are structs with the serde(flatten) attribute is duplicated in multiple places within the to_map method. Consider extracting this repeated code into a helper function or macro to improve maintainability and reduce code redundancy.

Also applies to: 132-138

rocketmq-macros/src/lib.rs (1)

81-114: Add documentation to is_struct_type function for clarity

Consider adding a doc comment to the is_struct_type function to explain its purpose and logic. This will improve code readability and assist future contributors in understanding the functionality.

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 515a349 and 30e5370.

📒 Files selected for processing (2)
  • rocketmq-macros/src/lib.rs (3 hunks)
  • rocketmq-macros/src/request_header_custom.rs (7 hunks)

Comment on lines +139 to +156
fn has_serde_flatten_attribute(field: &Field) -> bool {
for attr in &field.attrs {
if let Some(ident) = attr.path().get_ident() {
let mut has_serde_flatten_attribute = false;
if ident == "serde" {
let _ = attr.parse_nested_meta(|meta| {
has_serde_flatten_attribute = meta
.path
.segments
.iter()
.any(|segment| segment.ident == "flatten");
Ok(())
});
}
return has_serde_flatten_attribute;
}
}
false
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Fix premature return in has_serde_flatten_attribute function

The has_serde_flatten_attribute function currently returns after checking the first attribute. This may cause the function to miss the #[serde(flatten)] attribute if it's not the first in the list. To ensure all attributes are checked, modify the function to continue iterating through all attributes.

Apply this diff to fix the issue:

 fn has_serde_flatten_attribute(field: &Field) -> bool {
     for attr in &field.attrs {
         if let Some(ident) = attr.path().get_ident() {
             let mut has_serde_flatten_attribute = false;
             if ident == "serde" {
                 let _ = attr.parse_nested_meta(|meta| {
                     has_serde_flatten_attribute = meta
                         .path
                         .segments
                         .iter()
                         .any(|segment| segment.ident == "flatten");
                     Ok(())
                 });
-                return has_serde_flatten_attribute;
+                if has_serde_flatten_attribute {
+                    return true;
+                }
             }
         }
     }
     false
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
fn has_serde_flatten_attribute(field: &Field) -> bool {
for attr in &field.attrs {
if let Some(ident) = attr.path().get_ident() {
let mut has_serde_flatten_attribute = false;
if ident == "serde" {
let _ = attr.parse_nested_meta(|meta| {
has_serde_flatten_attribute = meta
.path
.segments
.iter()
.any(|segment| segment.ident == "flatten");
Ok(())
});
}
return has_serde_flatten_attribute;
}
}
false
fn has_serde_flatten_attribute(field: &Field) -> bool {
for attr in &field.attrs {
if let Some(ident) = attr.path().get_ident() {
let mut has_serde_flatten_attribute = false;
if ident == "serde" {
let _ = attr.parse_nested_meta(|meta| {
has_serde_flatten_attribute = meta
.path
.segments
.iter()
.any(|segment| segment.ident == "flatten");
Ok(())
});
if has_serde_flatten_attribute {
return true;
}
}
}
}
false
}

@codecov
Copy link
Copy Markdown

codecov bot commented Dec 1, 2024

Codecov Report

Attention: Patch coverage is 49.05660% with 54 lines in your changes missing coverage. Please review.

Project coverage is 22.55%. Comparing base (515a349) to head (30e5370).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
rocketmq-macros/src/lib.rs 56.16% 32 Missing ⚠️
rocketmq-macros/src/request_header_custom.rs 33.33% 22 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1487      +/-   ##
==========================================
+ Coverage   22.53%   22.55%   +0.02%     
==========================================
  Files         450      450              
  Lines       58412    58500      +88     
==========================================
+ Hits        13162    13196      +34     
- Misses      45250    45304      +54     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

AI review first Ai review pr first approved PR has approved auto merge feature🚀 Suggest an idea for this project.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature🚀] derive RequestHeaderCodec support Struct

4 participants