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

367 tags come after link would be parse #382

Merged
merged 5 commits into from
Oct 18, 2024

Conversation

Kilerd
Copy link
Collaborator

@Kilerd Kilerd commented Oct 18, 2024

Summary by CodeRabbit

  • New Features

    • Introduced support for handling transactions in Chinese Yuan (CNY) with related entries for purchases.
    • Consolidated parsing of tags and links into a unified structure, improving flexibility in transaction definitions.
  • Bug Fixes

    • Enhanced workflow by allowing coverage, formatting, and linting checks to run independently.
  • Documentation

    • Added a new validation structure for API responses to ensure compliance with expected formats.
  • Refactor

    • Streamlined integration testing logic to accommodate multiple file types without redundancy.

@Kilerd Kilerd linked an issue Oct 18, 2024 that may be closed by this pull request
@Kilerd
Copy link
Collaborator Author

Kilerd commented Oct 18, 2024

@CodeRabbit review

Copy link
Contributor

coderabbitai bot commented Oct 18, 2024

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

Copy link
Contributor

coderabbitai bot commented Oct 18, 2024

Walkthrough

The pull request includes modifications to several files aimed at improving the handling of tags and links in financial transactions and enhancing the CI workflow. Changes in the .github/workflows/build-latest.yml file allow jobs to run independently of the test job. The beancount.pest and parser.rs files consolidate parsing logic for tags and links into a single construct. New financial transaction entries are added for the Chinese Yuan (CNY) currency, and a validations JSON file is introduced to ensure API response integrity.

Changes

File Change Summary
.github/workflows/build-latest.yml Jobs coverage, formatter, and clippy-check modified to remove needs: test dependency.
extensions/beancount/src/beancount.pest tags and links rules removed; replaced with tags_or_links for unified parsing.
extensions/beancount/src/parser.rs Functions tags and links removed; replaced with tags_or_links returning a tuple.
integration-tests/random-order-of-links-and-tags/main.bean Added transactions in CNY with tags and links for purchases from KFC.
integration-tests/random-order-of-links-and-tags/main.zhang Similar changes as in main.bean, with transactions in CNY and varied tags/links.
integration-tests/random-order-of-links-and-tags/validations.json New file added with validation rules for API responses related to tags and links.
zhang-cli/src/main.rs Refactored integration_test function to handle multiple main files without redundancy.
zhang-core/src/data_type/text/parser.rs Removed tags and links methods; added tags_or_links method consolidating their logic.
zhang-core/src/data_type/text/zhang.pest Updated grammar to use tags_or_links instead of separate tags and links.

Possibly related PRs

  • Ci/typo checker #362: This PR modifies the .github/workflows/build-latest.yml file, which is directly related to the main PR as it also involves changes to the same workflow file, enhancing the CI process.
  • Feat/link and tags #368: This PR introduces features related to linking and tagging, which aligns with the main PR's focus on modifying jobs in the workflow that may involve handling tags and links in the context of CI processes.

🐰 In the meadow, hops a little hare,
With tags and links, we dance with flair.
CNY flows for KFC delight,
Our workflow now runs with all its might.
Parsing's simpler, oh what a treat,
In the world of finance, we can't be beat! 🥕


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.

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: 3

🧹 Outside diff range and nitpick comments (8)
integration-tests/random-order-of-links-and-tags/validations.json (2)

5-28: LGTM: Consistent validations for first two records

The validations for records[0] and records[1] are correctly structured and consistent. Both records are expected to have a single link ("link1") and a single tag ("tag1").

Consider if this repetition is intentional. If these two records always have identical expectations, you might optimize the JSON structure by grouping them. For example:

[
  {
    "paths": ["$.data.records[0]", "$.data.records[1]"],
    "expectations": {
      "links": ["link1"],
      "tags": ["tag1"]
    }
  }
]

This could make the validation file more concise and easier to maintain.


29-84: LGTM: Consistent validations for records[2] to records[5]

The validations for records[2] through records[5] are correctly structured and consistent. Each record is expected to have two links ("link1", "link2") and two tags ("tag1", "tag2").

To reduce repetition and improve maintainability, consider optimizing this section. You could use a single validation rule for all four records. For example:

[
  {
    "paths": ["$.data.records[2]", "$.data.records[3]", "$.data.records[4]", "$.data.records[5]"],
    "expectations": {
      "links": ["link1", "link2"],
      "tags": ["tag1", "tag2"]
    }
  }
]

This approach would make the validation file more concise and easier to update if the expectations change in the future.

extensions/beancount/src/beancount.pest (1)

40-43: New tags_or_links rule enhances parsing flexibility

The introduction of the tags_or_links rule is a good solution for handling tags and links more flexibly. This change allows for any combination and order of tags and links within a transaction, which is a significant improvement in the grammar's expressiveness.

Consider a minor optimization to the rule:

-tags_or_links = { (space* ~ (tag | link))* }
+tags_or_links = { (space+ ~ (tag | link))+ }

This change ensures at least one tag or link is present and requires a space before each, which may better reflect typical Beancount syntax. However, if you need to allow for no tags or links, or tags/links without preceding spaces, the current implementation is correct.

zhang-cli/src/main.rs (3)

269-274: LGTM! Consider using a constant for main file names.

The new loop for detecting main files (main.zhang and main.bean) improves flexibility and maintainability. This approach allows easy addition of new file types in the future.

Consider defining the main file names as a constant array at the module level for better maintainability:

const MAIN_FILE_NAMES: [&str; 2] = ["main.zhang", "main.bean"];

// Then in the function:
for main_file in MAIN_FILE_NAMES {
    // ... rest of the code
}

275-292: LGTM! Consider improving error handling for ledger loading.

The creation of OpendalDataSource and Ledger for each detected main file is well-implemented. The use of async/await for ledger loading is appropriate.

Consider improving the error handling for ledger loading. Instead of using expect, which may terminate the test abruptly, you could use unwrap_or_else to provide more context:

let ledger = Ledger::async_load(test_temp_folder.to_path_buf(), main_file.to_string(), data_source.clone())
    .await
    .unwrap_or_else(|e| panic!("Failed to load ledger for {}: {}", main_file, e));

This approach will provide more informative error messages if ledger loading fails.


298-338: LGTM! Consider extracting validation logic to a separate function.

The validation logic is thorough and well-implemented. The use of JSON Path for validation is flexible and allows for complex checks. The error messages are detailed, which is excellent for debugging failed tests.

To improve readability and maintainability, consider extracting the validation logic into a separate function:

fn validate_response(res: Value, validations: &[ValidationPoint], test_case: &str, uri: &str) {
    for point in validations {
        // ... (existing validation logic)
    }
}

// In the main test function:
validate_response(res, &validation.validations, &original_test_source_folder.display().to_string(), &validation.uri);

This refactoring would make the main test function more concise and easier to read, while also making the validation logic reusable if needed.

zhang-core/src/data_type/text/zhang.pest (1)

28-28: Consider adding test cases for the new tags and links ordering

Now that the grammar permits tags and links in any order, it's advisable to add or update unit tests to cover various combinations. This ensures that the parser correctly handles all possible scenarios and maintains robustness.

Would you like assistance in generating test cases to validate these changes?

extensions/beancount/src/parser.rs (1)

Line range hint 384-392: Handle potential cases of both Posting and Meta being present

In the loop processing ret.5, the match statement does not handle cases where both Posting and Meta are Some. This could lead to missing data if a transaction line contains both a posting and metadata.

Consider updating the match statement to handle this case explicitly:

 for line in ret.5 {
     match line {
         (Some(trx), None) => {
             transaction.postings.push(trx);
         }
         (None, Some(meta)) => {
             transaction.meta.insert(meta.0, meta.1);
+        }
+        (Some(trx), Some(meta)) => {
+            transaction.postings.push(trx);
+            transaction.meta.insert(meta.0, meta.1);
         }
-        _ => {}
+        (None, None) => {
+            // Handle or log unexpected empty lines if necessary
+        }
     }
 }
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 2b1f0d9 and f2e4dbe.

📒 Files selected for processing (9)
  • .github/workflows/build-latest.yml (0 hunks)
  • extensions/beancount/src/beancount.pest (1 hunks)
  • extensions/beancount/src/parser.rs (2 hunks)
  • integration-tests/random-order-of-links-and-tags/main.bean (1 hunks)
  • integration-tests/random-order-of-links-and-tags/main.zhang (1 hunks)
  • integration-tests/random-order-of-links-and-tags/validations.json (1 hunks)
  • zhang-cli/src/main.rs (1 hunks)
  • zhang-core/src/data_type/text/parser.rs (2 hunks)
  • zhang-core/src/data_type/text/zhang.pest (1 hunks)
💤 Files with no reviewable changes (1)
  • .github/workflows/build-latest.yml
🧰 Additional context used
🔇 Additional comments (16)
integration-tests/random-order-of-links-and-tags/main.bean (4)

1-1: LGTM: Currency setup is correct.

The operating currency is properly set to CNY (Chinese Yuan) using the "option" directive.


3-5: LGTM: Account openings are correct and consistent.

The Assets:BankCard and Expenses:Food accounts are properly opened with CNY as the currency, consistent with the operating currency. The use of 1970-01-01 as the opening date is a common practice for account initialization in Beancount.


7-27: LGTM: Varied tag and link ordering aligns with PR objective.

The intentional variation in the order of tags (#tag1, #tag2) and links (^link1, ^link2) across transactions appears to be designed to test parsing capabilities. This aligns well with the PR title "367 tags come after link would be parse".

The consistent use of the same tags and links across transactions, but in different orders, provides a good test suite for ensuring that the parser can handle various combinations correctly.


7-29: LGTM: Transaction structure is consistent, but note the repetitive nature.

The transactions are structured consistently, with proper date, payee, narration, and account postings. All transactions deduct 50 CNY from Assets:BankCard and credit Expenses:Food.

Note: The repetitive nature of these transactions (same amount, payee, and description) suggests this file might be used for testing purposes, particularly for tag and link ordering.

To confirm the consistency of the transactions, run the following script:

The output of both commands should be 6, matching the number of transactions in the file.

✅ Verification successful

Verification Successful: All transactions are consistent.

All transactions have matching amounts and descriptions as expected.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify consistency of transaction amounts and descriptions

# Test: Check if all transactions have the same amount and description
rg --type-add 'bean:*.bean' --type bean '"KFC" "VME50 Package"' | wc -l
rg --type-add 'bean:*.bean' --type bean 'Assets:BankCard -50 CNY' | wc -l

Length of output: 153

integration-tests/random-order-of-links-and-tags/main.zhang (3)

1-5: LGTM: Currency and account setup is correct.

The operating currency is properly set to CNY, and the necessary accounts (Assets:BankCard and Expenses:Food) are opened with the correct currency. Using 1970-01-01 as the opening date is a common practice in financial systems.


7-29: LGTM: Transactions are correctly structured with intentional variations.

The six transactions from 2023-12-01 to 2023-12-06 are consistently structured:

  • Each is for "KFC" "VME50 Package" with a value of 50 CNY.
  • Correct debiting from Assets:BankCard and crediting to Expenses:Food.
  • Varying combinations and orders of tags (#tag1, #tag2) and links (^link1, ^link2).

The intentional variation in tag and link order appears to be for testing parsing capabilities, which aligns with the PR objective of addressing tag parsing after links.


1-29: File effectively serves as a test case for tag and link parsing.

This file successfully sets up a test environment for validating improvements in parsing tags and links in financial transactions. It includes:

  1. Proper currency and account setup.
  2. Multiple transactions with intentionally varied combinations and orders of tags and links.

These variations provide an excellent test case for ensuring that the parser can handle different arrangements of tags and links, directly addressing the PR's objective of improving tag parsing after links.

integration-tests/random-order-of-links-and-tags/validations.json (2)

1-4: LGTM: Well-structured JSON for API validations

The overall structure of this JSON file is well-organized and clearly defines the purpose of the validations. It correctly specifies the API endpoint ("/api/journals") and provides an array for individual validation rules.

Also applies to: 85-87


1-87: Overall: Well-structured but with room for optimization

This validation file successfully defines the expected structure and content for the "/api/journals" API response. It correctly validates the presence and values of links and tags across multiple records.

To enhance maintainability and reduce redundancy, consider the following optimizations:

  1. Group identical validations for records[0] and records[1].
  2. Use a single validation rule for records[2] through records[5].

These changes would make the file more concise and easier to update in the future, while maintaining the same validation coverage.

To ensure these validations align with the actual API implementation, you may want to run the following check:

This script will help confirm that the API implementation aligns with these validation expectations.

extensions/beancount/src/beancount.pest (2)

36-37: Improved flexibility in parsing tags and links

The modification to the transaction rule enhances the grammar's ability to handle tags and links more flexibly. By replacing tags? ~ links? with tags_or_links?, the parser can now accept tags and links in any order within a transaction, addressing the issue mentioned in the PR title.

This change improves the robustness of the parser while maintaining backward compatibility with existing Beancount files.


36-43: Summary: Improved tag and link parsing in transactions

The changes to the transaction rule and the introduction of the tags_or_links rule effectively address the issue of parsing tags that come after links. These modifications improve the flexibility of the Beancount grammar while maintaining backward compatibility with existing files.

To ensure the changes work as expected, consider running the following verification:

This script helps identify relevant test files. Ensure that your test suite includes cases that cover various combinations of tags and links, including scenarios where tags come after links.

zhang-cli/src/main.rs (1)

269-338: Overall, excellent improvements to the integration test.

The changes to the integration_test function significantly enhance its flexibility and robustness. The new implementation supports multiple main file types, provides detailed error messages, and uses a more maintainable approach for file detection and validation.

Key improvements:

  1. Support for multiple main file types (main.zhang and main.bean).
  2. Flexible and reusable validation logic using JSON Path.
  3. Detailed error messages for failed tests.

The suggested minor improvements (using constants, improving error handling, and extracting validation logic) would further enhance the code's maintainability and readability. Great work on improving the testing process!

zhang-core/src/data_type/text/zhang.pest (2)

28-28: Consolidation of tags and links into tags_or_links is appropriate

The modification to use tags_or_links? in the transaction rule simplifies the grammar and allows tags and links to appear in any order within a transaction. This change effectively addresses the issue where tags following links were not being parsed correctly.


37-40: The new tags_or_links rule effectively combines tags and links

Introducing the tags_or_links rule with the definition { (space* ~ (tag | link))* } appropriately merges the previous tags and links rules. This allows for a flexible ordering of tags and links, enhancing the grammar's expressiveness.

extensions/beancount/src/parser.rs (1)

367-371: Verify all transaction parsing patterns are updated

With the consolidation of tags and links into tags_or_links, ensure that all transaction patterns in the match_nodes! macro are correctly updated to reflect the new structure. This helps prevent parsing errors for different transaction syntaxes.

To verify, consider reviewing all transaction test cases to confirm they are parsed correctly.

zhang-core/src/data_type/text/parser.rs (1)

377-381: Transaction patterns correctly updated to use tags_or_links

The transaction parsing patterns have been appropriately updated to incorporate the tags_or_links method. The changes ensure that both tags and links are parsed together, simplifying the logic.

extensions/beancount/src/parser.rs Outdated Show resolved Hide resolved
zhang-core/src/data_type/text/parser.rs Show resolved Hide resolved
zhang-core/src/data_type/text/parser.rs Outdated Show resolved Hide resolved
coderabbitai[bot]
coderabbitai bot previously approved these changes Oct 18, 2024
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Signed-off-by: Kilerd Chan <[email protected]>
@Kilerd Kilerd force-pushed the 367-tags-come-after-link-would-be-parse branch from e8494bc to 59d8348 Compare October 18, 2024 07:11
@Kilerd Kilerd merged commit c424339 into main Oct 18, 2024
21 checks passed
@Kilerd Kilerd deleted the 367-tags-come-after-link-would-be-parse branch October 18, 2024 07:13
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.

tags come after link would be parse
1 participant