Search in sources :

Example 11 with PyStringExpr

use of com.google.template.soy.pysrc.restricted.PyStringExpr in project closure-templates by google.

the class StrLenFunctionTest method testComputeForPySrc.

@Test
public void testComputeForPySrc() {
    StrLenFunction strLen = new StrLenFunction();
    PyExpr string = new PyStringExpr("'data'");
    assertThat(strLen.computeForPySrc(ImmutableList.of(string))).isEqualTo(new PyExpr("len('data')", Integer.MAX_VALUE));
    PyExpr data = new PyExpr("data", Integer.MAX_VALUE);
    assertThat(strLen.computeForPySrc(ImmutableList.of(data))).isEqualTo(new PyExpr("len(str(data))", Integer.MAX_VALUE));
}
Also used : PyExpr(com.google.template.soy.pysrc.restricted.PyExpr) PyStringExpr(com.google.template.soy.pysrc.restricted.PyStringExpr) Test(org.junit.Test)

Example 12 with PyStringExpr

use of com.google.template.soy.pysrc.restricted.PyStringExpr in project closure-templates by google.

the class BidiSpanWrapDirectiveTest method testApplyForPySrc.

@Test
public void testApplyForPySrc() {
    BidiSpanWrapDirective codeSnippet = new BidiSpanWrapDirective(SharedRestrictedTestUtils.BIDI_GLOBAL_DIR_FOR_PY_ISRTL_CODE_SNIPPET_PROVIDER);
    PyExpr data = new PyStringExpr("'data'");
    assertThat(codeSnippet.applyForPySrc(data, ImmutableList.<PyExpr>of()).getText()).isEqualTo("bidi.span_wrap(-1 if IS_RTL else 1, 'data')");
}
Also used : PyExpr(com.google.template.soy.pysrc.restricted.PyExpr) PyStringExpr(com.google.template.soy.pysrc.restricted.PyStringExpr) Test(org.junit.Test)

Example 13 with PyStringExpr

use of com.google.template.soy.pysrc.restricted.PyStringExpr in project closure-templates by google.

the class BidiTextDirFunctionTest method testComputeForPySrc.

@Test
public void testComputeForPySrc() {
    BidiTextDirFunction bidiTextDirFunction = new BidiTextDirFunction();
    PyExpr data = new PyStringExpr("'data'");
    assertThat(bidiTextDirFunction.computeForPySrc(ImmutableList.of(data)).getText()).isEqualTo("bidi.text_dir('data')");
    PyExpr isHtml = new PyExpr("is_html", Integer.MAX_VALUE);
    assertThat(bidiTextDirFunction.computeForPySrc(ImmutableList.of(data, isHtml)).getText()).isEqualTo("bidi.text_dir('data', is_html)");
}
Also used : PyExpr(com.google.template.soy.pysrc.restricted.PyExpr) PyStringExpr(com.google.template.soy.pysrc.restricted.PyStringExpr) Test(org.junit.Test)

Example 14 with PyStringExpr

use of com.google.template.soy.pysrc.restricted.PyStringExpr in project closure-templates by google.

the class MsgFuncGenerator method pyFuncForPluralMsg.

private PyStringExpr pyFuncForPluralMsg() {
    SoyMsgPluralPart pluralPart = (SoyMsgPluralPart) msgParts.get(0);
    MsgPluralNode pluralNode = msgNode.getRepPluralNode(pluralPart.getPluralVarName());
    Map<PyExpr, PyExpr> nodePyVarToPyExprMap = collectVarNameListAndToPyExprMap();
    Map<PyExpr, PyExpr> caseSpecStrToMsgTexts = new LinkedHashMap<>();
    for (Case<SoyMsgPluralCaseSpec> pluralCase : pluralPart.getCases()) {
        caseSpecStrToMsgTexts.put(new PyStringExpr("'" + pluralCase.spec() + "'"), new PyStringExpr("'" + processMsgPartsHelper(pluralCase.parts(), escaperForIcuSection) + "'"));
    }
    prepareFunc.addArg(msgId).addArg(PyExprUtils.convertMapToPyExpr(caseSpecStrToMsgTexts)).addArg(PyExprUtils.convertIterableToPyTupleExpr(nodePyVarToPyExprMap.keySet()));
    // Translates {@link MsgPluralNode#pluralExpr} into a Python lookup expression.
    // Note that pluralExpr represent the Soy expression inside the attributes of a plural tag.
    PyExpr pluralPyExpr = translateToPyExprVisitor.exec(pluralNode.getExpr());
    return renderFunc.addArg(prepareFunc.asPyExpr()).addArg(pluralPyExpr).addArg(PyExprUtils.convertMapToPyExpr(nodePyVarToPyExprMap)).asPyStringExpr();
}
Also used : SoyMsgPluralCaseSpec(com.google.template.soy.msgs.restricted.SoyMsgPluralCaseSpec) PyExpr(com.google.template.soy.pysrc.restricted.PyExpr) SoyMsgPluralPart(com.google.template.soy.msgs.restricted.SoyMsgPluralPart) PyStringExpr(com.google.template.soy.pysrc.restricted.PyStringExpr) MsgPluralNode(com.google.template.soy.soytree.MsgPluralNode) LinkedHashMap(java.util.LinkedHashMap)

Example 15 with PyStringExpr

use of com.google.template.soy.pysrc.restricted.PyStringExpr in project closure-templates by google.

the class GenPyCallExprVisitor method genObjToPass.

/**
 * Generates the Python expression for the object to pass in a given call. This expression will be
 * a combination of passed data and additional content params. If both are passed, they'll be
 * combined into one dictionary.
 *
 * @param callNode The call to generate code for.
 * @return The Python expression for the object to pass in the call.
 */
public String genObjToPass(CallNode callNode) {
    TranslateToPyExprVisitor translator = new TranslateToPyExprVisitor(localVarStack, errorReporter);
    // Generate the expression for the original data to pass.
    String dataToPass;
    if (callNode.isPassingAllData()) {
        dataToPass = "data";
    } else if (callNode.isPassingData()) {
        dataToPass = translator.exec(callNode.getDataExpr()).getText();
    } else {
        dataToPass = "{}";
    }
    // Case 1: No additional params.
    if (callNode.numChildren() == 0) {
        return dataToPass;
    }
    // Build an object literal containing the additional params.
    Map<PyExpr, PyExpr> additionalParams = new LinkedHashMap<>();
    for (CallParamNode child : callNode.getChildren()) {
        PyExpr key = new PyStringExpr("'" + child.getKey().identifier() + "'");
        if (child instanceof CallParamValueNode) {
            CallParamValueNode cpvn = (CallParamValueNode) child;
            additionalParams.put(key, translator.exec(cpvn.getExpr()));
        } else {
            CallParamContentNode cpcn = (CallParamContentNode) child;
            PyExpr valuePyExpr;
            if (isComputableAsPyExprVisitor.exec(cpcn)) {
                valuePyExpr = PyExprUtils.concatPyExprs(genPyExprsVisitorFactory.create(localVarStack, errorReporter).exec(cpcn));
            } else {
                // This is a param with content that cannot be represented as Python expressions, so we
                // assume that code has been generated to define the temporary variable 'param<n>'.
                String paramExpr = "param" + cpcn.getId();
                // The param can be assumed to be a list at this point since it was created as an output
                // variable.
                valuePyExpr = new PyListExpr(paramExpr, Integer.MAX_VALUE);
            }
            // Param content nodes require a content kind in strict autoescaping, so the content must be
            // wrapped as SanitizedContent.
            valuePyExpr = InternalPyExprUtils.wrapAsSanitizedContent(cpcn.getContentKind(), valuePyExpr.toPyString());
            additionalParams.put(key, valuePyExpr);
        }
    }
    PyExpr additionalParamsExpr = PyExprUtils.convertMapToPyExpr(additionalParams);
    // Cases 2 and 3: Additional params with and without original data to pass.
    if (callNode.isPassingData()) {
        // make a shallow copy so we don't accidentally modify the param
        dataToPass = "dict(" + dataToPass + ")";
        return "runtime.merge_into_dict(" + dataToPass + ", " + additionalParamsExpr.getText() + ")";
    } else {
        return additionalParamsExpr.getText();
    }
}
Also used : PyExpr(com.google.template.soy.pysrc.restricted.PyExpr) CallParamContentNode(com.google.template.soy.soytree.CallParamContentNode) CallParamNode(com.google.template.soy.soytree.CallParamNode) PyListExpr(com.google.template.soy.pysrc.restricted.PyListExpr) CallParamValueNode(com.google.template.soy.soytree.CallParamValueNode) PyStringExpr(com.google.template.soy.pysrc.restricted.PyStringExpr) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

PyStringExpr (com.google.template.soy.pysrc.restricted.PyStringExpr)22 PyExpr (com.google.template.soy.pysrc.restricted.PyExpr)21 Test (org.junit.Test)14 LinkedHashMap (java.util.LinkedHashMap)3 MsgPluralNode (com.google.template.soy.soytree.MsgPluralNode)2 SoyNode (com.google.template.soy.soytree.SoyNode)2 ParentSoyNode (com.google.template.soy.soytree.SoyNode.ParentSoyNode)2 ExprRootNode (com.google.template.soy.exprtree.ExprRootNode)1 SoyMsgPluralCaseSpec (com.google.template.soy.msgs.restricted.SoyMsgPluralCaseSpec)1 SoyMsgPluralPart (com.google.template.soy.msgs.restricted.SoyMsgPluralPart)1 PyFunctionExprBuilder (com.google.template.soy.pysrc.restricted.PyFunctionExprBuilder)1 PyListExpr (com.google.template.soy.pysrc.restricted.PyListExpr)1 SoyPySrcPrintDirective (com.google.template.soy.pysrc.restricted.SoyPySrcPrintDirective)1 SoyPrintDirective (com.google.template.soy.shared.restricted.SoyPrintDirective)1 AbstractParentSoyNode (com.google.template.soy.soytree.AbstractParentSoyNode)1 CallNode (com.google.template.soy.soytree.CallNode)1 CallParamContentNode (com.google.template.soy.soytree.CallParamContentNode)1 CallParamNode (com.google.template.soy.soytree.CallParamNode)1 CallParamValueNode (com.google.template.soy.soytree.CallParamValueNode)1 IfCondNode (com.google.template.soy.soytree.IfCondNode)1