-
Notifications
You must be signed in to change notification settings - Fork 64
feat: Add add autogenerated javadoc sample for selecting REST transport over gRPC #983
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,21 @@ | |
|
||
package com.google.api.generator.gapic.composer.grpcrest; | ||
|
||
import com.google.api.generator.engine.ast.CommentStatement; | ||
import com.google.api.generator.engine.ast.TypeNode; | ||
import com.google.api.generator.gapic.composer.comment.ServiceClientCommentComposer; | ||
import com.google.api.generator.gapic.composer.common.AbstractServiceClientClassComposer; | ||
import com.google.api.generator.gapic.composer.samplecode.SampleCodeWriter; | ||
import com.google.api.generator.gapic.composer.samplecode.ServiceClientHeaderSampleComposer; | ||
import com.google.api.generator.gapic.composer.store.TypeStore; | ||
import com.google.api.generator.gapic.composer.utils.ClassNames; | ||
import com.google.api.generator.gapic.model.Message; | ||
import com.google.api.generator.gapic.model.ResourceName; | ||
import com.google.api.generator.gapic.model.Sample; | ||
import com.google.api.generator.gapic.model.Service; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class ServiceClientClassComposer extends AbstractServiceClientClassComposer { | ||
private static final ServiceClientClassComposer INSTANCE = new ServiceClientClassComposer(); | ||
|
@@ -26,4 +40,37 @@ protected ServiceClientClassComposer() { | |
public static ServiceClientClassComposer instance() { | ||
return INSTANCE; | ||
} | ||
|
||
@Override | ||
protected List<CommentStatement> createClassHeaderComments( | ||
Service service, | ||
TypeStore typeStore, | ||
Map<String, ResourceName> resourceNames, | ||
Map<String, Message> messageTypes, | ||
List<Sample> samples) { | ||
TypeNode clientType = typeStore.get(ClassNames.getServiceClientClassName(service)); | ||
TypeNode settingsType = typeStore.get(ClassNames.getServiceSettingsClassName(service)); | ||
Sample classMethodSampleCode = | ||
ServiceClientHeaderSampleComposer.composeClassHeaderSample( | ||
service, clientType, resourceNames, messageTypes); | ||
Sample credentialsSampleCode = | ||
ServiceClientHeaderSampleComposer.composeSetCredentialsSample(clientType, settingsType); | ||
Sample endpointSampleCode = | ||
ServiceClientHeaderSampleComposer.composeSetEndpointSample(clientType, settingsType); | ||
Sample transportSampleCode = | ||
ServiceClientHeaderSampleComposer.composeTransportSample( | ||
clientType, settingsType, "defaultHttpJsonTransportProviderBuilder"); | ||
samples.addAll( | ||
Arrays.asList( | ||
classMethodSampleCode, credentialsSampleCode, endpointSampleCode, transportSampleCode)); | ||
|
||
return ServiceClientCommentComposer.createClassHeaderComments( | ||
service, | ||
SampleCodeWriter.writeInlineSample(classMethodSampleCode.body()), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You could also combine all the different sample.body into one List and pass the whole list to SampleCodeWriter.writeInlineSample() |
||
SampleCodeWriter.writeInlineSample(credentialsSampleCode.body()), | ||
SampleCodeWriter.writeInlineSample(endpointSampleCode.body()), | ||
SampleCodeWriter.writeInlineSample(transportSampleCode.body()), | ||
"gRPC", | ||
"REST (HTTP1.1/JSON)"); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
// Copyright 2022 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package com.google.api.generator.gapic.composer.grpcrest; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
import com.google.api.generator.gapic.composer.common.TestProtoLoader; | ||
import com.google.api.generator.gapic.model.GapicContext; | ||
import com.google.api.generator.gapic.model.GapicServiceConfig; | ||
import com.google.api.generator.gapic.model.Message; | ||
import com.google.api.generator.gapic.model.ResourceName; | ||
import com.google.api.generator.gapic.model.Service; | ||
import com.google.api.generator.gapic.model.Transport; | ||
import com.google.api.generator.gapic.protoparser.Parser; | ||
import com.google.api.generator.gapic.protoparser.ServiceConfigParser; | ||
import com.google.longrunning.OperationsProto; | ||
import com.google.protobuf.Descriptors.FileDescriptor; | ||
import com.google.protobuf.Descriptors.ServiceDescriptor; | ||
import com.google.showcase.grpcrest.v1beta1.EchoGrpcrest; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.Set; | ||
|
||
public class GrpcRestTestProtoLoader extends TestProtoLoader { | ||
private static final GrpcRestTestProtoLoader INSTANCE = new GrpcRestTestProtoLoader(); | ||
|
||
protected GrpcRestTestProtoLoader() { | ||
super(Transport.GRPC_REST, "src/test/resources/"); | ||
} | ||
|
||
public static GrpcRestTestProtoLoader instance() { | ||
return INSTANCE; | ||
} | ||
|
||
@Override | ||
public GapicContext parseShowcaseEcho() { | ||
FileDescriptor echoFileDescriptor = EchoGrpcrest.getDescriptor(); | ||
|
||
ServiceDescriptor echoServiceDescriptor = echoFileDescriptor.getServices().get(0); | ||
assertEquals("Echo", echoServiceDescriptor.getName()); | ||
|
||
Map<String, Message> messageTypes = Parser.parseMessages(echoFileDescriptor); | ||
Map<String, Message> operationMessageTypes = | ||
Parser.parseMessages(OperationsProto.getDescriptor()); | ||
messageTypes.putAll(operationMessageTypes); | ||
Map<String, ResourceName> resourceNames = Parser.parseResourceNames(echoFileDescriptor); | ||
Set<ResourceName> outputResourceNames = new HashSet<>(); | ||
List<Service> services = | ||
Parser.parseService( | ||
echoFileDescriptor, messageTypes, resourceNames, Optional.empty(), outputResourceNames); | ||
|
||
String jsonFilename = "showcase_grpc_service_config.json"; | ||
Path jsonPath = Paths.get(getTestFilesDirectory(), jsonFilename); | ||
Optional<GapicServiceConfig> configOpt = ServiceConfigParser.parse(jsonPath.toString()); | ||
assertTrue(configOpt.isPresent()); | ||
GapicServiceConfig config = configOpt.get(); | ||
|
||
return GapicContext.builder() | ||
.setMessages(messageTypes) | ||
.setResourceNames(resourceNames) | ||
.setServices(services) | ||
.setServiceConfig(config) | ||
.setHelperResourceNames(outputResourceNames) | ||
.setTransport(getTransport()) | ||
.build(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// Copyright 2022 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package com.google.api.generator.gapic.composer.grpcrest; | ||
|
||
import com.google.api.generator.engine.writer.JavaWriterVisitor; | ||
import com.google.api.generator.gapic.composer.grpc.GrpcServiceCallableFactoryClassComposer; | ||
import com.google.api.generator.gapic.model.GapicClass; | ||
import com.google.api.generator.gapic.model.GapicContext; | ||
import com.google.api.generator.gapic.model.Service; | ||
import com.google.api.generator.test.framework.Assert; | ||
import com.google.api.generator.test.framework.Utils; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import org.junit.Test; | ||
|
||
public class GrpcServiceCallableFactoryClassComposerTest { | ||
@Test | ||
public void generateServiceClasses() { | ||
GapicContext context = GrpcRestTestProtoLoader.instance().parseShowcaseEcho(); | ||
Service echoProtoService = context.services().get(0); | ||
GapicClass clazz = | ||
GrpcServiceCallableFactoryClassComposer.instance().generate(context, echoProtoService); | ||
|
||
JavaWriterVisitor visitor = new JavaWriterVisitor(); | ||
clazz.classDefinition().accept(visitor); | ||
Utils.saveCodegenToFile(this.getClass(), "GrpcEchoCallableFactory.golden", visitor.write()); | ||
Path goldenFilePath = | ||
Paths.get(Utils.getGoldenDir(this.getClass()), "GrpcEchoCallableFactory.golden"); | ||
Assert.assertCodeEquals(goldenFilePath, visitor.write()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider checking the clazz.samples() as well. See similar grpc tests. See Assert.assertGoldenSamples and assertEmptySamples. Even if there aren't any samples, I think it's good to be clear in the tests which classes generate samples and which don't. |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// Copyright 2022 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package com.google.api.generator.gapic.composer.grpcrest; | ||
|
||
import com.google.api.generator.engine.writer.JavaWriterVisitor; | ||
import com.google.api.generator.gapic.composer.grpc.GrpcServiceStubClassComposer; | ||
import com.google.api.generator.gapic.model.GapicClass; | ||
import com.google.api.generator.gapic.model.GapicContext; | ||
import com.google.api.generator.gapic.model.Service; | ||
import com.google.api.generator.test.framework.Assert; | ||
import com.google.api.generator.test.framework.Utils; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import org.junit.Test; | ||
|
||
public class GrpcServiceStubClassComposerTest { | ||
@Test | ||
public void generateServiceClasses() { | ||
GapicContext context = GrpcRestTestProtoLoader.instance().parseShowcaseEcho(); | ||
Service echoProtoService = context.services().get(0); | ||
GapicClass clazz = GrpcServiceStubClassComposer.instance().generate(context, echoProtoService); | ||
|
||
JavaWriterVisitor visitor = new JavaWriterVisitor(); | ||
clazz.classDefinition().accept(visitor); | ||
Utils.saveCodegenToFile(this.getClass(), "GrpcEchoStub.golden", visitor.write()); | ||
Path goldenFilePath = Paths.get(Utils.getGoldenDir(this.getClass()), "GrpcEchoStub.golden"); | ||
Assert.assertCodeEquals(goldenFilePath, visitor.write()); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do you mean to include these null args?