-
Notifications
You must be signed in to change notification settings - Fork 2
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
documents utils #545
documents utils #545
Conversation
…JsonConverter to be completed
tests/PinguApps.Appwrite.Shared.Tests/Converters/DocumentGenericConverterTests.cs
Outdated
Show resolved
Hide resolved
…icConverterTests.cs Co-authored-by: codefactor-io[bot] <47775046+codefactor-io[bot]@users.noreply.github.com>
Test Results3 487 tests +32 3 487 ✅ +32 22s ⏱️ +2s Results for commit 4de2e4e. ± Comparison against base commit c85892a. This pull request removes 340 and adds 372 tests. Note that renamed tests count towards both.
|
/korbit-review |
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.
Review Summary by Korbit AI
Code Execution Comments
- Wrap
Activator.CreateInstance
calls in try-catch blocks and replacereader.GetSingle()
withreader.GetDouble()
for robustness and precision. - Enhance
WriteValue
method by supporting additional numeric types (decimal
,float
,byte
) for comprehensive data handling.
Code Health Comments
- Incorporate logging for exceptions to aid debugging and illustrate extending
DocumentBase
class with examples or comments.
Files scanned
File Path | Reviewed |
---|---|
src/PinguApps.Appwrite.Shared/Converters/DocumentGenericConverterFactory.cs | ✅ |
src/PinguApps.Appwrite.Shared/Responses/DocumentGeneric.cs | ✅ |
src/PinguApps.Appwrite.Shared/Responses/DocumentBase.cs | ✅ |
src/PinguApps.Appwrite.Shared/Responses/Document.cs | ✅ |
src/PinguApps.Appwrite.Shared/Converters/DocumentGenericConverter.cs | ✅ |
Explore our documentation to understand the languages and file types we support and the files we ignore.
Need a new review? Comment
/korbit-review
on this PR and I'll review your latest changes.Korbit Guide: Usage and Customization
Interacting with Korbit
- You can manually ask Korbit to review your PR using the
/korbit-review
command in a comment at the root of your PR.- You can ask Korbit to generate a new PR description using the
/korbit-generate-pr-description
command in any comment on your PR.- Too many Korbit comments? I can resolve all my comment threads if you use the
/korbit-resolve
command in any comment on your PR.- Chat with Korbit on issues we post by tagging @korbit-ai in your reply.
- Help train Korbit to improve your reviews by giving a 👍 or 👎 on the comments Korbit posts.
Customizing Korbit
- Check out our docs on how you can make Korbit work best for you and your team.
- Customize Korbit for your organization through the Korbit Console.
Current Korbit Configuration
General Settings
Setting Value Review Schedule Automatic excluding drafts Max Issue Count 10 Automatic PR Descriptions ✅ Issue Categories
Category Enabled Naming ✅ Database Operations ✅ Documentation ✅ Logging ✅ Error Handling ✅ Systems and Environment ✅ Objects and Data Structures ✅ Readability and Maintainability ✅ Asynchronous Processing ✅ Design Patterns ✅ Third-Party Libraries ✅ Performance ✅ Security ✅ Functionality ✅ Feedback and Support
return ReadObject(ref reader, options); | ||
|
||
default: | ||
throw new JsonException($"Unsupported token type: {reader.TokenType}"); |
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.
Consider logging the error before throwing the exception.
Tell me more
Before throwing this exception, consider logging the error. This can provide valuable information for debugging and monitoring. You could add a logging statement like this:
logger.LogError($"Unsupported token type encountered: {reader.TokenType}");
throw new JsonException($"Unsupported token type: {reader.TokenType}");
Ensure you have a proper logging mechanism in place.
|
||
// Create a specific generic converter for Doocument<TData> | ||
var converterType = typeof(DocumentGenericConverter<>).MakeGenericType(dataType); | ||
return (JsonConverter?)Activator.CreateInstance(converterType); |
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.
Include error handling in the CreateConverter
method.
Tell me more
The CreateConverter
method currently doesn't include any error handling. Consider wrapping the Activator.CreateInstance
call in a try-catch block to handle potential exceptions that might occur during the creation of the converter instance. This will improve the robustness of the code and prevent unexpected crashes.
{ | ||
return longValue; | ||
} | ||
return reader.GetSingle(); |
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.
Use reader.GetDouble() instead of reader.GetSingle() to avoid precision loss.
Tell me more
In the ReadValue method, you're currently using reader.GetSingle() for non-integer numeric values. This may lead to precision loss for decimal numbers. Consider using reader.GetDouble() instead to support a wider range of decimal values with higher precision. This change will ensure that the JSON deserialization process accurately represents decimal numbers in the resulting object.
case JsonValueKind.Number: | ||
if (element.TryGetInt32(out var intValue)) | ||
writer.WriteNumberValue(intValue); | ||
else if (element.TryGetInt64(out var longValue)) | ||
writer.WriteNumberValue(longValue); | ||
else if (element.TryGetDouble(out var doubleValue)) | ||
writer.WriteNumberValue(doubleValue); | ||
break; |
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.
Consider adding support for additional numeric types.
Tell me more
In the WriteValue method, you're currently handling int, long, and double numeric types. However, you might want to consider adding support for other numeric types such as decimal, float, and byte to ensure comprehensive handling of various numeric data. This would involve adding additional checks and using the appropriate writer methods for these types. For example, you could add else if (element.TryGetDecimal(out var decimalValue)) writer.WriteNumberValue(decimalValue);
for handling decimal values.
|
||
// Create a specific generic converter for Doocument<TData> | ||
var converterType = typeof(DocumentGenericConverter<>).MakeGenericType(dataType); | ||
return (JsonConverter?)Activator.CreateInstance(converterType); |
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.
Wrap Activator.CreateInstance() in a try-catch block.
Tell me more
The Activator.CreateInstance()
method can throw various exceptions, such as MissingMethodException
, TargetInvocationException
, or InvalidOperationException
. It's recommended to wrap this call in a try-catch block to handle potential exceptions gracefully. This will improve the robustness of your code and provide better error handling.
|
||
if (reader.TokenType is not JsonTokenType.StartObject) | ||
{ | ||
throw new JsonException("Expected StartObject token"); |
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.
Log exceptions before throwing.
Tell me more
Consider logging the exception before throwing it. This can help with debugging and monitoring. You could add a logging statement before throwing the exception, like this:
logger.LogError("Expected StartObject token");
throw new JsonException("Expected StartObject token");
Make sure to inject and use an appropriate logging mechanism.
/// <param name="UpdatedAt">Document update date in ISO 8601 format</param> | ||
/// <param name="Permissions">Document permissions. <see href="https://appwrite.io/docs/permissions">Learn more about permissions</see></param> | ||
[JsonConverter(typeof(DocumentConverter))] | ||
public abstract record DocumentBase( |
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.
Clarify usage of the abstract DocumentBase class.
Tell me more
The DocumentBase class is marked as abstract, which is good for defining a base structure. However, it's not clear how this class should be extended or used in practice. Consider adding a comment or example showing how to create a concrete implementation of this class. This will help other developers understand how to properly use this base class in their implementations.
Changes
Issue
#544
Categorise the PR
feature
bug
docs
security
meta
patch
minor
major
Description by Korbit AI
What change is being made?
Implement a generic JSON document converter for Appwrite documents by adding
DocumentGenericConverter
,DocumentGenericConverterFactory
, and associated tests to facilitate serialization and deserialization of documents with dynamic data types.Why are these changes being made?
These changes introduce a more flexible approach to handling Appwrite documents by allowing them to carry arbitrary data structures. This enhances the system's ability to process different schema dynamically, leveraging the power of generics for JSON serialization and deserialization, while also promoting code reuse and maintainability. Adding comprehensive tests ensures reliability of these new converter functionalities.