Skip to content

Patch for Issue 21 - #22

Open
m-tarhon wants to merge 6 commits into
Couchbase-Ecosystem:mainfrom
couchbaselabs:main
Open

Patch for Issue 21 #22
m-tarhon wants to merge 6 commits into
Couchbase-Ecosystem:mainfrom
couchbaselabs:main

Conversation

@m-tarhon

Copy link
Copy Markdown

Allows the backend to ignore nils when creating fields

@gemini-code-assist

Copy link
Copy Markdown

Summary of Changes

Hello @m-tarhon, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request introduces a crucial improvement to the backend's data normalization process by allowing it to gracefully ignore nil values when creating fields. Specifically, the normalizeFieldData function has been refactored to filter out nil entries and to more robustly handle 'time' fields, ensuring that only valid, parsed time strings are included. This change enhances data integrity and prevents potential issues arising from unhandled nils in data structures.

Highlights

  • Nil Value Handling: The normalizeFieldData function now explicitly filters out nil values from the input slice, preventing them from being included in the processed field data.
  • Improved Time Field Normalization: The logic for 'time' fields has been enhanced to only attempt parsing for string values, appending valid time.Time objects. Non-string or nil entries are now gracefully skipped.
  • Dynamic Result Construction: The result slice is now initialized as empty and values are appended dynamically, ensuring that the final slice contains only valid, non-nil entries, rather than pre-allocating and potentially leaving empty slots.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Code Review

This pull request updates the normalizeFieldData function to ignore nil values, which aligns with the stated goal. However, the changes introduce two critical issues. First, the plugin will still panic on time-parsing errors, which can cause it to crash. Second, if all values are filtered out, the function returns an empty slice, leading to a panic in the calling createField function due to an out-of-bounds access. I've also included a medium-severity suggestion to log when non-string values are encountered for time fields instead of silently skipping them. Addressing these points will make the plugin more robust and prevent unexpected crashes.

@@ -264,18 +264,29 @@ func (d *CouchbaseDatasource) query(channel *string, query_data *QueryRequest) b
}

func normalizeFieldData(name string, values []interface{}) (string, []interface{}) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

critical

This function may now return an empty values slice if all original values are nil or invalid (for time fields). The calling function, createField, does not handle this case and will panic with an "index out of range" error on line 301 when it tries to access values[0].

You should add a check for an empty slice in createField immediately after the call to normalizeFieldData to prevent this panic.

Example fix in createField:

// in createField()
name, values = normalizeFieldData(name, values)
if len(values) == 0 {
    // The original check for vlen == 0 can be moved here.
    return data.NewField(name, nil, []bool{})
}
// It's now safe to access values[0]
switch v := values[0].(type) {
// ...

Comment on lines +277 to +279
} else {
panic(err)
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

critical

Using panic(err) can crash the plugin if an invalid time string is encountered in the data. It's safer to handle this error by logging it and skipping the problematic value. This makes the plugin more robust against malformed data.

Suggested change
} else {
panic(err)
}
} else {
log.DefaultLogger.Error("Failed to parse time string, skipping value", "value", timeStr, "error", err)
}

Comment on lines +274 to +281
}
// If not a string, skip (or handle as needed)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

When a value for a time field is not a string, it is silently skipped. This could hide data problems. Consider logging a warning when this happens so that users are aware of potential issues with their data schema or query results.

Suggested change
}
// If not a string, skip (or handle as needed)
} else {
log.DefaultLogger.Warn("Expected a string for time field, but got a different type; skipping value.", "value", v)
}

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.

1 participant