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: merge filters and indexes #113

Merged
merged 4 commits into from
Jul 2, 2024
Merged

feat: merge filters and indexes #113

merged 4 commits into from
Jul 2, 2024

Conversation

koladilip
Copy link
Collaborator

@koladilip koladilip commented Jul 1, 2024

What are the changes introduced in this PR?

now users need to specify filters and indexes in one mappings and it will be automatically mapped to all applicable rules

What is the related Linear task?

Resolves INT-2240

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

  • New Features

    • Enhanced the parse method in the JSON Template Engine to support a new mappings option.
    • Introduced the mergeObjectFilterParts function for better handling of object filter parts.
  • Bug Fixes

    • Updated JSON path expressions in mapping configurations to ensure all products' attributes are correctly extracted.
  • Refactor

    • Refined method references within the JsonTemplateParser class to use this instead of direct class references, improving code maintainability.
  • Tests

    • Adjusted test scenarios to reflect new JSON path expressions and mappings configurations.

@koladilip koladilip requested a review from a team as a code owner July 1, 2024 16:32
@koladilip koladilip requested review from sanpj2292 and chandumlg July 1, 2024 16:32
Copy link

coderabbitai bot commented Jul 1, 2024

Walkthrough

This update involves modifications across several components of the JSON template processing system, including JsonTemplateEngine, JsonTemplateParser, type definitions, and utility functions. The changes mainly aim to enhance the mappings handling, improve method flexibility, and refine JSON path expressions in test scenarios.

Changes

File(s) Change Summary
src/engine.ts Updated JsonTemplateEngine.parse method to use a new options object affecting mappings property.
src/parser.ts Adjusted JsonTemplateParser methods: updatePathExpr is now non-static, and shouldPathBeConvertedAsBlock uses this instead of class references.
src/types.ts Enhanced EngineOptions interface to include a mappings property.
src/utils/converter.ts Introduced mergeObjectFilterParts function for merging object filter parts; modified handleAllFilterIndexFound to use the new function under specific conditions.
test/scenarios/mappings/all_features.json Updated JSON paths to select all products' names, categories, and variations instead of filtering by category.
test/scenarios/mappings/context_vars_mapping.json Changed source path in mapping configuration from $.a[*].foo to $.a[*].#index.foo.
test/scenarios/mappings/filters.json Modified JSON filter mappings to extract names and categories for all products rather than just those with a specific category.

Sequence Diagram(s)

N/A

Poem

In bytes and parsers we trust,
With mappings smooth and dust.
Paths now dance with ease,
JSON filters they appease.
🐇 Code so pure, updated delight,
Cheers to changes, coding light! ✨


Tip

Early access features: enabled

We are currently testing the following features in early access:

  • OpenAI gpt-4o model for code reviews and chat: OpenAI claims that this model is better at understanding and generating code than the previous models. We seek your feedback over the next few weeks before making it generally available.

Note:

  • You can enable or disable early access features from the CodeRabbit UI or by updating the CodeRabbit configuration file.
  • Please join our Discord Community to provide feedback and report issues.
  • OSS projects are currently opted into early access features by default.

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.
    • @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 as 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.

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.

@koladilip koladilip requested a review from utsabc July 1, 2024 16:32
Copy link
Contributor

github-actions bot commented Jul 1, 2024

Coverage report

St.
Category Percentage Covered / Total
🟢 Statements
93.31% (-6.69% 🔻)
4648/4981
🟢 Branches
92.81% (-7.19% 🔻)
1162/1252
🟢 Functions 100% 372/372
🟢 Lines
93.31% (-6.69% 🔻)
4648/4981
Show files with reduced coverage 🔻
St.
File Statements Branches Functions Lines
🟢 engine.ts
97.62% (-2.38% 🔻)
89.19% (-10.81% 🔻)
100%
97.62% (-2.38% 🔻)
🟢 lexer.ts
87.11% (-12.89% 🔻)
91.82% (-8.18% 🔻)
100%
87.11% (-12.89% 🔻)
🟢 parser.ts
88.14% (-11.86% 🔻)
89.51% (-10.49% 🔻)
100%
88.14% (-11.86% 🔻)
🟢 utils/common.ts 100%
88.89% (-11.11% 🔻)
100% 100%
🟢 utils/converter.ts
90.23% (-9.77% 🔻)
84.82% (-15.18% 🔻)
100%
90.23% (-9.77% 🔻)

Test suite run success

210 tests passing in 6 suites.

Report generated by 🧪jest coverage report action from 51faf11

Copy link

codecov bot commented Jul 1, 2024

Codecov Report

Attention: Patch coverage is 94.59459% with 2 lines in your changes missing coverage. Please review.

Project coverage is 91.54%. Comparing base (97d95e8) to head (51faf11).
Report is 44 commits behind head on main.

Files Patch % Lines
src/utils/converter.ts 89.47% 2 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##              main     #113      +/-   ##
===========================================
- Coverage   100.00%   91.54%   -8.46%     
===========================================
  Files           14       18       +4     
  Lines         4565     4981     +416     
  Branches      1082     1089       +7     
===========================================
- Hits          4565     4560       -5     
- Misses           0      412     +412     
- Partials         0        9       +9     

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

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

Line range hint 15-18: Avoid using Function as a type.

The Function type is used in multiple places. Prefer explicitly defining the function shape to avoid potential bugs.

-  private readonly fn: Function;
-  private constructor(fn: Function) {
+  private readonly fn: (data: unknown, bindings: Record<string, unknown>) => unknown;
+  private constructor(fn: (data: unknown, bindings: Record<string, unknown>) => unknown) {
-  private static compileAsSync(template: TemplateInput, options?: EngineOptions): Function {
+  private static compileAsSync(template: TemplateInput, options?: EngineOptions): (data: unknown, bindings: Record<string, unknown>) => unknown {
-  private static compileAsAsync(templateOrExpr: TemplateInput, options?: EngineOptions): Function {
+  private static compileAsAsync(templateOrExpr: TemplateInput, options?: EngineOptions): (data: unknown, bindings: Record<string, unknown>) => unknown {

Also applies to: 22-22, 26-26, 35-35

src/parser.ts (1)

Line range hint 1524-1524: Avoid using this in a static context.

Using this in a static context can be confusing. Consider using the class name instead.

- return this.isArrayFilterExpressionSimple(part as ArrayFilterExpression);
+ return JsonTemplateParser.isArrayFilterExpressionSimple(part as ArrayFilterExpression);

Also applies to: 1530-1530, 1537-1537

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 5f1d213 and 78fbe36.

Files selected for processing (6)
  • src/engine.ts (1 hunks)
  • src/parser.ts (4 hunks)
  • src/types.ts (1 hunks)
  • src/utils/converter.ts (3 hunks)
  • test/scenarios/mappings/all_features.json (1 hunks)
  • test/scenarios/mappings/context_vars_mapping.json (1 hunks)
Additional context used
Biome
src/engine.ts

[error] 20-20: Don't use 'Function' as a type.

Prefer explicitly define the function shape. This type accepts any function-like value, which can be a common source of bugs.

(lint/complexity/noBannedTypes)


[error] 22-22: Don't use 'Function' as a type.

Prefer explicitly define the function shape. This type accepts any function-like value, which can be a common source of bugs.

(lint/complexity/noBannedTypes)


[error] 26-26: Don't use 'Function' as a type.

Prefer explicitly define the function shape. This type accepts any function-like value, which can be a common source of bugs.

(lint/complexity/noBannedTypes)


[error] 35-35: Don't use 'Function' as a type.

Prefer explicitly define the function shape. This type accepts any function-like value, which can be a common source of bugs.

(lint/complexity/noBannedTypes)

src/parser.ts

[error] 1524-1524: Using this in a static context can be confusing.

this refers to the class.
Unsafe fix: Use the class name instead.

(lint/complexity/noThisInStatic)


[error] 1530-1530: Using this in a static context can be confusing.

this refers to the class.
Unsafe fix: Use the class name instead.

(lint/complexity/noThisInStatic)


[error] 1537-1537: Using this in a static context can be confusing.

this refers to the class.
Unsafe fix: Use the class name instead.

(lint/complexity/noThisInStatic)


[error] 562-562: Do not add then to an object.

(lint/suspicious/noThenProperty)

GitHub Check: codecov/patch
src/utils/converter.ts

[warning] 120-121: src/utils/converter.ts#L120-L121
Added lines #L120 - L121 were not covered by tests

GitHub Check: Coverage annotations (🧪 jest-coverage-report-action)
src/utils/converter.ts

[warning] 120-120: 🧾 Statement is not covered
Warning! Not covered statement


[warning] 121-121: 🧾 Statement is not covered
Warning! Not covered statement


[warning] 119-119: 🌿 Branch is not covered
Warning! Not covered branch


[warning] 119-121: 🌿 Branch is not covered
Warning! Not covered branch

src/parser.ts

[warning] 1580-1583: 🌿 Branch is not covered
Warning! Not covered branch

Additional comments not posted (13)
test/scenarios/mappings/context_vars_mapping.json (1)

7-7: Verify the correctness of the updated JSON path.

The from path has been updated to $.a[*].#index.foo. Ensure that the updated path is correct and consistent with the rest of the logic.

test/scenarios/mappings/all_features.json (8)

23-23: Verify the correctness of the updated JSON path.

The input path has been updated to $.products[*].name. Ensure that the updated path is correct and consistent with the rest of the logic.


27-27: Verify the correctness of the updated JSON path.

The from path has been updated to $.products[*].category. Ensure that the updated path is correct and consistent with the rest of the logic.


31-31: Verify the correctness of the updated JSON path.

The input path has been updated to $.products[*].variations[*].size. Ensure that the updated path is correct and consistent with the rest of the logic.


35-35: Verify the correctness of the updated JSON path.

The input path has been updated to $.products[*].(@.price * @.quantity * (1 - $.discount / 100)). Ensure that the updated path is correct and consistent with the rest of the logic.


43-43: Verify the correctness of the updated JSON path.

The input path has been updated to $.products[*].variations[*].length. Ensure that the updated path is correct and consistent with the rest of the logic.


47-47: Verify the correctness of the updated JSON path.

The input path has been updated to $.products[*].variations[*].width. Ensure that the updated path is correct and consistent with the rest of the logic.


51-51: Verify the correctness of the updated JSON path.

The input path has been updated to $.products[*].variations[*].color. Ensure that the updated path is correct and consistent with the rest of the logic.


55-55: Verify the correctness of the updated JSON path.

The input path has been updated to $.products[*].variations[*].height. Ensure that the updated path is correct and consistent with the rest of the logic.

src/engine.ts (2)

90-90: Ensure the new mappings option is correctly integrated.

The createFlatMappingsAST method now includes a new mappings option. Verify that this option is correctly integrated and does not introduce any issues.


95-96: Verify the correctness of the updated parse method calls.

The parse method calls now include the new mappings option. Ensure that these calls are correct and consistent with the rest of the logic.

src/types.ts (1)

110-110: Ensure the new mappings property is correctly integrated.

The EngineOptions interface now includes a new mappings property. Verify that this property is correctly integrated and does not introduce any issues.

src/utils/converter.ts (1)

113-127: Add comments to explain the logic.

The function mergeObjectFilterParts could benefit from additional comments explaining its logic for better readability and maintainability.

+ // Merges object filter parts under specific conditions.
function mergeObjectFilterParts(existParts: Expression[], newParts: Expression[]) {
  for (let index = 0; index < newParts.length; index++) {
    if (
      newParts[index].type === SyntaxType.OBJECT_FILTER_EXPR &&
      existParts[index].type === SyntaxType.OBJECT_FILTER_EXPR
    ) {
      if (newParts[index].options?.index) {
        existParts[index].options = { ...existParts[index].options, ...newParts[index].options };
      }
      if (newParts[index].filter.type !== SyntaxType.ALL_FILTER_EXPR) {
        existParts[index].filter = newParts[index].filter;
      }
    }
  }
}
Tools
GitHub Check: codecov/patch

[warning] 120-121: src/utils/converter.ts#L120-L121
Added lines #L120 - L121 were not covered by tests

GitHub Check: Coverage annotations (🧪 jest-coverage-report-action)

[warning] 120-120: 🧾 Statement is not covered
Warning! Not covered statement


[warning] 121-121: 🧾 Statement is not covered
Warning! Not covered statement


[warning] 119-119: 🌿 Branch is not covered
Warning! Not covered branch


[warning] 119-121: 🌿 Branch is not covered
Warning! Not covered branch

src/utils/converter.ts Show resolved Hide resolved
src/utils/converter.ts Show resolved Hide resolved
src/parser.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: 0

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

Line range hint 20-35: Avoid using Function as a type.

Using Function as a type is discouraged. Prefer explicitly defining the function shape to avoid potential bugs.

-  private readonly fn: Function;
-  private constructor(fn: Function) {
+  private readonly fn: (data: unknown, bindings: Record<string, unknown>) => unknown;
+  private constructor(fn: (data: unknown, bindings: Record<string, unknown>) => unknown) {
-  private static compileAsSync(template: TemplateInput, options?: EngineOptions): Function {
+  private static compileAsSync(template: TemplateInput, options?: EngineOptions): (data: unknown, bindings: Record<string, unknown>) => unknown {
-  private static compileAsAsync(templateOrExpr: TemplateInput, options?: EngineOptions): Function {
+  private static compileAsAsync(templateOrExpr: TemplateInput, options?: EngineOptions): (data: unknown, bindings: Record<string, unknown>) => unknown {
-  private static translateExpression(expr: Expression): string {
+  private static translateExpression(expr: Expression): string {
Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 78fbe36 and 8e4fe75.

Files selected for processing (1)
  • src/engine.ts (2 hunks)
Additional context used
Biome
src/engine.ts

[error] 20-20: Don't use 'Function' as a type.

Prefer explicitly define the function shape. This type accepts any function-like value, which can be a common source of bugs.

(lint/complexity/noBannedTypes)


[error] 22-22: Don't use 'Function' as a type.

Prefer explicitly define the function shape. This type accepts any function-like value, which can be a common source of bugs.

(lint/complexity/noBannedTypes)


[error] 26-26: Don't use 'Function' as a type.

Prefer explicitly define the function shape. This type accepts any function-like value, which can be a common source of bugs.

(lint/complexity/noBannedTypes)


[error] 35-35: Don't use 'Function' as a type.

Prefer explicitly define the function shape. This type accepts any function-like value, which can be a common source of bugs.

(lint/complexity/noBannedTypes)

Additional comments not posted (2)
src/engine.ts (2)

135-135: LGTM!

The change to parse the mapping paths if expr is an array improves the robustness of the reverseTranslate method.


90-90: Verify the impact of the mappings property.

Ensure that adding the mappings property to the options parameter does not cause unintended side effects in other parts of the codebase.

Verification successful

Verify the impact of the mappings property.

The addition of the mappings property in the createFlatMappingsAST method does not cause unintended side effects. The property is explicitly set to true and is used in a conditional check in src/parser.ts, ensuring it is being utilized as intended.

  • src/parser.ts
    • Line: !this.options?.mappings &&
  • src/engine.ts
    • Line: const newOptions = { ...options, mappings: true };
Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify the impact of the `mappings` property in the `options` parameter.

# Test: Search for the usage of the `createFlatMappingsAST` method. Expect: No unintended side effects.
rg --type-add 'typescript' --type 'typescript' 'createFlatMappingsAST'

Length of output: 126



Script:

#!/bin/bash
# Description: Verify the impact of the `mappings` property in the `options` parameter.

# Test: Search for the usage of the `createFlatMappingsAST` method.
rg 'createFlatMappingsAST'

# Test: Search for any references to the `options` object that include the `mappings` property.
rg '\boptions\b.*\bmappings\b'

Length of output: 339



Script:

#!/bin/bash
# Description: Extract the relevant code surrounding the references to `options.mappings`.

# Extract code from `src/parser.ts` around the reference to `options.mappings`.
rg -C 5 '\boptions\b.*\bmappings\b' src/parser.ts

# Extract code from `src/engine.ts` around the reference to `options.mappings`.
rg -C 5 '\boptions\b.*\bmappings\b' src/engine.ts

Length of output: 905

Copy link

sonarcloud bot commented Jul 2, 2024

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

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 8e4fe75 and 51faf11.

Files selected for processing (1)
  • test/scenarios/mappings/filters.json (1 hunks)
Additional comments not posted (2)
test/scenarios/mappings/filters.json (2)

7-7: Verify the removal of the filter condition.

The JSON path expression $.products[*].name now selects all product names. Ensure this change is intentional and aligns with the desired behavior of including all products regardless of category.


11-11: Verify the removal of the filter condition.

The JSON path expression $.products[*].category now selects all product categories. Ensure this change is intentional and aligns with the desired behavior of including all products regardless of category.

@koladilip koladilip merged commit ab652ff into main Jul 2, 2024
13 checks passed
@koladilip koladilip deleted the feat.order-mappings branch July 2, 2024 06:24
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