Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,8 @@ static String buildAutoConfigRegistrationString(GapicContext context) {
service ->
sb.add(
String.format(
"%s.spring.%sSpringAutoConfig", service.pakkage(), service.name())));
"%s.spring.%s",
service.pakkage(), Utils.getServiceAutoConfigurationClassName(service))));
return sb.toString();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,12 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class SpringAutoConfigClassComposer implements ClassComposer {

private static final String CLASS_NAME_PATTERN = "%sSpringAutoConfiguration";

private static final SpringAutoConfigClassComposer INSTANCE = new SpringAutoConfigClassComposer();

private static final Map<String, TypeNode> STATIC_TYPES = createStaticTypes();
Expand All @@ -85,7 +83,7 @@ public GapicClass generate(GapicContext context, Service service) {
String serviceName = service.name();
String serviceNameLowerCamel = CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_CAMEL, serviceName);
String serviceNameLowerHyphen = CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_HYPHEN, serviceName);
String className = getThisClassName(serviceName);
String className = Utils.getServiceAutoConfigurationClassName(service);
String credentialsProviderName = serviceNameLowerCamel + "Credentials";
String transportChannelProviderName = "default" + serviceName + "TransportChannelProvider";
String clientName = serviceNameLowerCamel + "Client";
Expand All @@ -110,7 +108,7 @@ public GapicClass generate(GapicContext context, Service service) {
.setAnnotations(createClassAnnotations(service, types))
.setMethods(
Arrays.asList(
createConstructor(service.name(), className, types, thisExpr),
createConstructor(service, className, types, thisExpr),
createCredentialsProviderBeanMethod(
service, className, credentialsProviderName, types, thisExpr),
createTransportChannelProviderBeanMethod(transportChannelProviderName, types),
Expand Down Expand Up @@ -143,7 +141,7 @@ private static List<Statement> createMemberVariables(
Variable clientPropertiesVar =
Variable.builder()
.setName("clientProperties")
.setType(types.get(serviceName + "Properties"))
.setType(types.get(Utils.getServicePropertiesClassName(service)))
.build();
VariableExpr clientPropertiesVarExpr =
VariableExpr.builder()
Expand All @@ -155,22 +153,23 @@ private static List<Statement> createMemberVariables(
ExprStatement clientPropertiesStatement = ExprStatement.withExpr(clientPropertiesVarExpr);

Statement loggerStatement =
LoggerUtils.getLoggerDeclarationExpr(serviceName + "AutoConfig", types);
LoggerUtils.getLoggerDeclarationExpr(
Utils.getServiceAutoConfigurationClassName(service), types);
return Arrays.asList(clientPropertiesStatement, loggerStatement);
}

private static MethodDefinition createConstructor(
String serviceName, String className, Map<String, TypeNode> types, Expr thisExpr) {
Service service, String className, Map<String, TypeNode> types, Expr thisExpr) {
VariableExpr propertiesVarExpr =
VariableExpr.withVariable(
Variable.builder()
.setName("clientProperties")
.setType(types.get(serviceName + "Properties"))
.setType(types.get(Utils.getServicePropertiesClassName(service)))
.build());
Variable clientPropertiesVar =
Variable.builder()
.setName("clientProperties")
.setType(types.get(serviceName + "Properties"))
.setType(types.get(Utils.getServicePropertiesClassName(service)))
.build();

// this.clientProperties = clientProperties;
Expand Down Expand Up @@ -248,7 +247,7 @@ private static List<AnnotationNode> createClassAnnotations(
VariableExpr.builder()
.setVariable(
Variable.builder().setType(TypeNode.CLASS_OBJECT).setName("class").build())
.setStaticReferenceType(types.get(service.name() + "Properties"))
.setStaticReferenceType(types.get(Utils.getServicePropertiesClassName(service)))
.build())
.build();

Expand All @@ -274,7 +273,7 @@ private static MethodDefinition createCredentialsProviderBeanMethod(
Variable clientPropertiesVar =
Variable.builder()
.setName("clientProperties")
.setType(types.get(service.name() + "Properties"))
.setType(types.get(Utils.getServicePropertiesClassName(service)))
.build();

VariableExpr thisClientProperties =
Expand Down Expand Up @@ -505,7 +504,7 @@ private static MethodDefinition createClientBeanMethod(
Variable clientPropertiesVar =
Variable.builder()
.setName("clientProperties")
.setType(types.get(service.name() + "Properties"))
.setType(types.get(Utils.getServicePropertiesClassName(service)))
.build();
VariableExpr thisClientPropertiesVarExpr =
VariableExpr.withVariable(clientPropertiesVar)
Expand Down Expand Up @@ -665,8 +664,8 @@ private static MethodDefinition createClientBeanMethod(
bodyStatements.add(setTransportChannelProviderStatement);
}
// retry settings for each method
TypeNode thisClassType = types.get(service.name() + "AutoConfig");
List<Statement> retrySettings =
TypeNode thisClassType = types.get(Utils.getServiceAutoConfigurationClassName(service));
List retrySettings =
Utils.processRetrySettings(
service,
gapicServiceConfig,
Expand Down Expand Up @@ -777,12 +776,13 @@ private static MethodDefinition createClientBeanMethod(

String methodName =
CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_CAMEL, service.name()) + "Client";
String propertiesClassName = service.name() + "Properties";

return MethodDefinition.builder()
.setHeaderCommentStatements(
SpringAutoconfigCommentComposer.createClientBeanComment(
service.name(), propertiesClassName, transportChannelProviderName))
service.name(),
Utils.getServicePropertiesClassName(service),
transportChannelProviderName))
.setName(methodName)
.setScope(ScopeNode.PUBLIC)
.setReturnType(types.get("ServiceClient"))
Expand Down Expand Up @@ -895,35 +895,27 @@ private static Map<String, TypeNode> createStaticTypes() {
}

private static Map<String, TypeNode> createDynamicTypes(Service service, String packageName) {
Map<String, TypeNode> typeMap =
Arrays.asList(CLASS_NAME_PATTERN).stream()
.collect(
Collectors.toMap(
p -> String.format(p, service.name()),
p ->
TypeNode.withReference(
VaporReference.builder()
.setName(String.format(p, service.name()))
.setPakkage(packageName)
.build())));
TypeNode credentialsProvider =
Map<String, TypeNode> typeMap = new HashMap<>();

TypeNode clientAutoconfiguration =
TypeNode.withReference(
VaporReference.builder()
.setName("CredentialsProvider")
.setPakkage("com.google.api.gax.core")
.setName(Utils.getServiceAutoConfigurationClassName(service))
.setPakkage(packageName)
.build());

TypeNode clientProperties =
TypeNode.withReference(
VaporReference.builder()
.setName(service.name() + "SpringProperties")
.setName(Utils.getServicePropertiesClassName(service))
.setPakkage(packageName)
.build());

TypeNode clientAutoconfig =
TypeNode credentialsProvider =
TypeNode.withReference(
VaporReference.builder()
.setName(service.name() + "SpringAutoConfig")
.setPakkage(packageName)
.setName("CredentialsProvider")
.setPakkage("com.google.api.gax.core")
.build());

TypeNode gcpProjectIdProvider =
Expand Down Expand Up @@ -1021,9 +1013,9 @@ private static Map<String, TypeNode> createDynamicTypes(Service service, String
.setPakkage("org.springframework.beans.factory.annotation")
.build());

typeMap.put(Utils.getServiceAutoConfigurationClassName(service), clientAutoconfiguration);
typeMap.put(Utils.getServicePropertiesClassName(service), clientProperties);
typeMap.put("CredentialsProvider", credentialsProvider);
typeMap.put(service.name() + "Properties", clientProperties);
typeMap.put(service.name() + "AutoConfig", clientAutoconfig);
typeMap.put("GcpProjectIdProvider", gcpProjectIdProvider);
typeMap.put("Credentials", credentials);
typeMap.put("DefaultCredentialsProvider", defaultCredentialsProvider);
Expand All @@ -1042,8 +1034,4 @@ private static Map<String, TypeNode> createDynamicTypes(Service service, String

return typeMap;
}

private static String getThisClassName(String serviceName) {
return String.format(CLASS_NAME_PATTERN, serviceName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,12 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class SpringPropertiesClassComposer implements ClassComposer {

private static final String CLASS_NAME_PATTERN = "%sSpringProperties";

private static final Map<String, TypeNode> staticTypes = createStaticTypes();
private static final String RETRY_PARAM_DEFINITIONS_VAR_NAME = "RETRY_PARAM_DEFINITIONS";

Expand All @@ -70,7 +68,7 @@ public static SpringPropertiesClassComposer instance() {
@Override
public GapicClass generate(GapicContext context, Service service) {
String packageName = Utils.getSpringPackageName(service.pakkage());
String className = String.format(CLASS_NAME_PATTERN, service.name());
String className = Utils.getServicePropertiesClassName(service);
GapicServiceConfig gapicServiceConfig = context.serviceConfig();
Map<String, TypeNode> types = createDynamicTypes(service, packageName);
boolean hasRestOption = context.transport().equals(Transport.GRPC_REST);
Expand Down Expand Up @@ -187,7 +185,7 @@ private static List<Statement> createMemberVariables(
// private static final ImmutableMap<String, RetrySettings> RETRY_PARAM_DEFINITIONS;

// declare each retry settings with its default value. use defaults from serviceConfig
TypeNode thisClassType = types.get(service.name() + "Properties");
TypeNode thisClassType = types.get(Utils.getServicePropertiesClassName(service));
List<Statement> retrySettings =
Utils.processRetrySettings(
service,
Expand Down Expand Up @@ -220,7 +218,7 @@ private static List<MethodDefinition> createGetterSetters(
GapicServiceConfig gapicServiceConfig,
boolean hasRestOption) {

TypeNode thisClassType = types.get(service.name() + "Properties");
TypeNode thisClassType = types.get(Utils.getServicePropertiesClassName(service));
List<MethodDefinition> methodDefinitions = new ArrayList<>();

methodDefinitions.add(
Expand Down Expand Up @@ -344,21 +342,12 @@ private static MethodDefinition createSetterMethod(
}

private static Map<String, TypeNode> createDynamicTypes(Service service, String packageName) {
Map<String, TypeNode> typeMap =
Arrays.asList(CLASS_NAME_PATTERN).stream()
.collect(
Collectors.toMap(
p -> String.format(p, service.name()),
p ->
TypeNode.withReference(
VaporReference.builder()
.setName(String.format(p, service.name()))
.setPakkage(packageName)
.build())));
Map<String, TypeNode> typeMap = new HashMap<>();

TypeNode clientProperties =
TypeNode.withReference(
VaporReference.builder()
.setName(service.name() + "SpringProperties")
.setName(Utils.getServicePropertiesClassName(service))
.setPakkage(packageName)
.build());

Expand Down Expand Up @@ -393,7 +382,7 @@ private static Map<String, TypeNode> createDynamicTypes(Service service, String
.setPakkage("org.springframework.boot.context.properties")
.build());

typeMap.put(service.name() + "Properties", clientProperties);
typeMap.put(Utils.getServicePropertiesClassName(service), clientProperties);
typeMap.put("Credentials", credentials);
typeMap.put("CredentialsSupplier", credentialsSupplier);
typeMap.put("ConfigurationProperties", configurationProperties);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ public class Utils {

private static final String BRAND_NAME = "spring.cloud.gcp";

public static String getServiceAutoConfigurationClassName(Service service) {
return service.name() + "SpringAutoConfiguration";
}

public static String getServicePropertiesClassName(Service service) {
return service.name() + "SpringProperties";
}

public static String getLibName(GapicContext context) {
// Returns parsed name of client library
// This should only be used in descriptive context, such as metadata and javadocs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public void setUp() {
@Test
public void buildAutoConfigRegistrationStringTest() {
String result = SpringWriter.buildAutoConfigRegistrationString(context);
String expected = "com.google.showcase.v1beta1.spring.EchoSpringAutoConfig";
String expected = "com.google.showcase.v1beta1.spring.EchoSpringAutoConfiguration";
assertEquals(expected, result);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ import org.threeten.bp.Duration;
@EnableConfigurationProperties(EchoSpringProperties.class)
public class EchoSpringAutoConfiguration {
private final EchoSpringProperties clientProperties;
private static final Log LOGGER = LogFactory.getLog(EchoSpringAutoConfig.class);
private static final Log LOGGER = LogFactory.getLog(EchoSpringAutoConfiguration.class);

protected EchoSpringAutoConfiguration(EchoSpringProperties clientProperties) {
this.clientProperties = clientProperties;
Expand Down Expand Up @@ -97,7 +97,7 @@ public class EchoSpringAutoConfiguration {
* client
*
* <p>Individual retry settings are configured as well. It will use the relevant client library's
* default retry settings when they are not specified in EchoProperties.
* default retry settings when they are not specified in EchoSpringProperties.
*/
@Bean
@ConditionalOnMissingBean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ import org.threeten.bp.Duration;
@EnableConfigurationProperties(EchoSpringProperties.class)
public class EchoSpringAutoConfiguration {
private final EchoSpringProperties clientProperties;
private static final Log LOGGER = LogFactory.getLog(EchoSpringAutoConfig.class);
private static final Log LOGGER = LogFactory.getLog(EchoSpringAutoConfiguration.class);

protected EchoSpringAutoConfiguration(EchoSpringProperties clientProperties) {
this.clientProperties = clientProperties;
Expand Down Expand Up @@ -77,7 +77,7 @@ public class EchoSpringAutoConfiguration {
* client
*
* <p>Individual retry settings are configured as well. It will use the relevant client library's
* default retry settings when they are not specified in EchoProperties.
* default retry settings when they are not specified in EchoSpringProperties.
*/
@Bean
@ConditionalOnMissingBean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ import org.threeten.bp.Duration;
@EnableConfigurationProperties(EchoSpringProperties.class)
public class EchoSpringAutoConfiguration {
private final EchoSpringProperties clientProperties;
private static final Log LOGGER = LogFactory.getLog(EchoSpringAutoConfig.class);
private static final Log LOGGER = LogFactory.getLog(EchoSpringAutoConfiguration.class);

protected EchoSpringAutoConfiguration(EchoSpringProperties clientProperties) {
this.clientProperties = clientProperties;
Expand Down Expand Up @@ -78,7 +78,7 @@ public class EchoSpringAutoConfiguration {
* client
*
* <p>Individual retry settings are configured as well. It will use the relevant client library's
* default retry settings when they are not specified in EchoProperties.
* default retry settings when they are not specified in EchoSpringProperties.
*/
@Bean
@ConditionalOnMissingBean
Expand Down