com.github.jknack.handlebars.context.FieldValueResolver Java Examples

The following examples show how to use com.github.jknack.handlebars.context.FieldValueResolver. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: HandlebarsEngineAdapter.java    From openapi-generator with Apache License 2.0 5 votes vote down vote up
public String compileTemplate(TemplatingExecutor executor,
                              Map<String, Object> bundle, String templateFile) throws IOException {
    TemplateLoader loader = new AbstractTemplateLoader() {
        @Override
        public TemplateSource sourceAt(String location) {
            return findTemplate(executor, location);
        }
    };

    Context context = Context
            .newBuilder(bundle)
            .resolver(
                    MapValueResolver.INSTANCE,
                    JavaBeanValueResolver.INSTANCE,
                    FieldValueResolver.INSTANCE)
            .build();

    Handlebars handlebars = new Handlebars(loader);
    handlebars.registerHelperMissing((obj, options) -> {
        LOGGER.warn(String.format(Locale.ROOT, "Unregistered helper name '%s', processing template:\n%s", options.helperName, options.fn.text()));
        return "";
    });
    handlebars.registerHelper("json", Jackson2Helper.INSTANCE);
    StringHelpers.register(handlebars);
    handlebars.registerHelpers(ConditionalHelpers.class);
    handlebars.registerHelpers(org.openapitools.codegen.templating.handlebars.StringHelpers.class);
    Template tmpl = handlebars.compile(templateFile);
    return tmpl.apply(context);
}
 
Example #2
Source File: StringHelpersTest.java    From openapi-generator with Apache License 2.0 5 votes vote down vote up
private void evaluate(HashMap<String, Object> data, String template, String expect) throws IOException {

        Context context = Context
                .newBuilder(data)
                .resolver(
                        FieldValueResolver.INSTANCE)
                .build();

        Template tmpl = handlebars.compileInline(template);
        String actual = tmpl.apply(context);
        assertEquals(actual, expect);
    }
 
Example #3
Source File: ThrottlePolicyGenerator.java    From product-microgateway with Apache License 2.0 5 votes vote down vote up
/**
 * Retrieve generated source content as a String value.
 *
 * @param object context to be used by template engine
 * @return String with populated template
 * @throws IOException when template population fails
 */
private String getContent(ThrottlePolicy object) throws IOException {
    Template template = CodegenUtils.compileTemplate(GeneratorConstants.DEFAULT_TEMPLATE_DIR,
            GeneratorConstants.THROTTLE_POLICY_TEMPLATE_NAME);
    Context context = Context.newBuilder(object)
            .resolver(MapValueResolver.INSTANCE, JavaBeanValueResolver.INSTANCE, FieldValueResolver.INSTANCE)
            .build();

    return template.apply(context);
}
 
Example #4
Source File: ThrottlePolicyGenerator.java    From product-microgateway with Apache License 2.0 5 votes vote down vote up
private String getPolicyInitContent(ThrottlePolicyInitializer object)
        throws IOException {
    Template template = CodegenUtils.compileTemplate(GeneratorConstants.DEFAULT_TEMPLATE_DIR,
            GeneratorConstants.THROTTLE_POLICY_INIT_TEMPLATE_NAME);
    Context context = Context.newBuilder(object)
            .resolver(MapValueResolver.INSTANCE, JavaBeanValueResolver.INSTANCE, FieldValueResolver.INSTANCE)
            .build();

    return template.apply(context);
}
 
Example #5
Source File: CodeGenerator.java    From product-microgateway with Apache License 2.0 3 votes vote down vote up
/**
 * Retrieve generated source content as a String value.
 *
 * @param endpoints    context to be used by template engine
 * @param templateName name of the template to be used for this code generation
 * @return String with populated template
 * @throws IOException when template population fails
 */
private String getContent(Object endpoints, String templateName) throws IOException {
    Template template = CodegenUtils.compileTemplate(GeneratorConstants.DEFAULT_TEMPLATE_DIR, templateName);
    Context context = Context.newBuilder(endpoints)
            .resolver(MapValueResolver.INSTANCE, JavaBeanValueResolver.INSTANCE, FieldValueResolver.INSTANCE)
            .build();
    return template.apply(context);
}