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

documents utils #545

Merged
merged 3 commits into from
Nov 27, 2024
Merged

documents utils #545

merged 3 commits into from
Nov 27, 2024

Conversation

pingu2k4
Copy link
Contributor

@pingu2k4 pingu2k4 commented Nov 27, 2024

Changes

  • Added utils for Documents response type and ability to get back Typed data if the user so wishes

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.

Is this description stale? Ask me to generate a new description by commenting /korbit-generate-pr-description

…icConverterTests.cs

Co-authored-by: codefactor-io[bot] <47775046+codefactor-io[bot]@users.noreply.github.com>
Copy link

Code Coverage

Package Line Rate Branch Rate Complexity Health
PinguApps.Appwrite.Client 100% 100% 131
PinguApps.Appwrite.Server 100% 100% 108
PinguApps.Appwrite.Shared 100% 100% 1317
Summary 100% (3371 / 3372) 100% (651 / 653) 1556

Copy link

Test Results

3 487 tests  +32   3 487 ✅ +32   22s ⏱️ +2s
    3 suites ± 0       0 💤 ± 0 
    3 files   ± 0       0 ❌ ± 0 

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.
PinguApps.Appwrite.Server.Tests.Clients.Users.UsersClientTests ‑ CreateUserJwt_ShouldReturnSuccess_WhenApiCallSucceeds(request: CreateUserJwtRequest { Duration = 1800, SessionId = "67461541003b0d29a878", UserId = "67461541003b0866e760", ValidationContext = None })
PinguApps.Appwrite.Server.Tests.Clients.Users.UsersClientTests ‑ CreateUserJwt_ShouldReturnSuccess_WhenApiCallSucceeds(request: CreateUserJwtRequest { Duration = null, SessionId = null, UserId = "67461541003af75200f5", ValidationContext = None })
PinguApps.Appwrite.Server.Tests.Clients.Users.UsersClientTests ‑ ListUserLogs_ShouldReturnSuccess_WhenApiCallSucceeds(request: ListUserLogsRequest { Queries = null, UserId = "67461541003b154027eb", ValidationContext = None })
PinguApps.Appwrite.Server.Tests.Clients.Users.UsersClientTests ‑ ListUserLogs_ShouldReturnSuccess_WhenApiCallSucceeds(request: ListUserLogsRequest { Queries = null, UserId = "67461541003b1a15370b", ValidationContext = None })
PinguApps.Appwrite.Server.Tests.Clients.Users.UsersClientTests ‑ ListUserTargets_ShouldReturnSuccess_WhenApiCallSucceeds(request: ListUserTargetsRequest { Queries = null, UserId = "67461541003b10a3a705", ValidationContext = None })
PinguApps.Appwrite.Server.Tests.Clients.Users.UsersClientTests ‑ ListUserTargets_ShouldReturnSuccess_WhenApiCallSucceeds(request: ListUserTargetsRequest { Queries = null, UserId = "67461541003b1fb115bb", ValidationContext = None })
PinguApps.Appwrite.Server.Tests.Clients.Users.UsersClientTests ‑ UpdateUserLabels_ShouldReturnSuccess_WhenApiCallSucceeds(request: UpdateUserLabelsRequest { Labels = ["label1", "label2"], UserId = "67461541003b10ef8138", ValidationContext = None })
PinguApps.Appwrite.Server.Tests.Clients.Users.UsersClientTests ‑ UpdateUserLabels_ShouldReturnSuccess_WhenApiCallSucceeds(request: UpdateUserLabelsRequest { Labels = ["label3", "label4"], UserId = "67461541003b1ec2ee41", ValidationContext = None })
PinguApps.Appwrite.Shared.Tests.Requests.Account.CreatePushTargetRequestTests ‑ IsValid_WithInvalidData_ReturnsFalse(request: CreatePushTargetRequest { Identifier = "", ProviderId = "provider123", TargetId = "6746153a003935de5f58", ValidationContext = None })
PinguApps.Appwrite.Shared.Tests.Requests.Account.CreatePushTargetRequestTests ‑ IsValid_WithInvalidData_ReturnsFalse(request: CreatePushTargetRequest { Identifier = null, ProviderId = "provider123", TargetId = "6746153a00393d71666d", ValidationContext = None })
…
PinguApps.Appwrite.Server.Tests.Clients.Users.UsersClientTests ‑ CreateUserJwt_ShouldReturnSuccess_WhenApiCallSucceeds(request: CreateUserJwtRequest { Duration = 1800, SessionId = "6747a63e00368fccbfc4", UserId = "6747a63e0036895612d8", ValidationContext = None })
PinguApps.Appwrite.Server.Tests.Clients.Users.UsersClientTests ‑ CreateUserJwt_ShouldReturnSuccess_WhenApiCallSucceeds(request: CreateUserJwtRequest { Duration = null, SessionId = null, UserId = "6747a63e00367b1e15eb", ValidationContext = None })
PinguApps.Appwrite.Server.Tests.Clients.Users.UsersClientTests ‑ ListUserLogs_ShouldReturnSuccess_WhenApiCallSucceeds(request: ListUserLogsRequest { Queries = null, UserId = "6747a63e00368e0f7fd0", ValidationContext = None })
PinguApps.Appwrite.Server.Tests.Clients.Users.UsersClientTests ‑ ListUserLogs_ShouldReturnSuccess_WhenApiCallSucceeds(request: ListUserLogsRequest { Queries = null, UserId = "6747a63e00368f52a29f", ValidationContext = None })
PinguApps.Appwrite.Server.Tests.Clients.Users.UsersClientTests ‑ ListUserTargets_ShouldReturnSuccess_WhenApiCallSucceeds(request: ListUserTargetsRequest { Queries = null, UserId = "6747a63e0036821ac059", ValidationContext = None })
PinguApps.Appwrite.Server.Tests.Clients.Users.UsersClientTests ‑ ListUserTargets_ShouldReturnSuccess_WhenApiCallSucceeds(request: ListUserTargetsRequest { Queries = null, UserId = "6747a63e0036824568c6", ValidationContext = None })
PinguApps.Appwrite.Server.Tests.Clients.Users.UsersClientTests ‑ UpdateUserLabels_ShouldReturnSuccess_WhenApiCallSucceeds(request: UpdateUserLabelsRequest { Labels = ["label1", "label2"], UserId = "6747a63e003682fa0b97", ValidationContext = None })
PinguApps.Appwrite.Server.Tests.Clients.Users.UsersClientTests ‑ UpdateUserLabels_ShouldReturnSuccess_WhenApiCallSucceeds(request: UpdateUserLabelsRequest { Labels = ["label3", "label4"], UserId = "6747a63e00368b4b8e57", ValidationContext = None })
PinguApps.Appwrite.Shared.Tests.Converters.DocumentGenericConverterFactoryTests ‑ CanConvert_DifferentGenericType_ReturnsFalse
PinguApps.Appwrite.Shared.Tests.Converters.DocumentGenericConverterFactoryTests ‑ CanConvert_DocumentGenericType_ReturnsTrue
…

@pingu2k4
Copy link
Contributor Author

/korbit-review

Copy link

@korbit-ai korbit-ai bot left a 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 replace reader.GetSingle() with reader.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}");
Copy link

Choose a reason for hiding this comment

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

category Error Handling

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.

Chat with Korbit by mentioning @korbit-ai, and give a 👍 or 👎 to help Korbit improve your reviews.


// Create a specific generic converter for Doocument<TData>
var converterType = typeof(DocumentGenericConverter<>).MakeGenericType(dataType);
return (JsonConverter?)Activator.CreateInstance(converterType);
Copy link

Choose a reason for hiding this comment

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

category Functionality

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.

Chat with Korbit by mentioning @korbit-ai, and give a 👍 or 👎 to help Korbit improve your reviews.

{
return longValue;
}
return reader.GetSingle();
Copy link

Choose a reason for hiding this comment

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

category Functionality

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.

Chat with Korbit by mentioning @korbit-ai, and give a 👍 or 👎 to help Korbit improve your reviews.

Comment on lines +239 to +246
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;
Copy link

Choose a reason for hiding this comment

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

category Functionality

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.

Chat with Korbit by mentioning @korbit-ai, and give a 👍 or 👎 to help Korbit improve your reviews.


// Create a specific generic converter for Doocument<TData>
var converterType = typeof(DocumentGenericConverter<>).MakeGenericType(dataType);
return (JsonConverter?)Activator.CreateInstance(converterType);
Copy link

Choose a reason for hiding this comment

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

category Error Handling

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.

Chat with Korbit by mentioning @korbit-ai, and give a 👍 or 👎 to help Korbit improve your reviews.


if (reader.TokenType is not JsonTokenType.StartObject)
{
throw new JsonException("Expected StartObject token");
Copy link

Choose a reason for hiding this comment

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

category Error Handling

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.

Chat with Korbit by mentioning @korbit-ai, and give a 👍 or 👎 to help Korbit improve your reviews.

/// <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(
Copy link

Choose a reason for hiding this comment

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

category Functionality

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.

Chat with Korbit by mentioning @korbit-ai, and give a 👍 or 👎 to help Korbit improve your reviews.

@pingu2k4 pingu2k4 merged commit ed37438 into dev Nov 27, 2024
2 of 3 checks passed
@pingu2k4 pingu2k4 deleted the 544-documents-utils branch November 27, 2024 23:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Task] Provide better utility where user has retrieved documents to bring out the data into a concrete type
1 participant