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: refactor mapping validators #122

Merged
merged 3 commits into from
Oct 17, 2024
Merged

feat: refactor mapping validators #122

merged 3 commits into from
Oct 17, 2024

Conversation

koladilip
Copy link
Collaborator

@koladilip koladilip commented Oct 16, 2024

What are the changes introduced in this PR?

Add more validators and also auto convert output mappings to JSON paths.

What is the related Linear task?

Resolves INT-2780

Please explain the objectives of your changes below

Put down any required details on the broader aspect of your changes. If there are any dependent changes, mandatorily mention them here

Any changes to existing capabilities/behaviour, mention the reason & what are the changes ?

N/A

Any new dependencies introduced with this change?

N/A

Any new generic utility introduced or modified. Please explain the changes.

N/A

Any technical or performance related pointers to consider with the change?

N/A

@coderabbitai review


Developer checklist

  • My code follows the style guidelines of this project

  • No breaking changes are being introduced.

  • All related docs linked with the PR?

  • All changes manually tested?

  • Any documentation changes needed with this change?

  • Is the PR limited to 10 file changes?

  • Is the PR limited to one linear task?

  • Are relevant unit and component test-cases added?

Reviewer checklist

  • Is the type of change in the PR title appropriate as per the changes?

  • Verified that there are no credentials or confidential data exposed with the changes.

Summary by CodeRabbit

Release Notes

  • New Features

    • Introduced a new method for validating JSON mappings in the JsonTemplateEngine.
    • Added a new test suite to validate the convertToObjectMapping function.
    • New mapping scenario added to enhance testing with various output formats.
    • Added a new JSON file containing predefined mappings for testing scenarios.
  • Bug Fixes

    • Improved error handling for invalid mappings in the convertToObjectMapping function.
  • Documentation

    • Updated test scenarios to reflect new and removed entries, streamlining the testing process.

@koladilip koladilip requested a review from a team as a code owner October 16, 2024 08:37
Copy link

coderabbitai bot commented Oct 16, 2024

Walkthrough

The pull request introduces several enhancements to the JsonTemplateEngine class, including new methods for validating and converting JSON paths. A new test suite for the isValidMapping method is added, along with a test for error handling in the convertToObjectMapping function. Additionally, the test scenarios are updated with a new mapping and the removal of an outdated scenario. These changes collectively improve the handling and validation of mappings within the engine.

Changes

File Change Summary
src/engine.ts Introduced methods: toJsonPath, convertToJSONPath, isValidMapping; updated prepareMappings, validateMappings, and createFlatMappingsAST to include options.
src/engine.test.ts Added a new test suite for isValidMapping with two test cases; imported PathType.
src/parser.ts Updated properties lexer, options, and pathTypesStack to private readonly.
src/reverse_translator.ts Changed options property to private readonly.
src/utils/converter.test.ts Added a test suite for convertToObjectMapping to validate error handling with invalid mappings.
test/scenarios/mappings/data.ts Added a new mapping scenario and removed an invalid mapping scenario from the data array.
test/scenarios/mappings/non_path_output.json Introduced a new JSON file with predefined mappings for testing scenarios.

Possibly related PRs

🐰 In the meadow, where paths intertwine,
New mappings emerge, oh how they shine!
With tests in place, and errors in sight,
The engine now dances, all paths feel right.
So hop with joy, let the JSON flow,
For valid mappings are the way to go! 🌼


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

github-actions bot commented Oct 16, 2024

Coverage report

St.
Category Percentage Covered / Total
🟢 Statements
93.34% (-6.66% 🔻)
4796/5138
🟢 Branches
92.82% (-7.18% 🔻)
1203/1296
🟢 Functions 100% 383/383
🟢 Lines
93.34% (-6.66% 🔻)
4796/5138
Show files with reduced coverage 🔻
St.
File Statements Branches Functions Lines
🟢 engine.ts
95.33% (-4.67% 🔻)
88.46% (-11.54% 🔻)
100%
95.33% (-4.67% 🔻)
🟢 lexer.ts
87.18% (-12.82% 🔻)
91.93% (-8.07% 🔻)
100%
87.18% (-12.82% 🔻)
🟢 parser.ts
88.78% (-11.22% 🔻)
89.88% (-10.12% 🔻)
100%
88.78% (-11.22% 🔻)
🟢 utils/common.ts 100%
88.89% (-11.11% 🔻)
100% 100%
🟢 utils/converter.ts
89.14% (-10.86% 🔻)
84.07% (-15.93% 🔻)
100%
89.14% (-10.86% 🔻)

Test suite run success

217 tests passing in 7 suites.

Report generated by 🧪jest coverage report action from e8bd423

Copy link

codecov bot commented Oct 16, 2024

Codecov Report

Attention: Patch coverage is 90.00000% with 6 lines in your changes missing coverage. Please review.

Project coverage is 91.63%. Comparing base (97d95e8) to head (e8bd423).
Report is 56 commits behind head on main.

Files with missing lines Patch % Lines
src/engine.ts 89.28% 5 Missing and 1 partial ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##              main     #122      +/-   ##
===========================================
- Coverage   100.00%   91.63%   -8.37%     
===========================================
  Files           14       18       +4     
  Lines         4565     5138     +573     
  Branches      1082     1130      +48     
===========================================
+ Hits          4565     4708     +143     
- Misses           0      420     +420     
- Partials         0       10      +10     

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

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

🧹 Outside diff range and nitpick comments (5)
test/scenarios/mappings/non_path_output.json (2)

18-21: LGTM: Complex mapping with nested structure.

This mapping provides an excellent test case for handling complex output structures, combining property access with quoted strings. It's valuable for ensuring the validator can process nested elements correctly.

Consider adding one more test case with an array access in the output, e.g., "output": "headers[0].'Content-Type'", to cover an even wider range of possible output structures.


1-22: Excellent set of test scenarios for non-path output mappings.

This file provides a comprehensive set of test scenarios for non-path output mappings, covering various input types (strings, numbers) and output formats (simple strings, header-like strings, quoted paths, and nested structures). These scenarios align well with the PR objectives of refactoring mapping validators and implementing automatic conversion of output mappings to JSON paths.

The variety of cases will help ensure robust validation and conversion processes. Great job on creating these test scenarios!

To make the test suite even more comprehensive, consider adding a few more edge cases:

  1. An empty string input or output
  2. A very long string input or output
  3. An input or output containing special characters (e.g., emojis, non-ASCII characters)
    These additions would help test the validators' behavior in more extreme scenarios.
src/utils/converter.test.ts (1)

1-14: Good start, but consider expanding test coverage.

This test file provides a solid foundation for testing the convertToObjectMapping function. However, to ensure comprehensive coverage:

  1. Consider adding more test cases to cover different scenarios, such as:

    • Valid mappings
    • Edge cases (e.g., empty array, null input)
    • Different combinations of SyntaxType for inputExpr and outputExpr
  2. If convertToObjectMapping has multiple responsibilities or branches, each should be tested separately.

To improve the overall test architecture:

  1. Consider using a test data factory or object mother pattern to generate test inputs. This can make tests more readable and easier to maintain.

  2. If there are many test cases, consider using Jest's test.each for parameterized tests.

Example:

const testCases = [
  { input: [...], expectedOutput: ... },
  { input: [...], expectedOutput: ... },
  // more test cases...
];

test.each(testCases)('convertToObjectMapping($input)', ({ input, expectedOutput }) => {
  expect(convertToObjectMapping(input)).toEqual(expectedOutput);
});

This approach can make it easier to add and manage multiple test cases.

src/engine.test.ts (1)

70-90: LGTM: New test suite for isValidMapping is well-structured, with suggestions for improvement.

The new test suite for isValidMapping is a good addition, covering important scenarios:

  1. Conversion to JSON paths when options are provided.
  2. Handling of invalid output paths without engine options.

However, consider the following suggestions to enhance test coverage and clarity:

  1. Add more test cases to cover additional scenarios, such as:

    • Both input and output being invalid.
    • Valid input and output without needing conversion.
  2. For the error handling test, consider checking for a specific error being thrown rather than just a falsy return value.

  3. Make the test descriptions more specific. For example:

    • "should successfully validate when output is converted to JSON path using options"
    • "should return false when output is not a valid JSON path and no conversion options are provided"

Would you like me to provide example code for these improvements?

test/scenarios/mappings/data.ts (1)

292-301: LGTM! Consider adding corresponding input for completeness.

The new scenario effectively tests the engine's ability to handle non-standard JSON paths in the output, including keys with dots and hyphens, nested structures, and different content types. This addition enhances the test coverage by including edge cases for output mapping.

To make the scenario more comprehensive, consider adding a corresponding input field. This would allow testing both the input parsing and output generation for these non-standard cases, ensuring full coverage of the mapping process.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 4c6105c and bfc8caa.

📒 Files selected for processing (6)
  • src/engine.test.ts (2 hunks)
  • src/engine.ts (2 hunks)
  • src/utils/converter.test.ts (1 hunks)
  • test/scenarios/mappings/data.ts (1 hunks)
  • test/scenarios/mappings/invalid_output_mapping.json (0 hunks)
  • test/scenarios/mappings/non_path_output.json (1 hunks)
💤 Files with no reviewable changes (1)
  • test/scenarios/mappings/invalid_output_mapping.json
🧰 Additional context used
🪛 GitHub Check: codecov/patch
src/engine.ts

[warning] 79-81: src/engine.ts#L79-L81
Added lines #L79 - L81 were not covered by tests

🔇 Additional comments (13)
test/scenarios/mappings/non_path_output.json (5)

1-22: LGTM: Well-structured test scenarios for non-path output mappings.

The JSON file is well-structured and contains a variety of test scenarios for non-path output mappings. This aligns well with the PR objective of refactoring mapping validators and implementing automatic conversion of output mappings to JSON paths. The file name accurately reflects its content.


2-5: LGTM: Valid mapping for Content-Type header.

This mapping correctly associates the MIME type 'application/json' with the Content-Type header. It's a good test case for handling quoted string inputs and header name outputs.


6-9: LGTM: Simple numeric to string mapping.

This mapping provides a good test case for converting a numeric input to a simple string output. It helps ensure the validator can handle different data types correctly.


10-13: LGTM: Numeric to custom header-like string mapping.

This mapping extends the previous test case by using an output that resembles a custom HTTP header. It's a valuable addition to ensure the validator can handle various output formats.


14-17: LGTM: Crucial test case for quoted path-like output.

This mapping is particularly important as it tests the handling of outputs that resemble quoted paths. It ensures the validator can distinguish between actual JSON paths and string literals containing dots, which is crucial for the PR's objective of automatic conversion to JSON paths.

src/utils/converter.test.ts (2)

1-2: LGTM: Imports are appropriate for the test file.

The imports are correctly bringing in the necessary dependencies:

  1. SyntaxType from '../types' is used in the test case.
  2. convertToObjectMapping from './converter' is the function being tested.

4-14: LGTM: Well-structured test suite.

The test suite follows Jest's best practices:

  • Uses describe for grouping related tests.
  • Nests describe blocks to provide clear context.
  • Uses it for individual test cases with clear descriptions.

This structure enhances readability and maintainability of the tests.

src/engine.test.ts (1)

2-2: LGTM: Import statement for PathType is correctly added.

The new import statement for PathType from './types' is correctly placed and necessary for the new test cases. It follows TypeScript best practices for importing types.

test/scenarios/mappings/data.ts (1)

292-301: Good addition to test coverage, aligns with PR objectives.

The new scenario effectively enhances the test suite by covering non-standard JSON path outputs. This addition aligns well with the PR objectives of improving mapping validators and implementing automatic conversion of output mappings to JSON paths. The change is non-breaking and focused, as it only adds a new test case without modifying existing ones.

src/engine.ts (4)

83-88: Ensure default behavior remains consistent in convertToJSONPath method

The addition of the options parameter to convertToJSONPath adds flexibility. Please verify that when options is undefined or options.defaultPathType is not specified, the method behaves as expected.


101-105: LGTM

The addition of the options parameter to prepareMappings aligns with the updates to prepareMapping.


109-109: LGTM

Updating validateMappings to pass options to prepareMappings ensures consistent behavior.


135-135: LGTM

Passing options to prepareMappings in createFlatMappingsAST ensures mappings are processed correctly.

src/utils/converter.test.ts Show resolved Hide resolved
src/engine.ts Show resolved Hide resolved
src/engine.ts Outdated Show resolved Hide resolved
src/engine.ts Outdated Show resolved Hide resolved
src/engine.ts 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: 3

🧹 Outside diff range and nitpick comments (1)
src/engine.ts (1)

62-62: Add type annotation for the input parameter

To improve type safety and code clarity, consider adding a type annotation for the input parameter in the toJsonPath method.

Apply this diff to add the type annotation:

-private static toJsonPath(input): string {
+private static toJsonPath(input: string): string {
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between bfc8caa and f8cff74.

📒 Files selected for processing (3)
  • src/engine.ts (2 hunks)
  • src/parser.ts (1 hunks)
  • src/reverse_translator.ts (1 hunks)
🧰 Additional context used
🪛 GitHub Check: codecov/patch
src/engine.ts

[warning] 79-81: src/engine.ts#L79-L81
Added lines #L79 - L81 were not covered by tests

🔇 Additional comments (9)
src/engine.ts (3)

83-88: LGTM: Conditional JSON path conversion

The convertToJSONPath method correctly implements the conditional conversion of paths to JSON paths based on the provided options. The logic is clear and concise.


101-105: LGTM: Improved mapping preparation

The modifications to the prepareMappings method, including the addition of the options parameter and the use of the new prepareMapping method, enhance the consistency and flexibility of mapping preparation.


109-109: LGTM: Consistent use of options in validation

The update to pass the options parameter to prepareMappings in the validateMappings method ensures consistency with the recent changes and improves the flexibility of the validation process.

src/reverse_translator.ts (1)

41-41: Excellent use of readonly for immutability!

The addition of the readonly modifier to the options property is a positive change. It enforces immutability, preventing accidental modifications to the options after initialization. This aligns with TypeScript best practices and enhances code safety without affecting the existing functionality.

src/parser.ts (5)

60-60: Improved immutability for lexer property.

The lexer property has been changed from private to private readonly. This is a good practice as it ensures that the lexer cannot be reassigned after initialization, potentially preventing bugs and improving code clarity.


62-62: Improved immutability for options property.

The options property has been changed from private to private readonly. This change is beneficial as it guarantees that the options object cannot be modified after it's set in the constructor, maintaining the integrity of the parser's configuration throughout its lifecycle.


64-64: Improved immutability for pathTypesStack property.

The pathTypesStack property has been changed from private to private readonly. This modification ensures that the array reference itself cannot be reassigned, although the contents of the array can still be modified. This change adds an extra layer of safety to the code.


60-64: Overall improvement in class design.

The changes made to the JsonTemplateParser class, specifically the modification of property declarations to private readonly, represent a positive step towards improved immutability and code safety. These changes help prevent accidental modifications to critical properties after initialization, potentially reducing bugs and improving the overall robustness of the parser.

The rest of the class implementation remains unchanged, which is appropriate given the nature of the modifications. The constructor and methods continue to work as expected with these new immutable properties.


Line range hint 1-1161: Focused improvements to JsonTemplateParser class.

The changes made to src/parser.ts are concentrated on improving the immutability of the JsonTemplateParser class. By changing key properties to private readonly, the code now better enforces the principle of least privilege and reduces the risk of unintended modifications to these properties.

These changes are beneficial and align well with best practices in TypeScript/JavaScript development. They improve the robustness of the parser without introducing new functionality or breaking existing behavior.

No further changes are necessary based on this review.

src/engine.ts Show resolved Hide resolved
src/engine.ts Outdated Show resolved Hide resolved
src/engine.ts Show resolved Hide resolved
Copy link

@koladilip koladilip merged commit bf08bce into main Oct 17, 2024
14 checks passed
@koladilip koladilip deleted the feat.refactor-validators branch October 17, 2024 09:25
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