Search in sources :

Example 1 with PyFunctionExprBuilder

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());
}
Also used : PyExpr(com.google.template.soy.pysrc.restricted.PyExpr) PyFunctionExprBuilder(com.google.template.soy.pysrc.restricted.PyFunctionExprBuilder) PyStringExpr(com.google.template.soy.pysrc.restricted.PyStringExpr) ExprRootNode(com.google.template.soy.exprtree.ExprRootNode)

Example 2 with PyFunctionExprBuilder

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);
}
Also used : AbstractParentExprNode(com.google.template.soy.exprtree.AbstractParentExprNode) ExprNode(com.google.template.soy.exprtree.ExprNode) PyExpr(com.google.template.soy.pysrc.restricted.PyExpr) PyFunctionExprBuilder(com.google.template.soy.pysrc.restricted.PyFunctionExprBuilder) LinkedHashMap(java.util.LinkedHashMap)

Example 3 with PyFunctionExprBuilder

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();
}
Also used : PyExpr(com.google.template.soy.pysrc.restricted.PyExpr) PyFunctionExprBuilder(com.google.template.soy.pysrc.restricted.PyFunctionExprBuilder)

Example 4 with PyFunctionExprBuilder

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();
}
Also used : PyExpr(com.google.template.soy.pysrc.restricted.PyExpr) PyFunctionExprBuilder(com.google.template.soy.pysrc.restricted.PyFunctionExprBuilder) LocaleString(com.google.template.soy.shared.restricted.ApiCallScopeBindingAnnotations.LocaleString)

Example 5 with PyFunctionExprBuilder

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();
}
Also used : PyExpr(com.google.template.soy.pysrc.restricted.PyExpr) PyFunctionExprBuilder(com.google.template.soy.pysrc.restricted.PyFunctionExprBuilder)

Aggregations

PyFunctionExprBuilder (com.google.template.soy.pysrc.restricted.PyFunctionExprBuilder)7 PyExpr (com.google.template.soy.pysrc.restricted.PyExpr)6 AbstractParentExprNode (com.google.template.soy.exprtree.AbstractParentExprNode)1 ExprNode (com.google.template.soy.exprtree.ExprNode)1 ExprRootNode (com.google.template.soy.exprtree.ExprRootNode)1 PyStringExpr (com.google.template.soy.pysrc.restricted.PyStringExpr)1 LocaleString (com.google.template.soy.shared.restricted.ApiCallScopeBindingAnnotations.LocaleString)1 LinkedHashMap (java.util.LinkedHashMap)1