Skip to content

Commit

Permalink
rename to global properties
Browse files Browse the repository at this point in the history
  • Loading branch information
diegomarquezp committed Nov 14, 2022
1 parent 2532e78 commit a8886c2
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 111 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,11 @@
import com.google.api.generator.gapic.model.Service;
import com.google.api.generator.gapic.model.Transport;
import com.google.api.generator.spring.composer.comment.SpringAutoconfigCommentComposer;
import com.google.api.generator.spring.utils.GlobalPropertiesUtils;
import com.google.api.generator.spring.utils.LoggerUtils;
import com.google.api.generator.spring.utils.SharedPropertiesUtils;
import com.google.api.generator.spring.utils.Utils;
import com.google.common.base.CaseFormat;
import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import java.io.IOException;
import java.util.ArrayList;
Expand All @@ -68,8 +67,6 @@
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.annotation.Generated;
import javax.management.StringValueExp;

public class SpringAutoConfigClassComposer implements ClassComposer {
private static final String CLASS_NAME_PATTERN = "%sSpringAutoConfiguration";
Expand Down Expand Up @@ -162,8 +159,9 @@ private static List<Statement> createMemberVariables(

Statement loggerStatement =
LoggerUtils.getLoggerDeclarationExpr(serviceName + "AutoConfig", types);
Statement sharedPropertiesStatement = SharedPropertiesUtils.getSharedPropertiesDeclaration(types);
return Arrays.asList(clientPropertiesStatement, sharedPropertiesStatement, loggerStatement);
Statement globalPropertiesStatement =
GlobalPropertiesUtils.getGlobalPropertiesDeclaration(types);
return Arrays.asList(clientPropertiesStatement, globalPropertiesStatement, loggerStatement);
}

private static MethodDefinition createConstructor(
Expand All @@ -189,11 +187,11 @@ private static MethodDefinition createConstructor(
.setName("clientProperties")
.setType(types.get(serviceName + "Properties"))
.build());
VariableExpr sharedPropertiesVarExpr =
VariableExpr globalPropertiesVarExpr =
VariableExpr.withVariable(
Variable.builder()
.setName("sharedProperties")
.setType(types.get("SharedProperties"))
.setName("globalProperties")
.setType(types.get("GlobalProperties"))
.build());
// Variable projectIdProviderVar =
// Variable.builder()
Expand All @@ -205,10 +203,10 @@ private static MethodDefinition createConstructor(
.setName("clientProperties")
.setType(types.get(serviceName + "Properties"))
.build();
Variable sharedPropertiesVar =
Variable globalPropertiesVar =
Variable.builder()
.setName("sharedProperties")
.setType(types.get("SharedProperties"))
.setName("globalProperties")
.setType(types.get("GlobalProperties"))
.build();

// this.clientProperties = clientProperties;
Expand All @@ -224,29 +222,28 @@ private static MethodDefinition createConstructor(
ExprStatement thisClientPropertiesAssignmentStatement =
ExprStatement.withExpr(thisClientPropertiesAssignmentExpr);

AssignmentExpr thisSharedPropertiesAssignmentExpr =
AssignmentExpr thisGlobalPropertiesAssignmentExpr =
AssignmentExpr.builder()
.setVariableExpr(
VariableExpr.withVariable(sharedPropertiesVar)
VariableExpr.withVariable(globalPropertiesVar)
.toBuilder()
.setExprReferenceExpr(thisExpr)
.build())
.setValueExpr(sharedPropertiesVarExpr)
.setValueExpr(globalPropertiesVarExpr)
.build();
ExprStatement thisSharedPropertiesAssignmentStatement =
ExprStatement.withExpr(thisSharedPropertiesAssignmentExpr);
ExprStatement thisGlobalPropertiesAssignmentStatement =
ExprStatement.withExpr(thisGlobalPropertiesAssignmentExpr);

return MethodDefinition.constructorBuilder()
.setScope(ScopeNode.PROTECTED)
.setReturnType(types.get(className))
.setArguments(Arrays.asList(
clientPropertiesVarExpr.toBuilder().setIsDecl(true).build(),
sharedPropertiesVarExpr.toBuilder().setIsDecl(true).build()
))
.setBody(Arrays.asList(
thisClientPropertiesAssignmentStatement,
thisSharedPropertiesAssignmentStatement
))
.setArguments(
Arrays.asList(
clientPropertiesVarExpr.toBuilder().setIsDecl(true).build(),
globalPropertiesVarExpr.toBuilder().setIsDecl(true).build()))
.setBody(
Arrays.asList(
thisClientPropertiesAssignmentStatement, thisGlobalPropertiesAssignmentStatement))
.build();
}

Expand Down Expand Up @@ -302,19 +299,26 @@ private static List<AnnotationNode> createClassAnnotations(
.setType(types.get("EnableConfigurationProperties"))
.setDescription(
ArrayExpr.builder()
.addExpr(VariableExpr.builder()
.setVariable(
Variable.builder().setType(TypeNode.CLASS_OBJECT).setName("class").build())
.setStaticReferenceType(types.get(service.name() + "Properties"))
.build())
.addExpr(VariableExpr.builder()
.setVariable(
Variable.builder().setType(TypeNode.CLASS_OBJECT).setName("class").build())
.setStaticReferenceType(types.get("SharedProperties"))
.build())
.build()

)
.setType(TypeNode.createArrayTypeOf(TypeNode.CLASS_OBJECT))
.addExpr(
VariableExpr.builder()
.setVariable(
Variable.builder()
.setType(TypeNode.CLASS_OBJECT)
.setName("class")
.build())
.setStaticReferenceType(types.get(service.name() + "Properties"))
.build())
.addExpr(
VariableExpr.builder()
.setVariable(
Variable.builder()
.setType(TypeNode.CLASS_OBJECT)
.setName("class")
.build())
.setStaticReferenceType(types.get("GlobalProperties"))
.build())
.build())
.build();

return Arrays.asList(
Expand All @@ -336,13 +340,13 @@ private static MethodDefinition createCredentialsProviderBeanMethod(
// if (this.clientProperties.getCredentials().hasKey()) {
// return new DefaultCredentialsProvider(this.clientProperties);
// }
// return new DefaultCredentialsProvider(this.sharedProperties);
// return new DefaultCredentialsProvider(this.globalProperties);
// }
List<Statement> bodyStatements = new ArrayList<>();
Variable sharedPropertiesVar =
Variable globalPropertiesVar =
Variable.builder()
.setName("sharedProperties")
.setType(types.get("SharedProperties"))
.setName("GlobalProperties")
.setType(types.get("GlobalProperties"))
.build();
Variable clientPropertiesVar =
Variable.builder()
Expand All @@ -355,8 +359,8 @@ private static MethodDefinition createCredentialsProviderBeanMethod(
.toBuilder()
.setExprReferenceExpr(thisExpr)
.build();
VariableExpr thisSharedProperties =
VariableExpr.withVariable(sharedPropertiesVar)
VariableExpr thisGlobalProperties =
VariableExpr.withVariable(globalPropertiesVar)
.toBuilder()
.setExprReferenceExpr(thisExpr)
.build();
Expand All @@ -369,42 +373,46 @@ private static MethodDefinition createCredentialsProviderBeanMethod(
.build())
.setType(types.get("CredentialsProvider"))
.build();
CastExpr sharedCredentialsCastExpr =
CastExpr globalCredentialsCastExpr =
CastExpr.builder()
.setExpr(
NewObjectExpr.builder()
.setType(types.get("DefaultCredentialsProvider"))
.setArguments(thisSharedProperties)
.setArguments(thisGlobalProperties)
.build())
.setType(types.get("CredentialsProvider"))
.build();
ExprStatement clientCredentialsReturnExpr = ExprStatement.withExpr(ReturnExpr.withExpr(clientCastExpr));
Expr clientPropertiesGetCredentials = MethodInvocationExpr.builder()
.setExprReferenceExpr(thisClientProperties)
.setMethodName("getCredentials")
.setReturnType(types.get("Credentials"))
.build();
Expr clientPropertiesCredentialsHasKey = MethodInvocationExpr.builder()
.setExprReferenceExpr(clientPropertiesGetCredentials)
.setMethodName("hasKey")
.setReturnType(TypeNode.BOOLEAN)
.build();
IfStatement clientCredentialsIfStatement = createIfStatement(clientPropertiesCredentialsHasKey,
Arrays.asList(clientCredentialsReturnExpr), null);
ExprStatement clientCredentialsReturnExpr =
ExprStatement.withExpr(ReturnExpr.withExpr(clientCastExpr));
Expr clientPropertiesGetCredentials =
MethodInvocationExpr.builder()
.setExprReferenceExpr(thisClientProperties)
.setMethodName("getCredentials")
.setReturnType(types.get("Credentials"))
.build();
Expr clientPropertiesCredentialsHasKey =
MethodInvocationExpr.builder()
.setExprReferenceExpr(clientPropertiesGetCredentials)
.setMethodName("hasKey")
.setReturnType(TypeNode.BOOLEAN)
.build();
IfStatement clientCredentialsIfStatement =
createIfStatement(
clientPropertiesCredentialsHasKey, Arrays.asList(clientCredentialsReturnExpr), null);
bodyStatements.add(clientCredentialsIfStatement);

// @ConditionalOnMissingBean(name = "[serviceName]ServiceCredentials")
AnnotationNode conditionalOnMissingBeanExpr = AnnotationNode.builder()
.setType(types.get("ConditionalOnMissingBean"))
.addDescription(AssignmentExpr.builder()
.setVariableExpr(VariableExpr.withVariable(
Variable.builder()
.setName("name")
.setType(TypeNode.STRING)
.build()))
.setValueExpr(ValueExpr.withValue(StringObjectValue.withValue(methodName)))
.build())
.build();
AnnotationNode conditionalOnMissingBeanExpr =
AnnotationNode.builder()
.setType(types.get("ConditionalOnMissingBean"))
.addDescription(
AssignmentExpr.builder()
.setVariableExpr(
VariableExpr.withVariable(
Variable.builder().setName("name").setType(TypeNode.STRING).build()))
.setValueExpr(ValueExpr.withValue(StringObjectValue.withValue(methodName)))
.build())
.build();

return MethodDefinition.builder()
.setName(methodName)
Expand All @@ -413,14 +421,10 @@ private static MethodDefinition createCredentialsProviderBeanMethod(
.setScope(ScopeNode.PUBLIC)
.setReturnType(types.get("CredentialsProvider"))
.setAnnotations(
Arrays.asList(
AnnotationNode.withType(types.get("Bean")),
conditionalOnMissingBeanExpr
)
)
Arrays.asList(AnnotationNode.withType(types.get("Bean")), conditionalOnMissingBeanExpr))
.setThrowsExceptions(Arrays.asList(TypeNode.withExceptionClazz(IOException.class)))
.setBody(bodyStatements)
.setReturnExpr(sharedCredentialsCastExpr)
.setReturnExpr(globalCredentialsCastExpr)
.build();
}

Expand Down Expand Up @@ -1156,7 +1160,7 @@ private static Map<String, TypeNode> createDynamicTypes(Service service, String
typeMap.put("Qualifier", qualifier);
typeMap.put("Log", LoggerUtils.getLoggerType());
typeMap.put("LogFactory", LoggerUtils.getLoggerFactoryType());
typeMap.put("SharedProperties", SharedPropertiesUtils.getSharedPropertiesType());
typeMap.put("GlobalProperties", GlobalPropertiesUtils.getGlobalPropertiesType());

return typeMap;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,31 +23,33 @@
import com.google.api.generator.engine.ast.VariableExpr;
import java.util.Map;

public class SharedPropertiesUtils {
public static final String SHARED_PROPERTIES_CLAZZ_NAME = "SharedProperties";
public static final String SHARED_PROPERTIES_PAKKAGE_NAME = "com.google.cloud.spring.shared";
public class GlobalPropertiesUtils {
public static final String GLOBAL_PROPERTIES_CLAZZ_NAME = "GLobalProperties";
public static final String GLOBAL_PROPERTIES_PAKKAGE_NAME = "com.google.cloud.global";

public static TypeNode getSharedPropertiesType() {
public static TypeNode getGlobalPropertiesType() {
TypeNode loggerType =
TypeNode.withReference(
VaporReference.builder()
.setName(SHARED_PROPERTIES_CLAZZ_NAME)
.setPakkage(SHARED_PROPERTIES_PAKKAGE_NAME)
.setName(GLOBAL_PROPERTIES_CLAZZ_NAME)
.setPakkage(GLOBAL_PROPERTIES_PAKKAGE_NAME)
.build());
return loggerType;
}

public static Statement getSharedPropertiesDeclaration(Map<String, TypeNode> types) {
Variable sharedPropertiesVar = Variable.builder()
.setName("sharedProperties")
.setType(types.get("SharedProperties"))
.build();
return ExprStatement.withExpr(VariableExpr.builder()
.setVariable(sharedPropertiesVar)
.setScope(ScopeNode.PRIVATE)
.setIsStatic(false)
.setIsFinal(true)
.setIsDecl(true)
.build());
public static Statement getGlobalPropertiesDeclaration(Map<String, TypeNode> types) {
Variable globalPropertiesVar =
Variable.builder()
.setName("globalProperties")
.setType(types.get("GlobalProperties"))
.build();
return ExprStatement.withExpr(
VariableExpr.builder()
.setVariable(globalPropertiesVar)
.setScope(ScopeNode.PRIVATE)
.setIsStatic(false)
.setIsFinal(true)
.setIsDecl(true)
.build());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ import com.google.api.gax.core.ExecutorProvider;
import com.google.api.gax.retrying.RetrySettings;
import com.google.api.gax.rpc.HeaderProvider;
import com.google.api.gax.rpc.TransportChannelProvider;
import com.google.cloud.global.GLobalProperties;
import com.google.cloud.spring.core.Credentials;
import com.google.cloud.spring.core.DefaultCredentialsProvider;
import com.google.cloud.spring.shared.SharedProperties;
import com.google.showcase.v1beta1.EchoClient;
import com.google.showcase.v1beta1.EchoSettings;
import java.io.IOException;
Expand Down Expand Up @@ -62,16 +62,16 @@ import org.threeten.bp.Duration;
@ConditionalOnProperty(
value = "com.google.showcase.v1beta1.spring.auto.echo.enabled",
matchIfMissing = false)
@EnableConfigurationProperties({EchoSpringProperties.class, SharedProperties.class})
@EnableConfigurationProperties({EchoSpringProperties.class, GLobalProperties.class})
public class EchoSpringAutoConfiguration {
private final EchoSpringProperties clientProperties;
private final SharedProperties sharedProperties;
private final GLobalProperties globalProperties;
private static final Log LOGGER = LogFactory.getLog(EchoSpringAutoConfig.class);

protected EchoSpringAutoConfiguration(
EchoSpringProperties clientProperties, SharedProperties sharedProperties) {
EchoSpringProperties clientProperties, GLobalProperties globalProperties) {
this.clientProperties = clientProperties;
this.sharedProperties = sharedProperties;
this.globalProperties = globalProperties;
}

/**
Expand All @@ -84,7 +84,7 @@ public class EchoSpringAutoConfiguration {
if (this.clientProperties.getCredentials().hasKey()) {
return ((CredentialsProvider) new DefaultCredentialsProvider(this.clientProperties));
}
return ((CredentialsProvider) new DefaultCredentialsProvider(this.sharedProperties));
return ((CredentialsProvider) new DefaultCredentialsProvider(this.GlobalProperties));
}

/**
Expand Down
Loading

0 comments on commit a8886c2

Please sign in to comment.