Skip to content

fix:non-string values#616

Merged
carlbrugger merged 1 commit intomainfrom
fix/non-string-field-titles
Sep 23, 2024
Merged

fix:non-string values#616
carlbrugger merged 1 commit intomainfrom
fix/non-string-field-titles

Conversation

@carlbrugger
Copy link
Contributor

@carlbrugger carlbrugger commented Sep 23, 2024

This PR closes https://github.com/FlatFilers/support-triage/issues/1583

For testing, create an excel sheet that has non-string values (date/integer) formatted as the appropriate non-string type in the header row. Uploading the file should not fail file extraction. This PR converts those non-string values to string.

Listener:

import type { FlatfileListener } from '@flatfile/listener'
import { ExcelExtractor } from '@flatfile/plugin-xlsx-extractor'

export default async function (listener: FlatfileListener) {
  listener.use(
    ExcelExtractor({
      rawNumbers: true,
      raw: true,
    })
  )
}

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Sep 23, 2024

Walkthrough

This release introduces a patch for the @flatfile/plugin-xlsx-extractor and @flatfile/util-extractor packages, focusing on resolving issues related to the handling of non-string values in the header row when using the raw and rawNumbers options. The changes include type checks to ensure that non-string values are preserved and processed correctly, enhancing the robustness of the extraction functionality.

Changes

File Change Summary
.changeset/wise-zoos-smoke.md Introduces a patch for @flatfile/plugin-xlsx-extractor and @flatfile/util-extractor to fix issues with non-string header values.
plugins/xlsx-extractor/src/utils.ts Adds a type check for newValue before calling the replace method to prevent runtime errors.
utils/extractor/src/index.ts Modifies keysToFields to ensure all keys are strings by converting non-string keys to strings.

Assessment against linked issues

Objective Addressed Explanation
Address extraction failure with non-string header values (#1583)

Possibly related PRs


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 (2)
.changeset/wise-zoos-smoke.md (1)

1-6: LGTM! Consider a minor clarification in the description.

The changeset file is well-structured and correctly indicates patch updates for both affected packages. The description accurately reflects the nature of the fix as described in the PR objectives.

Consider slightly expanding the description for added clarity:

- This release fixes an issue when using `raw` or `rawNumbers` with non-string values in the header row.
+ This release fixes an issue where the XLSX extractor fails when using `raw` or `rawNumbers` options with non-string values (such as dates or integers) in the header row.

This minor change provides more context about the specific component affected (XLSX extractor) and gives examples of non-string values, which aligns more closely with the detailed PR description.

plugins/xlsx-extractor/src/utils.ts (1)

Line range hint 1-3: Consider renaming the function for clarity

The current function name "prependNonUniqueHeaderColumns" might be slightly misleading as the function doesn't actually prepend anything. Instead, it appends a counter to duplicate values.

A more accurate name might be "deduplicateHeaderColumns" or "uniquifyHeaderColumns". For example:

- export function prependNonUniqueHeaderColumns(
+ export function deduplicateHeaderColumns(

This would better reflect the actual behavior of the function.

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between b5feb28 and 8320559.

Files selected for processing (3)
  • .changeset/wise-zoos-smoke.md (1 hunks)
  • plugins/xlsx-extractor/src/utils.ts (1 hunks)
  • utils/extractor/src/index.ts (1 hunks)
Additional comments not posted (1)
plugins/xlsx-extractor/src/utils.ts (1)

9-10: Approve: Robust handling of non-string values

The introduced type check for newValue effectively addresses the issue of handling non-string values in the header row. This change ensures that the replace method is only called on string values, preventing potential runtime errors and allowing non-string values (like dates or numbers) to be preserved as-is. This improvement aligns well with the PR objective of fixing XLSX file extraction failures for files containing non-string values in the header row.

Comment on lines 7 to 8
for (const [key, value] of Object.entries(record)) {
const newValue = value ? value : 'empty'
Copy link
Contributor

Choose a reason for hiding this comment

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

Consider revising empty value handling

The current implementation converts empty values to the string 'empty'. This approach might not be ideal in all scenarios, especially if distinguishing between truly empty values and the string 'empty' is important.

Consider allowing empty values to remain as empty strings or use a more distinct placeholder. For example:

- const newValue = value ? value : 'empty'
+ const newValue = value ?? ''  // or use a symbol like Symbol('empty')

This change would preserve the emptiness of the original value while still allowing for duplicate detection.

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
for (const [key, value] of Object.entries(record)) {
const newValue = value ? value : 'empty'
for (const [key, value] of Object.entries(record)) {
const newValue = value ?? '' // or use a symbol like Symbol('empty')

Comment on lines +240 to +242
if (typeof key !== 'string') {
key = String(key)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Approve changes with a minor suggestion for improvement

The added type check and string conversion effectively address the issue of non-string values in the header row, which aligns with the PR objectives. This change should resolve the XLSX file extraction failures mentioned in the linked issue #1583.

Consider adding a more robust type conversion to handle potential edge cases:

 if (typeof key !== 'string') {
-  key = String(key)
+  key = typeof key === 'object' && key !== null ? JSON.stringify(key) : String(key)
 }

This modification ensures that complex objects are properly stringified, providing more meaningful header values in such cases.

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 (typeof key !== 'string') {
key = String(key)
}
if (typeof key !== 'string') {
key = typeof key === 'object' && key !== null ? JSON.stringify(key) : String(key)
}

Tip

Codebase Verification

Potential Issues Found with Non-String Key Processing

The verification revealed multiple instances where keys are processed without explicit type checking or conversion. It's recommended to review these areas to ensure consistent and robust handling of non-string keys:

  • utils/extractor/src/index.ts
    • key.trim().replace(/%/g, '_PERCENT_').replace(/\$/g, '_DOLLAR_')
    • key = key.trim()
  • utils/common/src/simple.records.ts
    • Multiple uses of Object.entries(obj) and related methods
  • plugins/xlsx-extractor/src/parser.ts
    • Various Object.keys(workbook.Sheets) and similar patterns
  • plugins/record-hook/src/record.translator.ts
    • Uses of Object.entries(record.values) and related methods
  • ...and several other files as identified in the shell script output.

Ensure that all key manipulations include appropriate type checks or conversions to handle non-string values safely.

Analysis chain

Suggest comprehensive testing

The changes look good and address the issue effectively. To ensure robustness, it would be beneficial to conduct comprehensive testing with various types of non-string header values (e.g., dates, numbers, booleans, and potentially nested objects).

To assist with verification, you can use the following script to check for any remaining instances where non-string keys might be processed without conversion:

This script will help identify areas in the codebase where keys are being processed, potentially without proper type checking. Review the results to ensure that appropriate safeguards are in place throughout the project.


Update Verification Script to Correctly Target TypeScript Files

The previous verification script encountered an issue recognizing the typescript file type. To ensure accurate searching, please use the following updated script that targets .ts and .tsx files explicitly:

This script explicitly searches for .ts and .tsx files, ensuring that TypeScript files are correctly targeted. Please execute this updated script and review the results to verify that all potential non-string key processing instances are appropriately handled.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check for potential non-string key processing in the codebase

# Test 1: Search for direct key access or manipulation without type checking
echo "Test 1: Searching for potential unchecked key access..."
rg --type typescript --type javascript 'key\s*\.\s*(trim|toLowerCase|toUpperCase|replace|substring|slice)' -g '!*test*'

# Test 2: Search for array or object methods that might assume string keys
echo "Test 2: Searching for methods that might assume string keys..."
rg --type typescript --type javascript '(Object\.keys|Object\.entries|Object\.values|for\s*\(\s*\w+\s+in\s)' -g '!*test*'

echo "Review the results to ensure proper type checking is in place where necessary."

Length of output: 738


Script:

#!/bin/bash
# Description: Check for potential non-string key processing in the codebase

# Test 1: Search for direct key access or manipulation without type checking
echo "Test 1: Searching for potential unchecked key access..."
rg --glob '*.ts' --glob '*.tsx' 'key\s*\.\s*(trim|toLowerCase|toUpperCase|replace|substring|slice)' -g '!*test*'

# Test 2: Search for array or object methods that might assume string keys
echo "Test 2: Searching for methods that might assume string keys..."
rg --glob '*.ts' --glob '*.tsx' '(Object\.keys|Object\.entries|Object\.values|for\s*\(\s*\w+\s+in\s)' -g '!*test*'

echo "Review the results to ensure proper type checking is in place where necessary."

Length of output: 4657

Copy link
Contributor

@damonbanks damonbanks left a comment

Choose a reason for hiding this comment

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

👍

@carlbrugger carlbrugger merged commit 7667dd0 into main Sep 23, 2024
@carlbrugger carlbrugger deleted the fix/non-string-field-titles branch September 23, 2024 17:49
This was referenced Sep 23, 2024
@coderabbitai coderabbitai bot mentioned this pull request Oct 24, 2024
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