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));
}
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')");
}
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)");
}
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();
}
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();
}
}
Aggregations