forked from elastic/elasticsearch
-
Notifications
You must be signed in to change notification settings - Fork 1
Tweak lang-mustache factory (#92211) #4
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
MitchLewis930
wants to merge
1
commit into
pr_014_before
Choose a base branch
from
pr_014_after
base: pr_014_before
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
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
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.
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.
🧩 Analysis chain
🏁 Script executed:
Repository: Signal65/elasticsearch-CodeRabbit
Length of output: 175
🏁 Script executed:
cat -n modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/CustomMustacheFactory.java | head -100Repository: Signal65/elasticsearch-CodeRabbit
Length of output: 4362
🏁 Script executed:
Repository: Signal65/elasticsearch-CodeRabbit
Length of output: 1042
🏁 Script executed:
Repository: Signal65/elasticsearch-CodeRabbit
Length of output: 2028
🏁 Script executed:
Repository: Signal65/elasticsearch-CodeRabbit
Length of output: 1100
🌐 Web query:
mustache.java 0.9.10 TemplateFunction interface documentation behavior💡 Result:
In mustache.java 0.9.10,
TemplateFunctionis a marker interface for section lambdas that tells the engine to treat the lambda’s return value as Mustache source and compile/render it again (i.e., “reparse the results again”). [2]This contrasts with using a plain Java 8
Function<String,String>for a section ({{#func}}...{{/func}}):Function<String,String>: the section body is rendered first (post-substitution) and then passed toapply; the returned string is inserted as text (not recompiled as a Mustache template). [1]TemplateFunction: the lambda is invoked in the pre-substitution mode; the returned string is treated as Mustache template text and is reparsed/re-rendered against the current context. [1], [2]Practical implication: implement
TemplateFunctionwhen you want your lambda to generate Mustache tags (e.g., return"Hello {{name}}") and have those tags evaluated; use plainFunctionwhen you only want to transform already-rendered text. [1], [2]🌐 Web query:
mustache.java TemplateFunction vs Function lambda section rendering escaping💡 Result:
In mustache.java there are two “lambda section” styles, and they differ mainly in when rendering happens (which directly affects escaping):
Function<String,String>(Java 8Function) = post-substitution{{name}}etc. inside the section are already expanded/escaped), then passes the resulting text to yourFunction(“post-substitution”). (github.com)Function’s returned string is then written out as plain text (it is not re-parsed as a Mustache template). (github.com)Escaping implication: anything inside the section like
{{name}}was already escaped by the normal rules before your function sees it; your function output isn’t “auto-escaped” afterward (it’s just output).TemplateFunction= pre-substitution + reparseTemplateFunctionexists specifically so mustache.java will reparse (re-render) the function’s returned string as a Mustache template. (javadoc.io)Functionpost-substitution behavior. (github.com)Escaping implication: because the returned string is treated as a template, any
{{var}}that appears in what you return will be rendered with normal Mustache escaping rules during that re-render (and{{{var}}}/{{& var}}would be unescaped per Mustache rules). (github.com)If you tell me your exact template snippet + which type you’re using, I can spell out precisely what gets escaped at each step.
Citations:
Investigate whether TemplateFunction is the correct type for these lambdas
In mustache.java 0.9.10,
TemplateFunctioncauses the lambda's returned string to be re-parsed as Mustache template source, whereasFunction<String,String>treats the return value as plain text. This distinction is critical:ToJsonCode(lines 190-214): Returns JSON-formatted strings that may contain{{characters. If re-parsed as templates, literal JSON syntax could be misinterpreted as Mustache directives.JoinerCode(lines 241-255): Returns concatenated strings. If delimiters or items contain{{, they would trigger template re-parsing unintentionally.Verify that
TemplateFunctionis the intended type (not a regression from a priorFunction<String,String>implementation) and confirm these lambdas do not produce output that would cause unintended re-parsing.🤖 Prompt for AI Agents