use of com.google.api.generator.engine.ast.TypeNode in project gapic-generator-java by googleapis.
the class AbstractServiceStubSettingsClassComposer method createNestedClassInitDefaultsMethod.
private MethodDefinition createNestedClassInitDefaultsMethod(Service service, @Nullable GapicServiceConfig serviceConfig, TypeStore typeStore) {
// TODO(miraleung): Robustify this against a null serviceConfig.
TypeNode builderType = typeStore.get(NESTED_BUILDER_CLASS_NAME);
VariableExpr builderVarExpr = VariableExpr.withVariable(Variable.builder().setType(builderType).setName("builder").build());
List<Statement> bodyStatements = new ArrayList<>();
// Iterate through methods twice to so we can have LRO expressions appear last.
for (Method method : service.methods()) {
Method.Stream streamKind = method.stream();
if (streamKind.equals(Method.Stream.CLIENT) || streamKind.equals(Method.Stream.BIDI)) {
continue;
}
if (!Objects.isNull(serviceConfig) && serviceConfig.hasBatchingSetting(service, method)) {
Optional<GapicBatchingSettings> batchingSettingOpt = serviceConfig.getBatchingSetting(service, method);
Preconditions.checkState(batchingSettingOpt.isPresent(), String.format("No batching setting found for service %s, method %s", service.name(), method.name()));
String settingsGetterMethodName = String.format("%sSettings", JavaStyle.toLowerCamelCase(method.name()));
bodyStatements.add(ExprStatement.withExpr(RetrySettingsComposer.createBatchingBuilderSettingsExpr(settingsGetterMethodName, batchingSettingOpt.get(), builderVarExpr)));
bodyStatements.add(EMPTY_LINE_STATEMENT);
}
bodyStatements.add(ExprStatement.withExpr(RetrySettingsComposer.createSimpleBuilderSettingsExpr(service, serviceConfig, method, builderVarExpr, NESTED_RETRYABLE_CODE_DEFINITIONS_VAR_EXPR, NESTED_RETRY_PARAM_DEFINITIONS_VAR_EXPR)));
bodyStatements.add(EMPTY_LINE_STATEMENT);
}
for (Method method : service.methods()) {
if (!method.hasLro()) {
continue;
}
bodyStatements.add(ExprStatement.withExpr(RetrySettingsComposer.createLroSettingsBuilderExpr(service, serviceConfig, method, builderVarExpr, NESTED_RETRYABLE_CODE_DEFINITIONS_VAR_EXPR, NESTED_RETRY_PARAM_DEFINITIONS_VAR_EXPR, getTransportContext().operationResponseTransformerType(), getTransportContext().operationMetadataTransformerType())));
bodyStatements.add(EMPTY_LINE_STATEMENT);
}
return MethodDefinition.builder().setScope(ScopeNode.PRIVATE).setIsStatic(true).setReturnType(builderType).setName("initDefaults").setArguments(builderVarExpr.toBuilder().setIsDecl(true).build()).setBody(bodyStatements).setReturnExpr(builderVarExpr).build();
}
use of com.google.api.generator.engine.ast.TypeNode in project gapic-generator-java by googleapis.
the class AbstractServiceStubSettingsClassComposer method createPagedListDescriptorAssignExpr.
private static Expr createPagedListDescriptorAssignExpr(VariableExpr pagedListDescVarExpr, Method method, TypeNode repeatedResponseType, String repeatedFieldName, Map<String, Message> messageTypes, TypeStore typeStore) {
MethodDefinition.Builder methodStarterBuilder = MethodDefinition.builder().setIsOverride(true).setScope(ScopeNode.PUBLIC);
List<MethodDefinition> anonClassMethods = new ArrayList<>();
// Create emptyToken method.
anonClassMethods.add(methodStarterBuilder.setReturnType(TypeNode.STRING).setName("emptyToken").setReturnExpr(ValueExpr.withValue(StringObjectValue.withValue(""))).build());
// Create injectToken method.
VariableExpr payloadVarExpr = VariableExpr.withVariable(Variable.builder().setType(method.inputType()).setName("payload").build());
VariableExpr strTokenVarExpr = VariableExpr.withVariable(Variable.builder().setType(TypeNode.STRING).setName("token").build());
TypeNode returnType = method.inputType();
Expr newBuilderExpr = MethodInvocationExpr.builder().setStaticReferenceType(method.inputType()).setMethodName("newBuilder").setArguments(payloadVarExpr).build();
Expr returnExpr = MethodInvocationExpr.builder().setExprReferenceExpr(newBuilderExpr).setMethodName("setPageToken").setArguments(strTokenVarExpr).build();
returnExpr = MethodInvocationExpr.builder().setExprReferenceExpr(returnExpr).setMethodName("build").setReturnType(returnType).build();
anonClassMethods.add(methodStarterBuilder.setReturnType(method.inputType()).setName("injectToken").setArguments(Arrays.asList(payloadVarExpr, strTokenVarExpr).stream().map(v -> v.toBuilder().setIsDecl(true).build()).collect(Collectors.toList())).setReturnExpr(returnExpr).build());
// Create injectPageSize method.
VariableExpr pageSizeVarExpr = VariableExpr.withVariable(Variable.builder().setType(TypeNode.INT).setName("pageSize").build());
// Re-declare for clarity and easier readability.
returnType = method.inputType();
returnExpr = MethodInvocationExpr.builder().setExprReferenceExpr(newBuilderExpr).setMethodName("set" + JavaStyle.toUpperCamelCase(method.pageSizeFieldName())).setArguments(pageSizeVarExpr).build();
returnExpr = MethodInvocationExpr.builder().setExprReferenceExpr(returnExpr).setMethodName("build").setReturnType(returnType).build();
anonClassMethods.add(methodStarterBuilder.setReturnType(method.inputType()).setName("injectPageSize").setArguments(Arrays.asList(payloadVarExpr, pageSizeVarExpr).stream().map(v -> v.toBuilder().setIsDecl(true).build()).collect(Collectors.toList())).setReturnExpr(returnExpr).build());
// TODO(miraleung): Test the edge cases where these proto fields aren't present.
// Create extractPageSize method.
returnType = TypeNode.INT_OBJECT;
anonClassMethods.add(methodStarterBuilder.setReturnType(returnType).setName("extractPageSize").setArguments(payloadVarExpr.toBuilder().setIsDecl(true).build()).setReturnExpr(MethodInvocationExpr.builder().setExprReferenceExpr(payloadVarExpr).setMethodName("get" + JavaStyle.toUpperCamelCase(method.pageSizeFieldName())).setReturnType(returnType).build()).build());
// Create extractNextToken method.
returnType = TypeNode.STRING;
payloadVarExpr = VariableExpr.withVariable(Variable.builder().setType(method.outputType()).setName("payload").build());
anonClassMethods.add(methodStarterBuilder.setReturnType(returnType).setName("extractNextToken").setArguments(payloadVarExpr.toBuilder().setIsDecl(true).build()).setReturnExpr(MethodInvocationExpr.builder().setExprReferenceExpr(payloadVarExpr).setMethodName("getNextPageToken").setReturnType(returnType).build()).build());
// Create extractResources method.
returnType = TypeNode.withReference(ConcreteReference.builder().setClazz(Iterable.class).setGenerics(Arrays.asList(repeatedResponseType.reference())).build());
Expr getResponsesExpr;
Expr elseExpr;
Expr thenExpr;
if (repeatedResponseType.reference() != null && "java.util.Map.Entry".equals(repeatedResponseType.reference().fullName())) {
getResponsesExpr = MethodInvocationExpr.builder().setExprReferenceExpr(payloadVarExpr).setMethodName(String.format("get%sMap", JavaStyle.toUpperCamelCase(repeatedFieldName))).setReturnType(returnType).build();
thenExpr = MethodInvocationExpr.builder().setStaticReferenceType(TypeNode.withReference(ConcreteReference.withClazz(Collections.class))).setGenerics(Arrays.asList(repeatedResponseType.reference())).setMethodName("emptySet").setReturnType(returnType).build();
elseExpr = MethodInvocationExpr.builder().setMethodName("entrySet").setExprReferenceExpr(MethodInvocationExpr.builder().setExprReferenceExpr(payloadVarExpr).setMethodName(String.format("get%sMap", JavaStyle.toUpperCamelCase(repeatedFieldName))).build()).setReturnType(returnType).build();
} else {
getResponsesExpr = MethodInvocationExpr.builder().setExprReferenceExpr(payloadVarExpr).setMethodName(String.format("get%sList", JavaStyle.toUpperCamelCase(repeatedFieldName))).setReturnType(returnType).build();
thenExpr = MethodInvocationExpr.builder().setStaticReferenceType(TypeNode.withReference(ConcreteReference.withClazz(ImmutableList.class))).setGenerics(Arrays.asList(repeatedResponseType.reference())).setMethodName("of").setReturnType(returnType).build();
elseExpr = getResponsesExpr;
}
// While protobufs should not be null, this null-check is needed to protect against NPEs
// in paged iteration on clients that use legacy HTTP/JSON types, as these clients can
// actually return null instead of an empty list.
// Context:
// Original issue: https://github.com/googleapis/google-cloud-java/issues/3736
// Relevant discussion where this check was first added:
// https://github.com/googleapis/google-cloud-java/pull/4499#discussion_r257057409
Expr conditionExpr = RelationalOperationExpr.equalToWithExprs(getResponsesExpr, ValueExpr.createNullExpr());
returnExpr = TernaryExpr.builder().setConditionExpr(conditionExpr).setThenExpr(thenExpr).setElseExpr(elseExpr).build();
anonClassMethods.add(methodStarterBuilder.setReturnType(returnType).setName("extractResources").setArguments(payloadVarExpr.toBuilder().setIsDecl(true).build()).setReturnExpr(returnExpr).build());
// Create the anonymous class.
AnonymousClassExpr pagedListDescAnonClassExpr = AnonymousClassExpr.builder().setType(pagedListDescVarExpr.type()).setMethods(anonClassMethods).build();
// Declare and assign the variable.
return AssignmentExpr.builder().setVariableExpr(pagedListDescVarExpr.toBuilder().setIsDecl(true).setScope(ScopeNode.PRIVATE).setIsStatic(true).setIsFinal(true).build()).setValueExpr(pagedListDescAnonClassExpr).build();
}
use of com.google.api.generator.engine.ast.TypeNode in project gapic-generator-java by googleapis.
the class AbstractServiceStubSettingsClassComposer method createDefaultTransportTransportProviderBuilderMethods.
protected List<MethodDefinition> createDefaultTransportTransportProviderBuilderMethods() {
// Create the defaultGrpcTransportProviderBuilder method.
Iterator<Class<?>> providerClassIt = getTransportContext().instantiatingChannelProviderClasses().iterator();
Iterator<Class<?>> providerBuilderClassIt = getTransportContext().instantiatingChannelProviderBuilderClasses().iterator();
Iterator<String> builderNamesIt = getTransportContext().defaultTransportProviderBuilderNames().iterator();
List<MethodDefinition> methods = new ArrayList<>();
while (providerClassIt.hasNext() && providerBuilderClassIt.hasNext() && builderNamesIt.hasNext()) {
TypeNode returnType = TypeNode.withReference(ConcreteReference.withClazz(providerBuilderClassIt.next()));
TypeNode channelProviderType = TypeNode.withReference(ConcreteReference.withClazz(providerClassIt.next()));
MethodInvocationExpr transportChannelProviderBuilderExpr = MethodInvocationExpr.builder().setStaticReferenceType(channelProviderType).setMethodName("newBuilder").setReturnType(returnType).build();
Expr returnExpr = initializeTransportProviderBuilder(transportChannelProviderBuilderExpr, returnType);
MethodDefinition method = MethodDefinition.builder().setHeaderCommentStatements(SettingsCommentComposer.DEFAULT_TRANSPORT_PROVIDER_BUILDER_METHOD_COMMENT).setScope(ScopeNode.PUBLIC).setIsStatic(true).setReturnType(returnType).setName(builderNamesIt.next()).setReturnExpr(returnExpr).build();
methods.add(method);
}
return methods;
}
use of com.google.api.generator.engine.ast.TypeNode in project gapic-generator-java by googleapis.
the class AbstractServiceStubSettingsClassComposer method createDefaultHelperAndGetterMethods.
private List<MethodDefinition> createDefaultHelperAndGetterMethods(Service service, TypeStore typeStore) {
List<MethodDefinition> javaMethods = new ArrayList<>();
// Create the defaultExecutorProviderBuilder method.
TypeNode returnType = TypeNode.withReference(ConcreteReference.withClazz(InstantiatingExecutorProvider.Builder.class));
javaMethods.add(MethodDefinition.builder().setHeaderCommentStatements(SettingsCommentComposer.DEFAULT_EXECUTOR_PROVIDER_BUILDER_METHOD_COMMENT).setScope(ScopeNode.PUBLIC).setIsStatic(true).setReturnType(returnType).setName("defaultExecutorProviderBuilder").setReturnExpr(MethodInvocationExpr.builder().setStaticReferenceType(FIXED_TYPESTORE.get("InstantiatingExecutorProvider")).setMethodName("newBuilder").setReturnType(returnType).build()).build());
// Create the getDefaultEndpoint method.
returnType = TypeNode.STRING;
javaMethods.add(MethodDefinition.builder().setHeaderCommentStatements(SettingsCommentComposer.DEFAULT_SERVICE_ENDPOINT_METHOD_COMMENT).setScope(ScopeNode.PUBLIC).setIsStatic(true).setReturnType(returnType).setName("getDefaultEndpoint").setReturnExpr(ValueExpr.withValue(StringObjectValue.withValue(service.defaultHost()))).build());
// Create the getDefaultMtlsEndpoint method.
returnType = TypeNode.STRING;
javaMethods.add(MethodDefinition.builder().setHeaderCommentStatements(SettingsCommentComposer.DEFAULT_SERVICE_MTLS_ENDPOINT_METHOD_COMMENT).setScope(ScopeNode.PUBLIC).setIsStatic(true).setReturnType(returnType).setName("getDefaultMtlsEndpoint").setReturnExpr(ValueExpr.withValue(StringObjectValue.withValue(service.defaultHost().replace(".googleapis.com", ".mtls.googleapis.com")))).build());
// Create the getDefaultServiceScopes method.
returnType = TypeNode.withReference(ConcreteReference.builder().setClazz(List.class).setGenerics(Arrays.asList(TypeNode.STRING.reference())).build());
javaMethods.add(MethodDefinition.builder().setHeaderCommentStatements(SettingsCommentComposer.DEFAULT_SERVICE_SCOPES_METHOD_COMMENT).setScope(ScopeNode.PUBLIC).setIsStatic(true).setReturnType(returnType).setName("getDefaultServiceScopes").setReturnExpr(DEFAULT_SERVICE_SCOPES_VAR_EXPR).build());
javaMethods.add(createDefaultCredentialsProviderBuilderMethod());
javaMethods.addAll(createDefaultTransportTransportProviderBuilderMethods());
javaMethods.add(createDefaultTransportChannelProviderMethod());
javaMethods.addAll(createApiClientHeaderProviderBuilderMethods(service, typeStore));
return javaMethods;
}
use of com.google.api.generator.engine.ast.TypeNode in project gapic-generator-java by googleapis.
the class AbstractServiceStubSettingsClassComposer method createNestedRetryableCodeDefinitionsVarExpr.
private static VariableExpr createNestedRetryableCodeDefinitionsVarExpr() {
TypeNode immutableSetType = TypeNode.withReference(ConcreteReference.builder().setClazz(ImmutableSet.class).setGenerics(Arrays.asList(ConcreteReference.withClazz(StatusCode.Code.class))).build());
TypeNode varType = TypeNode.withReference(ConcreteReference.builder().setClazz(ImmutableMap.class).setGenerics(Arrays.asList(TypeNode.STRING, immutableSetType).stream().map(t -> t.reference()).collect(Collectors.toList())).build());
return VariableExpr.withVariable(Variable.builder().setType(varType).setName(NESTED_RETRYABLE_CODE_DEFINITIONS_VAR_NAME).build());
}
Aggregations