Skip to content
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

feat: add support for rest-only services in generator #1849

Merged
merged 18 commits into from
Jun 12, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions spring-cloud-generator/java_gapic_spring.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ def java_gapic_spring_library(
gapic_yaml = None,
service_yaml = None,
transport = None,
rest_numeric_enums = False,
**kwargs):
library_name = name + "-spring"
raw_srcjar_name = name + "_raw"
Expand All @@ -89,6 +90,7 @@ def java_gapic_spring_library(
gapic_yaml = gapic_yaml,
service_yaml = service_yaml,
transport = transport,
rest_numeric_enums = rest_numeric_enums,
**kwargs
)

Expand All @@ -105,6 +107,7 @@ def _java_gapic_spring_srcjar(
gapic_yaml,
service_yaml,
transport,
rest_numeric_enums,
# Can be used to provide a java_library with a customized generator,
# like the one which dumps descriptor to a file for future debugging.
java_generator_name = "java_gapic_spring",
Expand All @@ -128,6 +131,9 @@ def _java_gapic_spring_srcjar(
if transport:
opt_args.append("transport=%s" % transport)

if rest_numeric_enums:
opt_args.append("rest-numeric-enums")

# Produces the GAPIC metadata file if this flag is set. to any value.
# Protoc invocation: --java_gapic_opt=metadata
plugin_args = ["metadata"]
Expand Down
6 changes: 6 additions & 0 deletions spring-cloud-generator/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.truth</groupId>
<artifactId>truth</artifactId>
<version>1.1.3</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
5 changes: 0 additions & 5 deletions spring-cloud-generator/scripts/generate-library-list.sh
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ for d in ./google-cloud-java/*java-*/; do
api_shortname=$(cat $d/.repo-metadata.json | jq -r .api_shortname)
distribution_name=$(cat $d/.repo-metadata.json | jq -r .distribution_name)
library_type=$(cat $d/.repo-metadata.json | jq -r .library_type)
transport=$(cat $d/.repo-metadata.json | jq -r .transport)
release_level=$(cat $d/.repo-metadata.json | jq -r .release_level)
monorepo_folder=$(basename $d)

Expand All @@ -44,10 +43,6 @@ for d in ./google-cloud-java/*java-*/; do
echo "$d: non auto type: $library_type"
continue
fi
if [[ $transport != *grpc* ]] ; then
echo "$d: transport type not in scope: $transport"
continue
fi
if [[ $group_id != "com.google.cloud" ]] ; then
echo "$d: group_id not in scope: $group_id"
continue
Expand Down
2 changes: 1 addition & 1 deletion spring-cloud-generator/scripts/setup-build-rule.sh
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ GAPIC_RULE_FULL="$(buildozer 'print rule' $googleapis_folder/BUILD.bazel:%java_g
buildozer "new java_gapic_spring_library $SPRING_RULE_NAME" $googleapis_folder/BUILD.bazel:__pkg__

# Copy attributes from java_gapic_library rule
attrs_array=("srcs" "grpc_service_config" "gapic_yaml" "service_yaml" "transport")
attrs_array=("srcs" "grpc_service_config" "gapic_yaml" "service_yaml" "transport" "rest_numeric_enums")
for attribute in "${attrs_array[@]}"
do
echo "$attribute"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public GapicClass generate(GapicContext context, Service service) {
Expr thisExpr = ValueExpr.withValue(ThisObjectValue.withType(dynamicTypes.get(className)));
Transport transport = context.transport();
boolean hasRestOption =
(transport.equals(Transport.GRPC_REST) || transport.equals(Transport.REST))
transport.equals(Transport.GRPC_REST)
&& service.hasAnyEnabledMethodsForTransport(Transport.REST);
String serviceSettingsMethodName = JavaStyle.toLowerCamelCase(service.name()) + "Settings";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import com.google.api.generator.gapic.model.GapicClass;
import com.google.api.generator.gapic.model.GapicContext;
import com.google.api.generator.gapic.model.GapicPackageInfo;
import com.google.api.generator.gapic.model.Transport;
import com.google.common.collect.ImmutableList;
import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -66,12 +65,8 @@ protected static List<GapicClass> generatePerServiceClasses(GapicContext context
.services()
.forEach(
s -> {
// Transport.REST is out of scope for Spring composers.
if (context.transport() == Transport.GRPC
|| context.transport() == Transport.GRPC_REST) {
clazzes.add(SpringAutoConfigClassComposer.instance().generate(context, s));
clazzes.add(SpringPropertiesClassComposer.instance().generate(context, s));
}
clazzes.add(SpringAutoConfigClassComposer.instance().generate(context, s));
clazzes.add(SpringPropertiesClassComposer.instance().generate(context, s));
});
return clazzes;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public GapicClass generate(GapicContext context, Service service) {
Map<String, TypeNode> dynamicTypes = createDynamicTypes(service, packageName);
Transport transport = context.transport();
boolean hasRestOption =
(transport.equals(Transport.GRPC_REST) || transport.equals(Transport.REST))
transport.equals(Transport.GRPC_REST)
&& service.hasAnyEnabledMethodsForTransport(Transport.REST);

// TODO: this is the prefix user will use to set properties, may need to change depending on
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,55 +16,82 @@

package com.google.cloud.generator.spring.composer;

import static com.google.common.truth.Truth.assertThat;

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.gapic.model.Transport;
import com.google.cloud.generator.spring.utils.Assert;
import com.google.cloud.generator.spring.utils.GrpcRestTestProtoLoader;
import com.google.cloud.generator.spring.utils.RestTestProtoLoader;
import com.google.cloud.generator.spring.utils.TestProtoLoader;
import org.junit.Before;
import org.junit.Test;

public class SpringAutoConfigClassComposerTest {
private GapicContext context;
private GapicContext echoContext;
private GapicContext echoGrpcRestContext;
private GapicContext echoRestContext;
private GapicContext wickedContext;

private GapicContext complianceContext;
private Service echoProtoService;
private Service echoGrpcRestProtoService;
private Service echoRestProtoService;
private Service wickedProtoService;
private Service complianceProtoService;

@Before
public void setUp() {
this.context = TestProtoLoader.instance().parseShowcaseEcho();
this.echoProtoService = this.context.services().get(0);
this.echoContext = TestProtoLoader.instance().parseShowcaseEcho();
this.echoProtoService = this.echoContext.services().get(0);
this.echoGrpcRestContext = GrpcRestTestProtoLoader.instance().parseShowcaseEcho();
this.echoGrpcRestProtoService = this.echoGrpcRestContext.services().get(0);
this.echoRestContext = RestTestProtoLoader.instance().parseShowcaseEcho();
this.echoRestProtoService = this.echoRestContext.services().get(0);
this.wickedContext = GrpcRestTestProtoLoader.instance().parseShowcaseWicked();
this.wickedProtoService = this.wickedContext.services().get(0);
this.complianceContext = RestTestProtoLoader.instance().parseCompliance();
this.complianceProtoService = this.complianceContext.services().get(0);
}

@Test
public void generateAutoConfigClazzGrpcTest() {
assertThat(this.echoContext.transport()).isEqualTo(Transport.GRPC);
GapicClass clazz =
SpringAutoConfigClassComposer.instance().generate(this.context, this.echoProtoService);
SpringAutoConfigClassComposer.instance().generate(this.echoContext, this.echoProtoService);
String fileName = clazz.classDefinition().classIdentifier() + "Grpc.golden";
Assert.assertGoldenClass(this.getClass(), clazz, fileName);
}

@Test
public void generateAutoConfigClazzGrpcRestTest() {
GapicContext contextGrpcRest =
this.context.toBuilder().setTransport(Transport.GRPC_REST).build();
assertThat(this.echoGrpcRestContext.transport()).isEqualTo(Transport.GRPC_REST);
GapicClass clazz =
SpringAutoConfigClassComposer.instance().generate(contextGrpcRest, this.echoProtoService);
SpringAutoConfigClassComposer.instance()
.generate(this.echoGrpcRestContext, this.echoGrpcRestProtoService);
String fileName = clazz.classDefinition().classIdentifier() + "GrpcRest.golden";
Assert.assertGoldenClass(this.getClass(), clazz, fileName);
}

@Test
public void generateAutoConfigClazzNoRestRpcsTest() {
GapicContext contextGrpcRest =
this.wickedContext.toBuilder().setTransport(Transport.GRPC_REST).build();
assertThat(this.wickedContext.transport()).isEqualTo(Transport.GRPC_REST);
GapicClass clazz =
SpringAutoConfigClassComposer.instance().generate(contextGrpcRest, this.wickedProtoService);
SpringAutoConfigClassComposer.instance()
.generate(this.wickedContext, this.wickedProtoService);
String fileName = clazz.classDefinition().classIdentifier() + "NoRestRpcs.golden";
Assert.assertGoldenClass(this.getClass(), clazz, fileName);
}

@Test
public void generateAutoConfigClazzRestTest() {
assertThat(this.complianceContext.transport()).isEqualTo(Transport.REST);
GapicClass clazz =
SpringAutoConfigClassComposer.instance()
.generate(this.complianceContext, this.complianceProtoService);
String fileName = clazz.classDefinition().classIdentifier() + "Rest.golden";
Assert.assertGoldenClass(this.getClass(), clazz, fileName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,55 +16,82 @@

package com.google.cloud.generator.spring.composer;

import static com.google.common.truth.Truth.assertThat;

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.gapic.model.Transport;
import com.google.cloud.generator.spring.utils.Assert;
import com.google.cloud.generator.spring.utils.GrpcRestTestProtoLoader;
import com.google.cloud.generator.spring.utils.RestTestProtoLoader;
import com.google.cloud.generator.spring.utils.TestProtoLoader;
import org.junit.Before;
import org.junit.Test;

public class SpringPropertiesClassComposerTest {
private GapicContext context;
private GapicContext echoContext;
private GapicContext echoGrpcRestContext;
private GapicContext echoRestContext;
private GapicContext wickedContext;

private GapicContext complianceContext;
private Service echoProtoService;
private Service echoGrpcRestProtoService;
private Service echoRestProtoService;
private Service wickedProtoService;
private Service complianceProtoService;

@Before
public void setUp() {
this.context = TestProtoLoader.instance().parseShowcaseEcho();
this.echoProtoService = this.context.services().get(0);
this.echoContext = TestProtoLoader.instance().parseShowcaseEcho();
this.echoProtoService = this.echoContext.services().get(0);
this.echoGrpcRestContext = GrpcRestTestProtoLoader.instance().parseShowcaseEcho();
this.echoGrpcRestProtoService = this.echoGrpcRestContext.services().get(0);
this.echoRestContext = RestTestProtoLoader.instance().parseShowcaseEcho();
this.echoRestProtoService = this.echoRestContext.services().get(0);
this.wickedContext = GrpcRestTestProtoLoader.instance().parseShowcaseWicked();
this.wickedProtoService = this.wickedContext.services().get(0);
this.complianceContext = RestTestProtoLoader.instance().parseCompliance();
this.complianceProtoService = this.complianceContext.services().get(0);
}

@Test
public void generatePropertiesClazzGrpcTest() {
assertThat(this.echoContext.transport()).isEqualTo(Transport.GRPC);
GapicClass clazz =
SpringPropertiesClassComposer.instance().generate(this.context, this.echoProtoService);
SpringPropertiesClassComposer.instance().generate(this.echoContext, this.echoProtoService);
String fileName = clazz.classDefinition().classIdentifier() + "Grpc.golden";
Assert.assertGoldenClass(this.getClass(), clazz, fileName);
}

@Test
public void generatePropertiesClazzGrpcRestTest() {
GapicContext contextGrpcRest =
this.context.toBuilder().setTransport(Transport.GRPC_REST).build();
assertThat(this.echoGrpcRestContext.transport()).isEqualTo(Transport.GRPC_REST);
GapicClass clazz =
SpringPropertiesClassComposer.instance().generate(contextGrpcRest, this.echoProtoService);
SpringPropertiesClassComposer.instance()
.generate(this.echoGrpcRestContext, this.echoGrpcRestProtoService);
String fileName = clazz.classDefinition().classIdentifier() + "GrpcRest.golden";
Assert.assertGoldenClass(this.getClass(), clazz, fileName);
}

@Test
public void generatePropertiesClazzNoRestRpcsTest() {
GapicContext contextGrpcRest =
this.wickedContext.toBuilder().setTransport(Transport.GRPC_REST).build();
assertThat(this.wickedContext.transport()).isEqualTo(Transport.GRPC_REST);
GapicClass clazz =
SpringPropertiesClassComposer.instance().generate(contextGrpcRest, this.wickedProtoService);
SpringPropertiesClassComposer.instance()
.generate(this.wickedContext, this.wickedProtoService);
String fileName = clazz.classDefinition().classIdentifier() + "NoRestRpcs.golden";
Assert.assertGoldenClass(this.getClass(), clazz, fileName);
}

@Test
public void generatePropertiesClazzRestTest() {
assertThat(this.complianceContext.transport()).isEqualTo(Transport.REST);
GapicClass clazz =
SpringPropertiesClassComposer.instance()
.generate(this.complianceContext, this.complianceProtoService);
String fileName = clazz.classDefinition().classIdentifier() + "Rest.golden";
Assert.assertGoldenClass(this.getClass(), clazz, fileName);
}
}
Loading