如何在Java中实现Mustache的lambda函数。

huangapple go评论80阅读模式
英文:

How to implement mustache lambda in java

问题

我想在Java中实现一个可以从Mustache模板调用的lambda函数。到目前为止,我只找到了一篇关于这个主题的写得很差的博客文章:https://spring.io/blog/2016/11/21/the-joy-of-mustache-server-side-templates-for-the-jvm。基本上,我有一个模板文件:

{{#getContentType}}
     {{http-response.headers}}
{{/getContentType}}

方法名是getContentType,它接受一个列表对象http-response.headers,并从键值对中返回值:

{
    "name": "Content-Type",
    "value": "application/json"
}

我甚至查看了文档:https://github.com/spullara/mustache.java#readme,但我仍然在努力弄清楚这个问题。任何帮助将不胜感激!

作为参考,getContentType lambda的示例输入如下:

[
    {
        "name": "Content-Type",
        "value": "application/json"
    },
    {
        "name": "Content-Length",
        "value": "363"
    },
    {
        "name": "Authorization",
        "value": "Bearer bearerToken"
    },
    {
        "name": "Host",
        "value": "host"
    }
]

没有关于使用TemplateFunctions的示例,但文档在这里:http://spullara.github.io/mustache/apidocs/。对于一个新手来说,很难"一下子就明白"。

英文:

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

答案1

得分: 2

我查阅了Spring文档中关于lambda函数的部分,成功解决了这个问题。问题出在一个拼写错误上,它写成了 "lamba" 而不是 "lambda":https://docs.spring.io/spring-restdocs/docs/current/reference/html5/#working-with-asciidoctor-customizing-tables-formatting-problems。

根据 tableCellContent lambda 函数,我在 StandardWriterResolver 的测试中找到了一个示例:https://www.codota.com/code/java/classes/org.springframework.restdocs.snippet.StandardWriterResolver。

这个 lambda 函数被添加到了 templateContext 映射中:

Map<String, Object> templateContext = new HashMap<>();
templateContext.put("tableCellContent", new AsciidoctorTableCellContentLambda());

AsciidoctorTableCellContentLambda 类的代码如下:

/*
 * 版权所有 2014-2016 原作者或作者。
 *
 * 根据 Apache License, Version 2.0 (the "License") 许可;
 * 除非符合许可,否则不得使用此文件。
 * 您可以在以下位置获取许可的副本:
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * 除非适用法律要求或书面同意,否则软件以"原样"分发,
 * 无论是明示还是暗示的,都不提供任何形式的担保或条件。
 * 详见许可证中的特定语言权限和限制。
 */

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;

/**
 * 一个 {@link Lambda},用于转义 {@code |} 字符,以免破坏表格的格式。
 *
 * 作者:Andy Wilkinson
 * 自版本 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);
		}
	}

}
英文:

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&lt;String, Object&gt; templateContext = new HashMap&lt;&gt;();
    templateContext.put(&quot;tableCellContent&quot;,
        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 &quot;License&quot;);
 * 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 &quot;AS IS&quot; 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&#39;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 &lt; output.length(); i++) {
			char current = output.charAt(i);
			if (current == &#39;|&#39; &amp;&amp; (i == 0 || output.charAt(i - 1) != &#39;\\&#39;)) {
				writer.append(&#39;\\&#39;);
			}
			writer.append(current);
		}
	}

}

huangapple
  • 本文由 发表于 2020年10月16日 23:27:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/64392102.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定