如何在 java 中实现小胡子 lambda

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 和 returns 来自键值对的值

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

我什至查看了文档:https://github.com/spullara/mustache.java#readmehttps://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/。新人很难“刚好”。

我通过参考有关 lambda 函数的 Spring 文档解决了这个问题。问题是他们打错了,它说的是“lamba”而不是 lambda:https://docs.spring.io/spring-restdocs/docs/current/reference/html5/#working-with-asciidoctor-customizing-tables-formatting-problems.

离开 tabelCellContent lambda 函数。我在 StandardWriterReslover 的测试中找到了一个例子。 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 的 class 看起来像:

/*
 * 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);
        }
    }

}