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: multi-line formatting for permission expressions #1629

Merged

Conversation

theoriginalstove
Copy link
Contributor

@theoriginalstove theoriginalstove commented Sep 24, 2024

#1628

Todo:

  • Add more tests with complex expressions

Summary by CodeRabbit

  • New Features

    • Enhanced parser functionality with the addition of a previousToken field for improved token processing.
    • Introduced a method to check if the previous token matches specified types, enhancing parsing logic.
  • Tests

    • Added multiple test cases to validate parsing of multi-line permission expressions, including success and error scenarios, ensuring robustness and accuracy.

Copy link

coderabbitai bot commented Sep 24, 2024

Warning

Rate limit exceeded

@tolgaOzen has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 9 minutes and 22 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

📥 Commits

Files that changed from the base of the PR and between 0aa070c and 14d20fc.

Walkthrough

The changes introduce a new field previousToken in the Parser struct to keep track of the token preceding the current one. The next method is updated to set this field, and a new method previousTokenIs is added for checking token types. Additionally, modifications to the parseExpression method allow the parser to handle specific conditions involving NEWLINE and various token types. The test file is enhanced with new test cases for multi-line permission expressions, including both valid and invalid scenarios.

Changes

File Change Summary
pkg/dsl/parser/parser.go Added previousToken field and previousTokenIs method; modified next and parseExpression methods.
pkg/dsl/parser/parser_test.go Introduced new test cases for multi-line permission expressions, including valid and invalid scenarios.

Possibly related PRs

  • test: add test cases for linked schema #1609: The test cases added in this PR involve a check_balance rule, which is relevant to the permission expressions being tested in the main PR, indicating a connection in the context of permission handling within the parser.

🐇 In the parser's dance, a new token's found,
Previous whispers now abound.
Multi-lines weave a tale so bright,
Valid paths and errors in sight.
With each test, our rules take flight! 🌟


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.

CodeRabbit Configuration 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.

@tolgaOzen tolgaOzen marked this pull request as ready for review September 25, 2024 07:52
Copy link

@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: 4

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 49c27f2 and 8912eaa.

🔇 Files ignored due to path filters (1)
  • playground/public/play.wasm is excluded by !**/*.wasm
📒 Files selected for processing (2)
  • pkg/dsl/parser/parser.go (4 hunks)
  • pkg/dsl/parser/parser_test.go (1 hunks)
🔇 Additional comments not posted (7)
pkg/dsl/parser/parser.go (5)

36-37: LGTM: Addition of previousToken field to Parser struct

The addition of the previousToken field to the Parser struct is a good improvement. It allows the parser to keep track of the previous token, which can be useful for more complex parsing scenarios, such as handling multi-line expressions.


87-88: LGTM: Update of previousToken in next method

The modification to the next method correctly updates the previousToken field before changing the currentToken. This change ensures that the previousToken always contains the token that precedes the current one, which is consistent with the addition of the previousToken field in the Parser struct.


125-135: LGTM: Addition of previousTokenIs method

The new previousTokenIs method is a well-implemented addition to the Parser struct. It correctly checks if the previousToken matches any of the specified token types, enhancing the parser's ability to make decisions based on the previous token. The implementation is consistent with the existing currentTokenIs and peekTokenIs methods, and the use of variadic parameters allows for flexible checking of multiple token types.


Line range hint 1-924: Summary: Successful implementation of multi-line formatting for permission expressions

The changes made to pkg/dsl/parser/parser.go successfully implement support for multi-line formatting in permission expressions. Key modifications include:

  1. Addition of a previousToken field to the Parser struct.
  2. Update of the next method to maintain the previousToken.
  3. Introduction of a previousTokenIs method for token type checking.
  4. Enhancement of the parseExpression method to handle newlines in specific contexts.

These changes collectively improve the parser's flexibility and capability to handle more complex permission expression syntax. The implementation is consistent and well-integrated with the existing codebase.


607-611: LGTM: Handling of multi-line formatting in parseExpression

The addition of the new condition at the beginning of the parseExpression method is a good improvement. It allows for multi-line formatting of permission expressions by advancing to the next token when a newline is encountered after specific tokens (LP, AND, OR, NOT, ASSIGN). This change enhances the parser's flexibility in handling permission expression syntax.

To ensure completeness, please verify if all relevant tokens are included in the condition. Consider if there are any other tokens that might be valid at the end of a line in a permission expression.

✅ Verification successful

Verified: Comprehensive Token Coverage in parseExpression

The verification confirms that all relevant tokens (LP, AND, OR, NOT, ASSIGN) are included in the condition checking for newline tokens. No additional tokens require inclusion. This change effectively enhances the parser's flexibility in handling multi-line permission expressions.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check for other potential tokens that might be relevant for multi-line expressions

# Test: Search for token definitions and usage in expression parsing
rg --type go -e "token\." -e "parseExpression" pkg/dsl

Length of output: 48776

pkg/dsl/parser/parser_test.go (2)

852-899: Test case effectively validates multi-line permission expression with rule

The test case for "Case 24" correctly verifies that the parser can handle a multi-line permission expression involving a rule. The assertions comprehensively check the parsed schema for accuracy.


900-946: Test case successfully tests alternative multi-line formatting

"Case 25" effectively tests the parser's ability to handle different formatting of a multi-line permission expression. The test ensures that variations in whitespace and line breaks do not affect the parsing outcome.

pkg/dsl/parser/parser_test.go Outdated Show resolved Hide resolved
pkg/dsl/parser/parser_test.go Outdated Show resolved Hide resolved
Copy link

@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: 0

🧹 Outside diff range and nitpick comments (2)
pkg/dsl/parser/parser_test.go (2)

947-970: LGTM! Consider renumbering this test case.

This negative test case is crucial for validating the parser's ability to detect and report syntax errors in multi-line permission expressions. It effectively ensures that the parser fails when encountering incorrectly formatted expressions.

However, this test case is labeled as "Case 26", which duplicates a test case number from a previous commit. To maintain clarity and avoid confusion, consider renaming this to "Case 27" or the next appropriate number in the sequence.


972-1009: LGTM! Consider enhancing assertions for the complex permission expression.

This test case is an excellent addition, validating the parser's capability to handle complex, multi-line permission expressions with multiple rules and various logical operators. It significantly improves the test coverage for real-world scenarios.

To further strengthen this test, consider adding assertions that verify the correctness of the parsed permission structure. This would ensure not only that the parser doesn't throw an error, but also that it correctly interprets the complex logical structure of the permission.

Here's a suggestion for additional assertions:

st := schema.Statements[0].(*ast.EntityStatement)
Expect(st.Name.Literal).Should(Equal("report"))

p1 := st.PermissionStatements[0].(*ast.PermissionStatement)
Expect(p1.Name.Literal).Should(Equal("view"))

es1 := p1.ExpressionStatement.(*ast.ExpressionStatement)
// Add assertions to check the structure of the complex permission expression
// For example:
Expect(es1.Expression).Should(BeAssignableToTypeOf(&ast.InfixExpression{}))
infix := es1.Expression.(*ast.InfixExpression)
Expect(infix.Operator).Should(Equal("or"))
// Continue with more detailed assertions based on the expected structure
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 8912eaa and 0aa070c.

📒 Files selected for processing (1)
  • pkg/dsl/parser/parser_test.go (1 hunks)
🔇 Additional comments not posted (3)
pkg/dsl/parser/parser_test.go (3)

853-898: LGTM! Excellent test case for multi-line permission expressions.

This test case effectively validates the parser's ability to handle multi-line permission expressions. It's a crucial addition that ensures the parser can correctly interpret permissions split across multiple lines, which is important for maintaining code readability in complex permission scenarios.


900-945: LGTM! Good coverage of alternative formatting styles.

This test case complements Case 24 by validating the parser's ability to handle a different style of multi-line permission expressions. Testing various formatting styles ensures the parser is robust and flexible, accommodating different coding preferences.


852-1009: Excellent enhancement of the test suite for multi-line permission expressions!

These additions significantly improve the robustness of the parser's test suite, particularly in handling complex, multi-line permission expressions. The new test cases cover a wide range of scenarios, including:

  1. Basic multi-line expressions
  2. Different formatting styles
  3. Failure cases with incorrect syntax
  4. Highly complex expressions with multiple rules and logical operators

These tests will ensure that the parser can handle real-world, complex permission scenarios while maintaining flexibility in expression formatting. The thoroughness of these tests will greatly contribute to the reliability and maintainability of the parser.

Great job on improving the test coverage! The suggested minor enhancements (renumbering Case 26 and adding more detailed assertions for the complex case) will further refine this already solid set of tests.

@tolgaOzen tolgaOzen merged commit 1d0ff29 into Permify:master Sep 25, 2024
6 of 8 checks passed
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.

2 participants