-
Notifications
You must be signed in to change notification settings - Fork 124
feat: add render template API support #922
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
wayosu
wants to merge
10
commits into
meilisearch:main
Choose a base branch
from
wayosu:feat/render-template
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 1 commit
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
f5e0c98
feat: add render template API support
wayosu 9f80a19
fix: address code review feedback
wayosu 4eb181b
style: fix phpdoc alignment in TemplateRenderQuery
wayosu 6a11352
feat: support flexible template/input kinds and null input in Templat…
wayosu 2167056
test: update integration tests for new array-based API and null input…
wayosu a30d487
docs: update render-template code sample to new array-based API
wayosu afaec4b
fix: use broader return type for toArray() to match variable shapes
wayosu 666ca08
chore: remove .hermes plan file and add to .gitignore
wayosu 60e0753
chore: revert .gitignore change — use global gitignore instead
wayosu 63f4bff
refactor: use constructor and precise PHPStan types in TemplateRender…
wayosu 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
Some comments aren't visible on the classic Files Changed page.
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
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,55 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Meilisearch\Contracts; | ||
|
|
||
| class TemplateRenderQuery | ||
| { | ||
| /** | ||
| * @var array{kind: string, inline: string} | ||
| */ | ||
| private array $template = ['kind' => '', 'inline' => '']; | ||
|
Collaborator
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'm not sure that it's useful to initialize this query with a wrong state, because |
||
|
|
||
| /** | ||
| * @var array{kind: string, inline: mixed} | ||
| */ | ||
| private array $input = ['kind' => '', 'inline' => null]; | ||
|
coderabbitai[bot] marked this conversation as resolved.
Outdated
|
||
|
|
||
| /** | ||
| * Set the template to render. | ||
| * | ||
| * @param string $kind e.g. 'inlineDocumentTemplate' | ||
| * @param string $inline the template string | ||
| */ | ||
| public function setTemplate(string $kind, string $inline): self | ||
| { | ||
| $this->template = ['kind' => $kind, 'inline' => $inline]; | ||
|
|
||
| return $this; | ||
| } | ||
|
|
||
| /** | ||
| * Set the input document for template rendering. | ||
| * | ||
| * @param string $kind e.g. 'inlineDocument' | ||
| * @param array|object|null $inline the document data | ||
| */ | ||
| public function setInput(string $kind, mixed $inline): self | ||
| { | ||
| $this->input = ['kind' => $kind, 'inline' => $inline]; | ||
|
|
||
| return $this; | ||
| } | ||
|
|
||
| /** | ||
| * @return array{template: array{kind: string, inline: string}, input: array{kind: string, inline: mixed}} | ||
| */ | ||
| public function toArray(): array | ||
| { | ||
| return [ | ||
| 'template' => $this->template, | ||
| 'input' => $this->input, | ||
| ]; | ||
| } | ||
| } | ||
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 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Meilisearch\Contracts; | ||
|
|
||
| class TemplateRenderResult | ||
| { | ||
| private string $template; | ||
| private ?string $rendered; | ||
|
|
||
| /** | ||
| * @param array{template: string, rendered: string|null} $data | ||
| */ | ||
| public function __construct(array $data) | ||
| { | ||
| $this->template = $data['template']; | ||
| $this->rendered = $data['rendered']; | ||
| } | ||
|
|
||
| public function getTemplate(): string | ||
| { | ||
| return $this->template; | ||
| } | ||
|
|
||
| public function getRendered(): ?string | ||
| { | ||
| return $this->rendered; | ||
| } | ||
| } |
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,19 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Meilisearch\Endpoints\Delegates; | ||
|
|
||
| use Meilisearch\Contracts\TemplateRenderQuery; | ||
| use Meilisearch\Contracts\TemplateRenderResult; | ||
| use Meilisearch\Endpoints\Templates; | ||
|
|
||
| trait HandlesTemplates | ||
| { | ||
| protected Templates $templates; | ||
|
|
||
| public function renderTemplate(TemplateRenderQuery $query): TemplateRenderResult | ||
| { | ||
| return $this->templates->render($query); | ||
| } | ||
| } |
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,21 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Meilisearch\Endpoints; | ||
|
|
||
| use Meilisearch\Contracts\Endpoint; | ||
| use Meilisearch\Contracts\TemplateRenderQuery; | ||
| use Meilisearch\Contracts\TemplateRenderResult; | ||
|
|
||
| final class Templates extends Endpoint | ||
| { | ||
| protected const PATH = '/render-template'; | ||
|
|
||
| public function render(TemplateRenderQuery $query): TemplateRenderResult | ||
| { | ||
| $response = $this->http->post(self::PATH, $query->toArray()); | ||
|
|
||
| return new TemplateRenderResult($response); | ||
| } | ||
| } |
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,32 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Tests\Endpoints; | ||
|
|
||
| use Meilisearch\Contracts\TemplateRenderQuery; | ||
| use Meilisearch\Http\Client; | ||
| use Tests\TestCase; | ||
|
|
||
| final class TemplatesTest extends TestCase | ||
| { | ||
| protected function setUp(): void | ||
| { | ||
| parent::setUp(); | ||
|
|
||
| $http = new Client($this->host, getenv('MEILISEARCH_API_KEY')); | ||
| $http->patch('/experimental-features', ['renderRoute' => true]); | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| public function testCanRenderInlineTemplate(): void | ||
| { | ||
| $query = (new TemplateRenderQuery()) | ||
| ->setTemplate('inlineDocumentTemplate', '{{ doc.breed }} called {{ doc.name }}') | ||
| ->setInput('inlineDocument', ['breed' => 'Jack Russell', 'name' => 'Iko']); | ||
|
|
||
| $response = $this->client->renderTemplate($query); | ||
|
|
||
| self::assertSame('{{ doc.breed }} called {{ doc.name }}', $response->getTemplate()); | ||
| self::assertSame('Jack Russell called Iko', $response->getRendered()); | ||
| } | ||
| } | ||
Oops, something went wrong.
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.