-
Notifications
You must be signed in to change notification settings - Fork 71
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 6 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 implmentation 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| using System.Linq; | ||
| using System.Text.Json; | ||
| using System.Threading.Tasks; | ||
|
|
||
| using FluentAssertions; | ||
|
|
@@ -32,6 +33,33 @@ public async Task InitializeAsync() | |
|
|
||
| public Task DisposeAsync() => Task.CompletedTask; | ||
|
|
||
| [Fact] | ||
| public async Task TestJsonConverter() | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is this method asynchronous if there is no
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess it's just a habit of making everything async 😄 |
||
| { | ||
| MovieWithIntId[] movies = | ||
| { | ||
| new MovieWithIntId { Id = 1, Name = "Batman" }, | ||
| new MovieWithIntId { Id = 2, Name = "Reservoir Dogs" }, | ||
| new MovieWithIntId { Id = 3, Name = "Taxi Driver" }, | ||
| new MovieWithIntId { Id = 4, Name = "Interstellar" }, | ||
| new MovieWithIntId { Id = 5, Name = "Titanic" }, | ||
| }; ; | ||
| var formattable = movies | ||
| .Select(x => new DefaultFormattable<MovieWithIntId, Movie>(x, new Movie() | ||
| { | ||
| Id = x.Id.ToString(), | ||
| Genre = x.Genre, | ||
| Name = x.Name | ||
| })) | ||
| .Cast<IFormatContainer<MovieWithIntId, Movie>>(); | ||
|
|
||
| var elements = formattable.Select(x => JsonSerializer.SerializeToElement(x)).ToList(); | ||
| elements.Should().AllSatisfy(x => x.GetProperty("_formatted").Should().NotBeNull()); | ||
|
|
||
| var deserialized = elements.Select(x => JsonSerializer.Deserialize<IFormatContainer<MovieWithIntId, Movie>>(x)).ToList(); | ||
| deserialized.Should().AllSatisfy(x => x.Formatted.Should().NotBeNull()); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task BasicSearch() | ||
| { | ||
|
|
@@ -107,46 +135,46 @@ public async Task CustomSearchWithAttributesToHighlight() | |
| task.TaskUid.Should().BeGreaterOrEqualTo(0); | ||
| await _basicIndex.WaitForTaskAsync(task.TaskUid); | ||
|
|
||
| var movies = await _basicIndex.SearchAsync<FormattedMovie>( | ||
| var movies = await _basicIndex.SearchAsync<Movie, Movie>( | ||
| "man", | ||
| new SearchQuery { AttributesToHighlight = new string[] { "name" } }); | ||
| movies.Hits.Should().NotBeEmpty(); | ||
| movies.Hits.First().Id.Should().NotBeEmpty(); | ||
| movies.Hits.First().Name.Should().NotBeEmpty(); | ||
| movies.Hits.First().Genre.Should().NotBeEmpty(); | ||
| movies.Hits.First()._Formatted.Name.Should().NotBeEmpty(); | ||
| movies.Hits.First().Original.Id.Should().NotBeEmpty(); | ||
| movies.Hits.First().Original.Name.Should().NotBeEmpty(); | ||
| movies.Hits.First().Original.Genre.Should().NotBeEmpty(); | ||
| movies.Hits.First().Formatted.Name.Should().NotBeEmpty(); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task CustomSearchWithNoQuery() | ||
| { | ||
| var movies = await _basicIndex.SearchAsync<FormattedMovie>( | ||
| var movies = await _basicIndex.SearchAsync<Movie, Movie>( | ||
| null, | ||
| new SearchQuery { AttributesToHighlight = new string[] { "name" } }); | ||
| movies.Hits.Should().NotBeEmpty(); | ||
| movies.Hits.First().Id.Should().NotBeNull(); | ||
| movies.Hits.First().Name.Should().NotBeNull(); | ||
| movies.Hits.First()._Formatted.Id.Should().NotBeNull(); | ||
| movies.Hits.First()._Formatted.Name.Should().NotBeNull(); | ||
| movies.Hits.First().Original.Id.Should().NotBeNull(); | ||
| movies.Hits.First().Original.Name.Should().NotBeNull(); | ||
| movies.Hits.First().Formatted.Id.Should().NotBeNull(); | ||
| movies.Hits.First().Formatted.Name.Should().NotBeNull(); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task CustomSearchWithEmptyQuery() | ||
| { | ||
| var movies = await _basicIndex.SearchAsync<FormattedMovie>( | ||
| var movies = await _basicIndex.SearchAsync<Movie, Movie>( | ||
| string.Empty, | ||
| new SearchQuery { AttributesToHighlight = new string[] { "name" } }); | ||
| movies.Hits.Should().NotBeEmpty(); | ||
| movies.Hits.First().Id.Should().NotBeNull(); | ||
| movies.Hits.First().Name.Should().NotBeNull(); | ||
| movies.Hits.First()._Formatted.Id.Should().NotBeNull(); | ||
| movies.Hits.First()._Formatted.Name.Should().NotBeNull(); | ||
| movies.Hits.First().Original.Id.Should().NotBeNull(); | ||
| movies.Hits.First().Original.Name.Should().NotBeNull(); | ||
| movies.Hits.First().Formatted.Id.Should().NotBeNull(); | ||
| movies.Hits.First().Formatted.Name.Should().NotBeNull(); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task CustomSearchWithMultipleOptions() | ||
| { | ||
| var movies = await _basicIndex.SearchAsync<FormattedMovie>( | ||
| var movies = await _basicIndex.SearchAsync<Movie, Movie>( | ||
| "man", | ||
| new SearchQuery | ||
| { | ||
|
|
@@ -158,12 +186,12 @@ public async Task CustomSearchWithMultipleOptions() | |
|
|
||
| Assert.NotEmpty(movies.Hits); | ||
| Assert.Single(movies.Hits); | ||
| Assert.NotEmpty(firstHit.Name); | ||
| Assert.NotEmpty(firstHit.Id); | ||
| Assert.Null(firstHit.Genre); | ||
| Assert.NotEmpty(firstHit._Formatted.Name); | ||
| Assert.Equal("15", firstHit._Formatted.Id); | ||
| Assert.Null(firstHit._Formatted.Genre); | ||
| Assert.NotEmpty(firstHit.Original.Name); | ||
| Assert.NotEmpty(firstHit.Original.Id); | ||
| Assert.Null(firstHit.Original.Genre); | ||
| Assert.NotEmpty(firstHit.Formatted.Name); | ||
| Assert.Equal("15", firstHit.Formatted.Id); | ||
| Assert.Null(firstHit.Formatted.Genre); | ||
| } | ||
|
|
||
| [Fact] | ||
|
|
@@ -363,31 +391,31 @@ public async Task CustomSearchWithSort() | |
| [Fact] | ||
| public async Task CustomSearchWithCroppingParameters() | ||
| { | ||
| var movies = await _basicIndex.SearchAsync<FormattedMovie>( | ||
| var movies = await _basicIndex.SearchAsync<Movie, Movie>( | ||
| "man", | ||
| new SearchQuery { CropLength = 1, AttributesToCrop = new string[] { "*" } } | ||
| ); | ||
|
|
||
| Assert.NotEmpty(movies.Hits); | ||
| Assert.Equal("…Man", movies.Hits.First()._Formatted.Name); | ||
| Assert.Equal("…Man", movies.Hits.First().Formatted.Name); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task CustomSearchWithCropMarker() | ||
| { | ||
| var movies = await _basicIndex.SearchAsync<FormattedMovie>( | ||
| var movies = await _basicIndex.SearchAsync<Movie, Movie>( | ||
| "man", | ||
| new SearchQuery { CropLength = 1, AttributesToCrop = new string[] { "*" }, CropMarker = "[…] " } | ||
| ); | ||
|
|
||
| Assert.NotEmpty(movies.Hits); | ||
| Assert.Equal("[…] Man", movies.Hits.First()._Formatted.Name); | ||
| Assert.Equal("[…] Man", movies.Hits.First().Formatted.Name); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task CustomSearchWithCustomHighlightTags() | ||
| { | ||
| var movies = await _basicIndex.SearchAsync<FormattedMovie>( | ||
| var movies = await _basicIndex.SearchAsync<Movie, Movie>( | ||
| "man", | ||
| new SearchQuery | ||
| { | ||
|
|
@@ -398,7 +426,7 @@ public async Task CustomSearchWithCustomHighlightTags() | |
| ); | ||
|
|
||
| Assert.NotEmpty(movies.Hits); | ||
| Assert.Equal("Iron <mark>Man</mark>", movies.Hits.First()._Formatted.Name); | ||
| Assert.Equal("Iron <mark>Man</mark>", movies.Hits.First().Formatted.Name); | ||
| } | ||
|
|
||
| [Fact] | ||
|
|
||
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.
Uh oh!
There was an error while loading. Please reload this page.