Reputation: 2052
I would like to implement a lambda in java that can be called from a mustache template. So far, I've only been able to find a poorly written blog article about this subject: https://spring.io/blog/2016/11/21/the-joy-of-mustache-server-side-templates-for-the-jvm. Essentially, I have a template file
{{#getContentType}}
{{http-response.headers}}
{{/getContentType}}
The method name is getContentType
it takes a list object http-response.headers
and returns the value from the key-value pair
{
"name": "Content-Type",
"value": "application/json"
}
I have even looked at the documentation: https://github.com/spullara/mustache.java#readmehttps://github.com/spullara/mustache.java#readme, but I am still struggling to figure this out. Any help would be greatly appreciated!
For reference an example input for the getContentType
lambda would like
[
{
"name": "Content-Type",
"value": "application/json"
},
{
"name": "Content-Length",
"value": "363"
},
{
"name": "Authorization",
"value": "Bearer bearerToken"
},
{
"name": "Host",
"value": "host"
}
]
There aren't any examples of using TemplateFunctions, but the documentation is here:http://spullara.github.io/mustache/apidocs/. It's pretty hard for a new person to "just get it".
Upvotes: 1
Views: 2764
Reputation: 2052
I was able to figure this out by referring to the Spring Docs on lambda functions. The problem is that they have a typo, it says "lamba" not lambda: https://docs.spring.io/spring-restdocs/docs/current/reference/html5/#working-with-asciidoctor-customizing-tables-formatting-problems.
Going off of the tabelCellContent lambda function. I found an example in the tests for StandardWriterReslover
. https://www.codota.com/code/java/classes/org.springframework.restdocs.snippet.StandardWriterResolver.
The lambda function is added to the map templateContext
:
Map<String, Object> templateContext = new HashMap<>();
templateContext.put("tableCellContent",
new AsciidoctorTableCellContentLambda());
The class for AsciidoctorTableCellContentLambda looks like:
/*
* Copyright 2014-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.restdocs.templates.mustache;
import java.io.IOException;
import java.io.Writer;
import org.springframework.restdocs.mustache.Mustache.Lambda;
import org.springframework.restdocs.mustache.Template.Fragment;
/**
* A {@link Lambda} that escapes {@code |} characters so that the do not break the table's
* formatting.
*
* @author Andy Wilkinson
* @since 1.1.0
*/
public final class AsciidoctorTableCellContentLambda implements Lambda {
@Override
public void execute(Fragment fragment, Writer writer) throws IOException {
String output = fragment.execute();
for (int i = 0; i < output.length(); i++) {
char current = output.charAt(i);
if (current == '|' && (i == 0 || output.charAt(i - 1) != '\\')) {
writer.append('\\');
}
writer.append(current);
}
}
}
Upvotes: 2