Skip to content

Commit

Permalink
qute
Browse files Browse the repository at this point in the history
Signed-off-by: Attila Mészáros <[email protected]>
  • Loading branch information
csviri committed Mar 29, 2024
1 parent 25729c7 commit 74cb4b7
Showing 1 changed file with 14 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -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<Glue> 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<String, Map> createMustacheContextWithResources(Glue primary,
@SuppressWarnings("rawtypes")
private static Map<String, Map> createDataWithResources(Glue primary,
Context<Glue> context) {
Map<String, Map> res = new HashMap<>();
var actualResourcesByName = Utils.getActualResourcesByNameInWorkflow(context, primary);

Map<String, 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;
}
}

0 comments on commit 74cb4b7

Please sign in to comment.