Search in sources :

Example 81 with TypeNode

use of com.google.api.generator.engine.ast.TypeNode in project gapic-generator-java by googleapis.

the class ImportWriterVisitorTest method writeMethodDefinitionImports_templatedMixedNamesAndTypes.

@Test
public void writeMethodDefinitionImports_templatedMixedNamesAndTypes() {
    Reference mapRef = ConcreteReference.withClazz(Map.class);
    List<VariableExpr> arguments = Arrays.asList(VariableExpr.builder().setVariable(createVariable("x", TypeNode.withReference(mapRef))).setIsDecl(true).setTemplateObjects(Arrays.asList("K", TypeNode.withReference(ConcreteReference.withClazz(AssignmentExpr.class)))).build(), VariableExpr.builder().setVariable(createVariable("y", TypeNode.withReference(mapRef))).setIsDecl(true).setTemplateObjects(Arrays.asList("T", "V")).build());
    TypeNode returnType = TypeNode.withReference(mapRef);
    MethodDefinition methodDefinition = MethodDefinition.builder().setName("close").setScope(ScopeNode.PUBLIC).setReturnType(returnType).setTemplateNames(Arrays.asList("T", "K", "V")).setReturnTemplateNames(Arrays.asList("K", "V")).setArguments(arguments).setReturnExpr(MethodInvocationExpr.builder().setMethodName("foobar").setReturnType(returnType).build()).build();
    methodDefinition.accept(writerVisitor);
    assertEquals(LineFormatter.lines("import com.google.api.generator.engine.ast.AssignmentExpr;\n", "import java.util.Map;\n\n"), writerVisitor.write());
}
Also used : Reference(com.google.api.generator.engine.ast.Reference) ConcreteReference(com.google.api.generator.engine.ast.ConcreteReference) VaporReference(com.google.api.generator.engine.ast.VaporReference) MethodDefinition(com.google.api.generator.engine.ast.MethodDefinition) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr) TypeNode(com.google.api.generator.engine.ast.TypeNode) Test(org.junit.Test)

Example 82 with TypeNode

use of com.google.api.generator.engine.ast.TypeNode in project gapic-generator-java by googleapis.

the class ImportWriterVisitorTest method writeAnonymousClassExprImports.

@Test
public void writeAnonymousClassExprImports() {
    // [Constructing] Function<List<IOException>, MethodDefinition>
    ConcreteReference exceptionListRef = ConcreteReference.builder().setClazz(List.class).setGenerics(Arrays.asList(ConcreteReference.withClazz(IOException.class))).build();
    ConcreteReference methodDefinitionRef = ConcreteReference.withClazz(MethodDefinition.class);
    ConcreteReference ref = ConcreteReference.builder().setClazz(Function.class).setGenerics(Arrays.asList(exceptionListRef, methodDefinitionRef)).build();
    TypeNode type = TypeNode.withReference(ref);
    // [Constructing] HashMap<String, Integer> map;
    ConcreteReference mapRef = ConcreteReference.builder().setClazz(HashMap.class).setGenerics(Arrays.asList(ConcreteReference.withClazz(String.class), ConcreteReference.withClazz(Integer.class))).build();
    VariableExpr mapExpr = VariableExpr.builder().setVariable(Variable.builder().setName("map").setType(TypeNode.withReference(mapRef)).build()).setIsDecl(true).build();
    ExprStatement exprStatement = ExprStatement.withExpr(mapExpr);
    // [Constructing] an input argument whose type is `List<IOException>`
    VariableExpr arg = VariableExpr.builder().setVariable(Variable.builder().setName("arg").setType(TypeNode.withReference(exceptionListRef)).build()).setIsDecl(true).build();
    // [Constructing] a return variable expression whose type is `MethodDefinition`
    VariableExpr returnArg = VariableExpr.builder().setVariable(Variable.builder().setName("returnArg").setType(TypeNode.withReference(methodDefinitionRef)).build()).build();
    MethodDefinition method = MethodDefinition.builder().setScope(ScopeNode.PUBLIC).setReturnType(TypeNode.withReference(methodDefinitionRef)).setArguments(Arrays.asList(arg)).setReturnExpr(returnArg).setName("apply").build();
    AnonymousClassExpr anonymousClassExpr = AnonymousClassExpr.builder().setType(type).setMethods(Arrays.asList(method)).setStatements(Arrays.asList(exprStatement)).build();
    anonymousClassExpr.accept(writerVisitor);
    assertEquals(LineFormatter.lines("import com.google.api.generator.engine.ast.MethodDefinition;\n", "import com.google.common.base.Function;\n", "import java.io.IOException;\n", "import java.util.HashMap;\n", "import java.util.List;\n\n"), writerVisitor.write());
}
Also used : MethodDefinition(com.google.api.generator.engine.ast.MethodDefinition) ExprStatement(com.google.api.generator.engine.ast.ExprStatement) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) IOException(java.io.IOException) TypeNode(com.google.api.generator.engine.ast.TypeNode) ConcreteReference(com.google.api.generator.engine.ast.ConcreteReference) AnonymousClassExpr(com.google.api.generator.engine.ast.AnonymousClassExpr) Test(org.junit.Test)

Example 83 with TypeNode

use of com.google.api.generator.engine.ast.TypeNode in project gapic-generator-java by googleapis.

the class ImportWriterVisitorTest method writeReferenceConstructorExprImports_basic.

@Test
public void writeReferenceConstructorExprImports_basic() {
    VaporReference ref = VaporReference.builder().setName("Parent").setPakkage("com.google.example.v1").build();
    TypeNode classType = TypeNode.withReference(ref);
    ReferenceConstructorExpr referenceConstructorExpr = ReferenceConstructorExpr.superBuilder().setType(classType).build();
    referenceConstructorExpr.accept(writerVisitor);
    assertEquals("import com.google.example.v1.Parent;\n\n", writerVisitor.write());
}
Also used : VaporReference(com.google.api.generator.engine.ast.VaporReference) TypeNode(com.google.api.generator.engine.ast.TypeNode) ReferenceConstructorExpr(com.google.api.generator.engine.ast.ReferenceConstructorExpr) Test(org.junit.Test)

Example 84 with TypeNode

use of com.google.api.generator.engine.ast.TypeNode in project gapic-generator-java by googleapis.

the class Message method findAndUnwrapPaginatedRepeatedField.

/**
 * Returns the first list repeated field in a message, unwrapped from its list type.
 */
@Nullable
public Field findAndUnwrapPaginatedRepeatedField() {
    for (Field field : fields()) {
        if (field.isMap()) {
            List<Reference> repeatedGenericMapRefs = field.type().reference().generics();
            TypeNode paginatedType = TypeNode.withReference(ConcreteReference.builder().setClazz(Map.Entry.class).setGenerics(Arrays.asList(repeatedGenericMapRefs.get(0), repeatedGenericMapRefs.get(1))).build());
            return field.toBuilder().setType(paginatedType).build();
        }
    }
    for (Field field : fields()) {
        if (field.isRepeated() && !field.isMap()) {
            Reference repeatedGenericRef = field.type().reference().generics().get(0);
            return field.toBuilder().setType(TypeNode.withReference(repeatedGenericRef)).build();
        }
    }
    return null;
}
Also used : ConcreteReference(com.google.api.generator.engine.ast.ConcreteReference) Reference(com.google.api.generator.engine.ast.Reference) TypeNode(com.google.api.generator.engine.ast.TypeNode) Nullable(javax.annotation.Nullable)

Example 85 with TypeNode

use of com.google.api.generator.engine.ast.TypeNode in project gapic-generator-java by googleapis.

the class MethodSignatureParserTest method flattenMethodSignatures_manyToOne.

@Test
public void flattenMethodSignatures_manyToOne() {
    String fooName = "fooName";
    String anInt = "anInt";
    TypeNode fooTypeOne = TypeNode.withReference(VaporReference.builder().setName("FooName").setPakkage("com.google.foobar").build());
    TypeNode fooTypeTwo = TypeNode.withReference(VaporReference.builder().setName("FooTwoName").setPakkage("com.google.foobar").build());
    List<String> argumentNames = Arrays.asList(fooName, anInt);
    BiFunction<String, TypeNode, MethodArgument> methodArgFn = (name, type) -> MethodArgument.builder().setName(name).setType(type).setField(Field.builder().setName(name).setType(type).build()).build();
    List<MethodArgument> fooArgs = Arrays.asList(TypeNode.STRING, fooTypeOne, fooTypeTwo).stream().map(t -> methodArgFn.apply(fooName, t)).collect(Collectors.toList());
    Map<String, List<MethodArgument>> argumentNameToOverloads = new HashMap<>();
    argumentNameToOverloads.put(fooName, fooArgs);
    argumentNameToOverloads.put(anInt, Arrays.asList(methodArgFn.apply(anInt, TypeNode.INT)));
    List<List<MethodArgument>> flattenedSignatures = MethodSignatureParser.flattenMethodSignatureVariants(argumentNames, argumentNameToOverloads);
    assertEquals(3, flattenedSignatures.size());
    assertTrue(containsTypes(flattenedSignatures, Arrays.asList(TypeNode.STRING, TypeNode.INT)));
    assertTrue(containsTypes(flattenedSignatures, Arrays.asList(fooTypeOne, TypeNode.INT)));
    assertTrue(containsTypes(flattenedSignatures, Arrays.asList(fooTypeTwo, TypeNode.INT)));
}
Also used : VaporReference(com.google.api.generator.engine.ast.VaporReference) Arrays(java.util.Arrays) TypeNode(com.google.api.generator.engine.ast.TypeNode) BiFunction(java.util.function.BiFunction) Assert.assertTrue(org.junit.Assert.assertTrue) HashMap(java.util.HashMap) Test(org.junit.Test) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) Field(com.google.api.generator.gapic.model.Field) List(java.util.List) MethodArgument(com.google.api.generator.gapic.model.MethodArgument) Map(java.util.Map) Assert.assertEquals(org.junit.Assert.assertEquals) MethodArgument(com.google.api.generator.gapic.model.MethodArgument) HashMap(java.util.HashMap) List(java.util.List) TypeNode(com.google.api.generator.engine.ast.TypeNode) Test(org.junit.Test)

Aggregations

TypeNode (com.google.api.generator.engine.ast.TypeNode)232 VariableExpr (com.google.api.generator.engine.ast.VariableExpr)119 MethodInvocationExpr (com.google.api.generator.engine.ast.MethodInvocationExpr)111 Test (org.junit.Test)103 AssignmentExpr (com.google.api.generator.engine.ast.AssignmentExpr)84 Message (com.google.api.generator.gapic.model.Message)81 ArrayList (java.util.ArrayList)81 Expr (com.google.api.generator.engine.ast.Expr)77 ValueExpr (com.google.api.generator.engine.ast.ValueExpr)77 Method (com.google.api.generator.gapic.model.Method)72 NewObjectExpr (com.google.api.generator.engine.ast.NewObjectExpr)68 ResourceName (com.google.api.generator.gapic.model.ResourceName)65 ExprStatement (com.google.api.generator.engine.ast.ExprStatement)64 List (java.util.List)63 ConcreteReference (com.google.api.generator.engine.ast.ConcreteReference)61 MethodDefinition (com.google.api.generator.engine.ast.MethodDefinition)59 Variable (com.google.api.generator.engine.ast.Variable)55 Statement (com.google.api.generator.engine.ast.Statement)53 Collectors (java.util.stream.Collectors)46 RelationalOperationExpr (com.google.api.generator.engine.ast.RelationalOperationExpr)45