-
Notifications
You must be signed in to change notification settings - Fork 8
refactor: simplify array handling and improve blob data validation in DataAdapter #73
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
Conversation
Summary of ChangesHello @jayant-dhingra-cb, 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 focuses on enhancing the Highlights
Using Gemini Code AssistThe 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
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 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
|
There was a problem hiding this comment.
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 successfully refactors array handling for conciseness and enhances blob data validation, making the code more robust. My review includes suggestions to further improve the code by eliminating duplication in index property processing, using more specific exceptions for clearer error handling, and simplifying data conversion logic for better readability and maintainability.
| val nestedMap = value["data"] as? Map<*, *> | ||
| ?: throw Exception("Error: Invalid blob object") | ||
| val contentType = nestedMap["contentType"] as? String | ||
| ?: throw Exception("Error: Missing blob contentType") | ||
| val rawList = nestedMap["data"] as? List<*> | ||
| ?: throw Exception("Error: Missing blob data array") | ||
| val doubleList = rawList.filterIsInstance<Double>() | ||
| .takeIf { it.size == rawList.size } | ||
| ?: throw Exception("Error: Invalid blob data format") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While the added validation is a great improvement for robustness, throwing generic Exception is not ideal. It's better practice to use more specific exception types like IllegalArgumentException to clearly indicate that the method received an invalid argument. This helps in better error handling and debugging. Additionally, including the key in the error message would provide more context for debugging.
| val nestedMap = value["data"] as? Map<*, *> | |
| ?: throw Exception("Error: Invalid blob object") | |
| val contentType = nestedMap["contentType"] as? String | |
| ?: throw Exception("Error: Missing blob contentType") | |
| val rawList = nestedMap["data"] as? List<*> | |
| ?: throw Exception("Error: Missing blob data array") | |
| val doubleList = rawList.filterIsInstance<Double>() | |
| .takeIf { it.size == rawList.size } | |
| ?: throw Exception("Error: Invalid blob data format") | |
| val nestedMap = value["data"] as? Map<*, *> | |
| ?: throw IllegalArgumentException("Error: Invalid blob object for key '$key'") | |
| val contentType = nestedMap["contentType"] as? String | |
| ?: throw IllegalArgumentException("Error: Missing blob contentType for key '$key'") | |
| val rawList = nestedMap["data"] as? List<*> | |
| ?: throw IllegalArgumentException("Error: Missing blob data array for key '$key'") | |
| val doubleList = rawList.filterIsInstance<Double>() | |
| .takeIf { it.size == rawList.size } | |
| ?: throw IllegalArgumentException("Error: Invalid blob data format for key '$key'") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this public API fun toMap(readableMap: ReadableMap): Map<String, Any>?
I am not sure that these will throw ever or should not ever throw? Would like to understand why would this throw is needed at RN level and not native JAK level.
On iOS, we do throw kCBLErrorMessageInvalidBlob when metadata is missing on get and kCBLErrorMessageBlobContentNull on set. I expect to similar for JAK as well.
| val intData = doubleList.map { it.toInt() }.toIntArray() | ||
| val data = ByteArray(intData.size) { i -> intData[i].toByte() } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The conversion from a list of Double to a ByteArray can be made more concise. You can chain the map and conversion operations directly to create the ByteArray without needing an intermediate IntArray.
| val intData = doubleList.map { it.toInt() }.toIntArray() | |
| val data = ByteArray(intData.size) { i -> intData[i].toByte() } | |
| val data = doubleList.map { it.toInt().toByte() }.toByteArray() |
fix: Handle null values in React Native bridge calls