This commit is contained in:
@@ -0,0 +1,56 @@
|
|||||||
|
package fr.cnd.compositor.blocks.pebble;
|
||||||
|
|
||||||
|
import io.quarkus.test.junit.QuarkusTest;
|
||||||
|
import jakarta.inject.Inject;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
|
@QuarkusTest
|
||||||
|
class PebbleJsonFilterTest {
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
PebbleBlockEngine pebbleBlockEngine;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void shouldSerializeString() {
|
||||||
|
String result = render("{{ value | json }}", Map.of("value", "hello"));
|
||||||
|
|
||||||
|
assertEquals("\"hello\"", result);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void shouldSerializeInteger() {
|
||||||
|
String result = render("{{ value | json }}", Map.of("value", 42));
|
||||||
|
|
||||||
|
assertEquals("42", result);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void shouldSerializeMap() {
|
||||||
|
String result = render("{{ value | json }}", Map.of("value", Map.of("key", "val")));
|
||||||
|
|
||||||
|
assertEquals("{\"key\":\"val\"}", result);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void shouldSerializeList() {
|
||||||
|
String result = render("{{ value | json }}", Map.of("value", List.of(1, 2, 3)));
|
||||||
|
|
||||||
|
assertEquals("[1,2,3]", result);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void shouldSerializeNull() {
|
||||||
|
String result = render("{{ value | json }}", Map.of());
|
||||||
|
|
||||||
|
assertEquals("null", result);
|
||||||
|
}
|
||||||
|
|
||||||
|
private String render(String template, Map<String, Object> context) {
|
||||||
|
return pebbleBlockEngine.render(template, context).await().indefinitely();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
package fr.cnd.compositor.blocks.pebble;
|
package fr.cnd.compositor.blocks.pebble;
|
||||||
|
|
||||||
import io.pebbletemplates.pebble.extension.AbstractExtension;
|
import io.pebbletemplates.pebble.extension.AbstractExtension;
|
||||||
|
import io.pebbletemplates.pebble.extension.Filter;
|
||||||
import io.pebbletemplates.pebble.extension.Function;
|
import io.pebbletemplates.pebble.extension.Function;
|
||||||
import jakarta.enterprise.context.ApplicationScoped;
|
import jakarta.enterprise.context.ApplicationScoped;
|
||||||
import jakarta.inject.Inject;
|
import jakarta.inject.Inject;
|
||||||
@@ -12,8 +13,18 @@ public class PebbleBlocksExtension extends AbstractExtension {
|
|||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
PebbleSlotFunction pebbleSlotFunction;
|
PebbleSlotFunction pebbleSlotFunction;
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
PebbleJsonFilter pebbleJsonFilter;
|
||||||
|
|
||||||
|
@Override
|
||||||
public Map<String, Function> getFunctions() {
|
public Map<String, Function> getFunctions() {
|
||||||
return Map.of("slot", pebbleSlotFunction);
|
return Map.of("slot", pebbleSlotFunction);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Map<String, Filter> getFilters() {
|
||||||
|
return Map.of("json", pebbleJsonFilter);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,35 @@
|
|||||||
|
package fr.cnd.compositor.blocks.pebble;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import io.pebbletemplates.pebble.error.PebbleException;
|
||||||
|
import io.pebbletemplates.pebble.extension.Filter;
|
||||||
|
import io.pebbletemplates.pebble.extension.escaper.SafeString;
|
||||||
|
import io.pebbletemplates.pebble.template.EvaluationContext;
|
||||||
|
import io.pebbletemplates.pebble.template.PebbleTemplate;
|
||||||
|
import jakarta.enterprise.context.ApplicationScoped;
|
||||||
|
import jakarta.inject.Inject;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
@ApplicationScoped
|
||||||
|
public class PebbleJsonFilter implements Filter {
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
ObjectMapper objectMapper;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object apply(Object input, Map<String, Object> args, PebbleTemplate self, EvaluationContext context, int lineNumber) {
|
||||||
|
try {
|
||||||
|
return new SafeString(objectMapper.writeValueAsString(input));
|
||||||
|
} catch (JsonProcessingException e) {
|
||||||
|
throw new PebbleException(e, "Failed to serialize object to JSON", lineNumber, self.getName());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<String> getArgumentNames() {
|
||||||
|
return List.of();
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user