Skip to content

Conversation

@tlrx
Copy link
Member

@tlrx tlrx commented Jun 14, 2016

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:

  • {{#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 #18970

@tlrx tlrx added >enhancement WIP :Core/Infra/Scripting Scripting abstractions, Painless, and Mustache v5.0.0-alpha4 labels Jun 14, 2016
Copy link
Member

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.

@tlrx
Copy link
Member Author

tlrx commented Jun 17, 2016

I updated the code to make it work with the {{#toJson}}ctx.payload.someMap{{/toJson}} syntax. I also added a {{#join}}ctx.payload.someIterable{{/join}} to render things like comma separated values.

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.

@tlrx tlrx added review and removed WIP labels Jun 17, 2016
@rjernst
Copy link
Member

rjernst commented Jun 18, 2016

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 {{ctx.payload.someMap.asJson}} or {{asJson(ctx.payload.someMap)}}

Copy link
Contributor

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?

Copy link
Member Author

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".

@spinscale
Copy link
Contributor

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

@tlrx
Copy link
Member Author

tlrx commented Jun 22, 2016

Thanks @spinscale for the review. I updated the code, changing few things around and added the possibility to set a custom delimiter in the join function. Please let me know what you think.

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 {{ctx.payload.someMap.asJson}} or {{asJson(ctx.payload.someMap)}}

@rjernst Yes, I don't have a strong feeling here. I agreed with you when I discussed with @spinscale about implementing a toJSON() (you called it asJson) method on arrays and maps.

But the Mustache specs also propose wrapped functions section with {{#wrapped}}...{{/wrapped}} so I think having {{#toJson}}...{{/toJson}} or {{#join}}...{{/join}} syntax is OK. It also has the side effect to allow things like {{#join}}{{#toJson}}terms{{/toJson}}{{/join}} which would be more difficult to express using the other syntax.

Copy link
Contributor

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))?

Copy link
Member Author

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.

@spinscale
Copy link
Contributor

left two minor comments. I like it, very helpful for mustache users!

LGTM

@tlrx tlrx removed the review label Jun 29, 2016
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
@tlrx tlrx force-pushed the render-map-as-json-in-mustache branch from a778cbb to 4820d49 Compare June 29, 2016 07:58
@tlrx tlrx merged commit 4820d49 into elastic:master Jun 29, 2016
@tlrx
Copy link
Member Author

tlrx commented Jun 29, 2016

Thanks @spinscale

@tlrx tlrx deleted the render-map-as-json-in-mustache branch June 29, 2016 08:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

:Core/Infra/Scripting Scripting abstractions, Painless, and Mustache >enhancement v5.0.0-alpha5

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ElasticSearch always true option.

4 participants