Skip to content

Commit

Permalink
chore(engx): Consolidate codegen/composer generate() iface (#662)
Browse files Browse the repository at this point in the history
  • Loading branch information
miraleung authored Feb 19, 2021
1 parent d29dfc7 commit d568163
Show file tree
Hide file tree
Showing 21 changed files with 206 additions and 121 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Message> messageTypes);
GapicClass generate(GapicContext context, Service serivce);
}
90 changes: 40 additions & 50 deletions src/main/java/com/google/api/generator/gapic/composer/Composer.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,51 +22,37 @@
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<GapicClass> composeServiceClasses(GapicContext context) {
List<GapicClass> clazzes = new ArrayList<>();
Map<String, ResourceName> 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);
}

public static GapicPackageInfo composePackageInfo(GapicContext context) {
return addApacheLicense(ClientLibraryPackageInfoComposer.generatePackageInfo(context));
}

public static List<GapicClass> generateServiceClasses(
@Nonnull Service service,
GapicContext context,
@Nonnull Map<String, ResourceName> resourceNames) {
public static List<GapicClass> generateServiceClasses(GapicContext context) {
List<GapicClass> 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;
}

Expand All @@ -78,42 +64,46 @@ public static List<GapicClass> generateResourceNameHelperClasses(
.collect(Collectors.toList());
}

public static List<GapicClass> generateStubClasses(
Service service,
GapicServiceConfig serviceConfig,
Map<String, Message> messageTypes,
Map<String, ResourceName> resourceNames) {
public static List<GapicClass> generateStubClasses(GapicContext context) {
List<GapicClass> 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<GapicClass> generateClientSettingsClasses(
Service service, GapicContext context, Map<String, ResourceName> resourceNames) {
public static List<GapicClass> generateClientSettingsClasses(GapicContext context) {
List<GapicClass> 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<GapicClass> generateMockClasses(
Service service, Map<String, ResourceName> resourceNames, Map<String, Message> messageTypes) {
public static List<GapicClass> generateMockClasses(GapicContext context, List<Service> services) {
List<GapicClass> 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<GapicClass> generateTestClasses(
Service service, GapicContext context, Map<String, ResourceName> resourceNames) {
List<GapicClass> clazzes = new ArrayList<>();
clazzes.add(
ServiceClientTestClassComposer.instance().generate(service, context, resourceNames));
return clazzes;
public static List<GapicClass> generateTestClasses(GapicContext context) {
return context.services().stream()
.map(s -> ServiceClientTestClassComposer.instance().generate(context, s))
.collect(Collectors.toList());
}

/** ====================== HELPERS ==================== */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -67,7 +66,7 @@ public static GrpcServiceCallableFactoryClassComposer instance() {
}

@Override
public GapicClass generate(Service service, Map<String, Message> ignore) {
public GapicClass generate(GapicContext ignored, Service service) {
TypeStore typeStore = createTypes(service);
String className = ClassNames.getGrpcServiceCallableFactoryClassName(service);
GapicClass.Kind kind = Kind.STUB;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -116,7 +116,7 @@ public static GrpcServiceStubClassComposer instance() {
}

@Override
public GapicClass generate(Service service, Map<String, Message> ignore) {
public GapicClass generate(GapicContext ignored, Service service) {
String pakkage = service.pakkage() + ".stub";
TypeStore typeStore = createDynamicTypes(service, pakkage);
String className = ClassNames.getGrpcServiceStubClassName(service);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -53,7 +52,7 @@ public static MockServiceClassComposer instance() {
}

@Override
public GapicClass generate(Service service, Map<String, Message> ignore) {
public GapicClass generate(GapicContext ignored, Service service) {
TypeStore typeStore = createTypes(service);
String className = ClassNames.getMockServiceClassName(service);
GapicClass.Kind kind = Kind.TEST;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -95,7 +94,7 @@ public static MockServiceImplClassComposer instance() {
}

@Override
public GapicClass generate(Service service, Map<String, Message> ignore) {
public GapicClass generate(GapicContext ignored, Service service) {
TypeStore typeStore = createDynamicTypes(service);
String className = ClassNames.getMockServiceImplClassName(service);
GapicClass.Kind kind = Kind.TEST;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -121,8 +121,9 @@ public static ServiceClientClassComposer instance() {
return INSTANCE;
}

public GapicClass generate(
Service service, GapicContext context, Map<String, ResourceName> resourceNames) {
@Override
public GapicClass generate(GapicContext context, Service service) {
Map<String, ResourceName> resourceNames = context.helperResourceNames();
Map<String, Message> messageTypes = context.messages();
TypeStore typeStore = createTypes(service, messageTypes);
String className = ClassNames.getServiceClientClassName(service);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -135,8 +134,9 @@ public static ServiceClientTestClassComposer instance() {
return INSTANCE;
}

public GapicClass generate(
Service service, GapicContext context, Map<String, ResourceName> resourceNames) {
@Override
public GapicClass generate(GapicContext context, Service service) {
Map<String, ResourceName> resourceNames = context.helperResourceNames();
Map<String, Message> messageTypes = context.messages();
String pakkage = service.pakkage();
TypeStore typeStore = new TypeStore();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -94,7 +93,7 @@ public static ServiceSettingsClassComposer instance() {
}

@Override
public GapicClass generate(Service service, Map<String, Message> ignore) {
public GapicClass generate(GapicContext ignored, Service service) {
String pakkage = service.pakkage();
TypeStore typeStore = createDynamicTypes(service);
String className = ClassNames.getServiceSettingsClassName(service);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -60,7 +61,8 @@ public static ServiceStubClassComposer instance() {
}

@Override
public GapicClass generate(Service service, Map<String, Message> messageTypes) {
public GapicClass generate(GapicContext context, Service service) {
Map<String, Message> messageTypes = context.messages();
TypeStore typeStore = createTypes(service, messageTypes);
String className = ClassNames.getServiceStubClassName(service);
GapicClass.Kind kind = Kind.STUB;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -159,11 +159,11 @@ public static ServiceStubSettingsClassComposer instance() {
return INSTANCE;
}

public GapicClass generate(
Service service,
@Nullable GapicServiceConfig serviceConfig,
Map<String, Message> messageTypes) {
@Override
public GapicClass generate(GapicContext context, Service service) {
// TODO(miraleung): Robustify this against a null serviceConfig.
GapicServiceConfig serviceConfig = context.serviceConfig();
Map<String, Message> messageTypes = context.messages();
String pakkage = String.format("%s.stub", service.pakkage());
TypeStore typeStore = createDynamicTypes(service, pakkage);
Map<String, VariableExpr> methodSettingsMemberVarExprs =
Expand Down
Loading

0 comments on commit d568163

Please sign in to comment.