-
Notifications
You must be signed in to change notification settings - Fork 145
Add render template support #975
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
MovinduJay
wants to merge
3
commits into
meilisearch:main
Choose a base branch
from
MovinduJay:add-render-template-route
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
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
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
30 changes: 30 additions & 0 deletions
30
src/main/java/com/meilisearch/sdk/RenderTemplateRequest.java
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,30 @@ | ||
| package com.meilisearch.sdk; | ||
|
|
||
| import java.util.Map; | ||
| import lombok.AccessLevel; | ||
| import lombok.AllArgsConstructor; | ||
| import lombok.Builder; | ||
| import lombok.Getter; | ||
| import lombok.Setter; | ||
| import lombok.experimental.Accessors; | ||
| import org.json.JSONObject; | ||
|
|
||
| @Builder | ||
| @AllArgsConstructor(access = AccessLevel.PACKAGE) | ||
| @Getter | ||
| @Setter | ||
| @Accessors(chain = true) | ||
| public class RenderTemplateRequest { | ||
| private Map<String, Object> template; | ||
| private Map<String, Object> input; | ||
|
|
||
| public RenderTemplateRequest() {} | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| JSONObject jsonObject = new JSONObject(); | ||
| jsonObject.put("template", this.template); | ||
| jsonObject.putOpt("input", this.input); | ||
| return jsonObject.toString(); | ||
| } | ||
| } |
15 changes: 15 additions & 0 deletions
15
src/main/java/com/meilisearch/sdk/model/RenderTemplateResult.java
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,15 @@ | ||
| package com.meilisearch.sdk.model; | ||
|
|
||
| import lombok.Getter; | ||
| import lombok.Setter; | ||
| import lombok.ToString; | ||
|
|
||
| @Getter | ||
| @Setter | ||
| @ToString | ||
| public class RenderTemplateResult { | ||
| private Object template; | ||
| private Object rendered; | ||
|
|
||
| public RenderTemplateResult() {} | ||
| } |
148 changes: 148 additions & 0 deletions
148
src/test/java/com/meilisearch/sdk/RenderTemplateRequestTest.java
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,148 @@ | ||
| package com.meilisearch.sdk; | ||
|
|
||
| import static org.hamcrest.MatcherAssert.assertThat; | ||
| import static org.hamcrest.Matchers.equalTo; | ||
| import static org.hamcrest.Matchers.is; | ||
|
|
||
| import com.meilisearch.sdk.http.request.BasicRequest; | ||
| import com.meilisearch.sdk.http.request.HttpMethod; | ||
| import com.meilisearch.sdk.http.request.HttpRequest; | ||
| import com.meilisearch.sdk.json.GsonJsonHandler; | ||
| import java.util.Collections; | ||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
| import org.json.JSONObject; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| class RenderTemplateRequestTest { | ||
| @Test | ||
| void toStringWithTemplateAndInput() { | ||
| Map<String, Object> template = new HashMap<>(); | ||
| template.put("kind", "inlineDocumentTemplate"); | ||
| template.put("inline", "Product {{doc.name}} is a {{doc.color}} {{doc.category}}."); | ||
|
|
||
| Map<String, Object> product = new HashMap<>(); | ||
| product.put("name", "Nike Air Max"); | ||
| product.put("color", "Black"); | ||
| product.put("category", "Shoes"); | ||
|
|
||
| Map<String, Object> input = new HashMap<>(); | ||
| input.put("kind", "inlineDocument"); | ||
| input.put("inline", product); | ||
|
|
||
| RenderTemplateRequest request = | ||
| new RenderTemplateRequest().setTemplate(template).setInput(input); | ||
|
|
||
| JSONObject json = new JSONObject(request.toString()); | ||
|
|
||
| assertThat( | ||
| json.getJSONObject("template").getString("kind"), | ||
| is(equalTo("inlineDocumentTemplate"))); | ||
| assertThat( | ||
| json.getJSONObject("template").getString("inline"), | ||
| is(equalTo("Product {{doc.name}} is a {{doc.color}} {{doc.category}}."))); | ||
|
|
||
| assertThat(json.getJSONObject("input").getString("kind"), is(equalTo("inlineDocument"))); | ||
| assertThat( | ||
| json.getJSONObject("input").getJSONObject("inline").getString("name"), | ||
| is(equalTo("Nike Air Max"))); | ||
| assertThat( | ||
| json.getJSONObject("input").getJSONObject("inline").getString("color"), | ||
| is(equalTo("Black"))); | ||
| assertThat( | ||
| json.getJSONObject("input").getJSONObject("inline").getString("category"), | ||
| is(equalTo("Shoes"))); | ||
| } | ||
|
|
||
| @Test | ||
| void toStringWithoutInput() { | ||
| Map<String, Object> template = new HashMap<>(); | ||
| template.put("kind", "inlineDocumentTemplate"); | ||
| template.put("inline", "Hello {{doc.name}}"); | ||
|
|
||
| RenderTemplateRequest request = new RenderTemplateRequest().setTemplate(template); | ||
|
|
||
| JSONObject json = new JSONObject(request.toString()); | ||
|
|
||
| assertThat(json.has("template"), is(true)); | ||
| assertThat(json.has("input"), is(false)); | ||
| assertThat( | ||
| json.getJSONObject("template").getString("kind"), | ||
| is(equalTo("inlineDocumentTemplate"))); | ||
| assertThat( | ||
| json.getJSONObject("template").getString("inline"), | ||
| is(equalTo("Hello {{doc.name}}"))); | ||
| } | ||
|
|
||
| @Test | ||
| void requestSerializerWithTemplateAndInput() { | ||
| Map<String, Object> template = new HashMap<>(); | ||
| template.put("kind", "inlineDocumentTemplate"); | ||
| template.put("inline", "Product {{doc.name}} is a {{doc.color}} {{doc.category}}."); | ||
|
|
||
| Map<String, Object> product = new HashMap<>(); | ||
| product.put("name", "Nike Air Max"); | ||
| product.put("color", "Black"); | ||
| product.put("category", "Shoes"); | ||
|
|
||
| Map<String, Object> input = new HashMap<>(); | ||
| input.put("kind", "inlineDocument"); | ||
| input.put("inline", product); | ||
|
|
||
| RenderTemplateRequest request = | ||
| new RenderTemplateRequest().setTemplate(template).setInput(input); | ||
|
|
||
| BasicRequest basicRequest = new BasicRequest(new GsonJsonHandler()); | ||
| HttpRequest httpRequest = | ||
| basicRequest.create( | ||
| HttpMethod.POST, "/render-template", Collections.emptyMap(), request); | ||
|
|
||
| JSONObject json = new JSONObject(httpRequest.getContent()); | ||
|
|
||
| assertThat(httpRequest.getMethod(), is(equalTo(HttpMethod.POST))); | ||
| assertThat(httpRequest.getPath(), is(equalTo("/render-template"))); | ||
| assertThat( | ||
| json.getJSONObject("template").getString("kind"), | ||
| is(equalTo("inlineDocumentTemplate"))); | ||
| assertThat(json.getJSONObject("input").getString("kind"), is(equalTo("inlineDocument"))); | ||
| assertThat( | ||
| json.getJSONObject("input").getJSONObject("inline").getString("name"), | ||
| is(equalTo("Nike Air Max"))); | ||
| } | ||
|
|
||
| @Test | ||
| void requestSerializerWithoutInput() { | ||
| Map<String, Object> template = new HashMap<>(); | ||
| template.put("kind", "inlineDocumentTemplate"); | ||
| template.put("inline", "Hello {{doc.name}}"); | ||
|
|
||
| RenderTemplateRequest request = new RenderTemplateRequest().setTemplate(template); | ||
|
|
||
| BasicRequest basicRequest = new BasicRequest(new GsonJsonHandler()); | ||
| HttpRequest httpRequest = | ||
| basicRequest.create( | ||
| HttpMethod.POST, "/render-template", Collections.emptyMap(), request); | ||
|
|
||
| JSONObject json = new JSONObject(httpRequest.getContent()); | ||
|
|
||
| assertThat(httpRequest.getMethod(), is(equalTo(HttpMethod.POST))); | ||
| assertThat(httpRequest.getPath(), is(equalTo("/render-template"))); | ||
| assertThat(json.has("template"), is(true)); | ||
| assertThat(json.has("input"), is(false)); | ||
| } | ||
|
|
||
| @Test | ||
| void gettersAndSetters() { | ||
| Map<String, Object> template = new HashMap<>(); | ||
| template.put("kind", "inlineDocumentTemplate"); | ||
|
|
||
| Map<String, Object> input = new HashMap<>(); | ||
| input.put("kind", "inlineDocument"); | ||
|
|
||
| RenderTemplateRequest request = | ||
| RenderTemplateRequest.builder().template(template).input(input).build(); | ||
|
|
||
| assertThat(request.getTemplate(), is(equalTo(template))); | ||
| assertThat(request.getInput(), is(equalTo(input))); | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| } | ||
48 changes: 48 additions & 0 deletions
48
src/test/java/com/meilisearch/sdk/model/RenderTemplateResultTest.java
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,48 @@ | ||
| package com.meilisearch.sdk.model; | ||
|
|
||
| import static org.hamcrest.MatcherAssert.assertThat; | ||
| import static org.hamcrest.Matchers.equalTo; | ||
| import static org.hamcrest.Matchers.is; | ||
|
|
||
| import com.meilisearch.sdk.json.JacksonJsonHandler; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| class RenderTemplateResultTest { | ||
| @Test | ||
| void deserializesWithJacksonJsonHandler() throws Exception { | ||
| RenderTemplateResult result = | ||
| new JacksonJsonHandler() | ||
| .decode( | ||
| "{\"template\":\"Hello {{doc.name}}\",\"rendered\":\"Hello Movindu\"}", | ||
| RenderTemplateResult.class); | ||
|
|
||
| assertThat(result.getTemplate(), is(equalTo("Hello {{doc.name}}"))); | ||
| assertThat(result.getRendered(), is(equalTo("Hello Movindu"))); | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| } | ||
|
|
||
| @Test | ||
| void deserializesStructuredRenderedValueWithJacksonJsonHandler() throws Exception { | ||
| RenderTemplateResult result = | ||
| new JacksonJsonHandler() | ||
| .decode( | ||
| "{" | ||
| + "\"template\":\"{{ doc }}\"," | ||
| + "\"rendered\":{" | ||
| + "\"title\":\"Ariel\"," | ||
| + "\"tags\":[\"movie\",\"classic\"]" | ||
| + "}" | ||
| + "}", | ||
| RenderTemplateResult.class); | ||
|
|
||
| assertThat(result.getTemplate(), is(equalTo("{{ doc }}"))); | ||
|
|
||
| Map<?, ?> rendered = (Map<?, ?>) result.getRendered(); | ||
| assertThat(rendered.get("title"), is(equalTo("Ariel"))); | ||
|
|
||
| List<?> tags = (List<?>) rendered.get("tags"); | ||
| assertThat(tags.get(0), is(equalTo("movie"))); | ||
| assertThat(tags.get(1), is(equalTo("classic"))); | ||
| } | ||
| } | ||
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.