Search in sources :

Example 71 with Expr

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

the class DefaultValueComposerTest method defaultValue_mapField.

@Test
public void defaultValue_mapField() {
    // Incorrect and will never happen in real usage, but proves that map detection is based on
    // isMap rather than type().
    Field field = Field.builder().setName("foobar").setType(TypeNode.STRING).setIsMap(true).setIsRepeated(true).build();
    Expr expr = DefaultValueComposer.createValue(field);
    expr.accept(writerVisitor);
    assertEquals("new HashMap<>()", writerVisitor.write());
    writerVisitor.clear();
    // isMap() and isRepeated() will be set by protoc simultaneously, but we check this edge case.
    // for completeness.
    field = Field.builder().setName("foobar").setType(TypeNode.STRING).setIsMap(true).build();
    expr = DefaultValueComposer.createValue(field);
    expr.accept(writerVisitor);
    assertEquals("\"foobar-1268878963\"", writerVisitor.write());
}
Also used : Field(com.google.api.generator.gapic.model.Field) Expr(com.google.api.generator.engine.ast.Expr) Test(org.junit.Test)

Example 72 with Expr

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

the class DefaultValueComposerTest method createSimpleMessage_containsMessagesEnumsAndResourceName.

@Test
public void createSimpleMessage_containsMessagesEnumsAndResourceName() {
    FileDescriptor echoFileDescriptor = EchoOuterClass.getDescriptor();
    Map<String, Message> messageTypes = Parser.parseMessages(echoFileDescriptor);
    Map<String, ResourceName> typeStringsToResourceNames = Parser.parseResourceNames(echoFileDescriptor);
    Message message = messageTypes.get("com.google.showcase.v1beta1.EchoRequest");
    Expr expr = DefaultValueComposer.createSimpleMessageBuilderValue(message, typeStringsToResourceNames, messageTypes);
    expr.accept(writerVisitor);
    assertEquals("EchoRequest.newBuilder().setName(" + "FoobarName.ofProjectFoobarName(\"[PROJECT]\", \"[FOOBAR]\").toString())" + ".setParent(FoobarName.ofProjectFoobarName(\"[PROJECT]\", \"[FOOBAR]\").toString())" + ".setSeverity(Severity.forNumber(0))" + ".setFoobar(Foobar.newBuilder().build()).build()", writerVisitor.write());
}
Also used : Message(com.google.api.generator.gapic.model.Message) Expr(com.google.api.generator.engine.ast.Expr) ResourceName(com.google.api.generator.gapic.model.ResourceName) ByteString(com.google.protobuf.ByteString) FileDescriptor(com.google.protobuf.Descriptors.FileDescriptor) Test(org.junit.Test)

Example 73 with Expr

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

the class DefaultValueComposerTest method defaultValue_resourceNameWithOnlyWildcards_valueOnly.

@Test
public void defaultValue_resourceNameWithOnlyWildcards_valueOnly() {
    // Edge case that occurs in GCS.
    // Wildcard, but the resource names map has only other names that contain only the deleted-topic
    // constant.
    FileDescriptor lockerServiceFileDescriptor = LockerProto.getDescriptor();
    Map<String, ResourceName> typeStringsToResourceNames = Parser.parseResourceNames(lockerServiceFileDescriptor);
    ResourceName resourceName = typeStringsToResourceNames.get("cloudresourcemanager.googleapis.com/Anything");
    String fallbackField = "foobar";
    Expr expr = DefaultValueComposer.createResourceHelperValue(resourceName, false, Collections.emptyList(), fallbackField, /* allowAnonResourceNameClass = */
    false);
    expr.accept(writerVisitor);
    assertEquals(String.format("\"%s%s\"", fallbackField, fallbackField.hashCode()), writerVisitor.write());
}
Also used : Expr(com.google.api.generator.engine.ast.Expr) ResourceName(com.google.api.generator.gapic.model.ResourceName) ByteString(com.google.protobuf.ByteString) FileDescriptor(com.google.protobuf.Descriptors.FileDescriptor) Test(org.junit.Test)

Example 74 with Expr

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

the class DefaultValueComposerTest method createSimpleMessage_onlyOneofs.

@Test
public void createSimpleMessage_onlyOneofs() {
    FileDescriptor echoFileDescriptor = EchoOuterClass.getDescriptor();
    Map<String, Message> messageTypes = Parser.parseMessages(echoFileDescriptor);
    Map<String, ResourceName> typeStringsToResourceNames = Parser.parseResourceNames(echoFileDescriptor);
    Message message = messageTypes.get("com.google.showcase.v1beta1.WaitRequest");
    Expr expr = DefaultValueComposer.createSimpleMessageBuilderValue(message, typeStringsToResourceNames, messageTypes);
    expr.accept(writerVisitor);
    assertEquals("WaitRequest.newBuilder().build()", writerVisitor.write());
}
Also used : Message(com.google.api.generator.gapic.model.Message) Expr(com.google.api.generator.engine.ast.Expr) ResourceName(com.google.api.generator.gapic.model.ResourceName) ByteString(com.google.protobuf.ByteString) FileDescriptor(com.google.protobuf.Descriptors.FileDescriptor) Test(org.junit.Test)

Example 75 with Expr

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

the class DefaultValueComposerTest method createSimpleMessage_containsRepeatedField.

@Test
public void createSimpleMessage_containsRepeatedField() {
    FileDescriptor echoFileDescriptor = EchoOuterClass.getDescriptor();
    Map<String, Message> messageTypes = Parser.parseMessages(echoFileDescriptor);
    Map<String, ResourceName> typeStringsToResourceNames = Parser.parseResourceNames(echoFileDescriptor);
    Message message = messageTypes.get("com.google.showcase.v1beta1.PagedExpandResponse");
    Expr expr = DefaultValueComposer.createSimpleMessageBuilderValue(message, typeStringsToResourceNames, messageTypes);
    expr.accept(writerVisitor);
    assertEquals("PagedExpandResponse.newBuilder().addAllResponses(new ArrayList<EchoResponse>())" + ".setNextPageToken(\"nextPageToken-1386094857\").build()", writerVisitor.write());
}
Also used : Message(com.google.api.generator.gapic.model.Message) Expr(com.google.api.generator.engine.ast.Expr) ResourceName(com.google.api.generator.gapic.model.ResourceName) ByteString(com.google.protobuf.ByteString) FileDescriptor(com.google.protobuf.Descriptors.FileDescriptor) Test(org.junit.Test)

Aggregations

Expr (com.google.api.generator.engine.ast.Expr)151 VariableExpr (com.google.api.generator.engine.ast.VariableExpr)130 MethodInvocationExpr (com.google.api.generator.engine.ast.MethodInvocationExpr)126 AssignmentExpr (com.google.api.generator.engine.ast.AssignmentExpr)124 ValueExpr (com.google.api.generator.engine.ast.ValueExpr)114 NewObjectExpr (com.google.api.generator.engine.ast.NewObjectExpr)96 TypeNode (com.google.api.generator.engine.ast.TypeNode)80 ExprStatement (com.google.api.generator.engine.ast.ExprStatement)72 ArrayList (java.util.ArrayList)72 RelationalOperationExpr (com.google.api.generator.engine.ast.RelationalOperationExpr)67 Statement (com.google.api.generator.engine.ast.Statement)64 CastExpr (com.google.api.generator.engine.ast.CastExpr)59 TernaryExpr (com.google.api.generator.engine.ast.TernaryExpr)59 AnonymousClassExpr (com.google.api.generator.engine.ast.AnonymousClassExpr)58 List (java.util.List)58 EnumRefExpr (com.google.api.generator.engine.ast.EnumRefExpr)55 Variable (com.google.api.generator.engine.ast.Variable)54 ThrowExpr (com.google.api.generator.engine.ast.ThrowExpr)51 ReferenceConstructorExpr (com.google.api.generator.engine.ast.ReferenceConstructorExpr)49 ReturnExpr (com.google.api.generator.engine.ast.ReturnExpr)47