Search in sources :

Example 16 with MethodArgument

use of com.google.api.generator.gapic.model.MethodArgument in project gapic-generator-java by googleapis.

the class MethodSignatureParserTest method flattenMethodSignatures_basic.

@Test
public void flattenMethodSignatures_basic() {
    String fooName = "fooName";
    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);
    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);
    List<List<MethodArgument>> flattenedSignatures = MethodSignatureParser.flattenMethodSignatureVariants(argumentNames, argumentNameToOverloads);
    assertEquals(3, flattenedSignatures.size());
    assertTrue(containsTypes(flattenedSignatures, Arrays.asList(TypeNode.STRING)));
    assertTrue(containsTypes(flattenedSignatures, Arrays.asList(fooTypeOne)));
    assertTrue(containsTypes(flattenedSignatures, Arrays.asList(fooTypeTwo)));
}
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)

Example 17 with MethodArgument

use of com.google.api.generator.gapic.model.MethodArgument in project gapic-generator-java by googleapis.

the class MethodSignatureParserTest method flattenMethodSignatures_manyToMany.

@Test
public void flattenMethodSignatures_manyToMany() {
    String fooName = "fooName";
    String barName = "barName";
    String anInt = "anInt";
    String anotherInt = "anotherInt";
    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());
    TypeNode barTypeOne = TypeNode.withReference(VaporReference.builder().setName("BarName").setPakkage("com.google.foobar").build());
    TypeNode barTypeTwo = TypeNode.withReference(VaporReference.builder().setName("BarTwoName").setPakkage("com.google.foobar").build());
    TypeNode barTypeThree = TypeNode.withReference(VaporReference.builder().setName("BarCarName").setPakkage("com.google.foobar").build());
    List<String> argumentNames = Arrays.asList(fooName, anInt, barName, anotherInt);
    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());
    List<MethodArgument> barArgs = Arrays.asList(TypeNode.STRING, barTypeOne, barTypeTwo, barTypeThree).stream().map(t -> methodArgFn.apply(barName, 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)));
    argumentNameToOverloads.put(barName, barArgs);
    argumentNameToOverloads.put(anotherInt, Arrays.asList(methodArgFn.apply(anotherInt, TypeNode.INT)));
    List<List<MethodArgument>> flattenedSignatures = MethodSignatureParser.flattenMethodSignatureVariants(argumentNames, argumentNameToOverloads);
    assertEquals(12, flattenedSignatures.size());
    // Types 0 - 4: String, int, {String, BarName, BarTwoName, BarCarName}, int.
    assertTrue(containsTypes(flattenedSignatures, Arrays.asList(TypeNode.STRING, TypeNode.INT, TypeNode.STRING, TypeNode.INT)));
    assertTrue(containsTypes(flattenedSignatures, Arrays.asList(TypeNode.STRING, TypeNode.INT, barTypeOne, TypeNode.INT)));
    assertTrue(containsTypes(flattenedSignatures, Arrays.asList(TypeNode.STRING, TypeNode.INT, barTypeTwo, TypeNode.INT)));
    assertTrue(containsTypes(flattenedSignatures, Arrays.asList(TypeNode.STRING, TypeNode.INT, barTypeThree, TypeNode.INT)));
    // Types 5 - 8: FooName, int, {String, BarName, BarTwoName, BarCarName}, int.
    assertTrue(containsTypes(flattenedSignatures, Arrays.asList(fooTypeOne, TypeNode.INT, TypeNode.STRING, TypeNode.INT)));
    assertTrue(containsTypes(flattenedSignatures, Arrays.asList(fooTypeOne, TypeNode.INT, barTypeOne, TypeNode.INT)));
    assertTrue(containsTypes(flattenedSignatures, Arrays.asList(fooTypeOne, TypeNode.INT, barTypeTwo, TypeNode.INT)));
    assertTrue(containsTypes(flattenedSignatures, Arrays.asList(fooTypeOne, TypeNode.INT, barTypeThree, TypeNode.INT)));
    // Types 9 - 12: FooTwoName, int, {String, BarName, BarTwoName, BarCarName}, int.
    assertTrue(containsTypes(flattenedSignatures, Arrays.asList(fooTypeTwo, TypeNode.INT, TypeNode.STRING, TypeNode.INT)));
    assertTrue(containsTypes(flattenedSignatures, Arrays.asList(fooTypeTwo, TypeNode.INT, barTypeOne, TypeNode.INT)));
    assertTrue(containsTypes(flattenedSignatures, Arrays.asList(fooTypeTwo, TypeNode.INT, barTypeTwo, TypeNode.INT)));
    assertTrue(containsTypes(flattenedSignatures, Arrays.asList(fooTypeTwo, TypeNode.INT, barTypeThree, 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)

Example 18 with MethodArgument

use of com.google.api.generator.gapic.model.MethodArgument in project gapic-generator-java by googleapis.

the class MethodSignatureParserTest method flattenMethodSignatures_oneToMany.

@Test
public void flattenMethodSignatures_oneToMany() {
    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(anInt, fooName);
    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.INT, TypeNode.STRING)));
    assertTrue(containsTypes(flattenedSignatures, Arrays.asList(TypeNode.INT, fooTypeOne)));
    assertTrue(containsTypes(flattenedSignatures, Arrays.asList(TypeNode.INT, fooTypeTwo)));
}
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)

Example 19 with MethodArgument

use of com.google.api.generator.gapic.model.MethodArgument in project gapic-generator-java by googleapis.

the class ParserTest method parseMethodSignatures_basic.

@Test
public void parseMethodSignatures_basic() {
    MethodDescriptor echoMethodDescriptor = echoService.getMethods().get(0);
    TypeNode inputType = TypeParser.parseType(echoMethodDescriptor.getInputType());
    Map<String, Message> messageTypes = Parser.parseMessages(echoFileDescriptor);
    Map<String, ResourceName> resourceNames = Parser.parseResourceNames(echoFileDescriptor);
    Set<ResourceName> outputResourceNames = new HashSet<>();
    List<List<MethodArgument>> methodSignatures = MethodSignatureParser.parseMethodSignatures(echoMethodDescriptor, ECHO_PACKAGE, inputType, messageTypes, resourceNames, outputResourceNames);
    assertEquals(8, methodSignatures.size());
    // Signature contents: [].
    List<MethodArgument> methodArgs = methodSignatures.get(0);
    assertEquals(Collections.emptyList(), methodArgs);
    // Signature contents: ["parent"].
    methodArgs = methodSignatures.get(1);
    assertEquals(1, methodArgs.size());
    MethodArgument argument = methodArgs.get(0);
    TypeNode resourceNameType = TypeNode.withReference(ConcreteReference.withClazz(com.google.api.resourcenames.ResourceName.class));
    assertMethodArgumentEquals("parent", resourceNameType, ImmutableList.of(), argument);
    // Signature contents: ["error"].
    methodArgs = methodSignatures.get(2);
    assertEquals(1, methodArgs.size());
    argument = methodArgs.get(0);
    assertMethodArgumentEquals("error", TypeNode.withReference(createStatusReference()), ImmutableList.of(), argument);
    // Signature contents: ["name"], resource helper variant.
    methodArgs = methodSignatures.get(3);
    assertEquals(1, methodArgs.size());
    argument = methodArgs.get(0);
    TypeNode foobarNameType = TypeNode.withReference(VaporReference.builder().setName("FoobarName").setPakkage(ECHO_PACKAGE).build());
    assertMethodArgumentEquals("name", foobarNameType, ImmutableList.of(), argument);
    // Signature contents: ["content"].
    methodArgs = methodSignatures.get(4);
    assertEquals(1, methodArgs.size());
    argument = methodArgs.get(0);
    assertMethodArgumentEquals("content", TypeNode.STRING, ImmutableList.of(), argument);
    // Signature contents: ["name"], String variant.
    methodArgs = methodSignatures.get(5);
    assertEquals(1, methodArgs.size());
    argument = methodArgs.get(0);
    assertMethodArgumentEquals("name", TypeNode.STRING, ImmutableList.of(), argument);
    // Signature contents: ["parent"], String variant.
    methodArgs = methodSignatures.get(6);
    assertEquals(1, methodArgs.size());
    argument = methodArgs.get(0);
    assertMethodArgumentEquals("parent", TypeNode.STRING, ImmutableList.of(), argument);
    // Signature contents: ["content", "severity"].
    methodArgs = methodSignatures.get(7);
    assertEquals(2, methodArgs.size());
    argument = methodArgs.get(0);
    assertMethodArgumentEquals("content", TypeNode.STRING, ImmutableList.of(), argument);
    argument = methodArgs.get(1);
    assertMethodArgumentEquals("severity", TypeNode.withReference(VaporReference.builder().setName("Severity").setPakkage(ECHO_PACKAGE).build()), ImmutableList.of(), argument);
}
Also used : Message(com.google.api.generator.gapic.model.Message) MethodArgument(com.google.api.generator.gapic.model.MethodArgument) ResourceName(com.google.api.generator.gapic.model.ResourceName) MethodDescriptor(com.google.protobuf.Descriptors.MethodDescriptor) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) TypeNode(com.google.api.generator.engine.ast.TypeNode) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

MethodArgument (com.google.api.generator.gapic.model.MethodArgument)19 TypeNode (com.google.api.generator.engine.ast.TypeNode)17 List (java.util.List)14 Message (com.google.api.generator.gapic.model.Message)13 Test (org.junit.Test)13 Field (com.google.api.generator.gapic.model.Field)12 ResourceName (com.google.api.generator.gapic.model.ResourceName)12 Method (com.google.api.generator.gapic.model.Method)10 Map (java.util.Map)8 Collectors (java.util.stream.Collectors)8 VaporReference (com.google.api.generator.engine.ast.VaporReference)7 Descriptors (com.google.protobuf.Descriptors)7 ArrayList (java.util.ArrayList)7 Arrays (java.util.Arrays)7 Function (java.util.function.Function)7 CommentStatement (com.google.api.generator.engine.ast.CommentStatement)5 LongrunningOperation (com.google.api.generator.gapic.model.LongrunningOperation)5 HashMap (java.util.HashMap)5 AssignmentExpr (com.google.api.generator.engine.ast.AssignmentExpr)4 ConcreteReference (com.google.api.generator.engine.ast.ConcreteReference)4