Search in sources :

Example 1 with FieldAccess

use of com.google.template.soy.jssrc.internal.NullSafeAccumulator.FieldAccess in project closure-templates by google.

the class TranslateExprNodeVisitor method genCodeForFieldAccess.

/**
 * Generates the code for a field access, e.g. {@code .foo} or {@code .getFoo()}.
 *
 * @param baseType The type of the object that contains the field.
 * @param fieldAccessNode The field access node.
 * @param fieldName The field name.
 */
private FieldAccess genCodeForFieldAccess(SoyType baseType, FieldAccessNode fieldAccessNode, String fieldName) {
    Preconditions.checkNotNull(baseType);
    // type, and then see if they all agree.
    if (baseType.getKind() == SoyType.Kind.UNION) {
        // TODO(msamuel): We will need to generate fallback code for each variant.
        UnionType unionType = (UnionType) baseType;
        FieldAccess fieldAccess = null;
        for (SoyType memberType : unionType.getMembers()) {
            if (memberType.getKind() != SoyType.Kind.NULL) {
                FieldAccess fieldAccessForType = genCodeForFieldAccess(memberType, fieldAccessNode, fieldName);
                if (fieldAccess == null) {
                    fieldAccess = fieldAccessForType;
                } else if (!fieldAccess.equals(fieldAccessForType)) {
                    errorReporter.report(fieldAccessNode.getSourceLocation(), UNION_ACCESSOR_MISMATCH, fieldName, baseType);
                }
            }
        }
        return fieldAccess;
    }
    if (baseType.getKind() == SoyType.Kind.PROTO) {
        SoyProtoType protoType = (SoyProtoType) baseType;
        FieldDescriptor desc = protoType.getFieldDescriptor(fieldName);
        Preconditions.checkNotNull(desc, "Error in proto %s, field not found: %s", protoType.getDescriptor().getFullName(), fieldName);
        return FieldAccess.protoCall(fieldName, desc);
    }
    return FieldAccess.id(fieldName);
}
Also used : UnionType(com.google.template.soy.types.UnionType) SoyType(com.google.template.soy.types.SoyType) FieldAccess(com.google.template.soy.jssrc.internal.NullSafeAccumulator.FieldAccess) SoyProtoType(com.google.template.soy.types.SoyProtoType) FieldDescriptor(com.google.protobuf.Descriptors.FieldDescriptor)

Aggregations

FieldDescriptor (com.google.protobuf.Descriptors.FieldDescriptor)1 FieldAccess (com.google.template.soy.jssrc.internal.NullSafeAccumulator.FieldAccess)1 SoyProtoType (com.google.template.soy.types.SoyProtoType)1 SoyType (com.google.template.soy.types.SoyType)1 UnionType (com.google.template.soy.types.UnionType)1