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());
}
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());
}
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());
}
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());
}
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());
}
Aggregations