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

refactor: add MergeRight derive macro #1723

Merged

Conversation

Shylock-Hg
Copy link
Contributor

@Shylock-Hg Shylock-Hg commented Apr 13, 2024

Summary:
Briefly describe the changes made in this PR.

Issue Reference(s):
Fixes #... (Replace "..." with the issue number)
Close #1700
/claim #1700

Build & Testing:

  • I ran cargo test successfully.
  • I have run ./lint.sh --mode=fix to fix all linting issues raised by ./lint.sh --mode=check.

Checklist:

  • I have added relevant unit & integration tests.
  • I have updated the documentation accordingly.
  • I have performed a self-review of my code.
  • PR follows the naming convention of <type>(<optional scope>): <title>

Summary by CodeRabbit

  • New Features

    • Enhanced configuration merging capabilities across various components using the MergeRight trait from tailcall-macros.
  • Refactor

    • Simplified configuration structures by integrating MergeRight trait, improving maintainability and readability.
  • Documentation

    • Updated documentation to reflect changes in configuration merging logic.
  • Chores

    • Added tailcall-macros dependency to support new merging functionalities.
  • Bug Fixes

    • Removed redundant code and fixed inconsistencies in configuration merging implementations.

@github-actions github-actions bot added the type: feature Brand new functionality, features, pages, workflows, endpoints, etc. label Apr 13, 2024
Copy link

codecov bot commented Apr 13, 2024

Codecov Report

Attention: Patch coverage is 83.33333% with 27 lines in your changes are missing coverage. Please review.

Project coverage is 87.03%. Comparing base (69f22a6) to head (a107d39).

Files Patch % Lines
tailcall-macros/src/merge_right.rs 80.45% 17 Missing ⚠️
src/config/config.rs 73.07% 7 Missing ⚠️
src/config/upstream.rs 72.72% 3 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1723      +/-   ##
==========================================
- Coverage   87.14%   87.03%   -0.11%     
==========================================
  Files         153      155       +2     
  Lines       15599    15540      -59     
==========================================
- Hits        13593    13525      -68     
- Misses       2006     2015       +9     

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

@Shylock-Hg
Copy link
Contributor Author

@tusharmath Some special container like self.vars = merge_key_value_vecs(&self.vars, &other.vars);, don't follow general merge right logical of Vec. Should we keep it?

@tusharmath
Copy link
Contributor

We could keep a few if they are very custom.

@Shylock-Hg
Copy link
Contributor Author

We could keep a few if they are very custom.

Ok, I added a helper attribute to custom it.

@Shylock-Hg Shylock-Hg marked this pull request as ready for review April 14, 2024 10:26
Copy link
Contributor

coderabbitai bot commented Apr 14, 2024

Walkthrough

Walkthrough

This update introduces the tailcall-macros package, implementing a procedural macro to derive the MergeRight trait, which simplifies and standardizes configuration merging across various structs and enums in the codebase. It effectively replaces verbose manual implementations with a more concise, macro-driven approach.

Changes

File Pattern Change Summary
Cargo.toml, tailcall-macros/Cargo.toml Added tailcall-macros dependency and set up new tailcall-macros package.
src/config/..., src/merge_right.rs Integrated MergeRight trait using tailcall-macros across multiple config-related structs.
src/config/server.rs Enhanced Server struct with custom merge logic using merge_right_vars function.
tailcall-macros/src/lib.rs Created a procedural macro for automatically deriving the MergeRight trait for structs and enums.

Assessment against linked issues

Objective Addressed Explanation
Use macros to derive MergeRight (#1700)
Move trait MergeRight into tailcall-macros (#1700)
Drop existing hand-written merge_right implementations (#1700) Hand-written implementations replaced by macro derivations.

Possibly related issues

  • Issue Improve merging logic in struct #1701: The changes in this PR seem to standardize and potentially improve the merging logic across various structs, which could relate to the need for more idiomatic Rust approaches in merging logic as discussed in the issue. Consider linking this PR to issue Improve merging logic in struct #1701 for a review of the improvements in merging logic.

Recent Review Details

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 9587972 and a107d39.
Files selected for processing (9)
  • src/config/config.rs (8 hunks)
  • src/config/config_module.rs (3 hunks)
  • src/config/cors.rs (1 hunks)
  • src/config/headers.rs (2 hunks)
  • src/config/server.rs (4 hunks)
  • src/config/telemetry.rs (5 hunks)
  • src/config/upstream.rs (3 hunks)
  • src/lib.rs (1 hunks)
  • src/rest/endpoint_set.rs (2 hunks)
Files skipped from review as they are similar to previous changes (6)
  • src/config/config.rs
  • src/config/config_module.rs
  • src/config/cors.rs
  • src/config/telemetry.rs
  • src/config/upstream.rs
  • src/rest/endpoint_set.rs
Additional Context Used
Path-based Instructions (3)
src/config/headers.rs (1)

Pattern **/*.rs: Programming Style Guidelines

  • When calling functions that do not need to modify values, pass references of those values.
  • When calling functions that need to modify values, pass ownership of the values, and ensure they are returned from the function.

IMPORTANT: This programming style may not be suitable for performance-sensitive components or hot code paths. In such cases, prioritize efficiency and optimization strategies to enhance performance.

Testing

  1. Write Tests: For every new feature or bugfix, ensure that you write appropriate tests.
    Structure your tests in the following way:

    use pretty_assertions::assert_eq;
    fn test_something_important() {
       let value = setup_something_using_a_function();
    
       let actual = perform_some_operation_on_the_value(value);
       let expected = ExpectedValue {foo: 1, bar: 2};
    
       assert_eq!(actual, expected);
    }
    • Setup the value using helper methods in tests.
    • Create an actual and an expected value.
    • Assert the two values in a new line.
    • Ensure there are only one assertions per test.
src/lib.rs (1)

Pattern **/*.rs: Programming Style Guidelines

  • When calling functions that do not need to modify values, pass references of those values.
  • When calling functions that need to modify values, pass ownership of the values, and ensure they are returned from the function.

IMPORTANT: This programming style may not be suitable for performance-sensitive components or hot code paths. In such cases, prioritize efficiency and optimization strategies to enhance performance.

Testing

  1. Write Tests: For every new feature or bugfix, ensure that you write appropriate tests.
    Structure your tests in the following way:

    use pretty_assertions::assert_eq;
    fn test_something_important() {
       let value = setup_something_using_a_function();
    
       let actual = perform_some_operation_on_the_value(value);
       let expected = ExpectedValue {foo: 1, bar: 2};
    
       assert_eq!(actual, expected);
    }
    • Setup the value using helper methods in tests.
    • Create an actual and an expected value.
    • Assert the two values in a new line.
    • Ensure there are only one assertions per test.
src/config/server.rs (1)

Pattern **/*.rs: Programming Style Guidelines

  • When calling functions that do not need to modify values, pass references of those values.
  • When calling functions that need to modify values, pass ownership of the values, and ensure they are returned from the function.

IMPORTANT: This programming style may not be suitable for performance-sensitive components or hot code paths. In such cases, prioritize efficiency and optimization strategies to enhance performance.

Testing

  1. Write Tests: For every new feature or bugfix, ensure that you write appropriate tests.
    Structure your tests in the following way:

    use pretty_assertions::assert_eq;
    fn test_something_important() {
       let value = setup_something_using_a_function();
    
       let actual = perform_some_operation_on_the_value(value);
       let expected = ExpectedValue {foo: 1, bar: 2};
    
       assert_eq!(actual, expected);
    }
    • Setup the value using helper methods in tests.
    • Create an actual and an expected value.
    • Assert the two values in a new line.
    • Ensure there are only one assertions per test.
Learnings (1)
Common
User: Shylock-Hg"
URL: https://github.com/tailcallhq/tailcall/pull/1723

Timestamp: 2024-04-14T10:42:00.789Z
Learning: JSON schema tests for the `schemars::JsonSchema` trait in `Config` are considered outside the scope of PR #1723.
Additional comments not posted (6)
src/config/headers.rs (2)

11-13: The integration of the MergeRight trait into the Headers struct is correctly implemented.


11-13: Removal of the merge_headers function is appropriate as its functionality is now handled by the MergeRight macro.

src/lib.rs (1)

48-48: Re-exporting tailcall_macros as macros is correctly implemented and enhances modularity.

src/config/server.rs (3)

12-14: The integration of the MergeRight trait into the Server, ScriptOptions, and HttpVersion structs is correctly implemented.

Also applies to: 118-118, 124-126


104-116: The merge_right_vars function is well-implemented, correctly handling the merging of key-value pairs.


12-14: Removal of old MergeRight implementations is appropriate as the macro now provides this functionality.

Also applies to: 118-118, 124-126


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>.
    • 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 generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @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.

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 as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

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.

@Shylock-Hg
Copy link
Contributor Author

@tusharmath Please take a look.

Copy link
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: 7

src/config/config.rs Show resolved Hide resolved
src/config/config.rs Show resolved Hide resolved
src/config/config.rs Show resolved Hide resolved
src/merge_right.rs Show resolved Hide resolved
src/config/headers.rs Show resolved Hide resolved
tailcall-macros/src/lib.rs Outdated Show resolved Hide resolved
tailcall-macros/src/lib.rs Outdated Show resolved Hide resolved
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Copy link
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: 2

tailcall-macros/src/lib.rs Outdated Show resolved Hide resolved
tailcall-macros/src/lib.rs Outdated Show resolved Hide resolved
@Shylock-Hg
Copy link
Contributor Author

@tusharmath https://github.com/tailcallhq/tailcall/actions/runs/8682076437/job/23805939822?pr=1723 One UT failed, but seems not related to this PR.

@tusharmath tusharmath changed the title feat: Add MergeRight derive macro. refactor: add MergeRight derive macro. Apr 16, 2024
@tusharmath tusharmath changed the title refactor: add MergeRight derive macro. refactor: add MergeRight derive macro Apr 16, 2024
@tusharmath
Copy link
Contributor

tusharmath commented Apr 16, 2024

@Shylock-Hg Thanks! Just tiny feedback here —

  • Move the macro logic into tailcall-macros/src/merge_right.rs so that we can make space of other macros in this workspace, and keep lib.rs light and simple exports as much as possible.
  • Resolve conflicts

Overall it looks like is ready to merge.

src/config/config.rs Outdated Show resolved Hide resolved
@github-actions github-actions bot added the type: chore Routine tasks like conversions, reorganization, and maintenance work. label Apr 16, 2024
@Shylock-Hg
Copy link
Contributor Author

@Shylock-Hg Thanks! Just tiny feedback here —

  • Move the macro logic into tailcall-macros/src/merge_right.rs so that we can make space of other macros in this workspace, and keep lib.rs light and simple exports as much as possible.
  • Resolve conflicts

Overall it looks like is ready to merge.

Ok, it's done.

@Shylock-Hg Shylock-Hg requested a review from tusharmath April 16, 2024 07:17
@tusharmath tusharmath enabled auto-merge (squash) April 16, 2024 07:21
@tusharmath
Copy link
Contributor

/tip 50$

Copy link

algora-pbc bot commented Apr 16, 2024

@Shylock-Hg: You just got a $50 tip! We'll notify you once it is processed.

Copy link
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: 2

src/config/headers.rs Show resolved Hide resolved
src/config/server.rs Show resolved Hide resolved
@Shylock-Hg
Copy link
Contributor Author

@tusharmath There may be some errors in this runner, https://github.com/tailcallhq/tailcall/actions/runs/8701504469/job/23863635963?pr=1723, it always reports errors unrelated.

@tusharmath tusharmath merged commit 4553eec into tailcallhq:main Apr 16, 2024
25 of 26 checks passed
@Shylock-Hg Shylock-Hg deleted the feature/merge-right-derive-macro branch April 16, 2024 08:21
Copy link

algora-pbc bot commented Apr 24, 2024

🎉🎈 @Shylock-Hg has been awarded $50! 🎈🎊

ssddOnTop pushed a commit that referenced this pull request May 2, 2024
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Co-authored-by: Tushar Mathur <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🙋 Bounty claim 💰 Rewarded type: chore Routine tasks like conversions, reorganization, and maintenance work. type: feature Brand new functionality, features, pages, workflows, endpoints, etc.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

refactor: use macros to derive merge_right
2 participants