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: add support wild card object props #56

Merged
merged 35 commits into from
Jun 6, 2024

Conversation

koladilip
Copy link
Collaborator

@koladilip koladilip commented Jun 4, 2024

What are the changes introduced in this PR?

Add support for wild card object props, also support wild card selector mappings

What is the related Linear task?

Resolves INT-2160

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

    • Introduced wildcard property value handling in JSON templates.
    • Added new test scenarios for wildcard property values.
  • Bug Fixes

    • Fixed issues related to object wildcard property value expressions in JSON templates.
  • Refactor

    • Standardized method and function names for consistency.
  • Tests

    • Enhanced end-to-end and scenario tests with new wildcard property value cases.
    • Added new test files for invalid and valid wildcard scenarios.

@koladilip koladilip requested a review from a team as a code owner June 4, 2024 19:24
Copy link

coderabbitai bot commented Jun 4, 2024

Walkthrough

The recent updates primarily introduce support for handling wildcard property values in JSON templates. This involves changes across various files to accommodate new methods and logic for processing wildcard selectors. Key modifications include updates to the JsonTemplateEngine, JsonTemplateLexer, JsonTemplateParser, and JsonTemplateTranslator classes, along with new utility functions and test scenarios to ensure comprehensive coverage of the new feature.

Changes

File(s) Change Summary
src/engine.ts Modified convertMappingsToTemplate to use parse instead of parseMappingPaths.
src/lexer.ts Added matchObjectWildCardPropValue method in JsonTemplateLexer class.
src/parser.ts Added methods and modified existing ones to handle object wildcard property values.
src/reverse_translator.ts Added handling for ObjectWildcardValueExpression in various methods.
src/translator.ts Added methods and cases to handle ObjectWildcardValueExpression.
src/types.ts Updated SyntaxType enum and ObjectPropExpression interface; introduced ObjectWildcardValueExpression.
src/utils/converter.ts Renamed and added functions for handling wildcard selectors in object mappings.
test/e2e.test.ts, test/scenario.test.ts Modified import statements for ScenarioUtils to use wildcard imports.
test/scenarios/mappings/data.ts Added context object, modified templatePath values, and introduced new objects with mappings.
test/scenarios/mappings/invalid_object_mappings.json Introduced a new file with mapping configuration for wildcard properties.
test/scenarios/mappings/object_mappings.json Introduced a new file with mappings for transforming JSON data using wildcard properties.
test/scenarios/objects/data.ts Added new objects for handling wildcard scenarios and invalid wildcard prop values.
test/scenarios/objects/invalid_wild_cards.jt Introduced a new file defining properties for a user with specific traits and values.
test/scenarios/objects/wild_cards.jt Introduced a new file defining properties for a user with wildcard entries for traits.
test/utils/scenario.ts Changed createTemplateEngine to call initializeScenario using the function name.

Sequence Diagram(s) (Beta)

sequenceDiagram
    participant User
    participant JsonTemplateEngine
    participant JsonTemplateLexer
    participant JsonTemplateParser
    participant JsonTemplateTranslator
    
    User->>JsonTemplateEngine: convertMappingsToTemplate(mappings, options)
    JsonTemplateEngine->>JsonTemplateLexer: matchObjectWildCardPropValue()
    JsonTemplateEngine->>JsonTemplateParser: parseObjectPropWildcardValueExpr()
    JsonTemplateParser->>JsonTemplateTranslator: translateObjectWildcardValueExpr()
    JsonTemplateTranslator-->>JsonTemplateEngine: Translated Template
    JsonTemplateEngine-->>User: Return Translated Template
Loading

Poem

In the code where wildcards play,
JSON paths now find their way.
With new expressions, bold and free,
Data flows like poetry.
Templates dance, so wild, so grand,
In this enchanted data land. 🌟


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.

Copy link
Contributor

github-actions bot commented Jun 4, 2024

Coverage report

St.
Category Percentage Covered / Total
🟢 Statements
100% (+0.05% 🔼)
4546/4546
🟢 Branches
100% (+0.08% 🔼)
1218/1218
🟢 Functions 100% 342/342
🟢 Lines
100% (+0.05% 🔼)
4546/4546

Test suite run success

162 tests passing in 3 suites.

Report generated by 🧪jest coverage report action from 001011d

Copy link

codecov bot commented Jun 4, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 100.00%. Comparing base (19d94c2) to head (001011d).
Report is 42 commits behind head on main.

Additional details and impacted files
@@             Coverage Diff             @@
##              main       #56     +/-   ##
===========================================
  Coverage   100.00%   100.00%             
===========================================
  Files           10        14      +4     
  Lines         3078      4546   +1468     
  Branches       726      1074    +348     
===========================================
+ Hits          3078      4546   +1468     

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

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

Line range hint 899-899: Correct the use of this in a static context.

- return `${varName} = ${varName}.length < 2 ? ${varName}[0] : ${varName};`;
+ return `${JsonTemplateTranslator.name}.returnIsNotEmpty(${varName}) ? (${varName} = Array.isArray(${varName}) ? ${varName} : [${varName}]) : ${varName};`;

The static method convertToSingleValueIfSafe incorrectly uses this which can lead to confusion and potential errors in a static context. Replace this with the class name JsonTemplateTranslator to correctly reference the static method.

src/parser.ts (1)

Line range hint 1477-1477: Refactor to avoid using this in static methods.

Static methods should not use this as it can lead to confusion and potential runtime errors. Replace this with the class name JsonTemplateParser.

- this.pathContainsVariables(newPathExpr.parts);
+ JsonTemplateParser.pathContainsVariables(newPathExpr.parts);

- this.isRichPath(newPathExpr);
+ JsonTemplateParser.isRichPath(newPathExpr);

- this.convertToFunctionCallExpr(fnExpr, newPathExpr);
+ JsonTemplateParser.convertToFunctionCallExpr(fnExpr, newPathExpr);

- this.convertToBlockExpr(expr);
+ JsonTemplateParser.convertToBlockExpr(expr);

Also applies to: 1483-1483, 1490-1490, 1521-1521

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 4ee3874 and 409f4c5.

Files selected for processing (14)
  • src/engine.ts (1 hunks)
  • src/lexer.ts (1 hunks)
  • src/parser.ts (6 hunks)
  • src/reverse_translator.ts (3 hunks)
  • src/translator.ts (3 hunks)
  • src/types.ts (2 hunks)
  • src/utils/converter.ts (7 hunks)
  • test/scenarios/mappings/data.ts (4 hunks)
  • test/scenarios/mappings/invalid_object_mappings.json (1 hunks)
  • test/scenarios/mappings/object_mappings.json (1 hunks)
  • test/scenarios/objects/data.ts (1 hunks)
  • test/scenarios/objects/invalid_wild_cards.jt (1 hunks)
  • test/scenarios/objects/wild_cards.jt (1 hunks)
  • test/utils/scenario.ts (1 hunks)
Files skipped from review due to trivial changes (4)
  • test/scenarios/mappings/invalid_object_mappings.json
  • test/scenarios/mappings/object_mappings.json
  • test/scenarios/objects/invalid_wild_cards.jt
  • test/scenarios/objects/wild_cards.jt
Additional context used
Biome
test/utils/scenario.ts

[error] 6-36: Avoid classes that contain only static members. (lint/complexity/noStaticOnlyClass)

Prefer using simple functions instead of classes with only static members.

src/engine.ts

[error] 22-22: Using this in a static context can be confusing. (lint/complexity/noThisInStatic)

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


[error] 29-29: Using this in a static context can be confusing. (lint/complexity/noThisInStatic)

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


[error] 51-51: Using this in a static context can be confusing. (lint/complexity/noThisInStatic)

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


[error] 58-58: Using this in a static context can be confusing. (lint/complexity/noThisInStatic)

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


[error] 70-70: Using this in a static context can be confusing. (lint/complexity/noThisInStatic)

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


[error] 74-74: Using this in a static context can be confusing. (lint/complexity/noThisInStatic)

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


[error] 74-74: Using this in a static context can be confusing. (lint/complexity/noThisInStatic)

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


[error] 83-83: Using this in a static context can be confusing. (lint/complexity/noThisInStatic)

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


[error] 83-83: Using this in a static context can be confusing. (lint/complexity/noThisInStatic)

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


[error] 11-11: Don't use 'Function' as a type. (lint/complexity/noBannedTypes)

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


[error] 13-13: Don't use 'Function' as a type. (lint/complexity/noBannedTypes)

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


[error] 20-20: Don't use 'Function' as a type. (lint/complexity/noBannedTypes)

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


[error] 25-25: Don't use 'Function' as a type. (lint/complexity/noBannedTypes)

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

src/lexer.ts

[error] 366-366: Using this in a static context can be confusing. (lint/complexity/noThisInStatic)

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

src/translator.ts

[error] 899-899: Using this in a static context can be confusing. (lint/complexity/noThisInStatic)

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

src/parser.ts

[error] 1477-1477: Using this in a static context can be confusing. (lint/complexity/noThisInStatic)

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


[error] 1483-1483: Using this in a static context can be confusing. (lint/complexity/noThisInStatic)

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


[error] 1490-1490: Using this in a static context can be confusing. (lint/complexity/noThisInStatic)

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


[error] 1521-1521: Using this in a static context can be confusing. (lint/complexity/noThisInStatic)

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

Additional comments not posted (24)
test/scenarios/objects/data.ts (2)

4-7: LGTM! The test scenario for invalid wildcards is well-defined.


15-35: LGTM! The test scenario for valid wildcards is comprehensive and well-structured.

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

160-160: LGTM! The test scenario for invalid array mappings is well-defined.


166-173: LGTM! The test scenario for invalid object mappings is comprehensive and well-structured.


254-288: LGTM! The test scenario for object mappings with wildcards is comprehensive and well-structured.

src/types.ts (2)

87-87: LGTM! The addition of OBJECT_PROP_WILD_CARD_VALUE_EXPR is necessary for supporting wildcard properties.


151-153: LGTM! The ObjectWildcardValueExpression interface is well-defined and necessary for handling wildcard expressions.

src/utils/converter.ts (5)

86-88: LGTM! The isWildcardSelector function correctly identifies wildcard selectors.


90-95: LGTM! The createWildcardObjectPropValueExpression function is correctly implemented for creating wildcard property expressions.


97-133: LGTM! The processWildCardSelector function is comprehensive and correctly handles wildcard selectors in mappings.


136-156: LGTM! The handleNextPart function effectively manages the processing of the next part in flat mappings.


201-201: LGTM! The convertToObjectMapping function is well-implemented and crucial for converting flat mappings to object expressions.

src/reverse_translator.ts (2)

123-125: The implementation of translateWildcardObjectPropValueExpression correctly translates wildcard object property expressions.


114-117: The addition of the OBJECT_PROP_WILD_CARD_VALUE_EXPR case in translateExpression method correctly handles the new expression type.

src/lexer.ts (1)

96-98: The implementation of matchObjectWildCardPropValue correctly identifies wildcard property values.

src/translator.ts (4)

193-198: Add handling for OBJECT_PROP_WILD_CARD_VALUE_EXPR in translateExpr.

This addition correctly integrates the new wildcard property expression type into the translation process, ensuring that expressions of this type are handled appropriately.


535-541: Implement translation for ObjectWildcardValueExpression.

The method translateObjectWildcardValueExpr correctly assigns the wildcard expression value to the destination variable. This is a straightforward and effective implementation for handling wildcard values in object expressions.


543-559: Implement translation for wildcard properties in objects.

The method translateObjectWildcardProp effectively handles the translation of object properties that are marked as wildcards. The use of Object.entries to iterate over properties and the dynamic assignment based on the wildcard key expression are correctly implemented.


567-590: Enhance translateObjectExpr to support wildcard properties.

This modification to translateObjectExpr adds support for translating object expressions that contain wildcard properties. The conditional handling based on the wildcard property of each object property expression is well-integrated.

src/parser.ts (5)

28-28: Added ObjectWildcardValueExpression to imports.

This addition is necessary for the new functionality related to wildcard object properties.


1088-1098: New method parseObjectPropWildcardValueExpr correctly throws an error for invalid wildcard property values.

This method enhances error handling by providing clear feedback on incorrect wildcard usage, which is crucial for debugging and user experience.


1110-1111: Integration of parseObjectPropWildcardValueExpr in parseObjectKeyExpr.

This integration allows the parser to handle wildcard properties directly within object keys, aligning with the PR's objectives to support wildcard properties.


Line range hint 1142-1154: Method isWildcardPropKey checks if an expression is a wildcard property key.

This utility method is essential for determining when to treat properties as wildcards, which is a key part of supporting flexible JSON structures.


1390-1392: Handling of wildcard property values in parsePrimaryExpr.

Properly extends the parser's capabilities to recognize and process wildcard property values at a fundamental level.

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

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 409f4c5 and 1adddcc.

Files selected for processing (2)
  • test/scenarios/objects/data.ts (1 hunks)
  • test/scenarios/objects/wild_cards.jt (1 hunks)
Files skipped from review as they are similar to previous changes (2)
  • test/scenarios/objects/data.ts
  • test/scenarios/objects/wild_cards.jt

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

Outside diff range and nitpick comments (2)
test/e2e.test.ts (1)

Line range hint 19-36: Consider replacing forEach with for...of for better performance.

- scenarios.forEach((scenarioFileName) => {
+ for (const scenarioFileName of scenarios) {
src/engine.ts (1)

Line range hint 11-11: Replace Function type with more specific function signatures to prevent potential bugs.

- private readonly fn: Function;
+ private readonly fn: (data: unknown, bindings: Record<string, unknown>) => unknown;

Also applies to: 13-13, 20-20, 29-29

Tools
Biome

[error] 20-20: Don't use 'Function' as a type. (lint/complexity/noBannedTypes)

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


[error] 29-29: Don't use 'Function' as a type. (lint/complexity/noBannedTypes)

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

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 1adddcc and caf52cf.

Files selected for processing (4)
  • src/engine.ts (4 hunks)
  • test/e2e.test.ts (1 hunks)
  • test/scenario.test.ts (1 hunks)
  • test/utils/scenario.ts (1 hunks)
Files skipped from review due to trivial changes (1)
  • test/scenario.test.ts
Files skipped from review as they are similar to previous changes (1)
  • test/utils/scenario.ts
Additional context used
Biome
test/e2e.test.ts

[error] 19-36: Prefer for...of instead of forEach. (lint/complexity/noForEach)

forEach may lead to performance issues when working with large arrays. When combined with functions like filter or map, this causes multiple iterations over the same type.

src/engine.ts

[error] 11-11: Don't use 'Function' as a type. (lint/complexity/noBannedTypes)

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


[error] 13-13: Don't use 'Function' as a type. (lint/complexity/noBannedTypes)

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


[error] 20-20: Don't use 'Function' as a type. (lint/complexity/noBannedTypes)

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


[error] 29-29: Don't use 'Function' as a type. (lint/complexity/noBannedTypes)

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

Additional comments not posted (1)
test/e2e.test.ts (1)

4-4: LGTM! The change to a wildcard import aligns with the PR's objective to handle wildcard properties.

src/engine.ts Show resolved Hide resolved
Copy link

sonarqubecloud bot commented Jun 6, 2024

@koladilip koladilip requested a review from saikumarrs June 6, 2024 04:58
@koladilip koladilip merged commit b553782 into main Jun 6, 2024
10 checks passed
@koladilip koladilip deleted the feat.add-support-for-json-path branch June 6, 2024 06:57
@github-actions github-actions bot mentioned this pull request Jun 6, 2024
@coderabbitai coderabbitai bot mentioned this pull request Oct 16, 2024
10 tasks
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