From 74cb4b79fa860abc4d80530fd120c4e294f112d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Attila=20M=C3=A9sz=C3=A1ros?= Date: Fri, 29 Mar 2024 19:26:32 +0100 Subject: [PATCH] qute MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Attila Mészáros --- .../templating/GenericTemplateHandler.java | 31 +++++++++---------- 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/src/main/java/io/csviri/operator/resourceglue/templating/GenericTemplateHandler.java b/src/main/java/io/csviri/operator/resourceglue/templating/GenericTemplateHandler.java index c4b8273..84a00d4 100644 --- a/src/main/java/io/csviri/operator/resourceglue/templating/GenericTemplateHandler.java +++ b/src/main/java/io/csviri/operator/resourceglue/templating/GenericTemplateHandler.java @@ -1,46 +1,43 @@ package io.csviri.operator.resourceglue.templating; -import java.io.StringReader; -import java.io.StringWriter; import java.util.HashMap; import java.util.Map; import io.csviri.operator.resourceglue.Utils; import io.csviri.operator.resourceglue.customresource.glue.Glue; import io.javaoperatorsdk.operator.api.reconciler.Context; +import io.quarkus.qute.Engine; +import io.quarkus.qute.Template; import com.fasterxml.jackson.databind.ObjectMapper; -import com.github.mustachejava.DefaultMustacheFactory; -import com.github.mustachejava.MustacheFactory; public class GenericTemplateHandler { - private static final ObjectMapper objectMapper = new ObjectMapper(); - private static final MustacheFactory mustacheFactory = new DefaultMustacheFactory(); + public static final String WORKFLOW_METADATA_KEY = "glueMetadata"; + private static final ObjectMapper objectMapper = new ObjectMapper(); + private static final Engine engine = Engine.builder().addDefaults().build(); public String processTemplate(String template, Glue primary, Context context) { - // to precompile? - var mustache = mustacheFactory.compile(new StringReader(template), "desired"); + Template hello = engine.parse(template); - var mustacheContext = createMustacheContextWithResources(primary, context); - return mustache.execute(new StringWriter(), mustacheContext).toString(); + return hello.data(createDataWithResources(primary, context)).render(); } - private static Map createMustacheContextWithResources(Glue primary, + @SuppressWarnings("rawtypes") + private static Map createDataWithResources(Glue primary, Context context) { + Map res = new HashMap<>(); var actualResourcesByName = Utils.getActualResourcesByNameInWorkflow(context, primary); - Map mustacheContext = new HashMap<>(); - - actualResourcesByName.entrySet().stream().forEach(e -> mustacheContext.put(e.getKey(), - e.getValue() == null ? null : objectMapper.convertValue(e.getValue(), Map.class))); + actualResourcesByName.forEach((key, value) -> res.put(key, + value == null ? null : objectMapper.convertValue(value, Map.class))); - mustacheContext.put(WORKFLOW_METADATA_KEY, + res.put(WORKFLOW_METADATA_KEY, objectMapper.convertValue(primary.getMetadata(), Map.class)); - return mustacheContext; + return res; } }