Search in sources :

Example 16 with TemplateParam

use of com.google.template.soy.soytree.defn.TemplateParam in project closure-templates by google.

the class TemplateNodeTest method testParseSoyDoc.

@Test
public void testParseSoyDoc() {
    String soyDoc = "" + "/**\n" + " * Test template.\n" + " *\n" + " * @param foo Foo to print.\n" + " * @param? goo\n" + " *     Goo to print.\n" + " */";
    TemplateNode tn = parse("{namespace ns}\n" + "/**\n" + " * Test template.\n" + " *\n" + " * @param foo Foo to print.\n" + " * @param? goo\n" + " *     Goo to print.\n" + " */" + "{template .boo}{$foo}{$goo}{/template}");
    assertEquals(soyDoc, tn.getSoyDoc());
    assertEquals("Test template.", tn.getSoyDocDesc());
    List<TemplateParam> params = tn.getParams();
    assertEquals(2, params.size());
    SoyDocParam soyDocParam0 = (SoyDocParam) params.get(0);
    assertEquals(DeclLoc.SOY_DOC, soyDocParam0.declLoc());
    assertEquals("foo", soyDocParam0.name());
    assertEquals(true, soyDocParam0.isRequired());
    assertEquals("Foo to print.", soyDocParam0.desc());
    SoyDocParam soyDocParam1 = (SoyDocParam) params.get(1);
    assertEquals(DeclLoc.SOY_DOC, soyDocParam1.declLoc());
    assertEquals("goo", soyDocParam1.name());
    assertEquals(false, soyDocParam1.isRequired());
    assertEquals("Goo to print.", soyDocParam1.desc());
}
Also used : TemplateParam(com.google.template.soy.soytree.defn.TemplateParam) SoyDocParam(com.google.template.soy.soytree.defn.SoyDocParam) Test(org.junit.Test)

Example 17 with TemplateParam

use of com.google.template.soy.soytree.defn.TemplateParam in project closure-templates by google.

the class TemplateNodeTest method testParseHeaderDecls.

@Test
public void testParseHeaderDecls() {
    TemplateNode tn = parse("{namespace ns}\n" + "/**@param foo */\n" + "{template .boo}{$foo}{/template}");
    List<TemplateParam> params = tn.getParams();
    assertThat(params).hasSize(1);
    SoyDocParam soyDocParam0 = (SoyDocParam) params.get(0);
    assertEquals("foo", soyDocParam0.name());
    assertThat(ImmutableList.copyOf(tn.getAllParams())).hasSize(1);
}
Also used : TemplateParam(com.google.template.soy.soytree.defn.TemplateParam) SoyDocParam(com.google.template.soy.soytree.defn.SoyDocParam) Test(org.junit.Test)

Example 18 with TemplateParam

use of com.google.template.soy.soytree.defn.TemplateParam in project closure-templates by google.

the class TemplateCompiler method generateConstructor.

/**
 * Generate a public constructor that assigns our final field and checks for missing required
 * params.
 *
 * <p>This constructor is called by the generate factory classes.
 *
 * @param fieldInitializers additional statements to initialize fields (other than params)
 */
private void generateConstructor(Statement fieldInitializers) {
    final Label start = new Label();
    final Label end = new Label();
    final LocalVariable thisVar = createThisVar(template.typeInfo(), start, end);
    final LocalVariable paramsVar = createLocal("params", 1, SOY_RECORD_TYPE, start, end);
    final LocalVariable ijVar = createLocal("ij", 2, SOY_RECORD_TYPE, start, end);
    final List<Statement> assignments = new ArrayList<>();
    // for other fields needed by the compiler.
    assignments.add(fieldInitializers);
    assignments.add(paramsField.putInstanceField(thisVar, paramsVar));
    assignments.add(ijField.putInstanceField(thisVar, ijVar));
    for (TemplateParam param : template.node().getAllParams()) {
        Expression paramProvider = getParam(paramsVar, ijVar, param);
        assignments.add(paramFields.get(param.name()).putInstanceField(thisVar, paramProvider));
    }
    Statement constructorBody = new Statement() {

        @Override
        protected void doGen(CodeBuilder ga) {
            ga.mark(start);
            // call super()
            thisVar.gen(ga);
            ga.invokeConstructor(OBJECT.type(), NULLARY_INIT);
            for (Statement assignment : assignments) {
                assignment.gen(ga);
            }
            ga.visitInsn(Opcodes.RETURN);
            ga.visitLabel(end);
            thisVar.tableEntry(ga);
            paramsVar.tableEntry(ga);
            ijVar.tableEntry(ga);
        }
    };
    constructorBody.writeMethod(Opcodes.ACC_PUBLIC, template.constructor().method(), writer);
}
Also used : Expression(com.google.template.soy.jbcsrc.restricted.Expression) Statement(com.google.template.soy.jbcsrc.restricted.Statement) Label(org.objectweb.asm.Label) LocalVariable(com.google.template.soy.jbcsrc.restricted.LocalVariable) ArrayList(java.util.ArrayList) TemplateParam(com.google.template.soy.soytree.defn.TemplateParam) CodeBuilder(com.google.template.soy.jbcsrc.restricted.CodeBuilder)

Aggregations

TemplateParam (com.google.template.soy.soytree.defn.TemplateParam)18 TemplateNode (com.google.template.soy.soytree.TemplateNode)7 IndirectParamsInfo (com.google.template.soy.passes.FindIndirectParamsVisitor.IndirectParamsInfo)4 Test (org.junit.Test)4 TemplateBasicNode (com.google.template.soy.soytree.TemplateBasicNode)3 TemplateDelegateNode (com.google.template.soy.soytree.TemplateDelegateNode)3 SoyType (com.google.template.soy.types.SoyType)3 ArrayList (java.util.ArrayList)3 HashSet (java.util.HashSet)3 ImmutableList (com.google.common.collect.ImmutableList)2 ImmutableMap (com.google.common.collect.ImmutableMap)2 SourceLocation (com.google.template.soy.base.SourceLocation)2 VarRefNode (com.google.template.soy.exprtree.VarRefNode)2 FindIndirectParamsVisitor (com.google.template.soy.passes.FindIndirectParamsVisitor)2 HeaderParam (com.google.template.soy.soytree.defn.HeaderParam)2 SoyDocParam (com.google.template.soy.soytree.defn.SoyDocParam)2 LinkedHashMap (java.util.LinkedHashMap)2 ImmutableMultimap (com.google.common.collect.ImmutableMultimap)1 FieldDescriptor (com.google.protobuf.Descriptors.FieldDescriptor)1 IndentedLinesBuilder (com.google.template.soy.base.internal.IndentedLinesBuilder)1