|
1 | 1 | package io.csviri.operator.resourceglue.templating;
|
2 | 2 |
|
3 |
| -import java.io.StringReader; |
4 |
| -import java.io.StringWriter; |
5 | 3 | import java.util.HashMap;
|
6 | 4 | import java.util.Map;
|
7 | 5 |
|
8 | 6 | import io.csviri.operator.resourceglue.Utils;
|
9 | 7 | import io.csviri.operator.resourceglue.customresource.glue.Glue;
|
10 | 8 | import io.javaoperatorsdk.operator.api.reconciler.Context;
|
| 9 | +import io.quarkus.qute.Engine; |
| 10 | +import io.quarkus.qute.Template; |
11 | 11 |
|
12 | 12 | import com.fasterxml.jackson.databind.ObjectMapper;
|
13 |
| -import com.github.mustachejava.DefaultMustacheFactory; |
14 |
| -import com.github.mustachejava.MustacheFactory; |
15 | 13 |
|
16 | 14 | public class GenericTemplateHandler {
|
17 |
| - private static final ObjectMapper objectMapper = new ObjectMapper(); |
18 |
| - private static final MustacheFactory mustacheFactory = new DefaultMustacheFactory(); |
| 15 | + |
19 | 16 | public static final String WORKFLOW_METADATA_KEY = "glueMetadata";
|
20 | 17 |
|
| 18 | + private static final ObjectMapper objectMapper = new ObjectMapper(); |
| 19 | + private static final Engine engine = Engine.builder().addDefaults().build(); |
21 | 20 |
|
22 | 21 | public String processTemplate(String template, Glue primary,
|
23 | 22 | Context<Glue> context) {
|
24 | 23 |
|
25 |
| - // to precompile? |
26 |
| - var mustache = mustacheFactory.compile(new StringReader(template), "desired"); |
| 24 | + Template hello = engine.parse(template); |
27 | 25 |
|
28 |
| - var mustacheContext = createMustacheContextWithResources(primary, context); |
29 |
| - return mustache.execute(new StringWriter(), mustacheContext).toString(); |
| 26 | + return hello.data(createDataWithResources(primary, context)).render(); |
30 | 27 | }
|
31 | 28 |
|
32 |
| - private static Map<String, Map> createMustacheContextWithResources(Glue primary, |
| 29 | + @SuppressWarnings("rawtypes") |
| 30 | + private static Map<String, Map> createDataWithResources(Glue primary, |
33 | 31 | Context<Glue> context) {
|
| 32 | + Map<String, Map> res = new HashMap<>(); |
34 | 33 | var actualResourcesByName = Utils.getActualResourcesByNameInWorkflow(context, primary);
|
35 | 34 |
|
36 |
| - Map<String, Map> mustacheContext = new HashMap<>(); |
37 |
| - |
38 |
| - actualResourcesByName.entrySet().stream().forEach(e -> mustacheContext.put(e.getKey(), |
39 |
| - e.getValue() == null ? null : objectMapper.convertValue(e.getValue(), Map.class))); |
| 35 | + actualResourcesByName.forEach((key, value) -> res.put(key, |
| 36 | + value == null ? null : objectMapper.convertValue(value, Map.class))); |
40 | 37 |
|
41 |
| - mustacheContext.put(WORKFLOW_METADATA_KEY, |
| 38 | + res.put(WORKFLOW_METADATA_KEY, |
42 | 39 | objectMapper.convertValue(primary.getMetadata(), Map.class));
|
43 | 40 |
|
44 |
| - return mustacheContext; |
| 41 | + return res; |
45 | 42 | }
|
46 | 43 | }
|
0 commit comments