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

fix(server): use string as default field type in item import #1428

Merged
merged 2 commits into from
Mar 4, 2025

Conversation

yk-eukarya
Copy link
Contributor

@yk-eukarya yk-eukarya commented Mar 4, 2025

Summary by CodeRabbit

  • Bug Fixes

    • Enhanced the data import process to support an additional field and gracefully handle missing values. These improvements increase import accuracy and robustness, ensuring that newly imported data aligns with updated schema expectations for a smoother user experience.
  • Tests

    • Updated validations to reflect revised field expectations, verifying that the import process reliably processes all expected data elements.

@yk-eukarya yk-eukarya self-assigned this Mar 4, 2025
Copy link
Contributor

coderabbitai bot commented Mar 4, 2025

Walkthrough

This update enhances the integration model import tests by adding a new JSON field "text2" and updating the expected response. The integration test now expects four fields instead of three. Additionally, the internal logic in the schema guessing functions is refined: the loop in guessSchemaFields now processes keys with nil values, and fieldFrom only evaluates type when the value is not nil. No declarations of exported or public entities were altered.

Changes

Files Summary
server/e2e/.../integration_item_import_test.go Updated JSON input by adding "text2": null; modified expected field count from 3 to 4; assertions now check for "text2" with type "text".
server/internal/usecase/.../interactor/item_import.go Modified guessSchemaFields to process keys with nil values (only skipping "id"); updated fieldFrom to check for nil before type evaluation for more robust handling.

Sequence Diagram(s)

sequenceDiagram
    participant TestRunner as Integration Test
    participant Importer as Item Import Module
    participant SchemaAnalyzer as Schema Guessing

    TestRunner->>Importer: Send JSON input with new field "text2"
    Importer->>SchemaAnalyzer: Process each key (handle nil values, check for "id")
    SchemaAnalyzer-->>Importer: Return updated schema with field types (including "text2")
    Importer-->>TestRunner: Return import result with four fields
Loading

Possibly related PRs

Suggested labels

server

Poem

Hoppin' through code in the moonlight glow,
A new field dances where nil values flow.
With "text2" joining the rhythmic array,
Bugs hop away, in a playful display.
Cheers from this rabbit, in byte-sized delight,
🐇✨ Code evolves, shining ever so bright!

Warning

There were issues while running some tools. Please review the errors and either fix the tool’s configuration or disable the tool if it’s a critical failure.

🔧 golangci-lint (1.62.2)

Error: can't load config: the Go language version (go1.23) used to build golangci-lint is lower than the targeted Go version (1.24.0)
Failed executing command with error: can't load config: the Go language version (go1.23) used to build golangci-lint is lower than the targeted Go version (1.24.0)

✨ Finishing Touches
  • 📝 Generate Docstrings

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 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.

@github-actions github-actions bot added the server label Mar 4, 2025
Copy link

netlify bot commented Mar 4, 2025

Deploy Preview for reearth-cms canceled.

Name Link
🔨 Latest commit 550304f
🔍 Latest deploy log https://app.netlify.com/sites/reearth-cms/deploys/67c6f1d4ab692b00093f36e6

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

🧹 Nitpick comments (1)
server/internal/usecase/interactor/item_import.go (1)

531-540: Consider documenting null value handling in the CreateFieldParam struct.

The implementation now correctly handles null values, but the behavior isn't documented in the struct itself.

Consider adding a comment to the CreateFieldParam struct to document that null values default to the "text" type. This would make the behavior clearer for future developers:

 return interfaces.CreateFieldParam{
     ModelID:     nil,
     SchemaID:    sp.Schema().ID(),
-    Type:        t,
+    Type:        t, // defaults to value.TypeText for nil values
     Name:        k,
     Description: lo.ToPtr("auto created by json/geoJson import"),
     Key:         k,
     // type property is not supported in import
     TypeProperty: nil,
 }
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between f46c235 and 495823e.

📒 Files selected for processing (2)
  • server/e2e/integration_item_import_test.go (2 hunks)
  • server/internal/usecase/interactor/item_import.go (2 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (2)
  • GitHub Check: ci-server / test
  • GitHub Check: ci-server / lint
🔇 Additional comments (4)
server/e2e/integration_item_import_test.go (2)

216-216: Added null field test case, good enhancement!

This change adds a test case for handling null values in JSON imports by including "text2": null in the test data. This is an important edge case to test since real-world JSON often contains null fields.


269-272: Updated assertions to match schema changes, ensuring proper null field handling.

The test now correctly verifies that the text2 field with a null value is:

  1. Properly included in the schema (4 fields instead of 3)
  2. Added with key "text2" alongside other keys
  3. Assigned the default "text" type as expected

This ensures that null fields are properly handled during schema generation.

server/internal/usecase/interactor/item_import.go (2)

507-529: Added null safety check before type evaluation.

Good implementation of a null safety check that defaults to the text type for null values while still evaluating the appropriate type for non-null values.

This approach ensures that fields with null values are included in the schema with a sensible default type (text), while still correctly determining types for fields with actual values.


430-540:

✅ Verification successful

Verify that all field types are handled consistently.

While the current implementation handles null values by defaulting to text type, it would be good to verify that all field types have consistent behavior with null values.


🏁 Script executed:

#!/bin/bash
# Description: Check how different field types handle null values

# Find any other instances where null values might be handled differently
rg -A 5 "v\s*==\s*nil" --glob "*.go" server/internal/usecase/
rg -A 5 "nil\s*==" --glob "*.go" server/internal/usecase/

# Look for type conversion functions that might not handle null values properly
rg -A 10 "fieldFrom|guessType|getType" --glob "*.go" server/internal/usecase/

# Find places where schema fields are created to ensure null handling is consistent
rg -A 5 "NewField|CreateFieldParam" --glob "*.go" server/pkg/schema/

Length of output: 28253


Field Type Null Handling Verified
I've verified that null values and type deductions are handled consistently across the code. In the fieldFrom function, if the value (v) is nil the type defaults to text, and when non-nil, it correctly infers booleans or numbers based on the reflect kind. The null check in guessSchemaFields (skipping entries with v == nil) further confirms that the implementation intentionally avoids processing null values except when defaulting as designed. Additionally, related tests in the schema package confirm that field creation behaves as expected. No changes are needed here.

Comment on lines +476 to 478
if k == "id" {
continue
}
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

Fix for null field handling in schema detection.

This change allows the guessSchemaFields function to process keys with nil values by removing the condition that skipped them. Now it only skips the "id" field and processes all other fields, including those with null values.

This fix ensures that JSON fields with null values will be properly detected and included in the schema, providing better compatibility with diverse data sources.

@yk-eukarya yk-eukarya merged commit 63875d8 into main Mar 4, 2025
21 checks passed
@yk-eukarya yk-eukarya deleted the fix/string-as-defult-type branch March 4, 2025 12:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant