Search in sources :

Example 1 with Case

use of com.google.template.soy.msgs.restricted.SoyMsgPart.Case in project closure-templates by google.

the class MsgCompiler method partToPartExpression.

/**
 * Returns an {@link Expression} that evaluates to an equivalent SoyMsgPart as the argument.
 */
private Expression partToPartExpression(SoyMsgPart part) {
    if (part instanceof SoyMsgPlaceholderPart) {
        return SOY_MSG_PLACEHOLDER_PART.construct(constant(((SoyMsgPlaceholderPart) part).getPlaceholderName()), constantNull(STRING_TYPE));
    } else if (part instanceof SoyMsgPluralPart) {
        SoyMsgPluralPart pluralPart = (SoyMsgPluralPart) part;
        List<Expression> caseExprs = new ArrayList<>(pluralPart.getCases().size());
        for (Case<SoyMsgPluralCaseSpec> item : pluralPart.getCases()) {
            Expression spec;
            if (item.spec().getType() == Type.EXPLICIT) {
                spec = SOY_MSG_PLURAL_CASE_SPEC_LONG.construct(constant(item.spec().getExplicitValue()));
            } else {
                spec = SOY_MSG_PLURAL_CASE_SPEC_TYPE.construct(FieldRef.enumReference(item.spec().getType()).accessor());
            }
            caseExprs.add(CASE_CREATE.invoke(spec, partsToPartsList(item.parts())));
        }
        return SOY_MSG_PURAL_PART.construct(constant(pluralPart.getPluralVarName()), constant(pluralPart.getOffset()), BytecodeUtils.asList(caseExprs));
    } else if (part instanceof SoyMsgPluralRemainderPart) {
        return SOY_MSG_PLURAL_REMAINDER_PART.construct(constant(((SoyMsgPluralRemainderPart) part).getPluralVarName()));
    } else if (part instanceof SoyMsgRawTextPart) {
        return SOY_MSG_RAW_TEXT_PART_OF.invoke(constant(((SoyMsgRawTextPart) part).getRawText(), variables));
    } else if (part instanceof SoyMsgSelectPart) {
        SoyMsgSelectPart selectPart = (SoyMsgSelectPart) part;
        List<Expression> caseExprs = new ArrayList<>(selectPart.getCases().size());
        for (Case<String> item : selectPart.getCases()) {
            caseExprs.add(CASE_CREATE.invoke(item.spec() == null ? constantNull(STRING_TYPE) : constant(item.spec()), partsToPartsList(item.parts())));
        }
        return SOY_MSG_SELECT_PART.construct(constant(selectPart.getSelectVarName()), BytecodeUtils.asList(caseExprs));
    } else {
        throw new AssertionError("unrecognized part: " + part);
    }
}
Also used : SoyMsgPluralRemainderPart(com.google.template.soy.msgs.restricted.SoyMsgPluralRemainderPart) SoyMsgPluralPart(com.google.template.soy.msgs.restricted.SoyMsgPluralPart) ArrayList(java.util.ArrayList) SoyMsgSelectPart(com.google.template.soy.msgs.restricted.SoyMsgSelectPart) Case(com.google.template.soy.msgs.restricted.SoyMsgPart.Case) SoyMsgPlaceholderPart(com.google.template.soy.msgs.restricted.SoyMsgPlaceholderPart) SoyExpression(com.google.template.soy.jbcsrc.restricted.SoyExpression) Expression(com.google.template.soy.jbcsrc.restricted.Expression) SoyMsgRawTextPart(com.google.template.soy.msgs.restricted.SoyMsgRawTextPart) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List)

Aggregations

ImmutableList (com.google.common.collect.ImmutableList)1 Expression (com.google.template.soy.jbcsrc.restricted.Expression)1 SoyExpression (com.google.template.soy.jbcsrc.restricted.SoyExpression)1 Case (com.google.template.soy.msgs.restricted.SoyMsgPart.Case)1 SoyMsgPlaceholderPart (com.google.template.soy.msgs.restricted.SoyMsgPlaceholderPart)1 SoyMsgPluralPart (com.google.template.soy.msgs.restricted.SoyMsgPluralPart)1 SoyMsgPluralRemainderPart (com.google.template.soy.msgs.restricted.SoyMsgPluralRemainderPart)1 SoyMsgRawTextPart (com.google.template.soy.msgs.restricted.SoyMsgRawTextPart)1 SoyMsgSelectPart (com.google.template.soy.msgs.restricted.SoyMsgSelectPart)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1