use of com.google.template.soy.data.SoyDataException in project closure-templates by google.
the class RenderVisitor method visitCallDelegateNode.
@Override
protected void visitCallDelegateNode(CallDelegateNode node) {
ExprRootNode variantExpr = node.getDelCalleeVariantExpr();
String variant;
if (variantExpr == null) {
variant = "";
} else {
try {
SoyValue variantData = eval(variantExpr, node);
if (variantData instanceof IntegerData) {
// An integer constant is being used as variant. Use the value string representation as
// variant.
variant = String.valueOf(variantData.longValue());
} else {
// Variant is either a StringData or a SanitizedContent. Use the value as a string. If
// the value is not a string, and exception will be thrown.
variant = variantData.stringValue();
}
} catch (SoyDataException e) {
throw RenderException.createWithSource(String.format("Variant expression \"%s\" doesn't evaluate to a valid type " + "(Only string and integer are supported).", variantExpr.toSourceString()), e, node);
}
}
DelTemplateKey delegateKey = DelTemplateKey.create(node.getDelCalleeName(), variant);
TemplateDelegateNode callee;
try {
callee = templateRegistry.selectDelTemplate(delegateKey, activeDelPackageSelector);
} catch (IllegalArgumentException e) {
throw RenderException.createWithSource(e.getMessage(), e, node);
}
if (callee != null) {
visitCallNodeHelper(node, callee);
} else if (node.allowEmptyDefault()) {
// no active delegate implementation, so the call output is empty string
return;
} else {
throw RenderException.createWithSource("Found no active impl for delegate call to \"" + node.getDelCalleeName() + (variant.isEmpty() ? "" : ":" + variant) + "\" (and delcall does not set allowemptydefault=\"true\").", node);
}
}
use of com.google.template.soy.data.SoyDataException in project closure-templates by google.
the class RenderVisitorAssistantForMsgs method visitMsgSelectNode.
@Override
protected void visitMsgSelectNode(MsgSelectNode node) {
ExprRootNode selectExpr = node.getExpr();
String selectValue;
try {
selectValue = master.evalForUseByAssistants(selectExpr, node).stringValue();
} catch (SoyDataException e) {
throw RenderException.createWithSource(String.format("Select expression \"%s\" doesn't evaluate to string.", selectExpr.toSourceString()), e, node);
}
// Check each case.
for (CaseOrDefaultNode child : node.getChildren()) {
if (child instanceof MsgSelectDefaultNode) {
// This means it didn't match any other case.
visitChildren(child);
} else {
if (((MsgSelectCaseNode) child).getCaseValue().equals(selectValue)) {
visitChildren(child);
return;
}
}
}
}
use of com.google.template.soy.data.SoyDataException in project closure-templates by google.
the class RenderVisitorAssistantForMsgs method visitMsgPluralNode.
@Override
protected void visitMsgPluralNode(MsgPluralNode node) {
ExprRootNode pluralExpr = node.getExpr();
double pluralValue;
try {
pluralValue = master.evalForUseByAssistants(pluralExpr, node).numberValue();
} catch (SoyDataException e) {
throw RenderException.createWithSource(String.format("Plural expression \"%s\" doesn't evaluate to number.", pluralExpr.toSourceString()), e, node);
}
// Check each case.
for (CaseOrDefaultNode child : node.getChildren()) {
if (child instanceof MsgPluralDefaultNode) {
// This means it didn't match any other case.
visitChildren(child);
break;
} else {
if (((MsgPluralCaseNode) child).getCaseNumber() == pluralValue) {
visitChildren(child);
break;
}
}
}
}
use of com.google.template.soy.data.SoyDataException in project closure-templates by google.
the class InternalValueUtils method convertCompileTimeGlobalsMap.
/**
* Converts a compile-time globals map in user-provided format into one in the internal format.
*
* <p>The returned map will have the same iteration order as the provided map.
*
* @param compileTimeGlobalsMap Map from compile-time global name to value. The values can be any
* of the Soy primitive types: null, boolean, integer, float (Java double), or string.
* @return An equivalent map in the internal format.
* @throws IllegalArgumentException If the map contains an invalid value.
*/
public static ImmutableMap<String, PrimitiveData> convertCompileTimeGlobalsMap(Map<String, ?> compileTimeGlobalsMap) {
ImmutableMap.Builder<String, PrimitiveData> resultMapBuilder = ImmutableMap.builder();
for (Map.Entry<String, ?> entry : compileTimeGlobalsMap.entrySet()) {
Object valueObj = entry.getValue();
PrimitiveData value;
boolean isValidValue = true;
try {
SoyValue value0 = SoyValueConverter.INSTANCE.convert(valueObj).resolve();
if (!(value0 instanceof PrimitiveData)) {
isValidValue = false;
}
value = (PrimitiveData) value0;
} catch (SoyDataException sde) {
isValidValue = false;
// make compiler happy
value = null;
}
if (!isValidValue) {
throw new IllegalArgumentException("Compile-time globals map contains invalid value: " + valueObj + " for key: " + entry.getKey());
}
resultMapBuilder.put(entry.getKey(), value);
}
return resultMapBuilder.build();
}
use of com.google.template.soy.data.SoyDataException in project closure-templates by google.
the class CollectionData method put.
/**
* Puts data into this data tree at the specified key string.
*
* @param keyStr One or more map keys and/or list indices (separated by '.' if multiple parts).
* Indicates the path to the location within this data tree.
* @param value The data to put at the specified location.
*/
public void put(String keyStr, SoyData value) {
List<String> keys = split(keyStr, '.');
int numKeys = keys.size();
CollectionData collectionData = this;
for (int i = 0; i <= numKeys - 2; ++i) {
SoyData nextSoyData = collectionData.getSingle(keys.get(i));
if (nextSoyData != null && !(nextSoyData instanceof CollectionData)) {
throw new SoyDataException("Failed to evaluate key string \"" + keyStr + "\" for put().");
}
CollectionData nextCollectionData = (CollectionData) nextSoyData;
if (nextCollectionData == null) {
// Create the SoyData object that will be bound to keys.get(i). We need to check the first
// part of keys[i+1] to know whether to create a SoyMapData or SoyListData (checking the
// first char is sufficient).
nextCollectionData = (Character.isDigit(keys.get(i + 1).charAt(0))) ? new SoyListData() : new SoyMapData();
collectionData.putSingle(keys.get(i), nextCollectionData);
}
collectionData = nextCollectionData;
}
collectionData.putSingle(keys.get(numKeys - 1), ensureValidValue(value));
}
Aggregations