Skip to content

initial update to allow an optional parameter to retain required fields#767

Closed
elisadinsmore wants to merge 1 commit intomainfrom
feature/addRequired
Closed

initial update to allow an optional parameter to retain required fields#767
elisadinsmore wants to merge 1 commit intomainfrom
feature/addRequired

Conversation

@elisadinsmore
Copy link
Contributor

Please explain how to summarize this PR for the Changelog:

Adding an optional parameter to the plugin that allows the ability to retain required fields if they weren't mapped by the user

Tell code reviewer how and what to test:

Go through the mapping process on a sheet with at least one required field and do not map that field. Ensure it still is displayed in the review stage.

@elisadinsmore elisadinsmore marked this pull request as draft March 4, 2025 18:14
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Mar 4, 2025

Walkthrough

The pull request updates the viewMappedPlugin function to accept an optional options parameter with a boolean property includeRequired defaulting to false. Now, when a job:completed event is triggered, if includeRequired is set to true, the plugin inspects the unmappedDestinationFields for fields with a 'required' constraint and adds them to the mappedFields array. Additionally, associated comments have been modified to reflect that both mapped and required fields are processed when the option is enabled.

Changes

File(s) Change Summary
plugins/view-mapped/src/view-mapped.ts Updated the viewMappedPlugin function signature to include an optional options parameter with includeRequired. Modified the event listener to conditionally add required fields from unmappedDestinationFields to mappedFields and updated comments accordingly.

Sequence Diagram(s)

sequenceDiagram
    participant Plugin as viewMappedPlugin
    participant Listener as Job Completed Event Listener
    participant UFields as unmappedDestinationFields
    participant MFields as mappedFields

    Listener->>Plugin: Invoke viewMappedPlugin on job:completed
    Plugin->>Listener: Check if options.includeRequired is true
    alt options.includeRequired is true
        Plugin->>UFields: Iterate through fields
        UFields-->>Plugin: Identify fields with 'required' constraint
        Plugin->>MFields: Add required fields to mappedFields 
    end
    Plugin->>Listener: Proceed with updating workbook metadata
Loading

Possibly related PRs

  • view mapped fields only plugin #591: Introduces a variant of the viewMappedPlugin function focused on mapped fields, directly relating to how required fields are now optionally included.
  • fix: view mapped plugin #758: Alters the event listener logic and job handling for the viewMappedPlugin function, sharing similarities with the modifications for handling required fields.

Suggested reviewers

  • HJordan35
✨ Finishing Touches
  • 📝 Generate Docstrings

🪧 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 generate docstrings to generate docstrings for this 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: 2

🧹 Nitpick comments (3)
plugins/view-mapped/src/view-mapped.ts (3)

75-100: Consider simplifying the nested loop structure for required fields.

While the implementation is correct, the deeply nested loops and conditional checks make the code harder to read and could impact performance for large datasets.

Here's a more concise implementation using array methods:

  //If the includeRequired bool value is set to true, we will add all required fields to the mappedFields array
  if (includeRequired) {
-    for (
-      let i = 0;
-      i < jobPlan.data.plan.unmappedDestinationFields.length;
-      i++
-    ) {
-      for (
-        let j = 0;
-        j <
-        jobPlan.data.plan.unmappedDestinationFields[i].destinationField
-          .constraints?.length;
-        j++
-      ) {
-        if (
-          jobPlan.data.plan.unmappedDestinationFields[i]
-            .destinationField.constraints[j].type === 'required'
-        ) {
-          const requiredFieldKey =
-            jobPlan.data.plan.unmappedDestinationFields[i]
-              .destinationField.key
-          mappedFields.push(requiredFieldKey)
-        }
-      }
-    }
+    jobPlan.data.plan.unmappedDestinationFields.forEach(field => {
+      const hasRequiredConstraint = field.destinationField.constraints?.some(
+        constraint => constraint.type === 'required'
+      );
+      
+      if (hasRequiredConstraint) {
+        mappedFields.push(field.destinationField.key);
+      }
+    });
  }

13-14: Consider adding JSDoc documentation for the options parameter.

Adding JSDoc would provide better developer experience for users of this plugin.

Add JSDoc documentation:

 /**
  * This plugin allows you to make the post-mapping sheet only display mapped data
+ * @param options - Configuration options for the plugin
+ * @param options.includeRequired - When true, unmapped required fields will also be displayed
  */
 export function viewMappedPlugin(options?: { includeRequired?: boolean }) {
   const { includeRequired = false } = options

75-75: Fix typo in comment.

There's a small typo in the comment.

-  //If the includeRequired bool value is set to true, we will add all required fields to the mappedFields array
+  // If the includeRequired bool value is set to true, we will add all required fields to the mappedFields array
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 2cd727e and 0570fa6.

📒 Files selected for processing (1)
  • plugins/view-mapped/src/view-mapped.ts (3 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: Setup
🔇 Additional comments (3)
plugins/view-mapped/src/view-mapped.ts (3)

13-14: Good job on parameter addition with backward compatibility.

The addition of an optional parameter with a default value is a clean approach that maintains backward compatibility while adding new functionality.


116-116: Accurate comment update reflecting the new behavior.

The comment has been properly updated to reflect that both mapped fields and required fields (when includeRequired is true) will have their metadata updated.


13-14:

Details

❓ Verification inconclusive

Verify that this change works with the PR objectives.

The PR objectives indicate that this update should allow required fields to be retained even if they're not mapped. The implementation logically adds unmapped required fields to the mappedFields array, which should achieve this goal.

Also applies to: 75-100


🏁 Script executed:

#!/bin/bash
# Description: Verify that the implementation aligns with PR objectives

# Check if the implementation correctly identifies and adds required fields
ast-grep --pattern $'if (includeRequired) {
  $$$
  mappedFields.push($field)
  $$$
}'

# Check if there are unit tests for this new functionality
rg -A 3 -B 3 "includeRequired.*true" --type=test

Length of output: 167


Below is a revised verification approach to double-check that the new logic for including unmapped required fields is correctly implemented and covered by tests. The previous shell commands didn’t return output due to file type mismatches. Please run the scripts below to confirm:

#!/bin/bash
# Verify that the includeRequired logic is present and pushes unmapped required fields

echo "Searching for the includeRequired branch in plugins/view-mapped/src/view-mapped.ts..."
rg -n "if\s*\(includeRequired\)" plugins/view-mapped/src/view-mapped.ts

echo "Searching for mappedFields.push usage (indicating required field addition)..."
rg -n "mappedFields\.push" plugins/view-mapped/src/view-mapped.ts

echo "Searching for unit tests related to includeRequired functionality..."
rg -A 5 -B 5 "includeRequired" --glob "*spec.ts"

Please verify manually that:

  • The file plugins/view-mapped/src/view-mapped.ts contains the logic checking if (includeRequired) and pushing the required field into mappedFields.
  • Unit tests (typically in files matching *spec.ts) exist that exercise the functionality, confirming that unmapped required fields are retained when includeRequired is set to true.

Once you’ve confirmed these points, it’ll ensure that the change aligns with the PR objectives of retaining unmapped required fields.


Comment on lines +96 to +97
mappedFields.push(requiredFieldKey)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Prevent duplicate field keys in mappedFields array.

If a field is both mapped and required, it could be added to the mappedFields array twice, which might lead to unexpected behavior.

Add a check to prevent duplicates:

-  mappedFields.push(requiredFieldKey)
+  if (!mappedFields.includes(requiredFieldKey)) {
+    mappedFields.push(requiredFieldKey)
+  }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
mappedFields.push(requiredFieldKey)
}
if (!mappedFields.includes(requiredFieldKey)) {
mappedFields.push(requiredFieldKey)
}
}

Comment on lines +75 to +100
//If the includeRequired bool value is set to true, we will add all required fields to the mappedFields array
if (includeRequired) {
for (
let i = 0;
i < jobPlan.data.plan.unmappedDestinationFields.length;
i++
) {
for (
let j = 0;
j <
jobPlan.data.plan.unmappedDestinationFields[i].destinationField
.constraints?.length;
j++
) {
if (
jobPlan.data.plan.unmappedDestinationFields[i]
.destinationField.constraints[j].type === 'required'
) {
const requiredFieldKey =
jobPlan.data.plan.unmappedDestinationFields[i]
.destinationField.key
mappedFields.push(requiredFieldKey)
}
}
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Add null-safety checks for constraints property.

The current implementation might cause runtime errors if constraints is undefined for any field.

Add proper null-safety checks with optional chaining:

  if (includeRequired) {
    for (
      let i = 0;
      i < jobPlan.data.plan.unmappedDestinationFields.length;
      i++
    ) {
+     const constraints = jobPlan.data.plan.unmappedDestinationFields[i]?.destinationField?.constraints;
+     if (!constraints || constraints.length === 0) continue;
+     
      for (
        let j = 0;
-        j <
-        jobPlan.data.plan.unmappedDestinationFields[i].destinationField
-          .constraints?.length;
+        j < constraints.length;
        j++
      ) {
        if (
-          jobPlan.data.plan.unmappedDestinationFields[i]
-            .destinationField.constraints[j].type === 'required'
+          constraints[j].type === 'required'
        ) {
          const requiredFieldKey =
            jobPlan.data.plan.unmappedDestinationFields[i]
              .destinationField.key
          mappedFields.push(requiredFieldKey)
        }
      }
    }
  }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
//If the includeRequired bool value is set to true, we will add all required fields to the mappedFields array
if (includeRequired) {
for (
let i = 0;
i < jobPlan.data.plan.unmappedDestinationFields.length;
i++
) {
for (
let j = 0;
j <
jobPlan.data.plan.unmappedDestinationFields[i].destinationField
.constraints?.length;
j++
) {
if (
jobPlan.data.plan.unmappedDestinationFields[i]
.destinationField.constraints[j].type === 'required'
) {
const requiredFieldKey =
jobPlan.data.plan.unmappedDestinationFields[i]
.destinationField.key
mappedFields.push(requiredFieldKey)
}
}
}
}
//If the includeRequired bool value is set to true, we will add all required fields to the mappedFields array
if (includeRequired) {
for (
let i = 0;
i < jobPlan.data.plan.unmappedDestinationFields.length;
i++
) {
const constraints = jobPlan.data.plan.unmappedDestinationFields[i]?.destinationField?.constraints;
if (!constraints || constraints.length === 0) continue;
for (
let j = 0;
j < constraints.length;
j++
) {
if (constraints[j].type === 'required') {
const requiredFieldKey =
jobPlan.data.plan.unmappedDestinationFields[i]
.destinationField.key
mappedFields.push(requiredFieldKey)
}
}
}
}

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