-
Notifications
You must be signed in to change notification settings - Fork 62
make _formatted value user friendly :) #406
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
Open
ahmednfwela
wants to merge
7
commits into
meilisearch:main
Choose a base branch
from
Bdaya-Dev:support-formatted
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
5897b71
make _formatted value user friendly, also remove the FormattedMovie c…
ahmednfwela 72a5f3d
Implemented write to json
ahmednfwela c898eaf
Merge branch 'main' into support-formatted
ahmednfwela c55409c
Merge branch 'main' into support-formatted
ahmednfwela 9fb31d8
Merge
ahmednfwela 47a822a
Merge branch 'main' into support-formatted
ahmednfwela b16c4f2
Update src/Meilisearch/DefaultFormattable.cs
ahmednfwela File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
using System.Text.Json.Serialization; | ||
|
||
namespace Meilisearch | ||
{ | ||
/// <summary> | ||
/// The default implementation of <see cref="IFormatContainer{TOriginal,TFormatted}"/> | ||
/// </summary> | ||
/// <typeparam name="TOriginal"></typeparam> | ||
/// <typeparam name="TFormatted"></typeparam> | ||
public class DefaultFormattable<TOriginal, TFormatted> : IFormatContainer<TOriginal, TFormatted> | ||
{ | ||
/// <summary> | ||
/// Creates a formatted document | ||
/// </summary> | ||
/// <param name="original"></param> | ||
/// <param name="formatted"></param> | ||
public DefaultFormattable(TOriginal original, TFormatted formatted) | ||
{ | ||
Original = original; | ||
Formatted = formatted; | ||
} | ||
|
||
/// <summary> | ||
/// The original document | ||
/// </summary> | ||
public TOriginal Original { get; } | ||
|
||
/// <summary> | ||
/// The formatted document | ||
/// </summary> | ||
[JsonPropertyName("_formatted")] | ||
public TFormatted Formatted { get; } | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
using System.Text.Json.Serialization; | ||
|
||
namespace Meilisearch | ||
{ | ||
/// <summary> | ||
/// Used to receive document formatting information | ||
/// </summary> | ||
/// <typeparam name="TOriginal">Original data type</typeparam> | ||
/// <typeparam name="TFormatted">Formatted data type</typeparam> | ||
[JsonConverter(typeof(IFormatContainerJsonConverterFactory))] | ||
public interface IFormatContainer<TOriginal, TFormatted> | ||
{ | ||
/// <summary> | ||
/// The original result | ||
/// </summary> | ||
TOriginal Original { get; } | ||
|
||
/// <summary> | ||
/// The formatted result | ||
/// </summary> | ||
TFormatted Formatted { get; } | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
using System; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace Meilisearch | ||
{ | ||
public class IFormatContainerJsonConverterFactory : JsonConverterFactory | ||
{ | ||
public override bool CanConvert(Type typeToConvert) | ||
{ | ||
return typeToConvert.IsInterface | ||
&& typeToConvert.IsGenericType | ||
&& (typeToConvert.GetGenericTypeDefinition() == typeof(IFormatContainer<,>)); | ||
} | ||
|
||
public override JsonConverter CreateConverter( | ||
Type typeToConvert, | ||
JsonSerializerOptions options | ||
) | ||
{ | ||
var genericArgs = typeToConvert.GetGenericArguments(); | ||
var converterType = typeof(IFormatContainerJsonConverter<,>).MakeGenericType( | ||
genericArgs[0], | ||
genericArgs[1] | ||
); | ||
var converter = (JsonConverter)Activator.CreateInstance(converterType); | ||
return converter; | ||
} | ||
} | ||
|
||
public class IFormatContainerJsonConverter<TOriginal, TFormatted> | ||
: JsonConverter<IFormatContainer<TOriginal, TFormatted>> | ||
where TFormatted : class | ||
where TOriginal : class | ||
{ | ||
public override IFormatContainer<TOriginal, TFormatted> Read( | ||
ref Utf8JsonReader reader, | ||
Type typeToConvert, | ||
JsonSerializerOptions options | ||
) | ||
{ | ||
var document = JsonSerializer.Deserialize<JsonElement>(ref reader, options); | ||
var original = document.Deserialize<TOriginal>(options); | ||
|
||
if (document.TryGetProperty("_formatted", out var formattedElement)) | ||
{ | ||
var formatted = formattedElement.Deserialize<TFormatted>(options); | ||
return new DefaultFormattable<TOriginal, TFormatted>(original, formatted); | ||
} | ||
return new DefaultFormattable<TOriginal, TFormatted>(original, null); | ||
} | ||
|
||
public override void Write( | ||
Utf8JsonWriter writer, | ||
IFormatContainer<TOriginal, TFormatted> value, | ||
JsonSerializerOptions options | ||
) | ||
{ | ||
var serialized = JsonSerializer.SerializeToNode(value.Original, options).AsObject(); | ||
serialized["_formatted"] = JsonSerializer.SerializeToNode(value.Formatted, options); | ||
serialized.WriteTo(writer, options); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Why is this method asynchronous if there is no
await
inside?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.
I guess it's just a habit of making everything async 😄