From d56816364cbee107756ebb5304c7a907fffc64a6 Mon Sep 17 00:00:00 2001 From: Mira Leung Date: Thu, 18 Feb 2021 20:08:55 -0800 Subject: [PATCH] chore(engx): Consolidate codegen/composer generate() iface (#662) --- .../gapic/composer/ClassComposer.java | 5 +- .../generator/gapic/composer/Composer.java | 90 +++++++++---------- ...pcServiceCallableFactoryClassComposer.java | 5 +- .../GrpcServiceStubClassComposer.java | 4 +- .../composer/MockServiceClassComposer.java | 5 +- .../MockServiceImplClassComposer.java | 5 +- .../composer/ServiceClientClassComposer.java | 7 +- .../ServiceClientTestClassComposer.java | 8 +- .../ServiceSettingsClassComposer.java | 5 +- .../composer/ServiceStubClassComposer.java | 4 +- .../ServiceStubSettingsClassComposer.java | 12 +-- .../generator/gapic/model/GapicContext.java | 13 ++- ...rviceCallableFactoryClassComposerTest.java | 11 ++- .../GrpcServiceStubClassComposerTest.java | 34 ++++++- .../MockServiceClassComposerTest.java | 11 ++- .../MockServiceImplClassComposerTest.java | 12 ++- .../ServiceClientClassComposerTest.java | 11 +-- .../ServiceClientTestClassComposerTest.java | 21 ++--- .../ServiceSettingsClassComposerTest.java | 12 ++- .../ServiceStubClassComposerTest.java | 11 ++- .../ServiceStubSettingsClassComposerTest.java | 41 +++++++-- 21 files changed, 206 insertions(+), 121 deletions(-) diff --git a/src/main/java/com/google/api/generator/gapic/composer/ClassComposer.java b/src/main/java/com/google/api/generator/gapic/composer/ClassComposer.java index 131020c562..e7bcbc014f 100644 --- a/src/main/java/com/google/api/generator/gapic/composer/ClassComposer.java +++ b/src/main/java/com/google/api/generator/gapic/composer/ClassComposer.java @@ -15,10 +15,9 @@ package com.google.api.generator.gapic.composer; import com.google.api.generator.gapic.model.GapicClass; -import com.google.api.generator.gapic.model.Message; +import com.google.api.generator.gapic.model.GapicContext; import com.google.api.generator.gapic.model.Service; -import java.util.Map; public interface ClassComposer { - GapicClass generate(Service service, Map messageTypes); + GapicClass generate(GapicContext context, Service serivce); } diff --git a/src/main/java/com/google/api/generator/gapic/composer/Composer.java b/src/main/java/com/google/api/generator/gapic/composer/Composer.java index 6126859a25..85f5e80ce2 100644 --- a/src/main/java/com/google/api/generator/gapic/composer/Composer.java +++ b/src/main/java/com/google/api/generator/gapic/composer/Composer.java @@ -22,33 +22,24 @@ import com.google.api.generator.gapic.model.GapicClass.Kind; import com.google.api.generator.gapic.model.GapicContext; import com.google.api.generator.gapic.model.GapicPackageInfo; -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.common.annotations.VisibleForTesting; import java.util.ArrayList; -import java.util.HashMap; import java.util.List; -import java.util.Map; import java.util.Set; import java.util.stream.Collectors; -import javax.annotation.Nonnull; public class Composer { public static List composeServiceClasses(GapicContext context) { List clazzes = new ArrayList<>(); - Map availableResourceNames = new HashMap<>(); - for (ResourceName resourceName : context.helperResourceNames()) { - availableResourceNames.put(resourceName.resourceTypeString(), resourceName); - } - for (Service service : context.services()) { - clazzes.addAll(generateServiceClasses(service, context, availableResourceNames)); - } - for (Service mixinService : context.mixinServices()) { - clazzes.addAll(generateMockClasses(mixinService, availableResourceNames, context.messages())); - } - clazzes.addAll(generateResourceNameHelperClasses(context.helperResourceNames())); + clazzes.addAll(generateServiceClasses(context)); + clazzes.addAll(generateMockClasses(context, context.mixinServices())); + clazzes.addAll( + generateResourceNameHelperClasses( + context.helperResourceNames().values().stream() + .map(r -> r) + .collect(Collectors.toSet()))); return addApacheLicense(clazzes); } @@ -56,17 +47,12 @@ public static GapicPackageInfo composePackageInfo(GapicContext context) { return addApacheLicense(ClientLibraryPackageInfoComposer.generatePackageInfo(context)); } - public static List generateServiceClasses( - @Nonnull Service service, - GapicContext context, - @Nonnull Map resourceNames) { + public static List generateServiceClasses(GapicContext context) { List clazzes = new ArrayList<>(); - clazzes.addAll( - generateStubClasses(service, context.serviceConfig(), context.messages(), resourceNames)); - clazzes.addAll(generateClientSettingsClasses(service, context, resourceNames)); - clazzes.addAll(generateMockClasses(service, resourceNames, context.messages())); - clazzes.addAll(generateTestClasses(service, context, resourceNames)); - // TODO(miraleung): Generate test classes. + clazzes.addAll(generateStubClasses(context)); + clazzes.addAll(generateClientSettingsClasses(context)); + clazzes.addAll(generateMockClasses(context, context.services())); + clazzes.addAll(generateTestClasses(context)); return clazzes; } @@ -78,42 +64,46 @@ public static List generateResourceNameHelperClasses( .collect(Collectors.toList()); } - public static List generateStubClasses( - Service service, - GapicServiceConfig serviceConfig, - Map messageTypes, - Map resourceNames) { + public static List generateStubClasses(GapicContext context) { List clazzes = new ArrayList<>(); - clazzes.add(ServiceStubClassComposer.instance().generate(service, messageTypes)); - clazzes.add( - ServiceStubSettingsClassComposer.instance().generate(service, serviceConfig, messageTypes)); - clazzes.add(GrpcServiceCallableFactoryClassComposer.instance().generate(service, messageTypes)); - clazzes.add(GrpcServiceStubClassComposer.instance().generate(service, messageTypes)); + context + .services() + .forEach( + s -> { + clazzes.add(ServiceStubClassComposer.instance().generate(context, s)); + clazzes.add(ServiceStubSettingsClassComposer.instance().generate(context, s)); + clazzes.add(GrpcServiceCallableFactoryClassComposer.instance().generate(context, s)); + clazzes.add(GrpcServiceStubClassComposer.instance().generate(context, s)); + }); return clazzes; } - public static List generateClientSettingsClasses( - Service service, GapicContext context, Map resourceNames) { + public static List generateClientSettingsClasses(GapicContext context) { List clazzes = new ArrayList<>(); - clazzes.add(ServiceClientClassComposer.instance().generate(service, context, resourceNames)); - clazzes.add(ServiceSettingsClassComposer.instance().generate(service, context.messages())); + context + .services() + .forEach( + s -> { + clazzes.add(ServiceClientClassComposer.instance().generate(context, s)); + clazzes.add(ServiceSettingsClassComposer.instance().generate(context, s)); + }); return clazzes; } - public static List generateMockClasses( - Service service, Map resourceNames, Map messageTypes) { + public static List generateMockClasses(GapicContext context, List services) { List clazzes = new ArrayList<>(); - clazzes.add(MockServiceClassComposer.instance().generate(service, messageTypes)); - clazzes.add(MockServiceImplClassComposer.instance().generate(service, messageTypes)); + services.forEach( + s -> { + clazzes.add(MockServiceClassComposer.instance().generate(context, s)); + clazzes.add(MockServiceImplClassComposer.instance().generate(context, s)); + }); return clazzes; } - public static List generateTestClasses( - Service service, GapicContext context, Map resourceNames) { - List clazzes = new ArrayList<>(); - clazzes.add( - ServiceClientTestClassComposer.instance().generate(service, context, resourceNames)); - return clazzes; + public static List generateTestClasses(GapicContext context) { + return context.services().stream() + .map(s -> ServiceClientTestClassComposer.instance().generate(context, s)) + .collect(Collectors.toList()); } /** ====================== HELPERS ==================== */ diff --git a/src/main/java/com/google/api/generator/gapic/composer/GrpcServiceCallableFactoryClassComposer.java b/src/main/java/com/google/api/generator/gapic/composer/GrpcServiceCallableFactoryClassComposer.java index 4a7b7375ef..c781eba886 100644 --- a/src/main/java/com/google/api/generator/gapic/composer/GrpcServiceCallableFactoryClassComposer.java +++ b/src/main/java/com/google/api/generator/gapic/composer/GrpcServiceCallableFactoryClassComposer.java @@ -44,14 +44,13 @@ import com.google.api.generator.gapic.composer.utils.PackageChecker; import com.google.api.generator.gapic.model.GapicClass; import com.google.api.generator.gapic.model.GapicClass.Kind; -import com.google.api.generator.gapic.model.Message; +import com.google.api.generator.gapic.model.GapicContext; import com.google.api.generator.gapic.model.Service; import com.google.common.base.Preconditions; import com.google.longrunning.Operation; import java.util.ArrayList; import java.util.Arrays; import java.util.List; -import java.util.Map; import java.util.stream.Collectors; import javax.annotation.Generated; @@ -67,7 +66,7 @@ public static GrpcServiceCallableFactoryClassComposer instance() { } @Override - public GapicClass generate(Service service, Map ignore) { + public GapicClass generate(GapicContext ignored, Service service) { TypeStore typeStore = createTypes(service); String className = ClassNames.getGrpcServiceCallableFactoryClassName(service); GapicClass.Kind kind = Kind.STUB; diff --git a/src/main/java/com/google/api/generator/gapic/composer/GrpcServiceStubClassComposer.java b/src/main/java/com/google/api/generator/gapic/composer/GrpcServiceStubClassComposer.java index e20d679e4f..788c7272f6 100644 --- a/src/main/java/com/google/api/generator/gapic/composer/GrpcServiceStubClassComposer.java +++ b/src/main/java/com/google/api/generator/gapic/composer/GrpcServiceStubClassComposer.java @@ -55,7 +55,7 @@ import com.google.api.generator.gapic.composer.utils.PackageChecker; import com.google.api.generator.gapic.model.GapicClass; import com.google.api.generator.gapic.model.GapicClass.Kind; -import com.google.api.generator.gapic.model.Message; +import com.google.api.generator.gapic.model.GapicContext; import com.google.api.generator.gapic.model.Method; import com.google.api.generator.gapic.model.Service; import com.google.api.generator.gapic.utils.JavaStyle; @@ -116,7 +116,7 @@ public static GrpcServiceStubClassComposer instance() { } @Override - public GapicClass generate(Service service, Map ignore) { + public GapicClass generate(GapicContext ignored, Service service) { String pakkage = service.pakkage() + ".stub"; TypeStore typeStore = createDynamicTypes(service, pakkage); String className = ClassNames.getGrpcServiceStubClassName(service); diff --git a/src/main/java/com/google/api/generator/gapic/composer/MockServiceClassComposer.java b/src/main/java/com/google/api/generator/gapic/composer/MockServiceClassComposer.java index a029a25d39..c542153cf7 100644 --- a/src/main/java/com/google/api/generator/gapic/composer/MockServiceClassComposer.java +++ b/src/main/java/com/google/api/generator/gapic/composer/MockServiceClassComposer.java @@ -33,13 +33,12 @@ import com.google.api.generator.gapic.composer.utils.ClassNames; import com.google.api.generator.gapic.model.GapicClass; import com.google.api.generator.gapic.model.GapicClass.Kind; -import com.google.api.generator.gapic.model.Message; +import com.google.api.generator.gapic.model.GapicContext; import com.google.api.generator.gapic.model.Service; import com.google.protobuf.AbstractMessage; import io.grpc.ServerServiceDefinition; import java.util.Arrays; import java.util.List; -import java.util.Map; import javax.annotation.Generated; public class MockServiceClassComposer implements ClassComposer { @@ -53,7 +52,7 @@ public static MockServiceClassComposer instance() { } @Override - public GapicClass generate(Service service, Map ignore) { + public GapicClass generate(GapicContext ignored, Service service) { TypeStore typeStore = createTypes(service); String className = ClassNames.getMockServiceClassName(service); GapicClass.Kind kind = Kind.TEST; diff --git a/src/main/java/com/google/api/generator/gapic/composer/MockServiceImplClassComposer.java b/src/main/java/com/google/api/generator/gapic/composer/MockServiceImplClassComposer.java index afc2ceab24..2f5b777a6f 100644 --- a/src/main/java/com/google/api/generator/gapic/composer/MockServiceImplClassComposer.java +++ b/src/main/java/com/google/api/generator/gapic/composer/MockServiceImplClassComposer.java @@ -41,7 +41,7 @@ import com.google.api.generator.gapic.composer.utils.ClassNames; import com.google.api.generator.gapic.model.GapicClass; import com.google.api.generator.gapic.model.GapicClass.Kind; -import com.google.api.generator.gapic.model.Message; +import com.google.api.generator.gapic.model.GapicContext; import com.google.api.generator.gapic.model.Method; import com.google.api.generator.gapic.model.Method.Stream; import com.google.api.generator.gapic.model.Service; @@ -54,7 +54,6 @@ import java.util.Arrays; import java.util.LinkedList; import java.util.List; -import java.util.Map; import java.util.Queue; import java.util.function.Function; import java.util.stream.Collectors; @@ -95,7 +94,7 @@ public static MockServiceImplClassComposer instance() { } @Override - public GapicClass generate(Service service, Map ignore) { + public GapicClass generate(GapicContext ignored, Service service) { TypeStore typeStore = createDynamicTypes(service); String className = ClassNames.getMockServiceImplClassName(service); GapicClass.Kind kind = Kind.TEST; diff --git a/src/main/java/com/google/api/generator/gapic/composer/ServiceClientClassComposer.java b/src/main/java/com/google/api/generator/gapic/composer/ServiceClientClassComposer.java index 12e1d3b799..8fc7205746 100644 --- a/src/main/java/com/google/api/generator/gapic/composer/ServiceClientClassComposer.java +++ b/src/main/java/com/google/api/generator/gapic/composer/ServiceClientClassComposer.java @@ -96,7 +96,7 @@ import java.util.stream.Collectors; import javax.annotation.Generated; -public class ServiceClientClassComposer { +public class ServiceClientClassComposer implements ClassComposer { private static final ServiceClientClassComposer INSTANCE = new ServiceClientClassComposer(); private static final String PAGED_RESPONSE_TYPE_NAME_PATTERN = "%sPagedResponse"; private static final String CALLABLE_NAME_PATTERN = "%sCallable"; @@ -121,8 +121,9 @@ public static ServiceClientClassComposer instance() { return INSTANCE; } - public GapicClass generate( - Service service, GapicContext context, Map resourceNames) { + @Override + public GapicClass generate(GapicContext context, Service service) { + Map resourceNames = context.helperResourceNames(); Map messageTypes = context.messages(); TypeStore typeStore = createTypes(service, messageTypes); String className = ClassNames.getServiceClientClassName(service); diff --git a/src/main/java/com/google/api/generator/gapic/composer/ServiceClientTestClassComposer.java b/src/main/java/com/google/api/generator/gapic/composer/ServiceClientTestClassComposer.java index 954b4d5695..7ba0ba35f9 100644 --- a/src/main/java/com/google/api/generator/gapic/composer/ServiceClientTestClassComposer.java +++ b/src/main/java/com/google/api/generator/gapic/composer/ServiceClientTestClassComposer.java @@ -97,8 +97,7 @@ import org.junit.BeforeClass; import org.junit.Test; -// TODO(miraleung): Refactor classComposer. -public class ServiceClientTestClassComposer { +public class ServiceClientTestClassComposer implements ClassComposer { private static final Statement EMPTY_LINE_STATEMENT = EmptyLineStatement.create(); private static final String CHANNEL_PROVIDER_VAR_NAME = "channelProvider"; @@ -135,8 +134,9 @@ public static ServiceClientTestClassComposer instance() { return INSTANCE; } - public GapicClass generate( - Service service, GapicContext context, Map resourceNames) { + @Override + public GapicClass generate(GapicContext context, Service service) { + Map resourceNames = context.helperResourceNames(); Map messageTypes = context.messages(); String pakkage = service.pakkage(); TypeStore typeStore = new TypeStore(); diff --git a/src/main/java/com/google/api/generator/gapic/composer/ServiceSettingsClassComposer.java b/src/main/java/com/google/api/generator/gapic/composer/ServiceSettingsClassComposer.java index 229485efe6..6dbe6bec6a 100644 --- a/src/main/java/com/google/api/generator/gapic/composer/ServiceSettingsClassComposer.java +++ b/src/main/java/com/google/api/generator/gapic/composer/ServiceSettingsClassComposer.java @@ -57,7 +57,7 @@ import com.google.api.generator.gapic.composer.utils.PackageChecker; import com.google.api.generator.gapic.model.GapicClass; import com.google.api.generator.gapic.model.GapicClass.Kind; -import com.google.api.generator.gapic.model.Message; +import com.google.api.generator.gapic.model.GapicContext; import com.google.api.generator.gapic.model.Method; import com.google.api.generator.gapic.model.Method.Stream; import com.google.api.generator.gapic.model.Service; @@ -68,7 +68,6 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.List; -import java.util.Map; import java.util.Optional; import java.util.function.BiFunction; import java.util.function.Function; @@ -94,7 +93,7 @@ public static ServiceSettingsClassComposer instance() { } @Override - public GapicClass generate(Service service, Map ignore) { + public GapicClass generate(GapicContext ignored, Service service) { String pakkage = service.pakkage(); TypeStore typeStore = createDynamicTypes(service); String className = ClassNames.getServiceSettingsClassName(service); diff --git a/src/main/java/com/google/api/generator/gapic/composer/ServiceStubClassComposer.java b/src/main/java/com/google/api/generator/gapic/composer/ServiceStubClassComposer.java index f2f70a6e8e..a4f55c4eb8 100644 --- a/src/main/java/com/google/api/generator/gapic/composer/ServiceStubClassComposer.java +++ b/src/main/java/com/google/api/generator/gapic/composer/ServiceStubClassComposer.java @@ -36,6 +36,7 @@ import com.google.api.generator.gapic.composer.utils.PackageChecker; import com.google.api.generator.gapic.model.GapicClass; import com.google.api.generator.gapic.model.GapicClass.Kind; +import com.google.api.generator.gapic.model.GapicContext; import com.google.api.generator.gapic.model.Message; import com.google.api.generator.gapic.model.Method; import com.google.api.generator.gapic.model.Service; @@ -60,7 +61,8 @@ public static ServiceStubClassComposer instance() { } @Override - public GapicClass generate(Service service, Map messageTypes) { + public GapicClass generate(GapicContext context, Service service) { + Map messageTypes = context.messages(); TypeStore typeStore = createTypes(service, messageTypes); String className = ClassNames.getServiceStubClassName(service); GapicClass.Kind kind = Kind.STUB; diff --git a/src/main/java/com/google/api/generator/gapic/composer/ServiceStubSettingsClassComposer.java b/src/main/java/com/google/api/generator/gapic/composer/ServiceStubSettingsClassComposer.java index 2774b90c3e..39b66f6f0e 100644 --- a/src/main/java/com/google/api/generator/gapic/composer/ServiceStubSettingsClassComposer.java +++ b/src/main/java/com/google/api/generator/gapic/composer/ServiceStubSettingsClassComposer.java @@ -89,6 +89,7 @@ import com.google.api.generator.gapic.model.Field; import com.google.api.generator.gapic.model.GapicBatchingSettings; import com.google.api.generator.gapic.model.GapicClass; +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.Method; @@ -117,8 +118,7 @@ import javax.annotation.Nullable; import org.threeten.bp.Duration; -// TODO(miraleung): Refactor ClassComposer's interface. -public class ServiceStubSettingsClassComposer { +public class ServiceStubSettingsClassComposer implements ClassComposer { private static final Statement EMPTY_LINE_STATEMENT = EmptyLineStatement.create(); private static final String BATCHING_DESC_PATTERN = "%s_BATCHING_DESC"; @@ -159,11 +159,11 @@ public static ServiceStubSettingsClassComposer instance() { return INSTANCE; } - public GapicClass generate( - Service service, - @Nullable GapicServiceConfig serviceConfig, - Map messageTypes) { + @Override + public GapicClass generate(GapicContext context, Service service) { // TODO(miraleung): Robustify this against a null serviceConfig. + GapicServiceConfig serviceConfig = context.serviceConfig(); + Map messageTypes = context.messages(); String pakkage = String.format("%s.stub", service.pakkage()); TypeStore typeStore = createDynamicTypes(service, pakkage); Map methodSettingsMemberVarExprs = diff --git a/src/main/java/com/google/api/generator/gapic/model/GapicContext.java b/src/main/java/com/google/api/generator/gapic/model/GapicContext.java index c9d7729757..0bbe3cdcdb 100644 --- a/src/main/java/com/google/api/generator/gapic/model/GapicContext.java +++ b/src/main/java/com/google/api/generator/gapic/model/GapicContext.java @@ -17,12 +17,12 @@ import com.google.auto.value.AutoValue; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; -import com.google.common.collect.ImmutableSet; import com.google.gapic.metadata.GapicMetadata; import java.util.Collections; import java.util.List; import java.util.Map; import java.util.Set; +import java.util.stream.Collectors; import javax.annotation.Nullable; @AutoValue @@ -42,7 +42,7 @@ public abstract class GapicContext { // Ensures ordering for deterministic tests. public abstract ImmutableList mixinServices(); - public abstract ImmutableSet helperResourceNames(); + public abstract ImmutableMap helperResourceNames(); public GapicMetadata gapicMetadata() { return gapicMetadata; @@ -87,7 +87,14 @@ public abstract static class Builder { public abstract Builder setMixinServices(List mixinServices); - public abstract Builder setHelperResourceNames(Set helperResourceNames); + public Builder setHelperResourceNames(Set helperResourceNames) { + return setHelperResourceNames( + helperResourceNames.stream() + .map(r -> r) + .collect(Collectors.toMap(r -> r.resourceTypeString(), r -> r))); + } + + abstract Builder setHelperResourceNames(Map helperResourceNames); public abstract Builder setServiceConfig(GapicServiceConfig serviceConfig); diff --git a/src/test/java/com/google/api/generator/gapic/composer/GrpcServiceCallableFactoryClassComposerTest.java b/src/test/java/com/google/api/generator/gapic/composer/GrpcServiceCallableFactoryClassComposerTest.java index 0e09a19956..93211f1eeb 100644 --- a/src/test/java/com/google/api/generator/gapic/composer/GrpcServiceCallableFactoryClassComposerTest.java +++ b/src/test/java/com/google/api/generator/gapic/composer/GrpcServiceCallableFactoryClassComposerTest.java @@ -19,6 +19,7 @@ import com.google.api.generator.engine.writer.JavaWriterVisitor; import com.google.api.generator.gapic.composer.constants.ComposerConstants; import com.google.api.generator.gapic.model.GapicClass; +import com.google.api.generator.gapic.model.GapicContext; import com.google.api.generator.gapic.model.Message; import com.google.api.generator.gapic.model.ResourceName; import com.google.api.generator.gapic.model.Service; @@ -58,9 +59,17 @@ public void generateServiceClasses() { Parser.parseService( echoFileDescriptor, messageTypes, resourceNames, Optional.empty(), outputResourceNames); + GapicContext context = + GapicContext.builder() + .setMessages(messageTypes) + .setResourceNames(resourceNames) + .setServices(services) + .setHelperResourceNames(outputResourceNames) + .build(); + Service echoProtoService = services.get(0); GapicClass clazz = - GrpcServiceCallableFactoryClassComposer.instance().generate(echoProtoService, messageTypes); + GrpcServiceCallableFactoryClassComposer.instance().generate(context, echoProtoService); JavaWriterVisitor visitor = new JavaWriterVisitor(); clazz.classDefinition().accept(visitor); diff --git a/src/test/java/com/google/api/generator/gapic/composer/GrpcServiceStubClassComposerTest.java b/src/test/java/com/google/api/generator/gapic/composer/GrpcServiceStubClassComposerTest.java index fa56cad3b9..144ee8d1a0 100644 --- a/src/test/java/com/google/api/generator/gapic/composer/GrpcServiceStubClassComposerTest.java +++ b/src/test/java/com/google/api/generator/gapic/composer/GrpcServiceStubClassComposerTest.java @@ -19,6 +19,7 @@ import com.google.api.generator.engine.writer.JavaWriterVisitor; import com.google.api.generator.gapic.composer.constants.ComposerConstants; import com.google.api.generator.gapic.model.GapicClass; +import com.google.api.generator.gapic.model.GapicContext; import com.google.api.generator.gapic.model.Message; import com.google.api.generator.gapic.model.ResourceName; import com.google.api.generator.gapic.model.Service; @@ -54,9 +55,17 @@ public void generateGrpcServiceStubClass_simple() { List services = Parser.parseService( echoFileDescriptor, messageTypes, resourceNames, Optional.empty(), outputResourceNames); + + GapicContext context = + GapicContext.builder() + .setMessages(messageTypes) + .setResourceNames(resourceNames) + .setServices(services) + .setHelperResourceNames(outputResourceNames) + .build(); + Service echoProtoService = services.get(0); - GapicClass clazz = - GrpcServiceStubClassComposer.instance().generate(echoProtoService, messageTypes); + GapicClass clazz = GrpcServiceStubClassComposer.instance().generate(context, echoProtoService); JavaWriterVisitor visitor = new JavaWriterVisitor(); clazz.classDefinition().accept(visitor); @@ -81,9 +90,18 @@ public void generateGrpcServiceStubClass_httpBindings() { resourceNames, Optional.empty(), outputResourceNames); + + GapicContext context = + GapicContext.builder() + .setMessages(messageTypes) + .setResourceNames(resourceNames) + .setServices(services) + .setHelperResourceNames(outputResourceNames) + .build(); + Service testingProtoService = services.get(0); GapicClass clazz = - GrpcServiceStubClassComposer.instance().generate(testingProtoService, messageTypes); + GrpcServiceStubClassComposer.instance().generate(context, testingProtoService); JavaWriterVisitor visitor = new JavaWriterVisitor(); clazz.classDefinition().accept(visitor); @@ -115,8 +133,16 @@ public void generateGrpcServiceStubClass_httpBindingsWithSubMessageFields() { Optional.empty(), outputResourceNames); + GapicContext context = + GapicContext.builder() + .setMessages(messageTypes) + .setResourceNames(resourceNames) + .setServices(services) + .setHelperResourceNames(outputResourceNames) + .build(); + Service service = services.get(0); - GapicClass clazz = GrpcServiceStubClassComposer.instance().generate(service, messageTypes); + GapicClass clazz = GrpcServiceStubClassComposer.instance().generate(context, service); JavaWriterVisitor visitor = new JavaWriterVisitor(); clazz.classDefinition().accept(visitor); diff --git a/src/test/java/com/google/api/generator/gapic/composer/MockServiceClassComposerTest.java b/src/test/java/com/google/api/generator/gapic/composer/MockServiceClassComposerTest.java index 4138e9bbdb..abf1f31733 100644 --- a/src/test/java/com/google/api/generator/gapic/composer/MockServiceClassComposerTest.java +++ b/src/test/java/com/google/api/generator/gapic/composer/MockServiceClassComposerTest.java @@ -19,6 +19,7 @@ import com.google.api.generator.engine.writer.JavaWriterVisitor; import com.google.api.generator.gapic.composer.constants.ComposerConstants; import com.google.api.generator.gapic.model.GapicClass; +import com.google.api.generator.gapic.model.GapicContext; import com.google.api.generator.gapic.model.Message; import com.google.api.generator.gapic.model.ResourceName; import com.google.api.generator.gapic.model.Service; @@ -58,8 +59,16 @@ public void generateServiceClasses() { Parser.parseService( echoFileDescriptor, messageTypes, resourceNames, Optional.empty(), outputResourceNames); + GapicContext context = + GapicContext.builder() + .setMessages(messageTypes) + .setResourceNames(resourceNames) + .setServices(services) + .setHelperResourceNames(outputResourceNames) + .build(); + Service echoProtoService = services.get(0); - GapicClass clazz = MockServiceClassComposer.instance().generate(echoProtoService, messageTypes); + GapicClass clazz = MockServiceClassComposer.instance().generate(context, echoProtoService); JavaWriterVisitor visitor = new JavaWriterVisitor(); clazz.classDefinition().accept(visitor); diff --git a/src/test/java/com/google/api/generator/gapic/composer/MockServiceImplClassComposerTest.java b/src/test/java/com/google/api/generator/gapic/composer/MockServiceImplClassComposerTest.java index 83d8f10cd0..fd4724e302 100644 --- a/src/test/java/com/google/api/generator/gapic/composer/MockServiceImplClassComposerTest.java +++ b/src/test/java/com/google/api/generator/gapic/composer/MockServiceImplClassComposerTest.java @@ -19,6 +19,7 @@ import com.google.api.generator.engine.writer.JavaWriterVisitor; import com.google.api.generator.gapic.composer.constants.ComposerConstants; import com.google.api.generator.gapic.model.GapicClass; +import com.google.api.generator.gapic.model.GapicContext; import com.google.api.generator.gapic.model.Message; import com.google.api.generator.gapic.model.ResourceName; import com.google.api.generator.gapic.model.Service; @@ -58,9 +59,16 @@ public void generateServiceClasses() { Parser.parseService( echoFileDescriptor, messageTypes, resourceNames, Optional.empty(), outputResourceNames); + GapicContext context = + GapicContext.builder() + .setMessages(messageTypes) + .setResourceNames(resourceNames) + .setServices(services) + .setHelperResourceNames(outputResourceNames) + .build(); + Service echoProtoService = services.get(0); - GapicClass clazz = - MockServiceImplClassComposer.instance().generate(echoProtoService, messageTypes); + GapicClass clazz = MockServiceImplClassComposer.instance().generate(context, echoProtoService); JavaWriterVisitor visitor = new JavaWriterVisitor(); clazz.classDefinition().accept(visitor); diff --git a/src/test/java/com/google/api/generator/gapic/composer/ServiceClientClassComposerTest.java b/src/test/java/com/google/api/generator/gapic/composer/ServiceClientClassComposerTest.java index 3b4bbfa436..920f1cb4af 100644 --- a/src/test/java/com/google/api/generator/gapic/composer/ServiceClientClassComposerTest.java +++ b/src/test/java/com/google/api/generator/gapic/composer/ServiceClientClassComposerTest.java @@ -53,7 +53,7 @@ public void generateServiceClasses() { Parser.parseService( echoFileDescriptor, messageTypes, resourceNames, Optional.empty(), outputResourceNames); - GapicContext gapicContext = + GapicContext context = GapicContext.builder() .setMessages(messageTypes) .setResourceNames(resourceNames) @@ -62,9 +62,7 @@ public void generateServiceClasses() { .build(); Service echoProtoService = services.get(0); - GapicClass clazz = - ServiceClientClassComposer.instance() - .generate(echoProtoService, gapicContext, resourceNames); + GapicClass clazz = ServiceClientClassComposer.instance().generate(context, echoProtoService); JavaWriterVisitor visitor = new JavaWriterVisitor(); clazz.classDefinition().accept(visitor); @@ -86,7 +84,7 @@ public void generateServiceClasses_methodSignatureHasNestedFields() { Parser.parseService( fileDescriptor, messageTypes, resourceNames, Optional.empty(), outputResourceNames); - GapicContext gapicContext = + GapicContext context = GapicContext.builder() .setMessages(messageTypes) .setResourceNames(resourceNames) @@ -94,8 +92,7 @@ public void generateServiceClasses_methodSignatureHasNestedFields() { .setHelperResourceNames(outputResourceNames) .build(); Service protoService = services.get(0); - GapicClass clazz = - ServiceClientClassComposer.instance().generate(protoService, gapicContext, resourceNames); + GapicClass clazz = ServiceClientClassComposer.instance().generate(context, protoService); JavaWriterVisitor visitor = new JavaWriterVisitor(); clazz.classDefinition().accept(visitor); diff --git a/src/test/java/com/google/api/generator/gapic/composer/ServiceClientTestClassComposerTest.java b/src/test/java/com/google/api/generator/gapic/composer/ServiceClientTestClassComposerTest.java index 486ecfffbf..0161f1a2b3 100644 --- a/src/test/java/com/google/api/generator/gapic/composer/ServiceClientTestClassComposerTest.java +++ b/src/test/java/com/google/api/generator/gapic/composer/ServiceClientTestClassComposerTest.java @@ -61,7 +61,7 @@ public void generateClientTest_echoClient() { Parser.parseService( echoFileDescriptor, messageTypes, resourceNames, Optional.empty(), outputResourceNames); - GapicContext gapicContext = + GapicContext context = GapicContext.builder() .setServices(services) .setMessages(messageTypes) @@ -71,8 +71,7 @@ public void generateClientTest_echoClient() { Service echoProtoService = services.get(0); GapicClass clazz = - ServiceClientTestClassComposer.instance() - .generate(echoProtoService, gapicContext, resourceNames); + ServiceClientTestClassComposer.instance().generate(context, echoProtoService); JavaWriterVisitor visitor = new JavaWriterVisitor(); clazz.classDefinition().accept(visitor); @@ -99,7 +98,7 @@ public void generateClientTest_testingClientResnameWithOnePatternWithNonSlashSep Optional.empty(), outputResourceNames); - GapicContext gapicContext = + GapicContext context = GapicContext.builder() .setServices(services) .setMessages(messageTypes) @@ -109,8 +108,7 @@ public void generateClientTest_testingClientResnameWithOnePatternWithNonSlashSep Service testingProtoService = services.get(0); GapicClass clazz = - ServiceClientTestClassComposer.instance() - .generate(testingProtoService, gapicContext, resourceNames); + ServiceClientTestClassComposer.instance().generate(context, testingProtoService); JavaWriterVisitor visitor = new JavaWriterVisitor(); clazz.classDefinition().accept(visitor); @@ -142,7 +140,7 @@ public void generateClientTest_pubSubPublisherClient() { Optional.empty(), outputResourceNames); - GapicContext gapicContext = + GapicContext context = GapicContext.builder() .setServices(services) .setMessages(messageTypes) @@ -153,8 +151,7 @@ public void generateClientTest_pubSubPublisherClient() { Service subscriptionService = services.get(1); assertEquals("Subscriber", subscriptionService.name()); GapicClass clazz = - ServiceClientTestClassComposer.instance() - .generate(subscriptionService, gapicContext, resourceNames); + ServiceClientTestClassComposer.instance().generate(context, subscriptionService); JavaWriterVisitor visitor = new JavaWriterVisitor(); clazz.classDefinition().accept(visitor); @@ -197,7 +194,7 @@ public void generateClientTest_logging() { Optional.empty(), outputResourceNames); - GapicContext gapicContext = + GapicContext context = GapicContext.builder() .setServices(services) .setMessages(messageTypes) @@ -206,9 +203,7 @@ public void generateClientTest_logging() { .build(); Service loggingService = services.get(0); - GapicClass clazz = - ServiceClientTestClassComposer.instance() - .generate(loggingService, gapicContext, resourceNames); + GapicClass clazz = ServiceClientTestClassComposer.instance().generate(context, loggingService); JavaWriterVisitor visitor = new JavaWriterVisitor(); clazz.classDefinition().accept(visitor); diff --git a/src/test/java/com/google/api/generator/gapic/composer/ServiceSettingsClassComposerTest.java b/src/test/java/com/google/api/generator/gapic/composer/ServiceSettingsClassComposerTest.java index be19c78d8b..79b684d443 100644 --- a/src/test/java/com/google/api/generator/gapic/composer/ServiceSettingsClassComposerTest.java +++ b/src/test/java/com/google/api/generator/gapic/composer/ServiceSettingsClassComposerTest.java @@ -19,6 +19,7 @@ import com.google.api.generator.engine.writer.JavaWriterVisitor; import com.google.api.generator.gapic.composer.constants.ComposerConstants; import com.google.api.generator.gapic.model.GapicClass; +import com.google.api.generator.gapic.model.GapicContext; import com.google.api.generator.gapic.model.Message; import com.google.api.generator.gapic.model.ResourceName; import com.google.api.generator.gapic.model.Service; @@ -58,9 +59,16 @@ public void generateServiceClasses() { Parser.parseService( echoFileDescriptor, messageTypes, resourceNames, Optional.empty(), outputResourceNames); + GapicContext context = + GapicContext.builder() + .setMessages(messageTypes) + .setResourceNames(resourceNames) + .setServices(services) + .setHelperResourceNames(outputResourceNames) + .build(); + Service echoProtoService = services.get(0); - GapicClass clazz = - ServiceSettingsClassComposer.instance().generate(echoProtoService, messageTypes); + GapicClass clazz = ServiceSettingsClassComposer.instance().generate(context, echoProtoService); JavaWriterVisitor visitor = new JavaWriterVisitor(); clazz.classDefinition().accept(visitor); diff --git a/src/test/java/com/google/api/generator/gapic/composer/ServiceStubClassComposerTest.java b/src/test/java/com/google/api/generator/gapic/composer/ServiceStubClassComposerTest.java index f7c6c37ecd..3a0128f553 100644 --- a/src/test/java/com/google/api/generator/gapic/composer/ServiceStubClassComposerTest.java +++ b/src/test/java/com/google/api/generator/gapic/composer/ServiceStubClassComposerTest.java @@ -19,6 +19,7 @@ import com.google.api.generator.engine.writer.JavaWriterVisitor; import com.google.api.generator.gapic.composer.constants.ComposerConstants; import com.google.api.generator.gapic.model.GapicClass; +import com.google.api.generator.gapic.model.GapicContext; import com.google.api.generator.gapic.model.Message; import com.google.api.generator.gapic.model.ResourceName; import com.google.api.generator.gapic.model.Service; @@ -58,8 +59,16 @@ public void generateServiceClasses() { Parser.parseService( echoFileDescriptor, messageTypes, resourceNames, Optional.empty(), outputResourceNames); + GapicContext context = + GapicContext.builder() + .setMessages(messageTypes) + .setResourceNames(resourceNames) + .setServices(services) + .setHelperResourceNames(outputResourceNames) + .build(); + Service echoProtoService = services.get(0); - GapicClass clazz = ServiceStubClassComposer.instance().generate(echoProtoService, messageTypes); + GapicClass clazz = ServiceStubClassComposer.instance().generate(context, echoProtoService); JavaWriterVisitor visitor = new JavaWriterVisitor(); clazz.classDefinition().accept(visitor); diff --git a/src/test/java/com/google/api/generator/gapic/composer/ServiceStubSettingsClassComposerTest.java b/src/test/java/com/google/api/generator/gapic/composer/ServiceStubSettingsClassComposerTest.java index df9636830b..66fc64c406 100644 --- a/src/test/java/com/google/api/generator/gapic/composer/ServiceStubSettingsClassComposerTest.java +++ b/src/test/java/com/google/api/generator/gapic/composer/ServiceStubSettingsClassComposerTest.java @@ -21,6 +21,7 @@ import com.google.api.generator.gapic.composer.constants.ComposerConstants; import com.google.api.generator.gapic.model.GapicBatchingSettings; import com.google.api.generator.gapic.model.GapicClass; +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; @@ -49,6 +50,7 @@ import java.util.Map; import java.util.Optional; import java.util.Set; +import java.util.stream.Collectors; import org.junit.Test; public class ServiceStubSettingsClassComposerTest { @@ -89,9 +91,18 @@ public void generateServiceStubSettingsClasses_batchingWithEmptyResponses() thro GapicServiceConfig config = configOpt.get(); config.setBatchingSettings(batchingSettingsOpt); + GapicContext context = + GapicContext.builder() + .setMessages(messageTypes) + .setResourceNames(resourceNames) + .setServices(services) + .setServiceConfig(config) + .setHelperResourceNames( + resourceNames.values().stream().map(r -> r).collect(Collectors.toSet())) + .build(); + Service protoService = services.get(0); - GapicClass clazz = - ServiceStubSettingsClassComposer.instance().generate(protoService, config, messageTypes); + GapicClass clazz = ServiceStubSettingsClassComposer.instance().generate(context, protoService); JavaWriterVisitor visitor = new JavaWriterVisitor(); clazz.classDefinition().accept(visitor); @@ -132,10 +143,19 @@ public void generateServiceStubSettingsClasses_batchingWithNonemptyResponses() GapicServiceConfig config = configOpt.get(); config.setBatchingSettings(batchingSettingsOpt); + GapicContext context = + GapicContext.builder() + .setMessages(messageTypes) + .setResourceNames(resourceNames) + .setServices(services) + .setServiceConfig(config) + .setHelperResourceNames( + resourceNames.values().stream().map(r -> r).collect(Collectors.toSet())) + .build(); + Service protoService = services.get(0); assertEquals("Publisher", protoService.name()); - GapicClass clazz = - ServiceStubSettingsClassComposer.instance().generate(protoService, config, messageTypes); + GapicClass clazz = ServiceStubSettingsClassComposer.instance().generate(context, protoService); JavaWriterVisitor visitor = new JavaWriterVisitor(); clazz.classDefinition().accept(visitor); @@ -162,10 +182,19 @@ public void generateServiceStubSettingsClasses_basic() throws IOException { assertTrue(configOpt.isPresent()); GapicServiceConfig config = configOpt.get(); + GapicContext context = + GapicContext.builder() + .setMessages(messageTypes) + .setResourceNames(resourceNames) + .setServices(services) + .setServiceConfig(config) + .setHelperResourceNames( + resourceNames.values().stream().map(r -> r).collect(Collectors.toSet())) + .build(); + Service echoProtoService = services.get(0); GapicClass clazz = - ServiceStubSettingsClassComposer.instance() - .generate(echoProtoService, config, messageTypes); + ServiceStubSettingsClassComposer.instance().generate(context, echoProtoService); JavaWriterVisitor visitor = new JavaWriterVisitor(); clazz.classDefinition().accept(visitor);