use of com.google.template.soy.data.SoyValue in project closure-templates by google.
the class SoyMapImpl method render.
@Override
public void render(LoggingAdvisingAppendable appendable) throws IOException {
appendable.append('{');
boolean isFirst = true;
for (SoyValue key : keys()) {
SoyValue value = get(key);
if (isFirst) {
isFirst = false;
} else {
appendable.append(", ");
}
key.render(appendable);
appendable.append(": ");
value.render(appendable);
}
appendable.append('}');
}
use of com.google.template.soy.data.SoyValue in project closure-templates by google.
the class DictImpl method render.
@Override
public void render(LoggingAdvisingAppendable appendable) throws IOException {
appendable.append('{');
boolean isFirst = true;
boolean useNewSoyMap = typeTracker.type() == RuntimeMapTypeTracker.Type.MAP;
for (SoyValue key : (useNewSoyMap ? keys() : getItemKeys())) {
SoyValue value = useNewSoyMap ? get(key) : getItem(key);
if (isFirst) {
isFirst = false;
} else {
appendable.append(", ");
}
key.render(appendable);
appendable.append(": ");
value.render(appendable);
}
appendable.append('}');
}
use of com.google.template.soy.data.SoyValue in project closure-templates by google.
the class EvalVisitor method visitModOpNode.
@Override
protected SoyValue visitModOpNode(ModOpNode node) {
SoyValue operand0 = visit(node.getChild(0));
SoyValue operand1 = visit(node.getChild(1));
return convertResult(operand0.longValue() % operand1.longValue());
}
use of com.google.template.soy.data.SoyValue in project closure-templates by google.
the class EvalVisitor method visitNullSafeFieldAccessNode.
private SoyValue visitNullSafeFieldAccessNode(FieldAccessNode fieldAccess) {
SoyValue base = visitNullSafeNodeRecurse(fieldAccess.getBaseExprChild());
// attempting field access on non-SoyRecord
if (!(base instanceof SoyRecord) && !(base instanceof SoyProtoValue)) {
if (base == NullSafetySentinel.INSTANCE) {
// Bail out if base expression failed a null-safety check.
return NullSafetySentinel.INSTANCE;
}
if (fieldAccess.isNullSafe()) {
if (isNullOrUndefinedBase(base)) {
// Return the sentinel value that indicates that a null-safety check failed.
return NullSafetySentinel.INSTANCE;
} else {
throw RenderException.create(String.format("While evaluating \"%s\", encountered non-record just before accessing \"%s\".", fieldAccess.toSourceString(), fieldAccess.getSourceStringSuffix()));
}
}
// TODO: If feasible, find and fix existing instances, then throw RenderException here.
return UndefinedData.INSTANCE;
}
// the base type is possibly nullable, so remove null before testing for being a proto
if (SoyTypes.tryRemoveNull(fieldAccess.getBaseExprChild().getType()).getKind() == Kind.PROTO) {
return ((SoyProtoValue) base).getProtoField(fieldAccess.getFieldName());
}
maybeMarkBadProtoAccess(fieldAccess, base);
// base is a valid SoyRecord: get value
SoyValue value = ((SoyRecord) base).getField(fieldAccess.getFieldName());
// TODO(user): Consider cleaning up the null / NullData inconsistencies.
if (value != null && !TofuTypeChecks.isInstance(fieldAccess.getType(), value)) {
throw RenderException.create(String.format("Expected value of type '%s', but actual type was '%s'.", fieldAccess.getType(), value.getClass().getSimpleName()));
}
return (value != null) ? value : UndefinedData.INSTANCE;
}
use of com.google.template.soy.data.SoyValue in project closure-templates by google.
the class EvalVisitor method visitProtoInitNode.
@Override
protected SoyValue visitProtoInitNode(ProtoInitNode node) {
// The downcast is safe because if it was anything else, compilation would have already failed.
SoyProtoType soyProto = (SoyProtoType) node.getType();
ImmutableList<String> paramNames = node.getParamNames();
SoyProtoValueImpl.Builder builder = new SoyProtoValueImpl.Builder(soyProto.getDescriptor());
for (int i = 0; i < node.numChildren(); i++) {
SoyValue visit = visit(node.getChild(i));
// null means don't assign
if (visit instanceof NullData || visit instanceof UndefinedData) {
continue;
}
builder.setField(paramNames.get(i), visit);
}
return builder.build();
}
Aggregations