use of com.google.template.soy.pysrc.restricted.PyFunctionExprBuilder in project closure-templates by google.
the class GenPyCallExprVisitor method visitCallDelegateNode.
/**
* Visits a delegate call node and builds the call expression to retrieve the function and execute
* it. The get_delegate_fn returns the function directly, so its output can be called directly.
*
* @param node The delegate call node.
* @return The call Python expression.
*/
@Override
protected PyExpr visitCallDelegateNode(CallDelegateNode node) {
ExprRootNode variantSoyExpr = node.getDelCalleeVariantExpr();
PyExpr variantPyExpr;
if (variantSoyExpr == null) {
// Case 1: Delegate call with empty variant.
variantPyExpr = new PyStringExpr("''");
} else {
// Case 2: Delegate call with variant expression.
TranslateToPyExprVisitor translator = new TranslateToPyExprVisitor(localVarStack, errorReporter);
variantPyExpr = translator.exec(variantSoyExpr);
}
String calleeExprText = new PyFunctionExprBuilder("runtime.get_delegate_fn").addArg(node.getDelCalleeName()).addArg(variantPyExpr).addArg(node.allowEmptyDefault()).build();
String callExprText = calleeExprText + "(" + genObjToPass(node) + ", ijData)";
return escapeCall(callExprText, node.getEscapingDirectives());
}
use of com.google.template.soy.pysrc.restricted.PyFunctionExprBuilder in project closure-templates by google.
the class TranslateToPyExprVisitor method visitLegacyObjectMapLiteralOrMapLiteralNode.
private PyExpr visitLegacyObjectMapLiteralOrMapLiteralNode(AbstractParentExprNode node) {
Preconditions.checkState(node.getKind() == ExprNode.Kind.LEGACY_OBJECT_MAP_LITERAL_NODE || node.getKind() == ExprNode.Kind.MAP_LITERAL_NODE);
Preconditions.checkArgument(node.numChildren() % 2 == 0);
Map<PyExpr, PyExpr> dict = new LinkedHashMap<>();
boolean needsRuntimeNullCheck = node.getKind() == ExprNode.Kind.MAP_LITERAL_NODE;
for (int i = 0, n = node.numChildren(); i < n; i += 2) {
ExprNode keyNode = node.getChild(i);
PyExpr key = visit(keyNode);
if (needsRuntimeNullCheck) {
key = new PyFunctionExprBuilder("runtime.check_not_null").addArg(key).asPyExpr();
}
ExprNode valueNode = node.getChild(i + 1);
dict.put(key, visit(valueNode));
}
// to index into the map with the wrong convention.
return PyExprUtils.convertMapToOrderedDict(dict);
}
use of com.google.template.soy.pysrc.restricted.PyFunctionExprBuilder in project closure-templates by google.
the class MinFunction method computeForPySrc.
@Override
public PyExpr computeForPySrc(List<PyExpr> args) {
PyExpr arg0 = args.get(0);
PyExpr arg1 = args.get(1);
PyFunctionExprBuilder fnBuilder = new PyFunctionExprBuilder("min");
return fnBuilder.addArg(arg0).addArg(arg1).asPyExpr();
}
use of com.google.template.soy.pysrc.restricted.PyFunctionExprBuilder in project closure-templates by google.
the class FormatNumDirective method applyForPySrc.
@Override
public PyExpr applyForPySrc(PyExpr value, List<PyExpr> args) {
String numberFormatType = parseFormat(args);
PyFunctionExprBuilder builder = new PyFunctionExprBuilder(PyExprUtils.TRANSLATOR_NAME + // so that it works with our integration testing infrastructure.
".format_num").addArg(value).addArg(new PyExpr(numberFormatType, Integer.MAX_VALUE));
if (args.size() > 2) {
String minFractionDigits = args.get(2).getText();
String maxFractionDigits = args.size() > 3 ? args.get(3).getText() : minFractionDigits;
builder.addArg(minFractionDigits).addArg(maxFractionDigits);
}
return builder.asPyStringExpr();
}
use of com.google.template.soy.pysrc.restricted.PyFunctionExprBuilder in project closure-templates by google.
the class MaxFunction method computeForPySrc.
@Override
public PyExpr computeForPySrc(List<PyExpr> args) {
PyExpr arg0 = args.get(0);
PyExpr arg1 = args.get(1);
PyFunctionExprBuilder fnBuilder = new PyFunctionExprBuilder("max");
return fnBuilder.addArg(arg0).addArg(arg1).asPyExpr();
}
Aggregations