use of com.github.jknack.handlebars.HandlebarsException in project elide by yahoo.
the class ColumnContext method get.
public Object get(Object key) {
String keyStr = key.toString();
// Physical References starts with $
if (keyStr.lastIndexOf('$') == 0) {
return resolvePhysicalReference(this, keyStr);
}
if (keyStr.equals(TBL_PREFIX)) {
return TableSubContext.tableSubContextBuilder().tableArguments(this.tableArguments).build();
}
if (keyStr.equals(COL_PREFIX)) {
return ColumnSubContext.columnSubContextBuilder().queryable(this.getQueryable()).alias(this.getAlias()).metaDataStore(this.getMetaDataStore()).column(this.getColumn()).tableArguments(this.getTableArguments()).build();
}
if (this.queryable.hasJoin(keyStr)) {
return getJoinContext(keyStr);
}
// Check if key exists in Map.
Object value = getOrDefault(key, null);
if (value != null) {
return value;
}
// In case of colA references colB and user query has both colA and colB,
// we should use default arguments for colB while resolving colA instead of user provided argument for colB.
// so taking colB's details from current queryable's source.
ColumnProjection column = this.getQueryable().getSource().getColumnProjection(keyStr);
if (column != null) {
ColumnProjection newColumn = column.withArguments(mergedArgumentMap(column.getArguments(), this.getColumn().getArguments()));
return getNewContext(this, newColumn).resolve(newColumn.getExpression());
}
throw new HandlebarsException(new Throwable("Couldn't find: " + keyStr));
}
use of com.github.jknack.handlebars.HandlebarsException in project kestra by kestra-io.
the class HandleBarDeserializer method deserialize.
@Override
public String deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
String value = p.getText();
if (value.contains("[[") && value.contains("]]")) {
String contextPath = String.valueOf(ctxt.findInjectableValue(YamlFlowParser.CONTEXT_FLOW_DIRECTORY, null, null));
TemplateLoader loader = new FileTemplateLoader(contextPath, "");
Handlebars handlebars = HANDLEBARS.with(loader);
Template template = handlebars.compileInline(value);
try {
return template.apply(ImmutableMap.of());
} catch (IOException | HandlebarsException e) {
throw new ConstraintViolationException(Collections.singleton(ManualConstraintViolation.of(e.getMessage(), this, HandleBarDeserializer.class, "template", value)));
}
}
return super.deserialize(p, ctxt);
}
use of com.github.jknack.handlebars.HandlebarsException in project molgenis by molgenis.
the class HandlebarsMathHelperTest method testExceptions.
@ParameterizedTest
@MethodSource("exceptionInputProvider")
void testExceptions(String templateString, String exception) throws IOException {
Handlebars handlebars = new Handlebars();
handlebars.registerHelper("math", new HandlebarsMathHelper());
Template template = handlebars.compileInline(templateString);
HandlebarsException thrown = assertThrows(HandlebarsException.class, () -> template.apply(null));
assertThat(thrown.getCause().toString()).contains(exception);
}
use of com.github.jknack.handlebars.HandlebarsException in project vertx-web by vert-x3.
the class HandlebarsTemplateTest method testTemplateJsonArrayResolverError.
@Test
public void testTemplateJsonArrayResolverError() throws Exception {
TemplateEngine engine = HandlebarsTemplateEngine.create();
JsonArray jsonArray = new JsonArray();
jsonArray.add("badger").add("fox").add(new JsonObject().put("name", "joe"));
AtomicReference<RoutingContext> contextRef = new AtomicReference<>();
router.route().handler(context -> {
contextRef.set(context);
context.put("foo", jsonArray);
context.next();
});
router.route().handler(TemplateHandler.create(engine, "src/test/filesystemtemplates", "text/plain"));
testRequest(HttpMethod.GET, "/" + "test-handlebars-template6.hbs", 500, "Internal Server Error");
if (contextRef.get().failure() instanceof HandlebarsException) {
HandlebarsException exception = ((HandlebarsException) contextRef.get().failure());
assertTrue(exception.getMessage().contains("test-handlebars-template6.hbs:1:19"));
} else {
fail("We would expect an handlebars exception with detailed location information.");
}
}
use of com.github.jknack.handlebars.HandlebarsException in project elide by yahoo.
the class ColumnContext method resolveSQLHandlebar.
private Object resolveSQLHandlebar(final Object context, final Options options) throws UnsupportedEncodingException {
String from = options.hash("from");
String columnName = options.hash("column");
int argsIndex = columnName.indexOf('[');
String invokedColumnName = columnName;
ColumnContext currentCtx = (ColumnContext) context;
// 'from' is optional, so if not provided use the same table context.
ColumnContext invokedCtx = isBlank(from) ? currentCtx : (ColumnContext) currentCtx.get(from);
Map<String, Argument> pinnedArgs = new HashMap<>();
if (argsIndex >= 0) {
pinnedArgs = getArgumentMapFromString(columnName.substring(argsIndex));
invokedColumnName = columnName.substring(0, argsIndex);
}
// Physical References starts with $
if (invokedColumnName.lastIndexOf('$') == 0) {
return resolvePhysicalReference(invokedCtx, invokedColumnName);
}
ColumnProjection column = invokedCtx.getQueryable().getSource().getColumnProjection(invokedColumnName);
if (column != null) {
ColumnProjection newColumn = column.withArguments(mergedArgumentMap(column.getArguments(), invokedCtx.getColumn().getArguments(), pinnedArgs));
return getNewContext(invokedCtx, newColumn).resolve(newColumn.getExpression());
}
throw new HandlebarsException(new Throwable("Couldn't find: " + invokedColumnName));
}
Aggregations