-
Notifications
You must be signed in to change notification settings - Fork 25.6k
Mustache: Render Map as JSON #18856
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
Mustache: Render Map as JSON #18856
Conversation
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 don't think this can ever result in a ClassCastException because of type erasure.
8ff2e36 to
084dc1f
Compare
|
I updated the code to make it work with the It would be great if you can review and test it. @rjernst Thanks for your review! The pull request was marked as work in progress and I changed the whole implementation after discussing with @spinscale. I took your comment into consideration, I'd be glad if you find the time to review again. |
|
I have no opinion on the technical side of this change. I'm happy this allows rendering json, but I also think the syntax looks funky based on other templating languages I have used in the past. I would have expected something simpler like |
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.
does this make sense? is that valid JSON?
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.
Not really. Let's change that so that it renders "John Smith".
|
I had a quick look but did not find any possibility for having arguments to configure the string joiner char instead of hardcoding it as a comma, that would be nice. Apart from that there needs to be some documentation in the search template docs for those functions |
|
Thanks @spinscale for the review. I updated the code, changing few things around and added the possibility to set a custom delimiter in the
@rjernst Yes, I don't have a strong feeling here. I agreed with you when I discussed with @spinscale about implementing a But the Mustache specs also propose wrapped functions section with |
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.
return JSON_CONTENT_TYPE.equals(params.getOrDefault(CONTENT_TYPE_PARAM, JSON_CONTENT_TYPE))?
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.
Sure, this switch statement is really ugly. I suppose my brain was in auto-pilot mode when writing this.
|
left two minor comments. I like it, very helpful for mustache users! LGTM |
This pull request adds two util functions to the Mustache templating engine:
- {{#toJson}}my_map{{/toJson}} to render a Map parameter as a JSON string
- {{#join}}my_iterable{{/join}} to render any iterable (including arrays) as a comma separated list of values like `1, 2, 3`. It's also possible de change the default delimiter (comma) to something else.
closes elastic#18970
a778cbb to
4820d49
Compare
|
Thanks @spinscale |
This commit changes Mustache script engine so that every map is rendered as a JSON string (instead of default Map.toString())This pull request adds two util functions to the Mustache templating engine:
1, 2, 3. It's also possible de change the default delimiter (comma) to something else.closes #18970